Commit | Line | Data |
---|---|---|
2e7584da NR |
1 | package be.nikiroo.utils.test_code; |
2 | ||
3 | import java.io.ByteArrayInputStream; | |
473e5f31 | 4 | import java.io.IOException; |
2e7584da NR |
5 | |
6 | import be.nikiroo.utils.IOUtils; | |
8e76f6ab NR |
7 | import be.nikiroo.utils.streams.NextableInputStream; |
8 | import be.nikiroo.utils.streams.NextableInputStreamStep; | |
2e7584da NR |
9 | import be.nikiroo.utils.test.TestCase; |
10 | import be.nikiroo.utils.test.TestLauncher; | |
11 | ||
12 | public class NextableInputStreamTest extends TestLauncher { | |
13 | public NextableInputStreamTest(String[] args) { | |
14 | super("NextableInputStream test", args); | |
15 | ||
16 | addTest(new TestCase("Simple byte array reading") { | |
17 | @Override | |
18 | public void test() throws Exception { | |
19 | byte[] expected = new byte[] { 42, 12, 0, 127 }; | |
63b46ca9 NR |
20 | NextableInputStream in = new NextableInputStream( |
21 | new ByteArrayInputStream(expected), null); | |
d6f9bd9f | 22 | in.next(); |
2e7584da NR |
23 | byte[] actual = IOUtils.toByteArray(in); |
24 | ||
25 | assertEquals( | |
26 | "The resulting array has not the same number of items", | |
27 | expected.length, actual.length); | |
28 | for (int i = 0; i < expected.length; i++) { | |
29 | assertEquals("Item " + i + " (0-based) is not the same", | |
30 | expected[i], actual[i]); | |
31 | } | |
32 | } | |
33 | }); | |
4098af70 N |
34 | |
35 | addTest(new TestCase("Stop at 12") { | |
36 | @Override | |
37 | public void test() throws Exception { | |
38 | byte[] expected = new byte[] { 42, 12, 0, 127 }; | |
63b46ca9 NR |
39 | NextableInputStream in = new NextableInputStream( |
40 | new ByteArrayInputStream(expected), | |
41 | new NextableInputStreamStep(12)); | |
4098af70 | 42 | |
d6f9bd9f | 43 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
4098af70 N |
44 | } |
45 | }); | |
46 | ||
47 | addTest(new TestCase("Stop at 12, resume, stop again, resume") { | |
48 | @Override | |
49 | public void test() throws Exception { | |
63b46ca9 | 50 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; |
4098af70 | 51 | NextableInputStream in = new NextableInputStream( |
63b46ca9 NR |
52 | new ByteArrayInputStream(data), |
53 | new NextableInputStreamStep(12)); | |
4098af70 | 54 | |
d6f9bd9f NR |
55 | checkNext(this, "FIRST", in, new byte[] { 42 }); |
56 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); | |
57 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); | |
63b46ca9 NR |
58 | } |
59 | }); | |
4098af70 | 60 | |
63b46ca9 NR |
61 | addTest(new TestCase("Encapsulation") { |
62 | @Override | |
63 | public void test() throws Exception { | |
64 | byte[] data = new byte[] { 42, 12, 0, 4, 127, 12, 5 }; | |
65 | NextableInputStream in4 = new NextableInputStream( | |
66 | new ByteArrayInputStream(data), | |
67 | new NextableInputStreamStep(4)); | |
68 | NextableInputStream subIn12 = new NextableInputStream(in4, | |
69 | new NextableInputStreamStep(12)); | |
4098af70 | 70 | |
d6f9bd9f NR |
71 | in4.next(); |
72 | checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 }); | |
73 | checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 }); | |
63b46ca9 NR |
74 | |
75 | assertEquals("The subIn still has some data", false, | |
76 | subIn12.next()); | |
dd3034f7 | 77 | |
d6f9bd9f | 78 | checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); |
dd3034f7 NR |
79 | } |
80 | }); | |
81 | ||
82 | addTest(new TestCase("UTF-8 text lines test") { | |
83 | @Override | |
84 | public void test() throws Exception { | |
85 | String ln1 = "Ligne première"; | |
86 | String ln2 = "Ligne la deuxième du nom"; | |
87 | byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); | |
88 | NextableInputStream in = new NextableInputStream( | |
89 | new ByteArrayInputStream(data), | |
90 | new NextableInputStreamStep('\n')); | |
91 | ||
d6f9bd9f NR |
92 | checkNext(this, "FIRST", in, ln1.getBytes("UTF-8")); |
93 | checkNext(this, "SECOND", in, ln2.getBytes("UTF-8")); | |
94 | } | |
95 | }); | |
96 | ||
97 | addTest(new TestCase("nextAll()") { | |
98 | @Override | |
99 | public void test() throws Exception { | |
100 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
101 | NextableInputStream in = new NextableInputStream( | |
102 | new ByteArrayInputStream(data), | |
103 | new NextableInputStreamStep(12)); | |
104 | ||
105 | checkNext(this, "FIRST", in, new byte[] { 42 }); | |
473e5f31 N |
106 | checkNextAll(this, "REST", in, |
107 | new byte[] { 0, 127, 12, 51, 11, 12 }); | |
108 | assertEquals("The stream still has some data", false, | |
109 | in.next()); | |
d6f9bd9f NR |
110 | } |
111 | }); | |
112 | ||
113 | addTest(new TestCase("getBytesRead()") { | |
114 | @Override | |
115 | public void test() throws Exception { | |
116 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
117 | NextableInputStream in = new NextableInputStream( | |
118 | new ByteArrayInputStream(data), | |
119 | new NextableInputStreamStep(12)); | |
120 | ||
121 | in.nextAll(); | |
122 | IOUtils.toByteArray(in); | |
123 | ||
124 | assertEquals("The number of bytes read is not correct", | |
125 | data.length, in.getBytesRead()); | |
126 | } | |
127 | }); | |
128 | ||
129 | addTest(new TestCase("bytes array input") { | |
130 | @Override | |
131 | public void test() throws Exception { | |
132 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
133 | NextableInputStream in = new NextableInputStream(data, | |
134 | new NextableInputStreamStep(12)); | |
135 | ||
136 | checkNext(this, "FIRST", in, new byte[] { 42 }); | |
137 | checkNext(this, "SECOND", in, new byte[] { 0, 127 }); | |
138 | checkNext(this, "THIRD", in, new byte[] { 51, 11 }); | |
4098af70 N |
139 | } |
140 | }); | |
473e5f31 N |
141 | |
142 | addTest(new TestCase("Skip data") { | |
143 | @Override | |
144 | public void test() throws Exception { | |
145 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
146 | NextableInputStream in = new NextableInputStream(data, null); | |
147 | in.next(); | |
148 | ||
149 | in.skip(4); | |
150 | checkArrays(this, "ONLY", in, new byte[] { 12, 51, 11, 12 }); | |
151 | } | |
152 | }); | |
153 | ||
154 | addTest(new TestCase("Starts with") { | |
155 | @Override | |
156 | public void test() throws Exception { | |
157 | byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; | |
158 | NextableInputStream in = new NextableInputStream(data, null); | |
159 | in.next(); | |
160 | ||
161 | // yes | |
162 | assertEquals("It actually starts with that", true, | |
163 | in.startsWith(new byte[] { 42 })); | |
164 | assertEquals("It actually starts with that", true, | |
165 | in.startsWith(new byte[] { 42, 12 })); | |
166 | assertEquals("It actually is the same array", true, | |
167 | in.startsWith(data)); | |
168 | ||
169 | // no | |
170 | assertEquals("It actually does not start with that", false, | |
171 | in.startsWith(new byte[] { 12 })); | |
172 | assertEquals("It actually does not start with that", false, | |
173 | in.startsWith( | |
174 | new byte[] { 42, 12, 0, 127, 12, 51, 11, 11 })); | |
175 | ||
176 | // too big | |
177 | try { | |
178 | in.startsWith( | |
179 | new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, 0 }); | |
180 | fail("Searching a prefix bigger than the array should throw an IOException"); | |
181 | } catch (IOException e) { | |
182 | } | |
183 | } | |
184 | }); | |
185 | ||
186 | addTest(new TestCase("Starts with strings") { | |
187 | @Override | |
188 | public void test() throws Exception { | |
189 | String text = "Fanfan et Toto vont à la mer"; | |
190 | byte[] data = text.getBytes("UTF-8"); | |
191 | NextableInputStream in = new NextableInputStream(data, null); | |
192 | in.next(); | |
193 | ||
194 | // yes | |
195 | assertEquals("It actually starts with that", true, | |
196 | in.startsWiths("F")); | |
197 | assertEquals("It actually starts with that", true, | |
198 | in.startsWiths("Fanfan et")); | |
199 | assertEquals("It actually is the same text", true, | |
200 | in.startsWiths(text)); | |
201 | ||
202 | // no | |
203 | assertEquals("It actually does not start with that", false, | |
204 | in.startsWiths("Toto")); | |
205 | assertEquals("It actually does not start with that", false, | |
206 | in.startsWiths("Fanfan et Toto vont à la mee")); | |
207 | ||
208 | // too big | |
209 | try { | |
210 | in.startsWiths("Fanfan et Toto vont à la mer."); | |
211 | fail("Searching a prefix bigger than the array should throw an IOException"); | |
212 | } catch (IOException e) { | |
213 | } | |
214 | } | |
215 | }); | |
2e7584da | 216 | } |
63b46ca9 | 217 | |
d6f9bd9f NR |
218 | static void checkNext(TestCase test, String prefix, NextableInputStream in, |
219 | byte[] expected) throws Exception { | |
220 | test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); | |
473e5f31 | 221 | checkArrays(test, prefix, in, expected); |
d6f9bd9f NR |
222 | } |
223 | ||
224 | static void checkNextAll(TestCase test, String prefix, | |
225 | NextableInputStream in, byte[] expected) throws Exception { | |
226 | test.assertEquals("Cannot get " + prefix + " entries", true, | |
227 | in.nextAll()); | |
473e5f31 N |
228 | checkArrays(test, prefix, in, expected); |
229 | } | |
230 | ||
231 | static void checkArrays(TestCase test, String prefix, | |
232 | NextableInputStream in, byte[] expected) throws Exception { | |
63b46ca9 NR |
233 | byte[] actual = IOUtils.toByteArray(in); |
234 | test.assertEquals("The " + prefix | |
235 | + " resulting array has not the correct number of items", | |
236 | expected.length, actual.length); | |
237 | for (int i = 0; i < actual.length; i++) { | |
238 | test.assertEquals("Item " + i + " (0-based) is not the same", | |
239 | expected[i], actual[i]); | |
240 | } | |
241 | } | |
2e7584da | 242 | } |