diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2021-12-14 19:29:26 +0100 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2021-12-14 19:29:26 +0100 |
commit | 35f7a20b22271f8f28d15da8df85cff2fc9cd716 (patch) | |
tree | 496e011fd0cf8c34d2634ce991f287b6a4ee4838 /src/test/java | |
parent | 4c32c3998476e12c34984d52253eaa4ae3bf6769 (diff) |
Simplify types, replace Value with Variant.
Change-Id: I2e492ebfefc7e9a47c874ed22ff199412e9948ee
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/eu/mulk/jgvariant/core/DecoderTest.java | 123 |
1 files changed, 43 insertions, 80 deletions
diff --git a/src/test/java/eu/mulk/jgvariant/core/DecoderTest.java b/src/test/java/eu/mulk/jgvariant/core/DecoderTest.java index 13cd398..d37f6a2 100644 --- a/src/test/java/eu/mulk/jgvariant/core/DecoderTest.java +++ b/src/test/java/eu/mulk/jgvariant/core/DecoderTest.java @@ -4,13 +4,6 @@ import static java.nio.ByteOrder.LITTLE_ENDIAN; import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.assertEquals; -import eu.mulk.jgvariant.core.Value.Array; -import eu.mulk.jgvariant.core.Value.Bool; -import eu.mulk.jgvariant.core.Value.Int32; -import eu.mulk.jgvariant.core.Value.Int8; -import eu.mulk.jgvariant.core.Value.Maybe; -import eu.mulk.jgvariant.core.Value.Str; -import eu.mulk.jgvariant.core.Value.Structure; import java.nio.ByteBuffer; import java.util.List; import java.util.Optional; @@ -25,26 +18,23 @@ class DecoderTest { @Test void testString() { var data = new byte[] {0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x00}; - var decoder = Decoder.ofStr(UTF_8); - assertEquals(new Str("hello world"), decoder.decode(ByteBuffer.wrap(data))); + var decoder = Decoder.ofString(UTF_8); + assertEquals("hello world", decoder.decode(ByteBuffer.wrap(data))); } @Test void testMaybe() { var data = new byte[] {0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x00, 0x00}; - var decoder = Decoder.ofMaybe(Decoder.ofStr(UTF_8)); - assertEquals( - new Maybe<>(Optional.of(new Str("hello world"))), decoder.decode(ByteBuffer.wrap(data))); + var decoder = Decoder.ofMaybe(Decoder.ofString(UTF_8)); + assertEquals(Optional.of("hello world"), decoder.decode(ByteBuffer.wrap(data))); } @Test void testBooleanArray() { var data = new byte[] {0x01, 0x00, 0x00, 0x01, 0x01}; - var decoder = Decoder.ofArray(Decoder.ofBool()); - assertEquals( - new Array<>(List.of(Bool.TRUE, Bool.FALSE, Bool.FALSE, Bool.TRUE, Bool.TRUE)), - decoder.decode(ByteBuffer.wrap(data))); + var decoder = Decoder.ofArray(Decoder.ofBoolean()); + assertEquals(List.of(true, false, false, true, true), decoder.decode(ByteBuffer.wrap(data))); } @Test @@ -54,12 +44,10 @@ class DecoderTest { 0x66, 0x6F, 0x6F, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x04 }; - record TestRecord(Str s, Int32 i) {} + record TestRecord(String s, int i) {} - var decoder = Decoder.ofStructure(TestRecord.class, Decoder.ofStr(UTF_8), Decoder.ofInt32()); - assertEquals( - new Structure<>(new TestRecord(new Str("foo"), new Int32(-1))), - decoder.decode(ByteBuffer.wrap(data))); + var decoder = Decoder.ofStructure(TestRecord.class, Decoder.ofString(UTF_8), Decoder.ofInt()); + assertEquals(new TestRecord("foo", -1), decoder.decode(ByteBuffer.wrap(data))); } @Test @@ -91,19 +79,16 @@ class DecoderTest { 0x15 }; - record TestRecord(Str s, Int32 i) {} + record TestRecord(String s, int i) {} var decoder = Decoder.ofArray( Decoder.ofStructure( TestRecord.class, - Decoder.ofStr(UTF_8), - Decoder.ofInt32().withByteOrder(LITTLE_ENDIAN))); + Decoder.ofString(UTF_8), + Decoder.ofInt().withByteOrder(LITTLE_ENDIAN))); assertEquals( - new Array<>( - List.of( - new Structure<>(new TestRecord(new Str("hi"), new Int32(-2))), - new Structure<>(new TestRecord(new Str("bye"), new Int32(-1))))), + List.of(new TestRecord("hi", -2), new TestRecord("bye", -1)), decoder.decode(ByteBuffer.wrap(data))); } @@ -114,10 +99,8 @@ class DecoderTest { 0x69, 0x00, 0x63, 0x61, 0x6E, 0x00, 0x68, 0x61, 0x73, 0x00, 0x73, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x73, 0x3F, 0x00, 0x02, 0x06, 0x0a, 0x13 }; - var decoder = Decoder.ofArray(Decoder.ofStr(UTF_8)); - assertEquals( - new Array<>(List.of(new Str("i"), new Str("can"), new Str("has"), new Str("strings?"))), - decoder.decode(ByteBuffer.wrap(data))); + var decoder = Decoder.ofArray(Decoder.ofString(UTF_8)); + assertEquals(List.of("i", "can", "has", "strings?"), decoder.decode(ByteBuffer.wrap(data))); } @Test @@ -128,20 +111,17 @@ class DecoderTest { 0x73, 0x3F, 0x00, 0x04, 0x0d, 0x05 }; - record TestChild(Int8 b, Str s) {} - record TestParent(Structure<TestChild> tc, Array<Str> as) {} + record TestChild(byte b, String s) {} + record TestParent(TestChild tc, List<String> as) {} var decoder = Decoder.ofStructure( TestParent.class, - Decoder.ofStructure(TestChild.class, Decoder.ofInt8(), Decoder.ofStr(UTF_8)), - Decoder.ofArray(Decoder.ofStr(UTF_8))); + Decoder.ofStructure(TestChild.class, Decoder.ofByte(), Decoder.ofString(UTF_8)), + Decoder.ofArray(Decoder.ofString(UTF_8))); assertEquals( - new Structure<>( - new TestParent( - new Structure<>(new TestChild(new Int8((byte) 0x69), new Str("can"))), - new Array<>(List.of(new Str("has"), new Str("strings?"))))), + new TestParent(new TestChild((byte) 0x69, "can"), List.of("has", "strings?")), decoder.decode(ByteBuffer.wrap(data))); } @@ -149,51 +129,45 @@ class DecoderTest { void testSimpleStructure() { var data = new byte[] {0x60, 0x70}; - record TestRecord(Int8 b1, Int8 b2) {} + record TestRecord(byte b1, byte b2) {} var decoder = Decoder.ofStructure( TestRecord.class, - Decoder.ofInt8().withByteOrder(LITTLE_ENDIAN), - Decoder.ofInt8().withByteOrder(LITTLE_ENDIAN)); + Decoder.ofByte().withByteOrder(LITTLE_ENDIAN), + Decoder.ofByte().withByteOrder(LITTLE_ENDIAN)); - assertEquals( - new Structure<>(new TestRecord(new Int8((byte) 0x60), new Int8((byte) 0x70))), - decoder.decode(ByteBuffer.wrap(data))); + assertEquals(new TestRecord((byte) 0x60, (byte) 0x70), decoder.decode(ByteBuffer.wrap(data))); } @Test void testPaddedStructureRight() { var data = new byte[] {0x60, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00}; - record TestRecord(Int32 b1, Int8 b2) {} + record TestRecord(int b1, byte b2) {} var decoder = Decoder.ofStructure( TestRecord.class, - Decoder.ofInt32().withByteOrder(LITTLE_ENDIAN), - Decoder.ofInt8().withByteOrder(LITTLE_ENDIAN)); + Decoder.ofInt().withByteOrder(LITTLE_ENDIAN), + Decoder.ofByte().withByteOrder(LITTLE_ENDIAN)); - assertEquals( - new Structure<>(new TestRecord(new Int32(0x60), new Int8((byte) 0x70))), - decoder.decode(ByteBuffer.wrap(data))); + assertEquals(new TestRecord(0x60, (byte) 0x70), decoder.decode(ByteBuffer.wrap(data))); } @Test void testPaddedStructureLeft() { var data = new byte[] {0x60, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00}; - record TestRecord(Int8 b1, Int32 b2) {} + record TestRecord(byte b1, int b2) {} var decoder = Decoder.ofStructure( TestRecord.class, - Decoder.ofInt8().withByteOrder(LITTLE_ENDIAN), - Decoder.ofInt32().withByteOrder(LITTLE_ENDIAN)); + Decoder.ofByte().withByteOrder(LITTLE_ENDIAN), + Decoder.ofInt().withByteOrder(LITTLE_ENDIAN)); - assertEquals( - new Structure<>(new TestRecord(new Int8((byte) 0x60), new Int32(0x70))), - decoder.decode(ByteBuffer.wrap(data))); + assertEquals(new TestRecord((byte) 0x60, 0x70), decoder.decode(ByteBuffer.wrap(data))); } @Test @@ -218,20 +192,17 @@ class DecoderTest { 0x00 }; - record TestRecord(Int32 b1, Int8 b2) {} + record TestRecord(int b1, byte b2) {} var decoder = Decoder.ofArray( Decoder.ofStructure( TestRecord.class, - Decoder.ofInt32().withByteOrder(LITTLE_ENDIAN), - Decoder.ofInt8().withByteOrder(LITTLE_ENDIAN))); + Decoder.ofInt().withByteOrder(LITTLE_ENDIAN), + Decoder.ofByte().withByteOrder(LITTLE_ENDIAN))); assertEquals( - new Array<>( - List.of( - new Structure<>(new TestRecord(new Int32(96), new Int8((byte) 0x70))), - new Structure<>(new TestRecord(new Int32(648), new Int8((byte) 0xf7))))), + List.of(new TestRecord(96, (byte) 0x70), new TestRecord(648, (byte) 0xf7)), decoder.decode(ByteBuffer.wrap(data))); } @@ -239,15 +210,10 @@ class DecoderTest { void testByteArray() { var data = new byte[] {0x04, 0x05, 0x06, 0x07}; - var decoder = Decoder.ofArray(Decoder.ofInt8()); + var decoder = Decoder.ofArray(Decoder.ofByte()); assertEquals( - new Array<>( - List.of( - new Int8((byte) 0x04), - new Int8((byte) 0x05), - new Int8((byte) 0x06), - new Int8((byte) 0x07))), + List.of((byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07), decoder.decode(ByteBuffer.wrap(data))); } @@ -255,10 +221,9 @@ class DecoderTest { void testIntegerArray() { var data = new byte[] {0x04, 0x00, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00}; - var decoder = Decoder.ofArray(Decoder.ofInt32().withByteOrder(LITTLE_ENDIAN)); + var decoder = Decoder.ofArray(Decoder.ofInt().withByteOrder(LITTLE_ENDIAN)); - assertEquals( - new Array<>(List.of(new Int32(4), new Int32(258))), decoder.decode(ByteBuffer.wrap(data))); + assertEquals(List.of(4, 258), decoder.decode(ByteBuffer.wrap(data))); } @Test @@ -266,13 +231,11 @@ class DecoderTest { var data = new byte[] {0x61, 0x20, 0x6B, 0x65, 0x79, 0x00, 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x06}; - record TestEntry(Str key, Int32 value) {} + record TestEntry(String key, int value) {} var decoder = Decoder.ofStructure( - TestEntry.class, Decoder.ofStr(UTF_8), Decoder.ofInt32().withByteOrder(LITTLE_ENDIAN)); - assertEquals( - new Structure<>(new TestEntry(new Str("a key"), new Int32(514))), - decoder.decode(ByteBuffer.wrap(data))); + TestEntry.class, Decoder.ofString(UTF_8), Decoder.ofInt().withByteOrder(LITTLE_ENDIAN)); + assertEquals(new TestEntry("a key", 514), decoder.decode(ByteBuffer.wrap(data))); } } |