Debian Codegraph @

jbbp-plugins/jbbp-gradle-tests/jbbp-gradle6-plugin-test/src/test/java/org/example/MainTest.java

raw
1 package org.example;
2
3 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4 import static org.junit.jupiter.api.Assertions.assertEquals;
5
6 import com.igormaznitsa.jbbp.JBBPParser;
7 import com.igormaznitsa.jbbp.io.JBBPBitInputStream;
8 import com.igormaznitsa.jbbp.io.JBBPBitOutputStream;
9 import com.igormaznitsa.mvn.tst.VarCustomImpl;
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.util.Random;
13 import java.io.IOException;
14 import org.junit.jupiter.api.Test;
15
16 import com.igormaznitsa.mvn.test.jbbp.*;
17
18 public class MainTest {
19
20 @Test
21 void testReadWrite_Annotations_Static() throws IOException {
22 final byte[] testData = new byte[] {
23 4,
24 (byte)0xFF, (byte)0x1A, (byte)0x1B, (byte)0x1C,
25 (byte)0xFF, (byte)0x2A, (byte)0x2B, (byte)0x2C,
26 (byte) 0x12, (byte) 0x34, 3, 5, 6, 7
27 };
28
29 final GenAnnotations result =
30 new GenAnnotations().read(new JBBPBitInputStream(new ByteArrayInputStream(testData)));
31 assertEquals(4, result.getLEN());
32 assertEquals(3, result.getSOME1().getSOME2().getFIELD().length);
33
34 final String script = "ubyte len;"
35 + " uint uintField;"
36 + " uint [1] uintArr;"
37 + "some1 {"
38 + " bit:4 [len] someField;"
39 + " ubyte len;"
40 + " some2 {"
41 + " byte [len] field;"
42 + " }"
43 + "}";
44
45 final GenAnnotations instance =
46 JBBPParser.prepare(script).parse(testData).mapTo(new GenAnnotations());
47
48 assertEquals(result.getUINTFIELD(), instance.getUINTFIELD());
49 assertArrayEquals(result.getUINTARR(), instance.getUINTARR());
50 assertEquals(result.getLEN(), instance.getLEN());
51 assertEquals(result.getSOME1().getLEN(), instance.getSOME1().getLEN());
52 assertArrayEquals(result.getSOME1().getSOMEFIELD(), instance.getSOME1().getSOMEFIELD());
53 assertArrayEquals(result.getSOME1().getSOME2().getFIELD(),
54 instance.getSOME1().getSOME2().getFIELD());
55 }
56
57 @Test
58 void testReadWrite_Annotations_NonStatic() throws IOException {
59 final byte[] testData = new byte[] {4, (byte) 0x12, (byte) 0x34, 3, 5, 6, 7};
60
61 final GenAnnotationsNonStatic result = new GenAnnotationsNonStatic()
62 .read(new JBBPBitInputStream(new ByteArrayInputStream(testData)));
63 assertEquals(4, result.getLEN());
64 assertEquals(3, result.getSOME1().getSOME2().getFIELD().length);
65
66 final String script = "ubyte len;"
67 + "some1 {"
68 + " bit:4 [len] someField;"
69 + " ubyte len;"
70 + " some2 {"
71 + " byte [len] field;"
72 + " }"
73 + "}";
74
75 final GenAnnotationsNonStatic instance =
76 JBBPParser.prepare(script).parse(testData).mapTo(new GenAnnotationsNonStatic());
77 assertEquals(result.getLEN(), instance.getLEN());
78 assertEquals(result.getSOME1().getLEN(), instance.getSOME1().getLEN());
79 assertArrayEquals(result.getSOME1().getSOMEFIELD(), instance.getSOME1().getSOMEFIELD());
80 assertArrayEquals(result.getSOME1().getSOME2().getFIELD(),
81 instance.getSOME1().getSOME2().getFIELD());
82 }
83
84 private static final Random RND = new Random(12345);
85
86 @Test
87 public void testReadWrite_VarCustom() throws Exception {
88 final VarCustomImpl impl = new VarCustomImpl();
89
90 final byte[] etalonArray = new byte[319044];
91 RND.nextBytes(etalonArray);
92 etalonArray[0] = 1;
93
94 impl.read(new JBBPBitInputStream(new ByteArrayInputStream(etalonArray)));
95
96 assertEquals(1, impl.getBYTEA());
97
98 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
99 final JBBPBitOutputStream bios = new JBBPBitOutputStream(bos);
100
101 impl.write(bios);
102 bios.close();
103
104 assertArrayEquals(etalonArray, bos.toByteArray());
105 }
106
107 @Test
108 public void testRead_DefaultBitOrder() throws IOException {
109 final WholeStreamByteArray struct = new WholeStreamByteArray().read(new JBBPBitInputStream(
110 new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})));
111 assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, struct.getARRAY());
112 }
113
114 @Test
115 public void testWrite_DefaultBitOrder() throws IOException {
116 final WholeStreamByteArray struct = new WholeStreamByteArray();
117 struct.setARRAY(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
118 final ByteArrayOutputStream out = new ByteArrayOutputStream();
119 final JBBPBitOutputStream bitOut = new JBBPBitOutputStream(out);
120 struct.write(bitOut);
121 bitOut.close();
122 assertArrayEquals(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, out.toByteArray());
123 }
124 }