|
1 | 1 | package net.bluejekyll.wasmtime.tests;
|
2 | 2 |
|
| 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 | + |
3 | 12 | /**
|
4 | 13 | * Tests corresponding to the Rust based WASM programs in /tests/math
|
5 | 14 | */
|
6 | 15 | 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 | + } |
7 | 141 | }
|
0 commit comments