diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-21 16:37:44 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-21 16:37:44 +0000 |
| commit | 2242b12c09562b0227c8ee875750930f42b06b50 (patch) | |
| tree | 5e5724af6214ec75b10edefd234d69b353e5ae70 /src/test/java | |
| parent | 4ff129c58d805a6c5887c1d6a1063a238e37d70b (diff) | |
Check that we're not using placeholder unrelocatable values
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java new file mode 100644 index 00000000..b7effd0e --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/wasm/structure/binary/WasmBinaryWriterTests.java @@ -0,0 +1,50 @@ +package org.shedlang.compiler.backends.wasm.structure.binary; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.backends.wasm.structure.ast.linking.WasmMemoryAddress; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmFuncName; +import org.shedlang.compiler.backends.wasm.structure.ast.modules.WasmModule; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class WasmBinaryWriterTests { + @Test + public void whenNotInRelocatableSectionThenWritingMemoryAddressConstThrowsError() throws IOException { + var writer = new WasmBinaryWriter(WasmIndexTable.fromModule(WasmModule.EMPTY)); + + var error = assertThrows( + IllegalStateException.class, + () -> writer.writeSection( + WasmSectionId.TYPE, + () -> writer.writeMemoryAddressConst(new WasmMemoryAddress("x")) + ) + ); + + assertThat( + error.getMessage(), + equalTo("cannot write memory address outside of relocatable section") + ); + } + + @Test + public void whenNotInRelocatableSectionThenWritingTableElementIndexConstThrowsError() throws IOException { + var writer = new WasmBinaryWriter(WasmIndexTable.fromModule(WasmModule.EMPTY)); + + var error = assertThrows( + IllegalStateException.class, + () -> writer.writeSection( + WasmSectionId.TYPE, + () -> writer.writeTableElementIndexConst(new WasmFuncName("f")) + ) + ); + + assertThat( + error.getMessage(), + equalTo("cannot write memory address outside of relocatable section") + ); + } +} |
