X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest_code%2FNextableInputStreamTest.java;h=4664cbf7e161b41e1b85179debbbe95fc2991b42;hb=473e5f319a4cacc584b5daaf8f0c5d1f18bbf5d0;hp=b5f6780351512ed425fd7e00f060bd7a696126df;hpb=63b46ca9f1703134ef2979b72d474e9c9b8f5737;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java b/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java index b5f6780..4664cbf 100644 --- a/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java +++ b/src/be/nikiroo/utils/test_code/NextableInputStreamTest.java @@ -1,6 +1,7 @@ package be.nikiroo.utils.test_code; import java.io.ByteArrayInputStream; +import java.io.IOException; import be.nikiroo.utils.IOUtils; import be.nikiroo.utils.NextableInputStream; @@ -18,6 +19,7 @@ public class NextableInputStreamTest extends TestLauncher { byte[] expected = new byte[] { 42, 12, 0, 127 }; NextableInputStream in = new NextableInputStream( new ByteArrayInputStream(expected), null); + in.next(); byte[] actual = IOUtils.toByteArray(in); assertEquals( @@ -38,7 +40,7 @@ public class NextableInputStreamTest extends TestLauncher { new ByteArrayInputStream(expected), new NextableInputStreamStep(12)); - checkNext(this, false, "FIRST", in, new byte[] { 42 }); + checkNext(this, "FIRST", in, new byte[] { 42 }); } }); @@ -50,9 +52,9 @@ public class NextableInputStreamTest extends TestLauncher { new ByteArrayInputStream(data), new NextableInputStreamStep(12)); - checkNext(this, false, "FIRST", in, new byte[] { 42 }); - checkNext(this, true, "SECOND", in, new byte[] { 0, 127 }); - checkNext(this, true, "THIRD", in, new byte[] { 51, 11 }); + checkNext(this, "FIRST", in, new byte[] { 42 }); + checkNext(this, "SECOND", in, new byte[] { 0, 127 }); + checkNext(this, "THIRD", in, new byte[] { 51, 11 }); } }); @@ -66,23 +68,168 @@ public class NextableInputStreamTest extends TestLauncher { NextableInputStream subIn12 = new NextableInputStream(in4, new NextableInputStreamStep(12)); - checkNext(this, false, "SUB FIRST", subIn12, new byte[] { 42 }); - checkNext(this, true, "SUB SECOND", subIn12, new byte[] { 0 }); + in4.next(); + checkNext(this, "SUB FIRST", subIn12, new byte[] { 42 }); + checkNext(this, "SUB SECOND", subIn12, new byte[] { 0 }); assertEquals("The subIn still has some data", false, subIn12.next()); - - checkNext(this, true, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); + + checkNext(this, "MAIN LAST", in4, new byte[] { 127, 12, 5 }); + } + }); + + addTest(new TestCase("UTF-8 text lines test") { + @Override + public void test() throws Exception { + String ln1 = "Ligne première"; + String ln2 = "Ligne la deuxième du nom"; + byte[] data = (ln1 + "\n" + ln2).getBytes("UTF-8"); + NextableInputStream in = new NextableInputStream( + new ByteArrayInputStream(data), + new NextableInputStreamStep('\n')); + + checkNext(this, "FIRST", in, ln1.getBytes("UTF-8")); + checkNext(this, "SECOND", in, ln2.getBytes("UTF-8")); + } + }); + + addTest(new TestCase("nextAll()") { + @Override + public void test() throws Exception { + byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; + NextableInputStream in = new NextableInputStream( + new ByteArrayInputStream(data), + new NextableInputStreamStep(12)); + + checkNext(this, "FIRST", in, new byte[] { 42 }); + checkNextAll(this, "REST", in, + new byte[] { 0, 127, 12, 51, 11, 12 }); + assertEquals("The stream still has some data", false, + in.next()); + } + }); + + addTest(new TestCase("getBytesRead()") { + @Override + public void test() throws Exception { + byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; + NextableInputStream in = new NextableInputStream( + new ByteArrayInputStream(data), + new NextableInputStreamStep(12)); + + in.nextAll(); + IOUtils.toByteArray(in); + + assertEquals("The number of bytes read is not correct", + data.length, in.getBytesRead()); + } + }); + + addTest(new TestCase("bytes array input") { + @Override + public void test() throws Exception { + byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; + NextableInputStream in = new NextableInputStream(data, + new NextableInputStreamStep(12)); + + checkNext(this, "FIRST", in, new byte[] { 42 }); + checkNext(this, "SECOND", in, new byte[] { 0, 127 }); + checkNext(this, "THIRD", in, new byte[] { 51, 11 }); + } + }); + + addTest(new TestCase("Skip data") { + @Override + public void test() throws Exception { + byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; + NextableInputStream in = new NextableInputStream(data, null); + in.next(); + + in.skip(4); + checkArrays(this, "ONLY", in, new byte[] { 12, 51, 11, 12 }); } }); + + addTest(new TestCase("Starts with") { + @Override + public void test() throws Exception { + byte[] data = new byte[] { 42, 12, 0, 127, 12, 51, 11, 12 }; + NextableInputStream in = new NextableInputStream(data, null); + in.next(); + + // yes + assertEquals("It actually starts with that", true, + in.startsWith(new byte[] { 42 })); + assertEquals("It actually starts with that", true, + in.startsWith(new byte[] { 42, 12 })); + assertEquals("It actually is the same array", true, + in.startsWith(data)); + + // no + assertEquals("It actually does not start with that", false, + in.startsWith(new byte[] { 12 })); + assertEquals("It actually does not start with that", false, + in.startsWith( + new byte[] { 42, 12, 0, 127, 12, 51, 11, 11 })); + + // too big + try { + in.startsWith( + new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, 0 }); + fail("Searching a prefix bigger than the array should throw an IOException"); + } catch (IOException e) { + } + } + }); + + addTest(new TestCase("Starts with strings") { + @Override + public void test() throws Exception { + String text = "Fanfan et Toto vont à la mer"; + byte[] data = text.getBytes("UTF-8"); + NextableInputStream in = new NextableInputStream(data, null); + in.next(); + + // yes + assertEquals("It actually starts with that", true, + in.startsWiths("F")); + assertEquals("It actually starts with that", true, + in.startsWiths("Fanfan et")); + assertEquals("It actually is the same text", true, + in.startsWiths(text)); + + // no + assertEquals("It actually does not start with that", false, + in.startsWiths("Toto")); + assertEquals("It actually does not start with that", false, + in.startsWiths("Fanfan et Toto vont à la mee")); + + // too big + try { + in.startsWiths("Fanfan et Toto vont à la mer."); + fail("Searching a prefix bigger than the array should throw an IOException"); + } catch (IOException e) { + } + } + }); + } + + static void checkNext(TestCase test, String prefix, NextableInputStream in, + byte[] expected) throws Exception { + test.assertEquals("Cannot get " + prefix + " entry", true, in.next()); + checkArrays(test, prefix, in, expected); } - static void checkNext(TestCase test, boolean callNext, String prefix, + static void checkNextAll(TestCase test, String prefix, + NextableInputStream in, byte[] expected) throws Exception { + test.assertEquals("Cannot get " + prefix + " entries", true, + in.nextAll()); + checkArrays(test, prefix, in, expected); + } + + static void checkArrays(TestCase test, String prefix, NextableInputStream in, byte[] expected) throws Exception { - if (callNext) { - test.assertEquals("Cannot get " + prefix + " entry", true, - in.next()); - } byte[] actual = IOUtils.toByteArray(in); test.assertEquals("The " + prefix + " resulting array has not the correct number of items",