aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-24 20:43:59 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-24 20:43:59 +0100
commit41d4ba7f621b613a8c332addf2bd54f2c78e2d72 (patch)
treebb16976ff8557f5949076fae2c01fe53bf35de6e /src/test/java
parent7880da50fa71d100d0052530a8baa937fa2f3197 (diff)
WASM binary format: support functions
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java21
-rw-r--r--src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java29
2 files changed, 46 insertions, 4 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
index 6d3684db..cb1e7a76 100644
--- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/WasmBinaryFormatModuleTests.java
@@ -2,6 +2,8 @@ package org.shedlang.compiler.backends.wasm.structure;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI32Const;
+import org.shedlang.compiler.backends.wasm.structure.ast.instructions.WasmI64Const;
import org.shedlang.compiler.backends.wasm.structure.ast.modules.*;
import org.shedlang.compiler.backends.wasm.structure.ast.types.*;
import org.shedlang.compiler.testing.ProcessRunner;
@@ -40,6 +42,25 @@ public class WasmBinaryFormatModuleTests {
}
@Test
+ public void canSerializeModuleWithFunctions(Snapshotter snapshotter) throws IOException, InterruptedException {
+ var module = WasmModule.EMPTY
+ .addFunction(new WasmFunction(
+ new WasmFuncType(List.of(), List.of(WasmValueType.I32)),
+ List.of(WasmValueType.F32, WasmValueType.F64),
+ List.of(new WasmI32Const(0))
+ ))
+ .addFunction(new WasmFunction(
+ new WasmFuncType(List.of(), List.of(WasmValueType.I64)),
+ List.of(),
+ List.of(new WasmI64Const(0))
+ ));
+
+ var result = serializeModule(module);
+
+ snapshotter.assertSnapshot(result);
+ }
+
+ @Test
public void canSerializeModuleWithFunctionImports(Snapshotter snapshotter) throws IOException, InterruptedException {
var module = WasmModule.EMPTY
.addImport(new WasmImport(
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java
index acb5ab9f..f9ef502d 100644
--- a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java
+++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/Leb128EncodingTests.java
@@ -10,7 +10,7 @@ import static org.zwobble.precisely.Matchers.equalTo;
public class Leb128EncodingTests {
@Test
- public void unsignedEncoding() {
+ public void unsignedInt32Encoding() {
assertUnsignedInt32Encoding(0x0, "00");
assertUnsignedInt32Encoding(0x1, "01");
assertUnsignedInt32Encoding(0x7F, "7f");
@@ -25,7 +25,7 @@ public class Leb128EncodingTests {
}
@Test
- public void paddedUnsignedEncoding() {
+ public void paddedUnsignedInt32Encoding() {
assertPaddedUnsignedInt32Encoding(0x0, "8080808000");
assertPaddedUnsignedInt32Encoding(0x1, "8180808000");
assertPaddedUnsignedInt32Encoding(0x7F, "ff80808000");
@@ -40,7 +40,7 @@ public class Leb128EncodingTests {
}
@Test
- public void signedEncoding() {
+ public void signedInt32Encoding() {
assertSignedInt32Encoding(0x0, "00");
assertSignedInt32Encoding(0x1, "01");
assertSignedInt32Encoding(0x3F, "3f");
@@ -59,7 +59,7 @@ public class Leb128EncodingTests {
}
@Test
- public void paddedSignedEncoding() {
+ public void paddedSignedInt32Encoding() {
assertPaddedSignedInt32Encoding(0x0, "8080808000");
assertPaddedSignedInt32Encoding(0x1, "8180808000");
assertPaddedSignedInt32Encoding(0x3F, "bf80808000");
@@ -77,6 +77,27 @@ public class Leb128EncodingTests {
assertByteArray(writer.toBytes(), expectedHex);
}
+ @Test
+ public void signedInt64Encoding() {
+ assertSignedInt64Encoding(0x0, "00");
+ assertSignedInt64Encoding(0x1, "01");
+ assertSignedInt64Encoding(0x3F, "3f");
+ assertSignedInt64Encoding(0x40, "c000");
+ assertSignedInt64Encoding(0x7F, "ff00");
+ assertSignedInt64Encoding(0x80, "8001");
+ assertSignedInt64Encoding(-0x1, "7f");
+ assertSignedInt64Encoding(-0x40, "40");
+ assertSignedInt64Encoding(-0x41, "bf7f");
+ assertSignedInt64Encoding(Long.MAX_VALUE, "ffffffffffffffffff00");
+ assertSignedInt64Encoding(Long.MIN_VALUE, "8080808080808080807f");
+ }
+
+ private void assertSignedInt64Encoding(long value, String expectedHex) {
+ var writer = new BinaryWriter();
+ Leb128Encoding.encodeSignedInt64(value, writer);
+ assertByteArray(writer.toBytes(), expectedHex);
+ }
+
private void assertByteArray(byte[] actual, String expectedHex) {
assertThat(HexFormat.of().formatHex(actual), equalTo(expectedHex));
}