X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest_code%2FBufferedOutputStreamTest.java;fp=src%2Fbe%2Fnikiroo%2Futils%2Ftest_code%2FBufferedOutputStreamTest.java;h=2ed712e24da8f2ff3b391734deda05295870c1e4;hb=a26188d393b11040b8ee8476338a73bfadabffb6;hp=0000000000000000000000000000000000000000;hpb=eeb2cc17c4dbbbf9eb7f4c6ef695226a66fbf106;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java b/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java new file mode 100644 index 0000000..2ed712e --- /dev/null +++ b/src/be/nikiroo/utils/test_code/BufferedOutputStreamTest.java @@ -0,0 +1,80 @@ +package be.nikiroo.utils.test_code; + +import java.io.ByteArrayOutputStream; + +import be.nikiroo.utils.BufferedOutputStream; +import be.nikiroo.utils.test.TestCase; +import be.nikiroo.utils.test.TestLauncher; + +class BufferedOutputStreamTest extends TestLauncher { + public BufferedOutputStreamTest(String[] args) { + super("BufferedOutputStream test", args); + + addTest(new TestCase("Single write") { + @Override + public void test() throws Exception { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + BufferedOutputStream out = new BufferedOutputStream(bout); + + byte[] data = new byte[] { 42, 12, 0, 127 }; + + out.write(data); + out.close(); + + checkArrays(this, "FIRST", bout, data); + } + }); + + addTest(new TestCase("Multiple writes") { + @Override + public void test() throws Exception { + ByteArrayOutputStream bout = new ByteArrayOutputStream(); + BufferedOutputStream out = new BufferedOutputStream(bout); + + byte[] data1 = new byte[] { 42, 12, 0, 127 }; + byte[] data2 = new byte[] { 15, 55 }; + byte[] data3 = new byte[] {}; + + byte[] dataAll = new byte[] { 42, 12, 0, 127, 15, 55 }; + + out.write(data1); + out.write(data2); + out.write(data3); + out.close(); + + checkArrays(this, "FIRST", bout, dataAll); + } + }); + } + + static void checkArrays(TestCase test, String prefix, + ByteArrayOutputStream bout, byte[] expected) throws Exception { + byte[] actual = bout.toByteArray(); + + if (false) { + System.out.print("\nExpected data: [ "); + for (int i = 0; i < actual.length; i++) { + if (i > 0) + System.out.print(", "); + System.out.print(expected[i]); + } + System.out.println(" ]"); + + System.out.print("Actual data : [ "); + for (int i = 0; i < actual.length; i++) { + if (i > 0) + System.out.print(", "); + System.out.print(actual[i]); + } + System.out.println(" ]"); + } + + test.assertEquals("The " + prefix + + " resulting array has not the correct number of items", + expected.length, actual.length); + for (int i = 0; i < actual.length; i++) { + test.assertEquals(prefix + ": item " + i + + " (0-based) is not the same", expected[i], actual[i]); + } + } +}