From be55353a0bb22c39155e55c8571260c7dc3bba67 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 25 Jul 2026 09:37:53 +0100 Subject: Implement shared values properly in java-transient-0 --- .../java/org/zwobble/example/Transient0Tests.java | 24 ++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/11-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java b/examples/11-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java index 0110de3..a482612 100644 --- a/examples/11-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java +++ b/examples/11-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java @@ -5,6 +5,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -194,37 +195,44 @@ public class Transient0Tests { @Test public void structWithShared() throws IOException { - var value = new StructWithShared(List.of( - new StructWithInt32(10, 25) - )); + var a = new StructWithInt32(10, 25); + var b = new StructWithInt32(42, 47); - assertRoundTripEncoding( + var value = new StructWithShared(List.of(a, a, b, a, b)); + + var decodedValue = assertRoundTripEncoding( value, HobgoblinTransient0Data::encodeStructWithShared, HobgoblinTransient0Data::decodeStructWithShared ); + 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)); } - private void assertRoundTripEncoding( + private T assertRoundTripEncoding( T value, Encoder encoder, Decoder decoder ) throws IOException { var outputStream = new ByteArrayOutputStream(); + // TODO: should this use IdentityHashMap? Leave it up the caller? encoder.encode(value, outputStream, new HashMap<>()); var bytes = outputStream.toByteArray(); var inputStream = new ByteArrayInputStream(bytes); - var decodedValue = decoder.decode(inputStream, new HashMap<>()); + 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; + void encode(T value, OutputStream outputStream, Map sharedValues) throws IOException; } private interface Decoder { - T decode(InputStream inputStream, Map sharedValues) throws IOException; + T decode(InputStream inputStream, List sharedValues) throws IOException; } } -- cgit v1.2.3