private static void assertBytesStreams(Slice slice, int index) throws Exception { // fill slice with FF slice.fill((byte) 0xFF); byte[] value = new byte[slice.length()]; Arrays.fill(value, (byte) 0xFF); assertEquals(slice.getBytes(), value); // set and get the value value = new byte[(slice.length() - index) / 2]; for (int i = 0; i < value.length; i++) { value[i] = (byte) i; } slice.setBytes(index, new ByteArrayInputStream(value), value.length); assertEquals(slice.getBytes(index, value.length), value); ByteArrayOutputStream out = new ByteArrayOutputStream(); slice.getBytes(index, out, value.length); assertEquals(slice.getBytes(index, value.length), out.toByteArray()); for (int length = 0; length < value.length; length++) { slice.fill((byte) 0xFF); slice.setBytes(index, new ByteArrayInputStream(value), length); assertEquals(slice.getBytes(index, length), Arrays.copyOf(value, length)); out = new ByteArrayOutputStream(); slice.getBytes(index, out, length); assertEquals(slice.getBytes(index, length), out.toByteArray()); } }
private void assertBytesSlice(Slice slice, int index) { // fill slice with FF slice.fill((byte) 0xFF); // compare to self slice assertEquals(slice.slice(0, slice.length()), slice); Slice value = allocate(slice.length()); slice.getBytes(0, value, 0, slice.length()); assertEquals(value, slice); // set and get the value value = allocate((slice.length() - index) / 2); for (int i = 0; i < value.length(); i++) { value.setByte(i, i); } // check by slicing out the region slice.setBytes(index, value); assertEquals(value, slice.slice(index, value.length())); // check by getting out the region Slice tempValue = allocate(value.length()); slice.getBytes(index, tempValue, 0, tempValue.length()); assertEquals(tempValue, slice.slice(index, tempValue.length())); assertTrue(tempValue.equals(0, tempValue.length(), slice, index, tempValue.length())); for (int length = 0; length < value.length(); length++) { slice.fill((byte) 0xFF); slice.setBytes(index, value, 0, length); // check by slicing out the region assertEquals(value.slice(0, length), slice.slice(index, length)); assertTrue(value.equals(0, length, slice, index, length)); // check by getting out the region tempValue = allocate(length); slice.getBytes(index, tempValue); assertEquals(tempValue, slice.slice(index, length)); assertTrue(tempValue.equals(0, length, slice, index, length)); } }
@SuppressWarnings("CharUsedInArithmeticContext") private static void assertToStrings(Slice slice, int index) { // fill slice with FF slice.fill((byte) 0xFF); // set and get the value char[] chars = new char[(slice.length() - index) / 2]; for (int i = 0; i < chars.length; i++) { chars[i] = (char) ('a' + (i % 26)); } String string = new String(chars); Slice value = Slices.copiedBuffer(string, UTF_8); slice.setBytes(index, value); assertEquals(slice.toString(index, value.length(), UTF_8), string); for (int length = 0; length < value.length(); length++) { slice.fill((byte) 0xFF); slice.setBytes(index, value, 0, length); assertEquals(slice.toString(index, length, UTF_8), string.substring(0, length)); } }
private static void assertBytesArray(Slice slice, int index) { // fill slice with FF slice.fill((byte) 0xFF); byte[] value = new byte[slice.length()]; Arrays.fill(value, (byte) 0xFF); assertEquals(slice.getBytes(), value); // set and get the value value = new byte[(slice.length() - index) / 2]; for (int i = 0; i < value.length; i++) { value[i] = (byte) i; } slice.setBytes(index, value); assertEquals(slice.getBytes(index, value.length), value); for (int length = 0; length < value.length; length++) { slice.fill((byte) 0xFF); slice.setBytes(index, value, 0, length); assertEquals(slice.getBytes(index, length), Arrays.copyOf(value, length)); } }