Simple NextableInputStream
[fanfix.git] / src / be / nikiroo / utils / test_code / NextableInputStreamTest.java
CommitLineData
2e7584da
NR
1package be.nikiroo.utils.test_code;
2
3import java.io.ByteArrayInputStream;
2e7584da
NR
4
5import be.nikiroo.utils.IOUtils;
6import be.nikiroo.utils.NextableInputStream;
4098af70 7import be.nikiroo.utils.NextableInputStreamStep;
2e7584da
NR
8import be.nikiroo.utils.test.TestCase;
9import be.nikiroo.utils.test.TestLauncher;
10
11public class NextableInputStreamTest extends TestLauncher {
12 public NextableInputStreamTest(String[] args) {
13 super("NextableInputStream test", args);
14
15 addTest(new TestCase("Simple byte array reading") {
16 @Override
17 public void test() throws Exception {
18 byte[] expected = new byte[] { 42, 12, 0, 127 };
63b46ca9
NR
19 NextableInputStream in = new NextableInputStream(
20 new ByteArrayInputStream(expected), null);
2e7584da
NR
21 byte[] actual = IOUtils.toByteArray(in);
22
23 assertEquals(
24 "The resulting array has not the same number of items",
25 expected.length, actual.length);
26 for (int i = 0; i < expected.length; i++) {
27 assertEquals("Item " + i + " (0-based) is not the same",
28 expected[i], actual[i]);
29 }
30 }
31 });
4098af70
N
32
33 addTest(new TestCase("Stop at 12") {
34 @Override
35 public void test() throws Exception {
36 byte[] expected = new byte[] { 42, 12, 0, 127 };
63b46ca9
NR
37 NextableInputStream in = new NextableInputStream(
38 new ByteArrayInputStream(expected),
39 new NextableInputStreamStep(12));
4098af70 40
63b46ca9 41 checkNext(this, false, "FIRST", in, new byte[] { 42 });
4098af70
N
42 }
43 });
44
45 addTest(new TestCase("Stop at 12, resume, stop again, resume") {
46 @Override
47 public void test() throws Exception {
63b46ca9 48 byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 };
4098af70 49 NextableInputStream in = new NextableInputStream(
63b46ca9
NR
50 new ByteArrayInputStream(data),
51 new NextableInputStreamStep(12));
4098af70 52
63b46ca9
NR
53 checkNext(this, false, "FIRST", in, new byte[] { 42 });
54 checkNext(this, true, "SECOND", in, new byte[] { 0, 127 });
55 checkNext(this, true, "THIRD", in, new byte[] { 51, 11 });
56 }
57 });
4098af70 58
63b46ca9
NR
59 addTest(new TestCase("Encapsulation") {
60 @Override
61 public void test() throws Exception {
62 byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 };
63 NextableInputStream in4 = new NextableInputStream(
64 new ByteArrayInputStream(data),
65 new NextableInputStreamStep(4));
66 NextableInputStream subIn12 = new NextableInputStream(in4,
67 new NextableInputStreamStep(12));
4098af70 68
63b46ca9
NR
69 checkNext(this, false, "SUB FIRST", subIn12, new byte[] { 42 });
70 checkNext(this, true, "SUB SECOND", subIn12, new byte[] { 0 });
71
72 assertEquals("The subIn still has some data", false,
73 subIn12.next());
74
75 checkNext(this, true, "MAIN LAST", in4, new byte[] { 127, 12, 5 });
4098af70
N
76 }
77 });
2e7584da 78 }
63b46ca9
NR
79
80 static void checkNext(TestCase test, boolean callNext, String prefix,
81 NextableInputStream in, byte[] expected) throws Exception {
82 if (callNext) {
83 test.assertEquals("Cannot get " + prefix + " entry", true,
84 in.next());
85 }
86 byte[] actual = IOUtils.toByteArray(in);
87 test.assertEquals("The " + prefix
88 + " resulting array has not the correct number of items",
89 expected.length, actual.length);
90 for (int i = 0; i < actual.length; i++) {
91 test.assertEquals("Item " + i + " (0-based) is not the same",
92 expected[i], actual[i]);
93 }
94 }
2e7584da 95}