Skip to content

Commit c4dd649

Browse files
committed
BAEL-9118: added other tests
1 parent 9c4ee61 commit c4dd649

File tree

7 files changed

+96
-8
lines changed

7 files changed

+96
-8
lines changed
Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,83 @@
11
package com.baeldung.chicory;
22

33
import com.dylibso.chicory.runtime.ExportFunction;
4+
import com.dylibso.chicory.runtime.HostFunction;
45
import com.dylibso.chicory.runtime.Instance;
6+
import com.dylibso.chicory.runtime.Store;
57
import com.dylibso.chicory.wasm.Parser;
8+
import com.dylibso.chicory.wasm.WasmModule;
9+
import com.dylibso.chicory.wasm.types.FunctionType;
10+
import com.dylibso.chicory.wasm.types.ValType;
611
import org.junit.jupiter.api.Test;
712

813
import java.io.InputStream;
14+
import java.util.List;
915

1016
import static org.junit.jupiter.api.Assertions.assertEquals;
1117
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1219

1320
class ChicoryUnitTest {
1421

1522
@Test
1623
void givenAddModule_whenCallingAddWithTwoAndForty_thenResultIsFortyTwo() {
17-
InputStream wasm = getClass().getResourceAsStream("/wasm/add.wasm");
24+
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
1825
assertNotNull(wasm);
1926

20-
var module = Parser.parse(wasm);
27+
WasmModule module = Parser.parse(wasm);
2128
Instance instance = Instance.builder(module).build();
2229
ExportFunction add = instance.export("add");
2330

2431
long[] result = add.apply(2, 40);
2532

2633
assertEquals(42, (int) result[0]);
2734
}
28-
}
35+
36+
@Test
37+
void givenImportDouble_whenCallingUseDouble_thenResultIsDoubled() {
38+
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
39+
assertNotNull(wasm);
40+
41+
HostFunction doubleFn = new HostFunction(
42+
"host",
43+
"double",
44+
FunctionType.of(List.of(ValType.I32), List.of(ValType.I32)),
45+
(Instance instance, long... args) -> new long[] { args[0] * 2 }
46+
);
47+
48+
Store store = new Store();
49+
store.addFunction(doubleFn);
50+
51+
WasmModule module = Parser.parse(wasm);
52+
Instance instance = store.instantiate("imports", module);
53+
ExportFunction useDouble = instance.export("useDouble");
54+
55+
long[] result = useDouble.apply(21);
56+
57+
assertEquals(42L, result[0]);
58+
}
59+
60+
@Test
61+
void whenInstantiatingModuleWithoutRequiredImport_thenErrorIsThrown() {
62+
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
63+
assertNotNull(wasm);
64+
65+
WasmModule module = Parser.parse(wasm);
66+
67+
assertThrows(RuntimeException.class, () -> {
68+
Instance.builder(module).build();
69+
});
70+
}
71+
72+
@Test
73+
void whenRequestingMissingExport_thenErrorIsThrown() {
74+
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
75+
assertNotNull(wasm);
76+
77+
WasmModule module = Parser.parse(wasm);
78+
Instance instance = Instance.builder(module).build();
79+
80+
assertThrows(RuntimeException.class, () -> instance.export("sum"));
81+
}
82+
83+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(func (export "add") (param i32 i32) (result i32)
3+
local.get 0
4+
local.get 1
5+
i32.add))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
digraph WasmOnJVM {
2+
rankdir=LR;
3+
node [shape=box, style=rounded];
4+
5+
subgraph cluster_host {
6+
label="Host (JVM)";
7+
style=rounded;
8+
host_app [label="JUnit test / application"];
9+
chicory [label="Chicory runtime\n(Instance)"];
10+
}
11+
12+
subgraph cluster_module {
13+
label="Wasm module";
14+
style=rounded;
15+
module [label=".wasm binary"];
16+
export_add [label="Exported functions"];
17+
imports [label="Imported functions"];
18+
}
19+
20+
host_app -> chicory [label="Java calls"];
21+
chicory -> module [label="load & instantiate"];
22+
chicory -> export_add [label="invoke exports"];
23+
export_add -> chicory [label="results"];
24+
chicory -> host_app [label="map to JVM types"];
25+
26+
chicory -> imports [label="provide imports", style=dashed];
27+
}
28+
87 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(import "host" "double" (func $double (param i32) (result i32)))
3+
(func (export "useDouble") (param i32) (result i32)
4+
local.get 0
5+
call $double))

libraries-bytecode/src/test/resources/wasm/add.wat

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)