Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
preconditions_tests.cpp File Reference
#include <sysio/vm/backend.hpp>
#include <sysio/vm/host_function.hpp>
#include "utils.hpp"
#include <catch2/catch.hpp>
#include <exception>
Include dependency graph for preconditions_tests.cpp:

Go to the source code of this file.

Classes

struct  cnv
 

Macros

#define CHECK_MSG(Msg, ...)
 

Functions

void test_func_a (int a, float b)
 
void test_func_b (char *a, float *b, int &c)
 
void test_func_c (float a, const char *b, span< const char > c, int d)
 
void test_func_d ()
 
void test_func_e (float a, const char *b, span< const char > c, int d)
 
template<typename Ex , typename F >
bool check_exception_msg (F &&func, const std::string &chck)
 
 SYS_VM_PRECONDITION (int_equals_42, SYS_VM_INVOKE_ON(int, [&](auto arg, auto &&... rest) { if(arg !=42) throw std::runtime_error("arg != 42");}))
 
 SYS_VM_PRECONDITION (float_equals_42, SYS_VM_INVOKE_ON(float, [&](auto arg, auto &&... rest) { if(arg !=42.42f) throw std::runtime_error("arg != 42.42f");}))
 
 SYS_VM_PRECONDITION (str_equals_hello, SYS_VM_INVOKE_ON(const char *, [&](auto arg, auto &&... rest) { if(memcmp(arg, "hello", 5) !=0) throw std::runtime_error("str != hello");}))
 
 SYS_VM_PRECONDITION (span_and_check, SYS_VM_INVOKE_ON_ALL([&](auto arg, auto &&... rest) { if constexpr(is_span_type_v< decltype(arg)>) { if(memcmp(arg.data(), "hellohe", arg.size_bytes()) !=0) throw std::runtime_error("span<T> != hellohe");} }))
 
 SYS_VM_PRECONDITION (check2_once, SYS_VM_INVOKE_ONCE([&](auto &&... args) { check2++;}))
 
 SYS_VM_PRECONDITION (check2_all, SYS_VM_INVOKE_ON_ALL([&](auto &&... args) { check2++;}))
 
 BACKEND_TEST_CASE ("Testing invoke_on", "[preconditions_tests]")
 

Macro Definition Documentation

◆ CHECK_MSG

#define CHECK_MSG ( Msg,
... )
Value:
#define CHECK(cond)
Definition util.h:80
bool check_exception_msg(F &&func, const std::string &chck)
union _Msg Msg
Definition scp.h:66

Definition at line 37 of file preconditions_tests.cpp.

37#define CHECK_MSG(Msg, ...) \
38 CHECK(check_exception_msg<std::runtime_error>([&](){ __VA_ARGS__; }, Msg))

Function Documentation

◆ BACKEND_TEST_CASE()

BACKEND_TEST_CASE ( "Testing invoke_on" ,
"" [preconditions_tests] )

Definition at line 85 of file preconditions_tests.cpp.

85 {
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
backend_t bkend(hello_wasm, ehm, &wa)
std::vector< uint8_t > wasm_code
Definition types.hpp:147
#define CHECK_MSG(Msg,...)
static void add(const std::string &mod, const std::string &name)
Here is the call graph for this function:

◆ check_exception_msg()

template<typename Ex , typename F >
bool check_exception_msg ( F && func,
const std::string & chck )

Definition at line 28 of file preconditions_tests.cpp.

28 {
29 try {
30 func();
31 } catch( Ex ex ) {
32 return ex.what() == chck;
33 }
34 return false;
35}

◆ SYS_VM_PRECONDITION() [1/6]

SYS_VM_PRECONDITION ( check2_all ,
SYS_VM_INVOKE_ON_ALL([&](auto &&... args) { check2++;})  )

◆ SYS_VM_PRECONDITION() [2/6]

SYS_VM_PRECONDITION ( check2_once ,
SYS_VM_INVOKE_ONCE([&](auto &&... args) { check2++;})  )

◆ SYS_VM_PRECONDITION() [3/6]

SYS_VM_PRECONDITION ( float_equals_42 ,
SYS_VM_INVOKE_ON(float,[&](auto arg, auto &&... rest) { if(arg !=42.42f) throw std::runtime_error("arg != 42.42f");})  )

◆ SYS_VM_PRECONDITION() [4/6]

SYS_VM_PRECONDITION ( int_equals_42 ,
SYS_VM_INVOKE_ON(int,[&](auto arg, auto &&... rest) { if(arg !=42) throw std::runtime_error("arg != 42");})  )

◆ SYS_VM_PRECONDITION() [5/6]

SYS_VM_PRECONDITION ( span_and_check ,
SYS_VM_INVOKE_ON_ALL([&](auto arg, auto &&... rest) { if constexpr(is_span_type_v< decltype(arg)>) { if(memcmp(arg.data(), "hellohe", arg.size_bytes()) !=0) throw std::runtime_error("span<T> != hellohe");} })  )

◆ SYS_VM_PRECONDITION() [6/6]

SYS_VM_PRECONDITION ( str_equals_hello ,
SYS_VM_INVOKE_ON(const char *,[&](auto arg, auto &&... rest) { if(memcmp(arg, "hello", 5) !=0) throw std::runtime_error("str != hello");})  )

◆ test_func_a()

void test_func_a ( int a,
float b )

Definition at line 14 of file preconditions_tests.cpp.

14 {
15}

◆ test_func_b()

void test_func_b ( char * a,
float * b,
int & c )

Definition at line 16 of file preconditions_tests.cpp.

16 {
17}

◆ test_func_c()

void test_func_c ( float a,
const char * b,
span< const char > c,
int d )

Definition at line 18 of file preconditions_tests.cpp.

18 {
19 check = d;
20}
CK_ULONG d

◆ test_func_d()

void test_func_d ( )

Definition at line 21 of file preconditions_tests.cpp.

21 {
22}

◆ test_func_e()

void test_func_e ( float a,
const char * b,
span< const char > c,
int d )

Definition at line 23 of file preconditions_tests.cpp.

23 {
24 check = d;
25}