Skip to content

Commit 0edb270

Browse files
committed
Add math tests
1 parent a34c0ca commit 0edb270

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,141 @@
11
package net.bluejekyll.wasmtime.tests;
22

3+
import net.bluejekyll.wasmtime.*;
4+
import org.junit.Test;
5+
6+
import java.io.UnsupportedEncodingException;
7+
import java.nio.ByteBuffer;
8+
import java.util.Optional;
9+
10+
import static org.junit.Assert.*;
11+
312
/**
413
* Tests corresponding to the Rust based WASM programs in /tests/math
514
*/
615
public class MathTests {
16+
@Test
17+
public void testAddI32() throws Exception {
18+
Wasmtime wasm = new Wasmtime();
19+
try (WasmEngine engine = wasm.newWasmEngine();
20+
WasmModule module = engine.newModule(TestUtil.MATH_PATH);
21+
WasmStore store = engine.newStore();
22+
WasmLinker linker = store.newLinker()) {
23+
System.out.println("slices compiled");
24+
assertNotNull(module);
25+
26+
WasmInstance instance = linker.instantiate(module);
27+
Optional<WasmFunction> func = instance.getFunction("add_i32");
28+
29+
assertTrue("add_i32 isn't present in the module", func.isPresent());
30+
WasmFunction function = func.get();
31+
32+
int ret = function.call(instance, Integer.TYPE, 3, 2);
33+
assertEquals(ret, 5);
34+
}
35+
}
36+
37+
@Test
38+
public void testAddU32() throws Exception {
39+
Wasmtime wasm = new Wasmtime();
40+
try (WasmEngine engine = wasm.newWasmEngine();
41+
WasmModule module = engine.newModule(TestUtil.MATH_PATH);
42+
WasmStore store = engine.newStore();
43+
WasmLinker linker = store.newLinker()) {
44+
System.out.println("slices compiled");
45+
assertNotNull(module);
46+
47+
WasmInstance instance = linker.instantiate(module);
48+
Optional<WasmFunction> func = instance.getFunction("add_u32");
49+
50+
assertTrue("add_u32 isn't present in the module", func.isPresent());
51+
WasmFunction function = func.get();
52+
53+
int ret = function.call(instance, Integer.TYPE, 3, 2);
54+
assertEquals(ret, 5);
55+
}
56+
}
57+
58+
@Test
59+
public void testAddI64() throws Exception {
60+
Wasmtime wasm = new Wasmtime();
61+
try (WasmEngine engine = wasm.newWasmEngine();
62+
WasmModule module = engine.newModule(TestUtil.MATH_PATH);
63+
WasmStore store = engine.newStore();
64+
WasmLinker linker = store.newLinker()) {
65+
System.out.println("slices compiled");
66+
assertNotNull(module);
67+
68+
WasmInstance instance = linker.instantiate(module);
69+
Optional<WasmFunction> func = instance.getFunction("add_i64");
70+
71+
assertTrue("add_i64 isn't present in the module", func.isPresent());
72+
WasmFunction function = func.get();
73+
74+
long ret = function.call(instance, Long.TYPE, (long)3, (long)2);
75+
assertEquals(ret, 5);
76+
}
77+
}
78+
79+
@Test
80+
public void testAddU64() throws Exception {
81+
Wasmtime wasm = new Wasmtime();
82+
try (WasmEngine engine = wasm.newWasmEngine();
83+
WasmModule module = engine.newModule(TestUtil.MATH_PATH);
84+
WasmStore store = engine.newStore();
85+
WasmLinker linker = store.newLinker()) {
86+
System.out.println("slices compiled");
87+
assertNotNull(module);
88+
89+
WasmInstance instance = linker.instantiate(module);
90+
Optional<WasmFunction> func = instance.getFunction("add_u64");
91+
92+
assertTrue("add_u64 isn't present in the module", func.isPresent());
93+
WasmFunction function = func.get();
94+
95+
long ret = function.call(instance, Long.TYPE, (long)3, (long)2);
96+
assertEquals(ret, 5);
97+
}
98+
}
99+
100+
@Test
101+
public void testAddF32() throws Exception {
102+
Wasmtime wasm = new Wasmtime();
103+
try (WasmEngine engine = wasm.newWasmEngine();
104+
WasmModule module = engine.newModule(TestUtil.MATH_PATH);
105+
WasmStore store = engine.newStore();
106+
WasmLinker linker = store.newLinker()) {
107+
System.out.println("slices compiled");
108+
assertNotNull(module);
109+
110+
WasmInstance instance = linker.instantiate(module);
111+
Optional<WasmFunction> func = instance.getFunction("add_f32");
112+
113+
assertTrue("add_f32 isn't present in the module", func.isPresent());
114+
WasmFunction function = func.get();
115+
116+
float ret = function.call(instance, Float.TYPE, (float)1.1, (float)2.2);
117+
assertEquals(ret, (float)3.3, 0.1);
118+
}
119+
}
120+
121+
@Test
122+
public void testAddF64() throws Exception {
123+
Wasmtime wasm = new Wasmtime();
124+
try (WasmEngine engine = wasm.newWasmEngine();
125+
WasmModule module = engine.newModule(TestUtil.MATH_PATH);
126+
WasmStore store = engine.newStore();
127+
WasmLinker linker = store.newLinker()) {
128+
System.out.println("slices compiled");
129+
assertNotNull(module);
130+
131+
WasmInstance instance = linker.instantiate(module);
132+
Optional<WasmFunction> func = instance.getFunction("add_f64");
133+
134+
assertTrue("add_f64 isn't present in the module", func.isPresent());
135+
WasmFunction function = func.get();
136+
137+
double ret = function.call(instance, Double.TYPE, (double)1.1, (double)2.2);
138+
assertEquals(ret, (double)3.3, 0.1);
139+
}
140+
}
7141
}

tests/math/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
#[no_mangle]
12
pub extern "C" fn add_i32(a: i32, b: i32) -> i32 {
23
a + b
34
}
45

6+
#[no_mangle]
57
pub extern "C" fn add_u32(a: u32, b: u32) -> u32 {
68
a + b
79
}
810

11+
#[no_mangle]
912
pub extern "C" fn add_i64(a: i64, b: i64) -> i64 {
1013
a + b
1114
}
1215

16+
#[no_mangle]
1317
pub extern "C" fn add_u64(a: u64, b: u64) -> u64 {
1418
a + b
1519
}
1620

21+
#[no_mangle]
1722
pub extern "C" fn add_f32(a: f32, b: f32) -> f32 {
1823
a + b
1924
}
2025

26+
#[no_mangle]
2127
pub extern "C" fn add_f64(a: f64, b: f64) -> f64 {
2228
a + b
2329
}

0 commit comments

Comments
 (0)