1111import de .rub .nds .modifiablevariable .ModifiableVariableFactory ;
1212import de .rub .nds .modifiablevariable .bytearray .ModifiableByteArray ;
1313import java .math .BigInteger ;
14+ import java .util .Random ;
15+ import org .hamcrest .core .IsEqual ;
1416import static org .junit .Assert .assertArrayEquals ;
1517import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .assertThat ;
1619import org .junit .Test ;
1720
1821public class ArrayConverterTest {
@@ -38,15 +41,15 @@ public void testLongToUint32Bytes() {
3841 public void testIntToBytes () {
3942 int toParse = 5717 ;
4043 byte [] result = ArrayConverter .intToBytes (toParse , 2 );
41- assertArrayEquals ("The conversion result of 5717 should be {0x16} {0x55}" , new byte [] { 0x16 , 0x55 }, result );
44+ assertArrayEquals ("The conversion result of 5717 should be {0x16} {0x55}" , new byte []{ 0x16 , 0x55 }, result );
4245 }
4346
4447 /**
4548 * Test of bytesToInt method, of class ArrayConverter.
4649 */
4750 @ Test
4851 public void testBytesToInt () {
49- byte [] toParse = { 0x16 , 0x55 };
52+ byte [] toParse = {0x16 , 0x55 };
5053 int expectedResult = 5717 ;
5154 assertEquals ("The conversion result of {0x16, 0x55} should be 5717" , expectedResult ,
5255 ArrayConverter .bytesToInt (toParse ));
@@ -72,17 +75,17 @@ public void testBytesToLong() {
7275 */
7376 @ Test
7477 public void testBytesToHexString_byteArr () {
75- byte [] toTest = new byte [] { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 };
78+ byte [] toTest = new byte []{ 0x00 , 0x11 , 0x22 , 0x33 , 0x44 };
7679 assertEquals ("00 11 22 33 44" , ArrayConverter .bytesToHexString (toTest ));
77- toTest = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 };
80+ toTest = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 };
7881 assertEquals ("00 01 02 03 04 05 06 07 08" , ArrayConverter .bytesToHexString (toTest ));
79- toTest = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x10 };
82+ toTest = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x10 };
8083 assertEquals ("00 01 02 03 04 05 06 07 08 09 10" , ArrayConverter .bytesToHexString (toTest ));
81- toTest = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ,
82- 0x07 , };
84+ toTest = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ,
85+ 0x07 ,};
8386 assertEquals ("\n 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07" , ArrayConverter .bytesToHexString (toTest ));
84- toTest = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ,
85- 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , };
87+ toTest = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ,
88+ 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,};
8689 assertEquals (
8790 "\n 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07\n 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07" ,
8891 ArrayConverter .bytesToHexString (toTest ));
@@ -93,9 +96,9 @@ public void testBytesToHexString_byteArr() {
9396 */
9497 @ Test
9598 public void testBytesToHexString_byteArr_boolean () {
96- byte [] toTest = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 ,
97- 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
98- 0x06 , 0x07 };
99+ byte [] toTest = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 ,
100+ 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
101+ 0x06 , 0x07 };
99102 assertEquals ("00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07" ,
100103 ArrayConverter .bytesToHexString (toTest , false ));
101104 assertEquals (
@@ -115,9 +118,9 @@ public void testBytesToHexString_3args() {
115118 */
116119 @ Test
117120 public void testBytesToRawHexString () {
118- byte [] toTest = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 ,
119- 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
120- 0x06 , 0x07 };
121+ byte [] toTest = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 ,
122+ 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
123+ 0x06 , 0x07 };
121124 assertEquals ("0001020304050607000102030405060700010203040506070001020304050607" ,
122125 ArrayConverter .bytesToRawHexString (toTest ));
123126 }
@@ -170,13 +173,13 @@ public void testConvertListToArray() {
170173 @ Test
171174 public void testHexStringToByteArray () {
172175 String hex = "01" ;
173- assertArrayEquals ("Testing simple one byte hex value" , new byte [] { 0x01 },
176+ assertArrayEquals ("Testing simple one byte hex value" , new byte []{ 0x01 },
174177 ArrayConverter .hexStringToByteArray (hex ));
175178 hex = "FF" ;
176- assertArrayEquals ("Testing one byte hex value > 0x7f" , new byte [] { (byte ) 0xff },
179+ assertArrayEquals ("Testing one byte hex value > 0x7f" , new byte []{ (byte ) 0xff },
177180 ArrayConverter .hexStringToByteArray (hex ));
178181 hex = "FFFFFF" ;
179- assertArrayEquals ("Testing one byte hex value > 0x7f" , new byte [] { (byte ) 0xff , (byte ) 0xff , (byte ) 0xff },
182+ assertArrayEquals ("Testing one byte hex value > 0x7f" , new byte []{ (byte ) 0xff , (byte ) 0xff , (byte ) 0xff },
180183 ArrayConverter .hexStringToByteArray (hex ));
181184 }
182185
@@ -186,7 +189,7 @@ public void testBigIntegerToNullPaddedByteArray() {
186189
187190 assertArrayEquals ("Check zero output size" , new byte [0 ],
188191 ArrayConverter .bigIntegerToNullPaddedByteArray (test , 0 ));
189- assertArrayEquals ("Check check output size smaller than input" , new byte [] { (byte ) 0xEC },
192+ assertArrayEquals ("Check check output size smaller than input" , new byte []{ (byte ) 0xEC },
190193 ArrayConverter .bigIntegerToNullPaddedByteArray (test , 1 ));
191194 assertArrayEquals ("Check output size bigger than input size" ,
192195 ArrayConverter .hexStringToByteArray ("0000000000000000000000001D42C86F7923DFEC" ),
@@ -214,24 +217,24 @@ public void testLongToUint48Bytes() {
214217 @ Test
215218 public void testBytesToHexString_ModifiableByteArray () {
216219 ModifiableByteArray toTest = new ModifiableByteArray ();
217- toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte [] { 0x00 , 0x11 , 0x22 , 0x33 , 0x44 });
220+ toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte []{ 0x00 , 0x11 , 0x22 , 0x33 , 0x44 });
218221 assertEquals ("00 11 22 33 44" , ArrayConverter .bytesToHexString (toTest ));
219222
220- toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
221- 0x06 , 0x07 , 0x08 });
223+ toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
224+ 0x06 , 0x07 , 0x08 });
222225 assertEquals ("00 01 02 03 04 05 06 07 08" , ArrayConverter .bytesToHexString (toTest ));
223226
224- toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
225- 0x06 , 0x07 , 0x08 , 0x09 , 0x10 });
227+ toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
228+ 0x06 , 0x07 , 0x08 , 0x09 , 0x10 });
226229 assertEquals ("00 01 02 03 04 05 06 07 08 09 10" , ArrayConverter .bytesToHexString (toTest ));
227230
228- toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
229- 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , });
231+ toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
232+ 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,});
230233 assertEquals ("\n 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07" , ArrayConverter .bytesToHexString (toTest ));
231234
232- toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
233- 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ,
234- 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , });
235+ toTest = ModifiableVariableFactory .safelySetValue (toTest , new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ,
236+ 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ,
237+ 0x07 , 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,});
235238 assertEquals (
236239 "\n 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07\n 00 01 02 03 04 05 06 07 00 01 02 03 04 05 06 07" ,
237240 ArrayConverter .bytesToHexString (toTest ));
@@ -242,9 +245,20 @@ public void testBytesToHexString_ModifiableByteArray() {
242245 */
243246 @ Test
244247 public void testReverseByteOrder () {
245- byte [] array = new byte [] { 0x00 , 0x01 , 0x02 , 0x03 , 0x04 };
248+ byte [] array = new byte []{ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 };
246249
247- assertArrayEquals ("Testing byte order reversion" , new byte [] { 0x04 , 0x03 , 0x02 , 0x01 , 0x00 },
250+ assertArrayEquals ("Testing byte order reversion" , new byte []{ 0x04 , 0x03 , 0x02 , 0x01 , 0x00 },
248251 ArrayConverter .reverseByteOrder (array ));
249252 }
253+
254+ @ Test
255+ public void testBigIntegerReconversion () {
256+ Random r = new Random (0 );
257+ for (int i = 0 ; i < 10000 ; i ++) {
258+ BigInteger b = new BigInteger (1024 + r .nextInt (1000 ), r );
259+ byte [] bigIntegerToByteArray = ArrayConverter .bigIntegerToByteArray (b );
260+ BigInteger c = new BigInteger (1 , bigIntegerToByteArray );
261+ assertEquals (b , c );
262+ }
263+ }
250264}
0 commit comments