44
55#include < fizzy/fizzy.h>
66#include < gtest/gtest.h>
7+ #include < test/utils/hex.hpp>
8+
9+ using namespace fizzy ::test;
710
811TEST (capi, fizzy_validate)
912{
@@ -12,3 +15,94 @@ TEST(capi, fizzy_validate)
1215 wasm_prefix[7 ] = 1 ;
1316 EXPECT_FALSE (fizzy_validate (wasm_prefix, sizeof (wasm_prefix)));
1417}
18+
19+ TEST (capi, parse)
20+ {
21+ uint8_t wasm_prefix[]{0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 };
22+ auto module = fizzy_parse (wasm_prefix, sizeof (wasm_prefix));
23+ EXPECT_TRUE (module );
24+ fizzy_free_module (module );
25+ }
26+
27+ TEST (capi, instantiate)
28+ {
29+ uint8_t wasm_prefix[]{0x00 , 0x61 , 0x73 , 0x6d , 0x01 , 0x00 , 0x00 , 0x00 };
30+ auto module = fizzy_parse (wasm_prefix, sizeof (wasm_prefix));
31+
32+ auto imported_funcs = fizzy_new_external_function_vector (nullptr , 0 );
33+
34+ auto instance = fizzy_instantiate (module , imported_funcs);
35+ EXPECT_TRUE (instance);
36+
37+ fizzy_free_instance (instance);
38+ }
39+
40+ TEST (capi, execute)
41+ {
42+ /* wat2wasm
43+ (func (result i32) i32.const 42)
44+ (func (param i32 i32) (result i32)
45+ (i32.div_u (local.get 0) (local.get 1))
46+ )
47+ */
48+ const auto wasm = from_hex (
49+ " 0061736d01000000010b026000017f60027f7f017f03030200010a0e020400412a0b0700200020016e0b" );
50+
51+ auto module = fizzy_parse (wasm.data (), wasm.size ());
52+
53+ auto imported_funcs = fizzy_new_external_function_vector (nullptr , 0 );
54+
55+ auto instance = fizzy_instantiate (module , imported_funcs);
56+
57+ auto res = fizzy_execute (instance, 0 , nullptr , 0 , 0 );
58+ EXPECT_FALSE (res.trapped );
59+ EXPECT_TRUE (res.has_value );
60+ EXPECT_EQ (res.value .i64 , 42 );
61+
62+ fizzy_value args[] = {{42 }, {2 }};
63+ auto res2 = fizzy_execute (instance, 1 , args, 2 , 0 );
64+ EXPECT_FALSE (res2.trapped );
65+ EXPECT_TRUE (res2.has_value );
66+ EXPECT_EQ (res2.value .i64 , 21 );
67+
68+
69+ fizzy_free_instance (instance);
70+ }
71+
72+ TEST (capi, execute_with_host_function)
73+ {
74+ /* wat2wasm
75+ (func (import "mod1" "foo1") (result i32))
76+ (func (import "mod1" "foo2") (param i32 i32) (result i32))
77+ */
78+ const auto wasm = from_hex (
79+ " 0061736d01000000010b026000017f60027f7f017f021902046d6f643104666f6f310000046d6f643104666f6f"
80+ " 320001" );
81+ auto module = fizzy_parse (wasm.data (), wasm.size ());
82+
83+ fizzy_external_function host_funcs[] = {
84+ {[](void *, fizzy_instance*, const fizzy_value* /* args*/ , int ) {
85+ return fizzy_execution_result{false , true , {42 }};
86+ }},
87+ {[](void *, fizzy_instance*, const fizzy_value* args, int ) {
88+ return fizzy_execution_result{false , true , {args[0 ].i64 / args[1 ].i64 }};
89+ }}};
90+
91+ auto imported_funcs = fizzy_new_external_function_vector (host_funcs, 2 );
92+
93+ auto instance = fizzy_instantiate (module , imported_funcs);
94+
95+ auto res = fizzy_execute (instance, 0 , nullptr , 0 , 0 );
96+ EXPECT_FALSE (res.trapped );
97+ EXPECT_TRUE (res.has_value );
98+ EXPECT_EQ (res.value .i64 , 42 );
99+
100+
101+ fizzy_value args[] = {{42 }, {2 }};
102+ auto res2 = fizzy_execute (instance, 1 , args, 2 , 0 );
103+ EXPECT_FALSE (res2.trapped );
104+ EXPECT_TRUE (res2.has_value );
105+ EXPECT_EQ (res2.value .i64 , 21 );
106+
107+ fizzy_free_instance (instance);
108+ }
0 commit comments