public static boolean buffersEqual(Buffer b1, Buffer b2) { if (b1.length() != b2.length()) return false; for (int i = 0; i < b1.length(); i++) { if (b1.getByte(i) != b2.getByte(i)) return false; } return true; }
private void testSetByte(Buffer buff) throws Exception { for (int i = 0; i < numSets; i++) { buff.setByte(i, (byte) i); } for (int i = 0; i < numSets; i++) { assertEquals(i, buff.getByte(i)); } }
@Test public void testGetByte() throws Exception { int bytesLen = 100; byte[] bytes = TestUtils.generateRandomByteArray(bytesLen); Buffer b = new Buffer(bytes); for (int i = 0; i < bytesLen; i++) { assertEquals(bytes[i], b.getByte(i)); } }
@Test public void testGetOutOfBounds() throws Exception { int bytesLen = 100; byte[] bytes = TestUtils.generateRandomByteArray(bytesLen); Buffer b = new Buffer(bytes); try { b.getByte(bytesLen); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getByte(bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getByte(bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getByte(-1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getByte(-100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getInt(bytesLen); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getInt(bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getInt(bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getInt(-1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getInt(-100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getLong(bytesLen); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getLong(bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getLong(bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getLong(-1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getLong(-100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getFloat(bytesLen); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getFloat(bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getFloat(bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getFloat(-1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getFloat(-100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getDouble(bytesLen); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getDouble(bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getDouble(bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getDouble(-1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getDouble(-100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getShort(bytesLen); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getShort(bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getShort(bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getShort(-1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getShort(-100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getBytes(bytesLen + 1, bytesLen + 1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getBytes(bytesLen + 100, bytesLen + 100); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getBytes(-1, -1); fail(); } catch (IndexOutOfBoundsException e) { // expected } try { b.getBytes(-100, -100); fail(); } catch (IndexOutOfBoundsException e) { // expected } }