public void testToFloat() throws Exception { float [] floats = {-1f, 123.123f, Float.MAX_VALUE}; for (int i = 0; i < floats.length; i++) { byte [] b = Bytes.toBytes(floats[i]); assertEquals(floats[i], Bytes.toFloat(b), 0.0f); byte [] b2 = bytesWithOffset(b); assertEquals(floats[i], Bytes.toFloat(b2, 1), 0.0f); } }
public void testContains() { byte[] array = Bytes.toBytes("hello world"); assertTrue(Bytes.contains(array, (byte) 'e')); assertTrue(Bytes.contains(array, (byte) 'd')); assertFalse( Bytes.contains(array, (byte) 'a')); assertTrue(Bytes.contains(array, Bytes.toBytes("world"))); assertTrue(Bytes.contains(array, Bytes.toBytes("ello"))); assertFalse(Bytes.contains(array, Bytes.toBytes("owo"))); }
public void testNullHashCode() { byte [] b = null; Exception ee = null; try { Bytes.hashCode(b); } catch (Exception e) { ee = e; } assertNotNull(ee); }
public void testIncrementBytes() throws IOException { assertTrue(checkTestIncrementBytes(10, 1)); assertTrue(checkTestIncrementBytes(12, 123435445)); assertTrue(checkTestIncrementBytes(124634654, 1)); assertTrue(checkTestIncrementBytes(10005460, 5005645)); assertTrue(checkTestIncrementBytes(1, -1)); assertTrue(checkTestIncrementBytes(10, -1)); assertTrue(checkTestIncrementBytes(10, -5)); assertTrue(checkTestIncrementBytes(1005435000, -5)); assertTrue(checkTestIncrementBytes(10, -43657655)); assertTrue(checkTestIncrementBytes(-1, 1)); assertTrue(checkTestIncrementBytes(-26, 5034520)); assertTrue(checkTestIncrementBytes(-10657200, 5)); assertTrue(checkTestIncrementBytes(-12343250, 45376475)); assertTrue(checkTestIncrementBytes(-10, -5)); assertTrue(checkTestIncrementBytes(-12343250, -5)); assertTrue(checkTestIncrementBytes(-12, -34565445)); assertTrue(checkTestIncrementBytes(-1546543452, -34565445)); }
public void testGetBytesForByteBuffer() { byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ByteBuffer target = ByteBuffer.wrap(array); target.position(2); target.limit(7); byte[] actual = Bytes.getBytes(target); byte[] expected = { 2, 3, 4, 5, 6 }; assertTrue(Arrays.equals(expected, actual)); assertEquals(2, target.position()); assertEquals(7, target.limit()); }
private static void setUnsafe(boolean value) throws Exception { Field field = Bytes.class.getDeclaredField("UNSAFE_UNALIGNED"); field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers"); modifiersField.setAccessible(true); int oldModifiers = field.getModifiers(); modifiersField.setInt(field, oldModifiers & ~Modifier.FINAL); try { field.set(null, value); } finally { modifiersField.setInt(field, oldModifiers); } assertEquals(Bytes.UNSAFE_UNALIGNED, value); }
public void testZero() { byte[] array = Bytes.toBytes("hello"); Bytes.zero(array); for (int i = 0; i < array.length; i++) { assertEquals(0, array[i]); } array = Bytes.toBytes("hello world"); Bytes.zero(array, 2, 7); assertFalse(array[0] == 0); assertFalse(array[1] == 0); for (int i = 2; i < 9; i++) { assertEquals(0, array[i]); } for (int i = 9; i < array.length; i++) { assertFalse(array[i] == 0); } }
public void testFixedSizeString() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); Bytes.writeStringFixedSize(dos, "Hello", 5); Bytes.writeStringFixedSize(dos, "World", 18); Bytes.writeStringFixedSize(dos, "", 9); try { // Use a long dash which is three bytes in UTF-8. If encoding happens // using ISO-8859-1, this will fail. Bytes.writeStringFixedSize(dos, "Too\u2013Long", 9); fail("Exception expected"); } catch (IOException ex) { assertEquals( "Trying to write 10 bytes (Too\\xE2\\x80\\x93Long) into a field of " + "length 9", ex.getMessage()); } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DataInputStream dis = new DataInputStream(bais); assertEquals("Hello", Bytes.readStringFixedSize(dis, 5)); assertEquals("World", Bytes.readStringFixedSize(dis, 18)); assertEquals("", Bytes.readStringFixedSize(dis, 9)); }
public void testToBytesBinaryTrailingBackslashes() throws Exception { try { Bytes.toBytesBinary("abc\\x00\\x01\\"); } catch (StringIndexOutOfBoundsException ex) { fail("Illegal string access: " + ex.getMessage()); } }
private static void testShort(boolean unsafe) throws Exception { setUnsafe(unsafe); try { for (short n : Arrays.asList( Short.MIN_VALUE, (short) -100, (short) -1, (short) 0, (short) 1, (short) 300, Short.MAX_VALUE)) { byte[] bytes = Bytes.toBytes(n); assertEquals(Bytes.toShort(bytes, 0, bytes.length), n); } } finally { setUnsafe(UnsafeAvailChecker.unaligned()); } }
public void testToStringBytesBinaryReversible() { // let's run test with 1000 randomly generated byte arrays Random rand = new Random(System.currentTimeMillis()); byte[] randomBytes = new byte[1000]; for (int i = 0; i < 1000; i++) { rand.nextBytes(randomBytes); verifyReversibleForBytes(randomBytes); } // some specific cases verifyReversibleForBytes(new byte[] {}); verifyReversibleForBytes(new byte[] {'\\', 'x', 'A', 'D'}); verifyReversibleForBytes(new byte[] {'\\', 'x', 'A', 'D', '\\'}); }
public void testShort() throws Exception { testShort(false); }
public void testToBytesForByteBuffer() { byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; ByteBuffer target = ByteBuffer.wrap(array); target.position(2); target.limit(7); byte[] actual = Bytes.toBytes(target); byte[] expected = { 0, 1, 2, 3, 4, 5, 6 }; assertTrue(Arrays.equals(expected, actual)); assertEquals(2, target.position()); assertEquals(7, target.limit()); ByteBuffer target2 = target.slice(); assertEquals(0, target2.position()); assertEquals(5, target2.limit()); byte[] actual2 = Bytes.toBytes(target2); byte[] expected2 = { 2, 3, 4, 5, 6 }; assertTrue(Arrays.equals(expected2, actual2)); assertEquals(0, target2.position()); assertEquals(5, target2.limit()); }
public void testToStringBinaryForBytes() { byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 }; String actual = Bytes.toStringBinary(array); String expected = "09azAZ@\\x01"; assertEquals(expected, actual); String actual2 = Bytes.toStringBinary(array, 2, 3); String expected2 = "azA"; assertEquals(expected2, actual2); }
public void testZero() { byte[] array = Bytes.toBytes("hello"); Bytes.zero(array); for (int i = 0; i < array.length; i++) { assertEquals(0, array[i]); } array = Bytes.toBytes("hello world"); Bytes.zero(array, 2, 7); assertFalse(array[0] == 0); assertFalse(array[1] == 0); for (int i = 2; i < 9; i++) { assertEquals(0, array[i]); } for (int i = 9; i < array.length; i++) { assertFalse(array[i] == 0); } }
public void testIncrementBytes() throws IOException { assertTrue(checkTestIncrementBytes(10, 1)); assertTrue(checkTestIncrementBytes(12, 123435445)); assertTrue(checkTestIncrementBytes(124634654, 1)); assertTrue(checkTestIncrementBytes(10005460, 5005645)); assertTrue(checkTestIncrementBytes(1, -1)); assertTrue(checkTestIncrementBytes(10, -1)); assertTrue(checkTestIncrementBytes(10, -5)); assertTrue(checkTestIncrementBytes(1005435000, -5)); assertTrue(checkTestIncrementBytes(10, -43657655)); assertTrue(checkTestIncrementBytes(-1, 1)); assertTrue(checkTestIncrementBytes(-26, 5034520)); assertTrue(checkTestIncrementBytes(-10657200, 5)); assertTrue(checkTestIncrementBytes(-12343250, 45376475)); assertTrue(checkTestIncrementBytes(-10, -5)); assertTrue(checkTestIncrementBytes(-12343250, -5)); assertTrue(checkTestIncrementBytes(-12, -34565445)); assertTrue(checkTestIncrementBytes(-1546543452, -34565445)); }
public void testFixedSizeString() throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); Bytes.writeStringFixedSize(dos, "Hello", 5); Bytes.writeStringFixedSize(dos, "World", 18); Bytes.writeStringFixedSize(dos, "", 9); try { // Use a long dash which is three bytes in UTF-8. If encoding happens // using ISO-8859-1, this will fail. Bytes.writeStringFixedSize(dos, "Too\u2013Long", 9); fail("Exception expected"); } catch (IOException ex) { assertEquals( "Trying to write 10 bytes (Too\\xE2\\x80\\x93Long) into a field of " + "length 9", ex.getMessage()); } ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DataInputStream dis = new DataInputStream(bais); assertEquals("Hello", Bytes.readStringFixedSize(dis, 5)); assertEquals("World", Bytes.readStringFixedSize(dis, 18)); assertEquals("", Bytes.readStringFixedSize(dis, 9)); }
private void verifyReversibleForBytes(byte[] originalBytes) { String convertedString = Bytes.toStringBinary(originalBytes); byte[] convertedBytes = Bytes.toBytesBinary(convertedString); if (Bytes.compareTo(originalBytes, convertedBytes) != 0) { fail("Not reversible for\nbyte[]: " + Arrays.toString(originalBytes) + ",\nStringBinary: " + convertedString); } }
private static void testShort(boolean unsafe) throws Exception { setUnsafe(unsafe); try { for (short n : Arrays.asList( Short.MIN_VALUE, (short) -100, (short) -1, (short) 0, (short) 1, (short) 300, Short.MAX_VALUE)) { byte[] bytes = Bytes.toBytes(n); assertEquals(Bytes.toShort(bytes, 0, bytes.length), n); } } finally { setUnsafe(UnsafeAvailChecker.unaligned()); } }
public void testToStringBytesBinaryReversible() { // let's run test with 1000 randomly generated byte arrays Random rand = new Random(System.currentTimeMillis()); byte[] randomBytes = new byte[1000]; for (int i = 0; i < 1000; i++) { rand.nextBytes(randomBytes); verifyReversibleForBytes(randomBytes); } // some specific cases verifyReversibleForBytes(new byte[] {}); verifyReversibleForBytes(new byte[] {'\\', 'x', 'A', 'D'}); verifyReversibleForBytes(new byte[] {'\\', 'x', 'A', 'D', '\\'}); }