// NB: This method assumes // base == target.base // offset == target.offset // values.length == target.length private void doTestSet(byte[] base, int offset, ByteArray target, byte[] values) { for (int i = 0; i < values.length; ++i) { target.set(i, values[i]); assertEquals("Set wrong value [index = " + i + "].", values[i], target.get(i)); assertEquals( "Didn't set value in original base array " + "[index = " + i + "].", values[i], base[offset + i]); } }
private void doTestSetBoundaryCases(ByteArray target) { byte value = (byte) 25; if (target.length == 0) { try { target.set(0, value); fail("Shouldn't accept index 0 if length is 0."); } catch (ArrayIndexOutOfBoundsException aiobe) { } } try { target.set(-1, value); fail("Shouldn't accept negative index."); } catch (ArrayIndexOutOfBoundsException aiobe) { } try { target.set(target.length, value); fail("Shouldn't accept index greater than length-1."); } catch (ArrayIndexOutOfBoundsException aiobe) { } }
private void doTestSetStreamBoundaryCases(ByteArray target) throws IOException { MockInputStream stream = new MockInputStream(); try { target.set(0, 0, null); // Null always checked b/f int args. fail("Shouldn't accept null."); } catch (NullPointerException npe) { } assertEquals("Should do nothing and return 0 if maxLength <= 0.", 0, target.set(0, 0, stream)); assertEquals("Should do nothing and return 0 if maxLength <= 0.", 0, target.set(0, -1, stream)); if (target.length == 0) { try { target.set(0, 1, stream); fail("Shouldn't accept index 0 if length is 0."); } catch (ArrayIndexOutOfBoundsException aiobe) { } } try { target.set(-1, 1, stream); fail("Shouldn't accept negative index."); } catch (ArrayIndexOutOfBoundsException aiobe) { } try { target.set(target.length, 1, stream); fail("Shouldn't accept index greater than length-1."); } catch (ArrayIndexOutOfBoundsException aiobe) { } }
private void doTestSetStreamEndOfStream(ByteArray target, byte[] base, int offset) throws IOException { // Create mock, set up expected calls, and transition to // verification mode. MockInputStream stream = new MockInputStream(); stream.read(base, offset, 1, -1, null); stream.activate(); // Test. assertEquals("Failed to signal end of stream.", -1, target.set(0, 1, stream)); // Make sure all expected calls were performed. stream.verify(); }
@Test public void testSetStreamWhen1LengthSlice() throws IOException { // Create mock, set up expected calls, and transition to // verification mode. byte[] base = new byte[] {0, 0}; MockInputStream stream = new MockInputStream(); stream.read(base, 1, 1, 1, null); // Will set base[1]=1. stream.activate(); // Test. ByteArray target = new ByteArray(base, 1, 1); doTestSetStreamBoundaryCases(target); doTestSetStreamEndOfStream(target, base, 1); assertEquals("Should return the bytes written.", 1, target.set(0, 1, stream)); verifySetStream(base, 1, 0, target, 1); // Make sure all expected calls were performed. stream.verify(); }
private void doTestSetBufferBoundaryCases(ByteArray target) { try { target.set(0, null); // Null always checked b/f index. fail("Shouldn't accept null."); } catch (NullPointerException npe) { } target.set(0, new byte[] {}); // buf.len == 0 always checked b/f // index. byte[] values = new byte[] {25, -1}; if (target.length == 0) { try { target.set(0, values); fail("Shouldn't accept index 0 if length is 0."); } catch (ArrayIndexOutOfBoundsException aiobe) { } } try { target.set(-1, values); fail("Shouldn't accept negative index."); } catch (ArrayIndexOutOfBoundsException aiobe) { } try { target.set(target.length, values); fail("Shouldn't accept index greater than length-1."); } catch (ArrayIndexOutOfBoundsException aiobe) { } if (0 < target.length) { byte original = target.get(target.length - 1); values[0] = (byte) (original + 1); try { target.set(target.length - 1, values); fail("Shouldn't accept a buffer that doesn't fit " + "into the slice."); } catch (ArrayIndexOutOfBoundsException aiobe) { // Ok, but check that no value has been copied: assertEquals( "No value should be copied if the buffer " + "doesn't fit into the slice.", original, target.get(target.length - 1)); } } }