Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
preconditions_tests.cpp
Go to the documentation of this file.
1#include <sysio/vm/backend.hpp>
2#include <sysio/vm/host_function.hpp>
3
4#include "utils.hpp"
5
6#include <catch2/catch.hpp>
7#include <exception>
8
9using namespace sysio::vm;
10
11static int check = 0;
12static int check2 = 0;
13
14void test_func_a(int a, float b) {
15}
16void test_func_b(char* a, float* b, int& c) {
17}
18void test_func_c(float a, const char* b, span<const char> c, int d) {
19 check = d;
20}
22}
23void test_func_e(float a, const char* b, span<const char> c, int d) {
24 check = d;
25}
26
27template <typename Ex, typename F>
28bool check_exception_msg(F&& func, const std::string& chck) {
29 try {
30 func();
31 } catch( Ex ex ) {
32 return ex.what() == chck;
33 }
34 return false;
35}
36
37#define CHECK_MSG(Msg, ...) \
38 CHECK(check_exception_msg<std::runtime_error>([&](){ __VA_ARGS__; }, Msg))
39
40SYS_VM_PRECONDITION(int_equals_42,
41 SYS_VM_INVOKE_ON(int, [&](auto arg, auto&&... rest) {
42 if (arg != 42)
43 throw std::runtime_error("arg != 42");
44 }));
45
46SYS_VM_PRECONDITION(float_equals_42,
47 SYS_VM_INVOKE_ON(float, [&](auto arg, auto&&... rest) {
48 if (arg != 42.42f)
49 throw std::runtime_error("arg != 42.42f");
50 }));
51
52SYS_VM_PRECONDITION(str_equals_hello,
53 SYS_VM_INVOKE_ON(const char*, [&](auto arg, auto&&... rest) {
54 if (memcmp(arg, "hello", 5) != 0)
55 throw std::runtime_error("str != hello");
56 }));
57
58SYS_VM_PRECONDITION(span_and_check,
59 SYS_VM_INVOKE_ON_ALL([&](auto arg, auto&&... rest) {
60 if constexpr (is_span_type_v<decltype(arg)>) {
61 if (memcmp(arg.data(), "hellohe", arg.size_bytes()) != 0)
62 throw std::runtime_error("span<T> != hellohe");
63 }
64 }));
65
67 SYS_VM_INVOKE_ONCE([&](auto&&... args) {
68 check2++;
69 }));
70
72 SYS_VM_INVOKE_ON_ALL([&](auto&&... args) {
73 check2++;
74 }));
75
76struct cnv : type_converter<standalone_function_t> {
78 using type_converter::type_converter;
79 SYS_VM_FROM_WASM(char*, (void* ptr)) { return static_cast<char*>(ptr); }
80 SYS_VM_FROM_WASM(const char*, (void* ptr)) { return static_cast<char*>(ptr); }
81 SYS_VM_FROM_WASM(float*, (void* ptr)) { return static_cast<float*>(ptr); }
82 SYS_VM_FROM_WASM(int&, (void* ptr)) { return *static_cast<int*>(ptr); }
83};
84
85BACKEND_TEST_CASE("Testing invoke_on", "[preconditions_tests]") {
88
89/*
90(module
91 (type (;0;) (func (param i32 i32 i32)))
92 (type (;1;) (func (param i32 f32)))
93 (type (;2;) (func (param)))
94 (type (;3;) (func (param f32 i32 i32 i32 i32)))
95 (import "env" "test_func_b" (func (;0;) (type 0)))
96 (import "env" "test_func_a" (func (;1;) (type 1)))
97 (import "env" "test_func_c" (func (;2;) (type 3)))
98 (import "env" "test_func_d" (func (;3;) (type 2)))
99 (import "env" "test_func_e" (func (;4;) (type 3)))
100 (func (;5;) (type 2)
101 i32.const 42
102 f32.const 42.42
103 call 1)
104 (func (;6;) (type 2)
105 i32.const 8208
106 i32.const 8232
107 i32.const 8232
108 call 0)
109 (func (;7;) (type 2)
110 i32.const 32
111 f32.const 42
112 call 1)
113 (func (;8;) (type 2)
114 i32.const 42
115 f32.const 32
116 call 1)
117 (func (;9;) (type 2)
118 i32.const 0
119 i32.const 0
120 i32.const 0
121 call 0)
122 (func (;10;) (type 2)
123 f32.const 42.42
124 i32.const 8208
125 i32.const 8208
126 i32.const 7
127 i32.const 77
128 call 2)
129 (func (;11;) (type 2)
130 f32.const 42.42
131 i32.const 8232
132 i32.const 8232
133 i32.const 4
134 i32.const 77
135 call 2)
136 (func (;12;) (type 2)
137 call 3)
138 (func (;13;) (type 2)
139 f32.const 42.42
140 i32.const 8232
141 i32.const 8232
142 i32.const 4
143 i32.const 77
144 call 4)
145 (memory (;0;) 1)
146 (export "call_test_a" (func 5))
147 (export "call_test_b" (func 6))
148 (export "call_test_c" (func 7))
149 (export "call_test_d" (func 8))
150 (export "call_test_e" (func 9))
151 (export "call_test_f" (func 10))
152 (export "call_test_g" (func 11))
153 (export "call_test_h" (func 12))
154 (export "call_test_j" (func 13))
155 (data (i32.const 8208) "hellohello\00")
156 (data (i32.const 8232) "\ff\ff\ff\ff"))
157*/
158 wasm_code test_wasm = {
159 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x17, 0x04, 0x60,
160 0x03, 0x7f, 0x7f, 0x7f, 0x00, 0x60, 0x02, 0x7f, 0x7d, 0x00, 0x60, 0x00,
161 0x00, 0x60, 0x05, 0x7d, 0x7f, 0x7f, 0x7f, 0x7f, 0x00, 0x02, 0x5b, 0x05,
162 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75,
163 0x6e, 0x63, 0x5f, 0x62, 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74,
164 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x5f, 0x61, 0x00, 0x01,
165 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75,
166 0x6e, 0x63, 0x5f, 0x63, 0x00, 0x03, 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74,
167 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75, 0x6e, 0x63, 0x5f, 0x64, 0x00, 0x02,
168 0x03, 0x65, 0x6e, 0x76, 0x0b, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66, 0x75,
169 0x6e, 0x63, 0x5f, 0x65, 0x00, 0x03, 0x03, 0x0a, 0x09, 0x02, 0x02, 0x02,
170 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x05, 0x03, 0x01, 0x00, 0x01, 0x07,
171 0x7f, 0x09, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74,
172 0x5f, 0x61, 0x00, 0x05, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65,
173 0x73, 0x74, 0x5f, 0x62, 0x00, 0x06, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f,
174 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x00, 0x07, 0x0b, 0x63, 0x61, 0x6c,
175 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x64, 0x00, 0x08, 0x0b, 0x63,
176 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x65, 0x00, 0x09,
177 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x66,
178 0x00, 0x0a, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65, 0x73, 0x74,
179 0x5f, 0x67, 0x00, 0x0b, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f, 0x74, 0x65,
180 0x73, 0x74, 0x5f, 0x68, 0x00, 0x0c, 0x0b, 0x63, 0x61, 0x6c, 0x6c, 0x5f,
181 0x74, 0x65, 0x73, 0x74, 0x5f, 0x6a, 0x00, 0x0d, 0x0a, 0x8b, 0x01, 0x09,
182 0x0b, 0x00, 0x41, 0x2a, 0x43, 0x14, 0xae, 0x29, 0x42, 0x10, 0x01, 0x0b,
183 0x10, 0x00, 0x41, 0x90, 0xc0, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0xa8,
184 0xc0, 0x00, 0x10, 0x00, 0x0b, 0x0b, 0x00, 0x41, 0x20, 0x43, 0x00, 0x00,
185 0x28, 0x42, 0x10, 0x01, 0x0b, 0x0b, 0x00, 0x41, 0x2a, 0x43, 0x00, 0x00,
186 0x00, 0x42, 0x10, 0x01, 0x0b, 0x0a, 0x00, 0x41, 0x00, 0x41, 0x00, 0x41,
187 0x00, 0x10, 0x00, 0x0b, 0x16, 0x00, 0x43, 0x14, 0xae, 0x29, 0x42, 0x41,
188 0x90, 0xc0, 0x00, 0x41, 0x90, 0xc0, 0x00, 0x41, 0x07, 0x41, 0xcd, 0x00,
189 0x10, 0x02, 0x0b, 0x16, 0x00, 0x43, 0x14, 0xae, 0x29, 0x42, 0x41, 0xa8,
190 0xc0, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0x04, 0x41, 0xcd, 0x00, 0x10,
191 0x02, 0x0b, 0x04, 0x00, 0x10, 0x03, 0x0b, 0x16, 0x00, 0x43, 0x14, 0xae,
192 0x29, 0x42, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x41, 0x04,
193 0x41, 0xcd, 0x00, 0x10, 0x04, 0x0b, 0x0b, 0x1e, 0x02, 0x00, 0x41, 0x90,
194 0xc0, 0x00, 0x0b, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x68, 0x65, 0x6c,
195 0x6c, 0x6f, 0x00, 0x00, 0x41, 0xa8, 0xc0, 0x00, 0x0b, 0x04, 0xff, 0xff,
196 0xff, 0xff
197 };
198
200 rhf_t::add<&test_func_b, str_equals_hello>("env", "test_func_b");
202 rhf_t::add<&test_func_d, check2_once>("env", "test_func_d");
203 rhf_t::add<&test_func_e, check2_all>("env", "test_func_e");
204
206 backend_t bkend(test_wasm, &wa);
207
208 check = 0;
209 check2 = 0;
210 bkend("env", "call_test_a");
211 bkend("env", "call_test_b");
212 CHECK_MSG("arg != 42", bkend("env", "call_test_c"));
213 CHECK_MSG("arg != 42.42f", bkend("env", "call_test_d"));
214 bkend("env", "call_test_e");
215 CHECK(check == 0);
216 bkend("env", "call_test_f");
217 CHECK(check == 77);
218
219 // reset check
220 check = 0;
221 CHECK_MSG("str != hello", bkend("env", "call_test_g"));
222 CHECK(check == 0);
223
224 bkend("env", "call_test_h");
225 CHECK(check2 == 1);
226 bkend("env", "call_test_h");
227 CHECK(check2 == 2);
228
229 check2 = 0;
230 bkend("env", "call_test_j");
231 CHECK(check2 == 4);
232 bkend("env", "call_test_j");
233 CHECK(check2 == 8);
234}
wasm_allocator wa
Definition main.cpp:10
#define CHECK(cond)
Definition util.h:80
backend_t bkend(hello_wasm, ehm, &wa)
std::vector< uint8_t > wasm_code
Definition types.hpp:147
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
void test_func_a(int a, float b)
void test_func_d()
void test_func_e(float a, const char *b, span< const char > c, int d)
void test_func_b(char *a, float *b, int &c)
bool check_exception_msg(F &&func, const std::string &chck)
#define CHECK_MSG(Msg,...)
void test_func_c(float a, const char *b, span< const char > c, int d)
SYS_VM_FROM_WASM(char *,(void *ptr))
SYS_VM_FROM_WASM(const char *,(void *ptr))
SYS_VM_FROM_WASM(int &,(void *ptr))
SYS_VM_FROM_WASM(float *,(void *ptr))
auto from_wasm(vm::wasm_ptr_t ptr) const -> std::enable_if_t< std::is_pointer_v< T >, vm::argument_proxy< T > >
Definition common.hpp:88
static void add(const std::string &mod, const std::string &name)
#define BACKEND_TEST_CASE(name, tags)
Definition utils.hpp:59
#define SYS_VM_PRECONDITION(NAME,...)
#define SYS_VM_INVOKE_ONCE(CONDITION)
#define SYS_VM_INVOKE_ON_ALL(CONDITION)
#define SYS_VM_INVOKE_ON(TYPE, CONDITION)
CK_ULONG d