|
| 1 | +package core.bitstream; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +public class InputBitStreamTest { |
| 12 | + |
| 13 | + private InputStream reader; |
| 14 | + private InputBitStream bstream; |
| 15 | + |
| 16 | + @BeforeEach |
| 17 | + public void setUp() throws IOException { |
| 18 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 19 | + // 0000 0001 0000 0011 0000 0111 0000 1111 0001 1111 |
| 20 | + baos.write(1); |
| 21 | + baos.write(3); |
| 22 | + baos.write(7); |
| 23 | + baos.write(15); |
| 24 | + baos.write(31); |
| 25 | + reader = new ByteArrayInputStream(baos.toByteArray()); |
| 26 | + bstream = new InputBitStream(reader); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void countWhileTest() throws IOException { |
| 31 | + // 1 |
| 32 | + assertEquals(7, bstream.countWhile(false)); |
| 33 | + assertEquals(0, bstream.countWhile(false)); |
| 34 | + assertEquals(1, bstream.countWhile(true)); |
| 35 | + assertEquals(0, bstream.countWhile(true)); |
| 36 | + // 3 |
| 37 | + assertEquals(6, bstream.countWhile(false)); |
| 38 | + assertEquals(0, bstream.countWhile(false)); |
| 39 | + assertEquals(2, bstream.countWhile(true)); |
| 40 | + assertEquals(0, bstream.countWhile(true)); |
| 41 | + // 7 |
| 42 | + assertEquals(5, bstream.countWhile(false)); |
| 43 | + assertEquals(0, bstream.countWhile(false)); |
| 44 | + assertEquals(3, bstream.countWhile(true)); |
| 45 | + assertEquals(0, bstream.countWhile(true)); |
| 46 | + // 15 |
| 47 | + assertEquals(4, bstream.countWhile(false)); |
| 48 | + assertEquals(0, bstream.countWhile(false)); |
| 49 | + assertEquals(4, bstream.countWhile(true)); |
| 50 | + assertEquals(0, bstream.countWhile(true)); |
| 51 | + // 31 |
| 52 | + assertEquals(3, bstream.countWhile(false)); |
| 53 | + assertEquals(0, bstream.countWhile(false)); |
| 54 | + assertEquals(5, bstream.countWhile(true)); |
| 55 | + assertEquals(0, bstream.countWhile(true)); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void nextTest() throws IOException { |
| 60 | + assertEquals(0, bstream.next(0)); |
| 61 | + assertEquals(0, bstream.next(4)); |
| 62 | + assertEquals(1, bstream.next(4)); |
| 63 | + assertEquals(3, bstream.next(8)); |
| 64 | + assertEquals(7, bstream.next(8)); |
| 65 | + assertEquals(0, bstream.next(0)); |
| 66 | + } |
| 67 | +} |
0 commit comments