| 1 | package be.nikiroo.utils.test_code; |
| 2 | |
| 3 | import java.io.ByteArrayOutputStream; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import be.nikiroo.utils.streams.BufferedOutputStream; |
| 8 | import be.nikiroo.utils.test.TestCase; |
| 9 | import be.nikiroo.utils.test.TestLauncher; |
| 10 | |
| 11 | class BufferedOutputStreamTest extends TestLauncher { |
| 12 | public BufferedOutputStreamTest(String[] args) { |
| 13 | super("BufferedOutputStream test", args); |
| 14 | |
| 15 | addTest(new TestCase("Single write") { |
| 16 | @Override |
| 17 | public void test() throws Exception { |
| 18 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 19 | BufferedOutputStream out = new BufferedOutputStream(bout); |
| 20 | |
| 21 | byte[] data = new byte[] { 42, 12, 0, 127 }; |
| 22 | |
| 23 | out.write(data); |
| 24 | out.close(); |
| 25 | |
| 26 | checkArrays(this, "FIRST", bout, data); |
| 27 | } |
| 28 | }); |
| 29 | |
| 30 | addTest(new TestCase("Single write of 5000 bytes") { |
| 31 | @Override |
| 32 | public void test() throws Exception { |
| 33 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 34 | BufferedOutputStream out = new BufferedOutputStream(bout); |
| 35 | |
| 36 | byte[] data = new byte[5000]; |
| 37 | for (int i = 0; i < data.length; i++) { |
| 38 | data[i] = (byte) (i % 255); |
| 39 | } |
| 40 | |
| 41 | out.write(data); |
| 42 | out.close(); |
| 43 | |
| 44 | checkArrays(this, "FIRST", bout, data); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | addTest(new TestCase("Multiple writes") { |
| 49 | @Override |
| 50 | public void test() throws Exception { |
| 51 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 52 | BufferedOutputStream out = new BufferedOutputStream(bout); |
| 53 | |
| 54 | byte[] data1 = new byte[] { 42, 12, 0, 127 }; |
| 55 | byte[] data2 = new byte[] { 15, 55 }; |
| 56 | byte[] data3 = new byte[] {}; |
| 57 | |
| 58 | byte[] dataAll = new byte[] { 42, 12, 0, 127, 15, 55 }; |
| 59 | |
| 60 | out.write(data1); |
| 61 | out.write(data2); |
| 62 | out.write(data3); |
| 63 | out.close(); |
| 64 | |
| 65 | checkArrays(this, "FIRST", bout, dataAll); |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | addTest(new TestCase("Multiple writes for a 5000 bytes total") { |
| 70 | @Override |
| 71 | public void test() throws Exception { |
| 72 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 73 | BufferedOutputStream out = new BufferedOutputStream(bout); |
| 74 | |
| 75 | byte[] data = new byte[] { 42, 12, 0, 127, 51, 2, 32, 66, 7, 87 }; |
| 76 | |
| 77 | List<Byte> bytes = new ArrayList<Byte>(); |
| 78 | |
| 79 | // write 400 * 10 + 1000 bytes = 5000 |
| 80 | for (int i = 0; i < 400; i++) { |
| 81 | for (int j = 0; j < data.length; j++) { |
| 82 | bytes.add(data[j]); |
| 83 | } |
| 84 | out.write(data); |
| 85 | } |
| 86 | |
| 87 | for (int i = 0; i < 1000; i++) { |
| 88 | for (int j = 0; j < data.length; j++) { |
| 89 | bytes.add(data[j]); |
| 90 | } |
| 91 | out.write(data); |
| 92 | } |
| 93 | |
| 94 | out.close(); |
| 95 | |
| 96 | byte[] abytes = new byte[bytes.size()]; |
| 97 | for (int i = 0; i < bytes.size(); i++) { |
| 98 | abytes[i] = bytes.get(i); |
| 99 | } |
| 100 | |
| 101 | checkArrays(this, "FIRST", bout, abytes); |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | static void checkArrays(TestCase test, String prefix, |
| 107 | ByteArrayOutputStream bout, byte[] expected) throws Exception { |
| 108 | byte[] actual = bout.toByteArray(); |
| 109 | |
| 110 | if (false) { |
| 111 | System.out.print("\nExpected data: [ "); |
| 112 | for (int i = 0; i < expected.length; i++) { |
| 113 | if (i > 0) |
| 114 | System.out.print(", "); |
| 115 | System.out.print(expected[i]); |
| 116 | } |
| 117 | System.out.println(" ]"); |
| 118 | |
| 119 | System.out.print("Actual data : [ "); |
| 120 | for (int i = 0; i < actual.length; i++) { |
| 121 | if (i > 0) |
| 122 | System.out.print(", "); |
| 123 | System.out.print(actual[i]); |
| 124 | } |
| 125 | System.out.println(" ]"); |
| 126 | } |
| 127 | |
| 128 | test.assertEquals("The " + prefix |
| 129 | + " resulting array has not the correct number of items", |
| 130 | expected.length, actual.length); |
| 131 | for (int i = 0; i < actual.length; i++) { |
| 132 | test.assertEquals(prefix + ": item " + i |
| 133 | + " (0-based) is not the same", expected[i], actual[i]); |
| 134 | } |
| 135 | } |
| 136 | } |