1616
1717package com .igormaznitsa .jbbp .it ;
1818
19+ import static com .igormaznitsa .jbbp .io .JBBPByteOrder .LITTLE_ENDIAN ;
20+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertEquals ;
22+ import static org .junit .jupiter .api .Assertions .assertTrue ;
23+ import static org .junit .jupiter .api .Assertions .fail ;
24+
25+
1926import com .igormaznitsa .jbbp .JBBPParser ;
2027import com .igormaznitsa .jbbp .io .JBBPBitNumber ;
2128import com .igormaznitsa .jbbp .io .JBBPBitOrder ;
2936import com .igormaznitsa .jbbp .model .JBBPFieldArrayByte ;
3037import com .igormaznitsa .jbbp .model .JBBPFieldBit ;
3138import com .igormaznitsa .jbbp .model .JBBPFieldStruct ;
39+ import com .igormaznitsa .jbbp .utils .Function ;
3240import com .igormaznitsa .jbbp .utils .JBBPTextWriter ;
3341import com .igormaznitsa .jbbp .utils .JBBPTextWriterExtraAdapter ;
3442import com .igormaznitsa .jbbp .utils .JBBPUtils ;
35- import org .junit .jupiter .api .Test ;
36-
3743import java .io .ByteArrayOutputStream ;
3844import java .io .IOException ;
3945import java .io .InputStream ;
4046import java .lang .reflect .Field ;
41-
42- import static com .igormaznitsa .jbbp .io .JBBPByteOrder .LITTLE_ENDIAN ;
43- import com .igormaznitsa .jbbp .utils .Function ;
44- import static org .junit .jupiter .api .Assertions .*;
47+ import org .junit .jupiter .api .Test ;
4548
4649/**
4750 * Test to parse RLE encoded snapshots in well-known Z80 format (v.1) for
@@ -65,12 +68,30 @@ public void testRLEEncoding() throws Exception {
6568 assertArrayEquals (new byte [] {(byte ) 0xED , (byte ) 0xED , 8 , 5 , 1 , 2 , 3 , 0x00 , (byte ) 0xED , (byte ) 0xED , 0x00 }, JBBPOut .BeginBin ().Var (new RLEDataEncoder (), 1 , new byte [] {5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 1 , 2 , 3 }).End ().toByteArray ());
6669 }
6770
71+ private static final Function <Class <?>, Object > INSTANTIATOR = aClass -> {
72+ if (aClass == Flags .class ) {
73+ return new Flags ();
74+ }
75+ if (aClass == EmulFlags .class ) {
76+ return new EmulFlags ();
77+ }
78+ return null ;
79+ };
80+
81+ @ Test
82+ public void testParseAndWriteTestZ80WithoutCheckOfFields () throws Exception {
83+ assertParseAndPackBack ("test1.z80" , 16059 );
84+ assertParseAndPackBack ("test2.z80" , 29330 );
85+ assertParseAndPackBack ("test3.z80" , 5711 );
86+ assertParseAndPackBack ("test4.z80" , 9946 );
87+ }
88+
6889 private Z80Snapshot assertParseAndPackBack (final String name , final long etalonLen ) throws Exception {
6990 final Z80Snapshot z80sn ;
7091
7192 final InputStream resource = getResourceAsInputStream (name );
7293 try {
73- z80sn = z80Parser .parse (resource ).mapTo (new Z80Snapshot (), new DataProcessor (),INSTANTIATOR );
94+ z80sn = z80Parser .parse (resource ).mapTo (new Z80Snapshot (), new DataProcessor (), INSTANTIATOR );
7495 assertEquals (etalonLen , z80Parser .getFinalStreamByteCounter ());
7596 } finally {
7697 JBBPUtils .closeQuietly (resource );
@@ -103,14 +124,6 @@ private Z80Snapshot assertParseAndPackBack(final String name, final long etalonL
103124 return z80sn ;
104125 }
105126
106- @ Test
107- public void testParseAndWriteTestZ80WithoutCheckOfFields () throws Exception {
108- assertParseAndPackBack ("test1.z80" , 16059 );
109- assertParseAndPackBack ("test2.z80" , 29330 );
110- assertParseAndPackBack ("test3.z80" , 5711 );
111- assertParseAndPackBack ("test4.z80" , 9946 );
112- }
113-
114127 @ Test
115128 public void testParseAndWriteTestZ80WithCheckOfFields () throws Exception {
116129 final Z80Snapshot z80sn = assertParseAndPackBack ("test.z80" , 12429 );
@@ -119,8 +132,12 @@ public void testParseAndWriteTestZ80WithCheckOfFields() throws Exception {
119132
120133 @ Override
121134 public String doConvertCustomField (JBBPTextWriter context , Object obj , Field field , Bin annotation ) throws IOException {
122- final byte [] data = (byte []) extractFieldValue (obj , field );
123- return "byte array length [" + data .length + ']' ;
135+ try {
136+ final byte [] data = (byte []) field .get (obj );
137+ return "byte array length [" + data .length + ']' ;
138+ } catch (Exception ex ) {
139+ throw new RuntimeException (ex );
140+ }
124141 }
125142
126143 }).Bin (z80sn ).Close ().toString ();
@@ -170,12 +187,6 @@ public String doConvertCustomField(JBBPTextWriter context, Object obj, Field fie
170187 assertTrue (summ > 0 );
171188 }
172189
173- private static final Function <Class <?>,Object > INSTANTIATOR = aClass -> {
174- if (aClass == Flags .class ) return new Flags ();
175- if (aClass == EmulFlags .class ) return new EmulFlags ();
176- return null ;
177- };
178-
179190 @ Test
180191 public void testParseAndPackThrowMapping () throws Exception {
181192 final InputStream in = getResourceAsInputStream ("test.z80" );
0 commit comments