2323
2424import org .junit .Test ;
2525
26+ import java .math .BigInteger ;
2627import java .util .Arrays ;
2728import java .util .List ;
29+ import java .util .Random ;
2830
2931import static junit .framework .TestCase .assertTrue ;
3032import static org .junit .Assert .*;
@@ -223,4 +225,60 @@ private static void testReverse(byte[] input, int fromIndex, int toIndex, byte[]
223225 Util .reverse (input , fromIndex , toIndex );
224226 assertTrue (Arrays .equals (expectedOutput , input ));
225227 }
228+
229+ @ Test
230+ public void testLeftShift () {
231+ byte [] test = new byte []{0 , 0 , 1 , 0 };
232+ assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .shiftLeft (new byte []{0 , 0 , -128 , 0 }, 1 ));
233+ assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .shiftLeft (new byte []{0 , 0 , 64 , 0 }, 2 ));
234+ assertArrayEquals (new byte []{1 , 1 , 1 , 0 }, Util .shiftLeft (new byte []{-128 , -128 , -128 , -128 }, 1 ));
235+ assertArrayEquals (new byte []{0 , 0 , 2 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 1 ));
236+ assertArrayEquals (new byte []{0 , 0 , 4 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 2 ));
237+ assertArrayEquals (new byte []{0 , 0 , 8 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 3 ));
238+ assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 8 ));
239+ assertArrayEquals (new byte []{0 , 2 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 9 ));
240+ assertArrayEquals (new byte []{1 , 0 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 16 ));
241+ assertArrayEquals (new byte []{2 , 0 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 17 ));
242+ assertArrayEquals (new byte []{-128 , 0 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 23 ));
243+ assertArrayEquals (new byte []{0 , 0 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 24 ));
244+ assertArrayEquals (new byte []{0 , 0 , 0 , 0 }, Util .shiftLeft (Bytes .from (test ).array (), 24 ));
245+
246+ assertSame (test , Util .shiftLeft (test , 1 ));
247+
248+ for (int i = 0 ; i < 1000 ; i ++) {
249+ int shift = 1 ;
250+ Bytes rnd = Bytes .random (2 + new Random ().nextInt (128 ));
251+ assertArrayEquals (Bytes .wrap (new BigInteger (rnd .array ()).shiftLeft (shift ).toByteArray ()).resize (rnd .length ()).array (), Util .shiftLeft (rnd .copy ().array (), shift ));
252+ }
253+ }
254+
255+ @ Test
256+ public void testRightShift () {
257+ byte [] test = new byte []{0 , 0 , 16 , 0 };
258+ assertArrayEquals (new byte []{0 , -128 , -128 , -128 }, Util .shiftRight (new byte []{1 , 1 , 1 , 1 }, 1 ));
259+ assertArrayEquals (new byte []{0 , -128 , 66 , 0 }, Util .shiftRight (new byte []{2 , 1 , 8 , 2 }, 2 ));
260+ assertArrayEquals (new byte []{0 , -128 , 66 , 0 }, new BigInteger (new byte []{2 , 1 , 8 , 2 }).shiftRight (2 ).toByteArray ());
261+ assertArrayEquals (new byte []{0 , 0 , 0 , -128 }, Util .shiftRight (Bytes .from (test ).array (), 5 ));
262+ assertArrayEquals (new byte []{0 , 0 , 0 , -128 }, Util .shiftRight (new byte []{0 , 0 , 1 , 0 }, 1 ));
263+ assertArrayEquals (new byte []{0 , 0 , 8 , 0 }, Util .shiftRight (Bytes .from (test ).array (), 1 ));
264+ assertArrayEquals (new byte []{0 , 0 , 4 , 0 }, Util .shiftRight (Bytes .from (test ).array (), 2 ));
265+ assertArrayEquals (new byte []{0 , 0 , 2 , 0 }, Util .shiftRight (Bytes .from (test ).array (), 3 ));
266+ assertArrayEquals (new byte []{0 , 0 , 1 , 0 }, Util .shiftRight (Bytes .from (test ).array (), 4 ));
267+
268+ assertSame (test , Util .shiftRight (test , 1 ));
269+
270+
271+ for (int i = 0 ; i < 1000 ; i ++) {
272+ int shift = 1 ;
273+ Bytes rnd = Bytes .random (8 );
274+ byte [] expected = new BigInteger (rnd .array ()).shiftRight (shift ).toByteArray ();
275+ byte [] actual = Util .shiftRight (rnd .copy ().array (), shift );
276+
277+ System .out .println ("Original \t " + rnd .encodeBinary ());
278+ System .out .println ("Expected \t " + Bytes .wrap (expected ).encodeBinary ());
279+ System .out .println ("Actual \t " + Bytes .wrap (actual ).encodeBinary () + "\n \n " );
280+
281+ assertArrayEquals (expected , actual );
282+ }
283+ }
226284}
0 commit comments