public void testToStringBinaryForArrayBasedByteBuffer() { byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 }; ByteBuffer target = ByteBuffer.wrap(array); String actual = Bytes.toStringBinary(target); String expected = "09azAZ@\\x01"; assertEquals(expected, actual); }
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 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 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()); }
public void testToStringBinaryForReadOnlyByteBuffer() { byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 }; ByteBuffer target = ByteBuffer.wrap(array).asReadOnlyBuffer(); String actual = Bytes.toStringBinary(target); String expected = "09azAZ@\\x01"; assertEquals(expected, actual); }
public void testIndexOf() { byte[] array = Bytes.toBytes("hello"); assertEquals(1, Bytes.indexOf(array, (byte) 'e')); assertEquals(4, Bytes.indexOf(array, (byte) 'o')); assertEquals(-1, Bytes.indexOf(array, (byte) 'a')); assertEquals(0, Bytes.indexOf(array, Bytes.toBytes("hel"))); assertEquals(2, Bytes.indexOf(array, Bytes.toBytes("ll"))); assertEquals(-1, Bytes.indexOf(array, Bytes.toBytes("hll"))); }
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 testToInt() throws Exception { int [] ints = {-1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE}; for (int i = 0; i < ints.length; i++) { byte [] b = Bytes.toBytes(ints[i]); assertEquals(ints[i], Bytes.toInt(b)); byte [] b2 = bytesWithOffset(b); assertEquals(ints[i], Bytes.toInt(b2, 1)); assertEquals(ints[i], Bytes.toInt(b2, 1, Bytes.SIZEOF_INT)); } }
public void testToBigDecimal() throws Exception { BigDecimal [] decimals = {new BigDecimal("-1"), new BigDecimal("123.123"), new BigDecimal("123123123123")}; for (int i = 0; i < decimals.length; i++) { byte [] b = Bytes.toBytes(decimals[i]); assertEquals(decimals[i], Bytes.toBigDecimal(b)); byte [] b2 = bytesWithOffset(b); assertEquals(decimals[i], Bytes.toBigDecimal(b2, 1, b.length)); } }
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 testToLong() throws Exception { long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE}; for (int i = 0; i < longs.length; i++) { byte [] b = Bytes.toBytes(longs[i]); assertEquals(longs[i], Bytes.toLong(b)); byte [] b2 = bytesWithOffset(b); assertEquals(longs[i], Bytes.toLong(b2, 1)); assertEquals(longs[i], Bytes.toLong(b2, 1, Bytes.SIZEOF_LONG)); } }
public void testToDouble() throws Exception { double [] doubles = {Double.MIN_VALUE, Double.MAX_VALUE}; for (int i = 0; i < doubles.length; i++) { byte [] b = Bytes.toBytes(doubles[i]); assertEquals(doubles[i], Bytes.toDouble(b), 0.0); byte [] b2 = bytesWithOffset(b); assertEquals(doubles[i], Bytes.toDouble(b2, 1), 0.0); } }
public void testSplit2() throws Exception { // More split tests. byte [] lowest = Bytes.toBytes("http://A"); byte [] highest = Bytes.toBytes("http://z"); byte [] middle = Bytes.toBytes("http://]"); byte [][] parts = Bytes.split(lowest, highest, 1); for (int i = 0; i < parts.length; i++) { System.out.println(Bytes.toString(parts[i])); } assertEquals(3, parts.length); assertTrue(Bytes.equals(parts[1], middle)); }
public void testReadAsVLong() throws Exception { long [] longs = {-1l, 123l, Long.MIN_VALUE, Long.MAX_VALUE}; for (int i = 0; i < longs.length; i++) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream output = new DataOutputStream(baos); WritableUtils.writeVLong(output, longs[i]); byte[] long_bytes_no_offset = baos.toByteArray(); assertEquals(longs[i], Bytes.readAsVLong(long_bytes_no_offset, 0)); byte[] long_bytes_with_offset = bytesWithOffset(long_bytes_no_offset); assertEquals(longs[i], Bytes.readAsVLong(long_bytes_with_offset, 1)); } }
public void testSplit() throws Exception { byte [] lowest = Bytes.toBytes("AAA"); byte [] middle = Bytes.toBytes("CCC"); byte [] highest = Bytes.toBytes("EEE"); byte [][] parts = Bytes.split(lowest, highest, 1); for (int i = 0; i < parts.length; i++) { System.out.println(Bytes.toString(parts[i])); } assertEquals(3, parts.length); assertTrue(Bytes.equals(parts[1], middle)); // Now divide into three parts. Change highest so split is even. highest = Bytes.toBytes("DDD"); parts = Bytes.split(lowest, highest, 2); for (int i = 0; i < parts.length; i++) { System.out.println(Bytes.toString(parts[i])); } assertEquals(4, parts.length); // Assert that 3rd part is 'CCC'. assertTrue(Bytes.equals(parts[2], middle)); }
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 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 testToStringBinary_toBytesBinary_Reversable() throws Exception { String bytes = Bytes.toStringBinary(Bytes.toBytes(2.17)); assertEquals(2.17, Bytes.toDouble(Bytes.toBytesBinary(bytes)), 0); }
public void testAdd () throws Exception { byte[] a = {0,0,0,0,0,0,0,0,0,0}; byte[] b = {1,1,1,1,1,1,1,1,1,1,1}; byte[] c = {2,2,2,2,2,2,2,2,2,2,2,2}; byte[] d = {3,3,3,3,3,3,3,3,3,3,3,3,3}; byte[] result1 = Bytes.add (a, b, c); byte[] result2 = Bytes.add (new byte[][] {a, b, c}); assertEquals(0, Bytes.compareTo(result1, result2)); byte[] result4 = Bytes.add (result1, d); byte[] result5 = Bytes.add (new byte[][] {result1, d}); assertEquals(0, Bytes.compareTo(result1, result2)); }
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()); } }