diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-25 09:37:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-25 09:37:53 +0100 |
| commit | be55353a0bb22c39155e55c8571260c7dc3bba67 (patch) | |
| tree | 2d03c51908207872dd3d91515718230f09bdae07 /examples/11-transient-0/output/java | |
| parent | c09e0e47cfe3ff2938c84ccbef136fc56e277dcc (diff) | |
Implement shared values properly in java-transient-0
Diffstat (limited to 'examples/11-transient-0/output/java')
| -rw-r--r-- | examples/11-transient-0/output/java/src/test/java/org/zwobble/example/Transient0Tests.java | 24 |
1 files changed, 16 insertions, 8 deletions
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 <T> void assertRoundTripEncoding( + private <T> T assertRoundTripEncoding( T value, Encoder<T> encoder, Decoder<T> 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<T> { - void encode(T value, OutputStream outputStream, Map<String, Object> sharedValues) throws IOException; + void encode(T value, OutputStream outputStream, Map<Object, Long> sharedValues) throws IOException; } private interface Decoder<T> { - T decode(InputStream inputStream, Map<String, Object> sharedValues) throws IOException; + T decode(InputStream inputStream, List<Object> sharedValues) throws IOException; } } |
