2525import org .junit .Test ;
2626
2727import java .math .BigInteger ;
28+ import java .nio .ByteOrder ;
2829import java .util .Arrays ;
2930import java .util .Random ;
3031
@@ -162,21 +163,34 @@ private static void testReverse(byte[] input, int fromIndex, int toIndex, byte[]
162163 @ Test
163164 public void testLeftShift () {
164165 byte [] test = new byte []{0 , 0 , 1 , 0 };
165- assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .Byte .shiftLeft (new byte []{0 , 0 , -128 , 0 }, 1 ));
166- assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .Byte .shiftLeft (new byte []{0 , 0 , 64 , 0 }, 2 ));
167- assertArrayEquals (new byte []{1 , 1 , 1 , 0 }, Util .Byte .shiftLeft (new byte []{-128 , -128 , -128 , -128 }, 1 ));
168- assertArrayEquals (new byte []{0 , 0 , 2 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 1 ));
169- assertArrayEquals (new byte []{0 , 0 , 4 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 2 ));
170- assertArrayEquals (new byte []{0 , 0 , 8 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 3 ));
171- assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 8 ));
172- assertArrayEquals (new byte []{0 , 2 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 9 ));
173- assertArrayEquals (new byte []{1 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 16 ));
174- assertArrayEquals (new byte []{2 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 17 ));
175- assertArrayEquals (new byte []{-128 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 23 ));
176- assertArrayEquals (new byte []{0 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 24 ));
177- assertArrayEquals (new byte []{0 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 24 ));
178-
179- assertSame (test , Util .Byte .shiftLeft (test , 1 ));
166+ assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .Byte .shiftLeft (new byte []{0 , 0 , -128 , 0 }, 1 , ByteOrder .BIG_ENDIAN ));
167+ assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .Byte .shiftLeft (new byte []{0 , 0 , 64 , 0 }, 2 , ByteOrder .BIG_ENDIAN ));
168+ assertArrayEquals (new byte []{1 , 1 , 1 , 0 }, Util .Byte .shiftLeft (new byte []{-128 , -128 , -128 , -128 }, 1 , ByteOrder .BIG_ENDIAN ));
169+ assertArrayEquals (new byte []{0 , 0 , 2 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 1 , ByteOrder .BIG_ENDIAN ));
170+ assertArrayEquals (new byte []{0 , 0 , 4 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 2 , ByteOrder .BIG_ENDIAN ));
171+ assertArrayEquals (new byte []{0 , 0 , 8 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 3 , ByteOrder .BIG_ENDIAN ));
172+ assertArrayEquals (new byte []{0 , 1 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 8 , ByteOrder .BIG_ENDIAN ));
173+ assertArrayEquals (new byte []{0 , 2 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 9 , ByteOrder .BIG_ENDIAN ));
174+ assertArrayEquals (new byte []{1 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 16 , ByteOrder .BIG_ENDIAN ));
175+ assertArrayEquals (new byte []{2 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 17 , ByteOrder .BIG_ENDIAN ));
176+ assertArrayEquals (new byte []{-128 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 23 , ByteOrder .BIG_ENDIAN ));
177+ assertArrayEquals (new byte []{0 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 24 , ByteOrder .BIG_ENDIAN ));
178+ assertArrayEquals (new byte []{0 , 0 , 0 , 0 }, Util .Byte .shiftLeft (Bytes .from (test ).array (), 24 , ByteOrder .BIG_ENDIAN ));
179+
180+ assertSame (test , Util .Byte .shiftLeft (test , 1 , ByteOrder .BIG_ENDIAN ));
181+
182+ assertArrayEquals (new byte []{0 , 0 , 1 , 0 }, Util .Byte .shiftLeft (new byte []{0 , 1 , 0 , 0 }, 8 , ByteOrder .LITTLE_ENDIAN ));
183+ assertArrayEquals (new byte []{(byte ) 0 , 0 , 1 , 0 }, Util .Byte .shiftLeft (new byte []{(byte ) 0 , 1 , 0 , 1 }, 8 , ByteOrder .LITTLE_ENDIAN ));
184+ assertArrayEquals (new byte []{(byte ) 0x54 , 0x1 }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 1 , ByteOrder .LITTLE_ENDIAN ));
185+ assertArrayEquals (new byte []{(byte ) 0xA8 , 0x2 }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 2 , ByteOrder .LITTLE_ENDIAN ));
186+ assertArrayEquals (new byte []{(byte ) 0x50 , 0x5 }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 3 , ByteOrder .LITTLE_ENDIAN ));
187+ assertArrayEquals (new byte []{(byte ) 0xA0 , 0xA }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 4 , ByteOrder .LITTLE_ENDIAN ));
188+ assertArrayEquals (new byte []{(byte ) 0x40 , 0x15 }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 5 , ByteOrder .LITTLE_ENDIAN ));
189+ assertArrayEquals (new byte []{(byte ) 0x80 , 0x2A }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 6 , ByteOrder .LITTLE_ENDIAN ));
190+ assertArrayEquals (new byte []{(byte ) 0x00 , 0x55 }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 7 , ByteOrder .LITTLE_ENDIAN ));
191+ assertArrayEquals (new byte []{(byte ) 0 , (byte ) 0xAA }, Util .Byte .shiftLeft (new byte []{(byte ) 0xAA , 0 }, 8 , ByteOrder .LITTLE_ENDIAN ));
192+ assertArrayEquals (new byte []{0 , 0 , 0 , 0x40 }, Util .Byte .shiftLeft (new byte []{1 , 0 , 0 , 0 }, 30 , ByteOrder .LITTLE_ENDIAN ));
193+
180194 }
181195
182196 @ Test
@@ -187,7 +201,7 @@ public void testLeftShiftAgainstRefImpl() {
187201 Bytes rnd = Bytes .random (4 + new Random ().nextInt (14 ));
188202
189203 byte [] expected = Bytes .from (new BigInteger (rnd .array ()).shiftLeft (shift ).toByteArray ()).resize (rnd .length (), BytesTransformer .ResizeTransformer .Mode .RESIZE_KEEP_FROM_MAX_LENGTH ).array ();
190- byte [] actual = Bytes .from (Util .Byte .shiftLeft (rnd .copy ().array (), shift )).resize (rnd .length (), BytesTransformer .ResizeTransformer .Mode .RESIZE_KEEP_FROM_MAX_LENGTH ).array ();
204+ byte [] actual = Bytes .from (Util .Byte .shiftLeft (rnd .copy ().array (), shift , ByteOrder . BIG_ENDIAN )).resize (rnd .length (), BytesTransformer .ResizeTransformer .Mode .RESIZE_KEEP_FROM_MAX_LENGTH ).array ();
191205
192206 System .out .println ("Original \t " + rnd .encodeBinary () + " << " + shift );
193207 System .out .println ("Expected \t " + Bytes .wrap (expected ).encodeBinary ());
@@ -202,18 +216,23 @@ public void testLeftShiftAgainstRefImpl() {
202216 public void testRightShift () {
203217 byte [] test = new byte []{0 , 0 , 16 , 0 };
204218 assertEquals (0b01111110, 0b11111101 >>> 1 );
205- assertArrayEquals (new byte []{0b00101101, (byte ) 0b01111110}, Util .Byte .shiftRight (new byte []{0b01011010, (byte ) 0b11111101}, 1 ));
206- assertArrayEquals (new byte []{0 , -128 , -128 , -128 }, Util .Byte .shiftRight (new byte []{1 , 1 , 1 , 1 }, 1 ));
207- assertArrayEquals (new byte []{0 , -128 , 66 , 0 }, Util .Byte .shiftRight (new byte []{2 , 1 , 8 , 2 }, 2 ));
219+ assertArrayEquals (new byte []{0b00101101, (byte ) 0b01111110}, Util .Byte .shiftRight (new byte []{0b01011010, (byte ) 0b11111101}, 1 , ByteOrder . BIG_ENDIAN ));
220+ assertArrayEquals (new byte []{0 , -128 , -128 , -128 }, Util .Byte .shiftRight (new byte []{1 , 1 , 1 , 1 }, 1 , ByteOrder . BIG_ENDIAN ));
221+ assertArrayEquals (new byte []{0 , -128 , 66 , 0 }, Util .Byte .shiftRight (new byte []{2 , 1 , 8 , 2 }, 2 , ByteOrder . BIG_ENDIAN ));
208222 assertArrayEquals (new byte []{0 , -128 , 66 , 0 }, new BigInteger (new byte []{2 , 1 , 8 , 2 }).shiftRight (2 ).toByteArray ());
209- assertArrayEquals (new byte []{0 , 0 , 0 , -128 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 5 ));
210- assertArrayEquals (new byte []{0 , 0 , 0 , -128 }, Util .Byte .shiftRight (new byte []{0 , 0 , 1 , 0 }, 1 ));
211- assertArrayEquals (new byte []{0 , 0 , 8 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 1 ));
212- assertArrayEquals (new byte []{0 , 0 , 4 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 2 ));
213- assertArrayEquals (new byte []{0 , 0 , 2 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 3 ));
214- assertArrayEquals (new byte []{0 , 0 , 1 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 4 ));
223+ assertArrayEquals (new byte []{0 , 0 , 0 , -128 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 5 , ByteOrder .BIG_ENDIAN ));
224+ assertArrayEquals (new byte []{0 , 0 , 0 , -128 }, Util .Byte .shiftRight (new byte []{0 , 0 , 1 , 0 }, 1 , ByteOrder .BIG_ENDIAN ));
225+ assertArrayEquals (new byte []{0 , 0 , 8 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 1 , ByteOrder .BIG_ENDIAN ));
226+ assertArrayEquals (new byte []{0 , 0 , 4 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 2 , ByteOrder .BIG_ENDIAN ));
227+ assertArrayEquals (new byte []{0 , 0 , 2 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 3 , ByteOrder .BIG_ENDIAN ));
228+ assertArrayEquals (new byte []{0 , 0 , 1 , 0 }, Util .Byte .shiftRight (Bytes .from (test ).array (), 4 , ByteOrder .BIG_ENDIAN ));
229+
230+ assertSame (test , Util .Byte .shiftRight (test , 1 , ByteOrder .BIG_ENDIAN ));
215231
216- assertSame (test , Util .Byte .shiftRight (test , 1 ));
232+ assertArrayEquals (new byte []{0 , 0 }, Util .Byte .shiftRight (new byte []{1 , 0 }, 1 , ByteOrder .LITTLE_ENDIAN ));
233+ assertArrayEquals (new byte []{(byte ) 0x80 , 0 }, Util .Byte .shiftRight (new byte []{0 , 0x02 }, 2 , ByteOrder .LITTLE_ENDIAN ));
234+ assertArrayEquals (new byte []{(byte ) 0x80 , 0 , 0 , 0 }, Util .Byte .shiftRight (new byte []{0 , 0 , 0 , 1 }, 17 , ByteOrder .LITTLE_ENDIAN ));
235+ assertArrayEquals (new byte []{(byte ) 1 , 0 , 0 , 0 }, Util .Byte .shiftRight (new byte []{0 , 0 , 0 , (byte ) 0x80 }, 31 , ByteOrder .LITTLE_ENDIAN ));
217236 }
218237
219238 @ Test
@@ -223,7 +242,7 @@ public void testRightShiftAgainstRefImpl() {
223242 Bytes rnd = Bytes .random (4 + new Random ().nextInt (12 ));
224243 if (!rnd .bitAt (rnd .lengthBit () - 1 )) { //only unsigned
225244 byte [] expected = Bytes .from (new BigInteger (rnd .array ()).shiftRight (shift ).toByteArray ()).resize (rnd .length (), BytesTransformer .ResizeTransformer .Mode .RESIZE_KEEP_FROM_MAX_LENGTH ).array ();
226- byte [] actual = Bytes .from (Util .Byte .shiftRight (rnd .copy ().array (), shift )).resize (rnd .length (), BytesTransformer .ResizeTransformer .Mode .RESIZE_KEEP_FROM_MAX_LENGTH ).array ();
245+ byte [] actual = Bytes .from (Util .Byte .shiftRight (rnd .copy ().array (), shift , ByteOrder . BIG_ENDIAN )).resize (rnd .length (), BytesTransformer .ResizeTransformer .Mode .RESIZE_KEEP_FROM_MAX_LENGTH ).array ();
227246
228247// System.out.println("Original \t" + rnd.encodeBinary() + " >> " + shift);
229248// System.out.println("Expected \t" + Bytes.wrap(expected).encodeBinary());
0 commit comments