From 8455bb19b7d4235e667cab9112aa276557969dcc Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 5 Aug 2026 18:44:26 +0100 Subject: Type check generic native types --- examples/15-transient-0/output/java/.gitignore | 3 + examples/15-transient-0/output/java/pom.xml | 1 + .../java/org/zwobble/example/Transient0Tests.java | 353 +++++++++++++++++++++ 3 files changed, 357 insertions(+) create mode 100644 examples/15-transient-0/output/java/.gitignore create mode 120000 examples/15-transient-0/output/java/pom.xml create mode 100644 examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java (limited to 'examples/15-transient-0/output/java') diff --git a/examples/15-transient-0/output/java/.gitignore b/examples/15-transient-0/output/java/.gitignore new file mode 100644 index 0000000..2fcf0b8 --- /dev/null +++ b/examples/15-transient-0/output/java/.gitignore @@ -0,0 +1,3 @@ +/src/gen/ +/target/ +/transient-0/ diff --git a/examples/15-transient-0/output/java/pom.xml b/examples/15-transient-0/output/java/pom.xml new file mode 120000 index 0000000..445fa65 --- /dev/null +++ b/examples/15-transient-0/output/java/pom.xml @@ -0,0 +1 @@ +../../../templates/java-junit/pom.xml \ No newline at end of file diff --git a/examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java new file mode 100644 index 0000000..6732f72 --- /dev/null +++ b/examples/15-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java @@ -0,0 +1,353 @@ +package org.zwobble.example; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.zwobble.example.types.data.EnumWithVariants; +import org.zwobble.example.types.data.StructSingleton; +import org.zwobble.example.types.data.StructWithBool; +import org.zwobble.example.types.data.StructWithBox; +import org.zwobble.example.types.data.StructWithDifferentSharedTypes; +import org.zwobble.example.types.data.StructWithEnum; +import org.zwobble.example.types.data.StructWithInt8; +import org.zwobble.example.types.data.StructWithInt32; +import org.zwobble.example.types.data.StructWithInt64; +import org.zwobble.example.types.data.StructWithList; +import org.zwobble.example.types.data.StructWithListOfShared; +import org.zwobble.example.types.data.StructWithOption; +import org.zwobble.example.types.data.StructWithSharedSumAndVariant; +import org.zwobble.example.types.data.StructWithString; +import org.zwobble.example.types.data.StructWithStruct; +import org.zwobble.example.types.data.StructWithSum; +import org.zwobble.example.types.data.VariantOne; +import org.zwobble.example.types.data.VariantTwo; +import org.zwobble.example.types.data.transient0.HobgoblinTransient0Data; +import org.zwobble.example.types.transient0.HobgoblinTransient0TaggedSharedValue; + +public class Transient0Tests { + @Test + public void structSingleton() throws IOException { + var value = StructSingleton.INSTANCE; + + assertRoundTripEncoding( + "StructSingleton", + value, + HobgoblinTransient0Data::encodeStructSingleton, + HobgoblinTransient0Data::decodeStructSingleton + ); + } + + @Test + public void structWithBool() throws IOException { + var value = new StructWithBool(true, false); + + assertRoundTripEncoding( + "StructWithBool", + value, + HobgoblinTransient0Data::encodeStructWithBool, + HobgoblinTransient0Data::decodeStructWithBool + ); + } + + @Test + public void structWithInt8() throws IOException { + var value = new StructWithInt8((byte)10, (byte)25); + + assertRoundTripEncoding( + "StructWithInt8", + value, + HobgoblinTransient0Data::encodeStructWithInt8, + HobgoblinTransient0Data::decodeStructWithInt8 + ); + } + + @Test + public void structWithInt32() throws IOException { + var value = new StructWithInt32(10, 25); + + assertRoundTripEncoding( + "StructWithInt32", + value, + HobgoblinTransient0Data::encodeStructWithInt32, + HobgoblinTransient0Data::decodeStructWithInt32 + ); + } + + @Test + public void structWithInt64() throws IOException { + var value = new StructWithInt64(10, 25); + + assertRoundTripEncoding( + "StructWithInt64", + value, + HobgoblinTransient0Data::encodeStructWithInt64, + HobgoblinTransient0Data::decodeStructWithInt64 + ); + } + + @Test + public void structWithString() throws IOException { + var value = new StructWithString("abc", "def"); + + assertRoundTripEncoding( + "StructWithString", + value, + HobgoblinTransient0Data::encodeStructWithString, + HobgoblinTransient0Data::decodeStructWithString + ); + } + + @Test + public void structWithStruct() throws IOException { + var value = new StructWithStruct(new StructWithInt32(10, 25), 42); + + assertRoundTripEncoding( + "StructWithStruct", + value, + HobgoblinTransient0Data::encodeStructWithStruct, + HobgoblinTransient0Data::decodeStructWithStruct + ); + } + + @Test + public void structWithList() throws IOException { + var value = new StructWithList( + List.of(10L, 25L), + List.of( + new StructWithInt32(42, 47), + new StructWithInt32(52, 57), + new StructWithInt32(62, 67) + ) + ); + + assertRoundTripEncoding( + "StructWithList", + value, + HobgoblinTransient0Data::encodeStructWithList, + HobgoblinTransient0Data::decodeStructWithList + ); + } + + @Test + public void structWithOptionNone() throws IOException { + var value = new StructWithOption( + Optional.empty(), + Optional.empty() + ); + + assertRoundTripEncoding( + "StructWithOption_None", + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void structWithOptionSome() throws IOException { + var value = new StructWithOption( + Optional.of(10L), + Optional.of(new StructWithInt32(42, 47)) + ); + + assertRoundTripEncoding( + "StructWithOption_Some", + value, + HobgoblinTransient0Data::encodeStructWithOption, + HobgoblinTransient0Data::decodeStructWithOption + ); + } + + @Test + public void enumWithVariants() throws IOException { + assertRoundTripEncoding( + "EnumWithVariants_zero", + EnumWithVariants.ZERO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + "EnumWithVariants_one", + EnumWithVariants.ONE, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + + assertRoundTripEncoding( + "EnumWithVariants_two", + EnumWithVariants.TWO, + HobgoblinTransient0Data::encodeEnumWithVariants, + HobgoblinTransient0Data::decodeEnumWithVariants + ); + } + + @Test + public void structWithEnum() throws IOException { + var value = new StructWithEnum( + EnumWithVariants.ONE, + 42 + ); + + assertRoundTripEncoding( + "StructWithEnum", + value, + HobgoblinTransient0Data::encodeStructWithEnum, + HobgoblinTransient0Data::decodeStructWithEnum + ); + } + + @Test + public void sumWithVariants() throws IOException { + assertRoundTripEncoding( + "SumWithVariants_VariantOne", + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + + assertRoundTripEncoding( + "SumWithVariants_VariantTwo", + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithVariants, + HobgoblinTransient0Data::decodeSumWithVariants + ); + } + + @Test + public void sumWithBoxedVariants() throws IOException { + assertRoundTripEncoding( + "SumWithBoxedVariants_VariantOne", + new VariantOne(10), + HobgoblinTransient0Data::encodeSumWithBoxedVariants, + HobgoblinTransient0Data::decodeSumWithBoxedVariants + ); + + assertRoundTripEncoding( + "SumWithBoxedVariants_VariantTwo", + new VariantTwo(25), + HobgoblinTransient0Data::encodeSumWithBoxedVariants, + HobgoblinTransient0Data::decodeSumWithBoxedVariants + ); + } + + @Test + public void structWithSum() throws IOException { + var value = new StructWithSum( + new VariantTwo(25), + 42 + ); + + assertRoundTripEncoding( + "StructWithSum", + value, + HobgoblinTransient0Data::encodeStructWithSum, + HobgoblinTransient0Data::decodeStructWithSum + ); + } + + @Test + public void structWithBox() throws IOException { + var value = new StructWithBox( + new StructWithInt32(10, 25) + ); + + assertRoundTripEncoding( + "StructWithBox", + value, + HobgoblinTransient0Data::encodeStructWithBox, + HobgoblinTransient0Data::decodeStructWithBox + ); + } + + @Test + public void structWithListOfShared() throws IOException { + var a = new StructWithInt32(10, 25); + var b = new StructWithInt32(42, 47); + + var value = new StructWithListOfShared(List.of(a, a, b, a, b)); + + var decodedValue = assertRoundTripEncoding( + "StructWithListOfShared", + value, + HobgoblinTransient0Data::encodeStructWithListOfShared, + HobgoblinTransient0Data::decodeStructWithListOfShared + ); + Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(1)); + Assertions.assertSame(decodedValue.inner().get(0), decodedValue.inner().get(3)); + Assertions.assertSame(decodedValue.inner().get(2), decodedValue.inner().get(4)); + } + + @Test + public void structWithDifferentSharedTypes() throws IOException { + var a = new StructWithInt32(10, 25); + var b = new StructWithInt64(42, 47); + var c = new StructWithInt32(52, 57); + + var value = new StructWithDifferentSharedTypes(a, b, c); + + assertRoundTripEncoding( + "StructWithDifferentSharedTypes", + value, + HobgoblinTransient0Data::encodeStructWithDifferentSharedTypes, + HobgoblinTransient0Data::decodeStructWithDifferentSharedTypes + ); + } + + @Test + public void structWithSharedSumAndVariant() throws IOException { + var a = new VariantOne(10); + + var value = new StructWithSharedSumAndVariant(a, a); + + assertRoundTripEncoding( + "StructWithSharedSumAndVariant", + value, + HobgoblinTransient0Data::encodeStructWithSharedSumAndVariant, + HobgoblinTransient0Data::decodeStructWithSharedSumAndVariant + ); + } + + private T assertRoundTripEncoding( + String name, + T value, + Encoder encoder, + Decoder decoder + ) throws IOException { + var outputStream = new ByteArrayOutputStream(); + encoder.encode(value, outputStream, new HashMap<>()); + var bytes = outputStream.toByteArray(); + + var outputDir = System.getenv("HOBGOBLIN_OUTPUT_DIR"); + var transient0OutputDir = Path.of(outputDir).resolve("transient-0"); + Files.createDirectories(transient0OutputDir); + var transient0OutputPath = transient0OutputDir.resolve(name); + Files.write(transient0OutputPath, bytes); + + var inputStream = new ByteArrayInputStream(bytes); + var decodedValue = decoder.decode(inputStream, new ArrayList<>()); + + Assertions.assertEquals(value, decodedValue); + + return decodedValue; + } + + private interface Encoder { + void encode(T value, OutputStream outputStream, Map sharedValues) throws IOException; + } + + private interface Decoder { + T decode(InputStream inputStream, List sharedValues) throws IOException; + } +} -- cgit v1.2.3