/** Test that constructor offsets work */ @Test public void testConstructor() throws Exception { CountingInputStream cis; cis = new CountingInputStream(NULL_INPUTSTREAM, 5l); assertEquals(5l, cis.getPos()); cis.close(); cis = new CountingInputStream(NULL_INPUTSTREAM, 500000000000l); assertEquals(500000000000l, cis.getPos()); cis.close(); }
/** Test that single byte reads have the desired effect on counting */ @Test public void testSingleByteReads() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(TEST_ARRAY); CountingInputStream cis = new CountingInputStream(bais); assertEquals((long) 0, cis.getPos()); for (long i = 0; i < TEST_ARRAY.length; i++) { assertEquals(i, cis.getPos()); int b = cis.read(); assertEquals((byte) b, TEST_ARRAY[(int) i]); assertEquals(i + 1, cis.getPos()); } assertEquals(TEST_ARRAY.length, cis.getPos()); assertEquals(-1, cis.read()); assertEquals(TEST_ARRAY.length, cis.getPos()); }
/** Test that two byte reads have the desired effect on counting */ @Test public void testTwoByteReads() throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(TEST_ARRAY); CountingInputStream cis = new CountingInputStream(bais); assertEquals((long) 0, cis.getPos()); for (long i = 0; i < TEST_ARRAY.length / 2; i++) { assertEquals(i * 2, cis.getPos()); final byte[] read = new byte[2]; final int rtr = cis.read(read); assertEquals(rtr, read.length); assertEquals(read[0], TEST_ARRAY[(int) i * 2]); assertEquals(read[1], TEST_ARRAY[(int) (i * 2) + 1]); assertEquals("offset after read i=" + i + "; ", (i * 2) + read.length, cis.getPos()); } assertEquals(TEST_ARRAY.length, cis.getPos()); assertEquals(-1, cis.read()); assertEquals(TEST_ARRAY.length, cis.getPos()); }