1 package be
.nikiroo
.utils
.test_code
;
3 import java
.io
.ByteArrayOutputStream
;
4 import java
.util
.ArrayList
;
7 import be
.nikiroo
.utils
.streams
.BufferedOutputStream
;
8 import be
.nikiroo
.utils
.test
.TestCase
;
9 import be
.nikiroo
.utils
.test
.TestLauncher
;
11 class BufferedOutputStreamTest
extends TestLauncher
{
12 public BufferedOutputStreamTest(String
[] args
) {
13 super("BufferedOutputStream test", args
);
15 addTest(new TestCase("Single write") {
17 public void test() throws Exception
{
18 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
19 BufferedOutputStream out
= new BufferedOutputStream(bout
);
21 byte[] data
= new byte[] { 42, 12, 0, 127 };
26 checkArrays(this, "FIRST", bout
, data
);
30 addTest(new TestCase("Single write of 5000 bytes") {
32 public void test() throws Exception
{
33 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
34 BufferedOutputStream out
= new BufferedOutputStream(bout
);
36 byte[] data
= new byte[5000];
37 for (int i
= 0; i
< data
.length
; i
++) {
38 data
[i
] = (byte) (i
% 255);
44 checkArrays(this, "FIRST", bout
, data
);
48 addTest(new TestCase("Multiple writes") {
50 public void test() throws Exception
{
51 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
52 BufferedOutputStream out
= new BufferedOutputStream(bout
);
54 byte[] data1
= new byte[] { 42, 12, 0, 127 };
55 byte[] data2
= new byte[] { 15, 55 };
56 byte[] data3
= new byte[] {};
58 byte[] dataAll
= new byte[] { 42, 12, 0, 127, 15, 55 };
65 checkArrays(this, "FIRST", bout
, dataAll
);
69 addTest(new TestCase("Multiple writes for a 5000 bytes total") {
71 public void test() throws Exception
{
72 ByteArrayOutputStream bout
= new ByteArrayOutputStream();
73 BufferedOutputStream out
= new BufferedOutputStream(bout
);
75 byte[] data
= new byte[] { 42, 12, 0, 127, 51, 2, 32, 66, 7, 87 };
77 List
<Byte
> bytes
= new ArrayList
<Byte
>();
79 // write 400 * 10 + 1000 bytes = 5000
80 for (int i
= 0; i
< 400; i
++) {
81 for (int j
= 0; j
< data
.length
; j
++) {
87 for (int i
= 0; i
< 1000; i
++) {
88 for (int j
= 0; j
< data
.length
; j
++) {
96 byte[] abytes
= new byte[bytes
.size()];
97 for (int i
= 0; i
< bytes
.size(); i
++) {
98 abytes
[i
] = bytes
.get(i
);
101 checkArrays(this, "FIRST", bout
, abytes
);
106 static void checkArrays(TestCase test
, String prefix
,
107 ByteArrayOutputStream bout
, byte[] expected
) throws Exception
{
108 byte[] actual
= bout
.toByteArray();
111 System
.out
.print("\nExpected data: [ ");
112 for (int i
= 0; i
< expected
.length
; i
++) {
114 System
.out
.print(", ");
115 System
.out
.print(expected
[i
]);
117 System
.out
.println(" ]");
119 System
.out
.print("Actual data : [ ");
120 for (int i
= 0; i
< actual
.length
; i
++) {
122 System
.out
.print(", ");
123 System
.out
.print(actual
[i
]);
125 System
.out
.println(" ]");
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
]);