OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import "dart:io"; | |
6 import "dart:isolate"; | |
7 | |
8 void test1() { | |
9 void testWithChunkSize(var data, int chunkSize, Function testDone) { | |
10 ListInputStream list_input_stream = new ListInputStream(); | |
11 list_input_stream.write(data); | |
12 list_input_stream.markEndOfStream(); | |
13 ChunkedInputStream stream = new ChunkedInputStream(list_input_stream); | |
14 int chunkCount = 0; | |
15 int byteCount = 0; | |
16 void chunkData() { | |
17 List<int> chunk = stream.read(); | |
18 if (byteCount + chunkSize < data.length) { | |
19 Expect.equals(chunkSize, chunk.length); | |
20 } else { | |
21 if (byteCount == data.length) { | |
22 Expect.equals(null, chunk); | |
23 } else { | |
24 Expect.equals(data.length - byteCount, chunk.length); | |
25 } | |
26 } | |
27 if (chunk != null) { | |
28 for (int i = 0; i < chunk.length;i++) { | |
29 Expect.equals(data[byteCount], chunk[i]); | |
30 byteCount++; | |
31 } | |
32 chunkCount++; | |
33 } | |
34 } | |
35 | |
36 void closeHandler() { | |
37 Expect.equals(data.length, byteCount); | |
38 testDone(byteCount); | |
39 } | |
40 | |
41 stream.onData = chunkData; | |
42 stream.onClosed = closeHandler; | |
43 stream.chunkSize = chunkSize; | |
44 } | |
45 | |
46 for (int i = 1; i <= 10; i++) { | |
47 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
48 | |
49 void testDone(int byteCount) { | |
50 Expect.equals(data.length, byteCount); | |
51 } | |
52 | |
53 testWithChunkSize(data, i, testDone); | |
54 } | |
55 | |
56 var _16k = 1024 * 16; | |
57 var data = new List<int>.fixedLength(_16k); | |
58 for (int i = 0; i < _16k; i++) { data[i] = i % 256; } | |
59 | |
60 void testDone(int byteCount) { | |
61 Expect.equals(data.length, byteCount); | |
62 } | |
63 | |
64 testWithChunkSize(data, 512, testDone); | |
65 testWithChunkSize(data, 1024, testDone); | |
66 testWithChunkSize(data, 2048, testDone); | |
67 } | |
68 | |
69 | |
70 void test2() { | |
71 ListInputStream s = new ListInputStream(); | |
72 ChunkedInputStream stream = new ChunkedInputStream(s); | |
73 stream.chunkSize = 5; | |
74 ReceivePort donePort = new ReceivePort(); | |
75 | |
76 var stage = 0; | |
77 | |
78 void chunkData() { | |
79 var chunk; | |
80 if (stage == 0) { | |
81 Expect.equals(stream.chunkSize, 5); | |
82 chunk = stream.read(); // 5 bytes read from stream. | |
83 Expect.equals(stream.chunkSize, chunk.length); | |
84 chunk = stream.read(); // 5 bytes read from stream. | |
85 Expect.equals(null, chunk); | |
86 stage++; | |
87 s.write([7, 8, 9]); // 10 bytes written to stream. | |
88 } else if (stage == 1) { | |
89 Expect.equals(stream.chunkSize, 5); | |
90 chunk = stream.read(); // 10 bytes read from stream. | |
91 Expect.equals(stream.chunkSize, chunk.length); | |
92 chunk = stream.read(); // 10 bytes read from stream. | |
93 Expect.equals(null, chunk); | |
94 stage++; | |
95 s.write([10, 11, 12, 13, 14]); // 15 bytes written to stream. | |
96 s.write([15, 16, 17, 18]); // 19 bytes written to stream. | |
97 } else if (stage == 2) { | |
98 Expect.equals(stream.chunkSize, 5); | |
99 chunk = stream.read(); // 15 bytes read from stream. | |
100 Expect.equals(stream.chunkSize, chunk.length); | |
101 chunk = stream.read(); // 15 bytes read from stream. | |
102 Expect.equals(null, chunk); | |
103 stage++; | |
104 stream.chunkSize = 3; | |
105 } else if (stage == 3) { | |
106 Expect.equals(stream.chunkSize, 3); | |
107 chunk = stream.read(); // 18 bytes read from stream. | |
108 Expect.equals(stream.chunkSize, chunk.length); | |
109 chunk = stream.read(); // 18 bytes read from stream. | |
110 Expect.equals(null, chunk); | |
111 stage++; | |
112 s.markEndOfStream(); // 18 bytes written to stream. | |
113 } else if (stage == 4) { | |
114 chunk = stream.read(); // 19 bytes read from stream. | |
115 Expect.equals(1, chunk.length); | |
116 chunk = stream.read(); // 19 bytes read from stream. | |
117 Expect.equals(null, chunk); | |
118 stage++; | |
119 donePort.toSendPort().send(stage); | |
120 } | |
121 } | |
122 | |
123 void streamClosed() { | |
124 Expect.equals(5, stage); | |
125 } | |
126 | |
127 stream.onData = chunkData; | |
128 stream.onClosed = streamClosed; | |
129 s.write([0, 1, 2, 3]); // 4 bytes written to stream. | |
130 Expect.equals(0, stage); | |
131 s.write([4, 5, 6]); // 7 bytes written to stream. | |
132 | |
133 donePort.receive((x,y) => donePort.close()); | |
134 } | |
135 | |
136 | |
137 main() { | |
138 test1(); | |
139 test2(); | |
140 } | |
OLD | NEW |