Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(750)

Side by Side Diff: tests/standalone/src/ChunkedStreamTest.dart

Issue 8818009: Add chunked input stream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments from ager@ Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/input_stream.dart ('k') | tests/standalone/src/FileInputStreamTest.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, 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 class ListInputStream implements InputStream {
6 List<int> read() {
7 var result = _data;
8 _data = null;
9 return result;
10 }
11
12 void set dataHandler(void callback()) {
13 _dataHandler = callback;
14 }
15
16 void set closeHandler(void callback()) {
17 _closeHandler = callback;
18 }
19
20 void set errorHandler(void callback(int error)) {
21 }
22
23 void write(List<int> data) {
24 Expect.equals(null, _data);
25 _data = data;
26 if (_dataHandler != null) {
27 // Data handler is not called through the event loop here.
28 _dataHandler();
29 }
30 }
31
32 void close() {
33 Expect.equals(null, _data);
34 // Close handler is not called through the event loop here.
35 if (_closeHandler != null) _closeHandler();
36 }
37
38 List<int> _data;
39 var _dataHandler;
40 var _closeHandler;
41 }
42
43 void test1() {
44 void testWithChunkSize(var data, int chunkSize, Function testDone) {
45 InputStream s = new ListInputStream();
46 ChunkedInputStream stream = new ChunkedInputStream(s);
47 int chunkCount = 0;
48 int byteCount = 0;
49 void chunkData() {
50 List<int> chunk = stream.read();
51 if (byteCount + chunkSize < data.length) {
52 Expect.equals(chunkSize, chunk.length);
53 } else {
54 if (byteCount == data.length) {
55 Expect.equals(null, chunk);
56 } else {
57 Expect.equals(data.length - byteCount, chunk.length);
58 }
59 }
60 if (chunk != null) {
61 for (int i = 0; i < chunk.length;i++) {
62 Expect.equals(data[byteCount], chunk[i]);
63 byteCount++;
64 }
65 chunkCount++;
66 }
67 }
68
69 void closeHandler() {
70 Expect.equals(data.length, byteCount);
71 testDone(byteCount);
72 }
73
74 stream.dataHandler = chunkData;
75 stream.closeHandler = closeHandler;
76 stream.chunkSize = chunkSize;
77 s.write(data);
78 s.close();
79 }
80
81 for (int i = 1; i <= 10; i++) {
82 var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
83
84 void testDone(int byteCount) {
85 Expect.equals(data.length, byteCount);
86 }
87
88 testWithChunkSize(data, i, testDone);
89 }
90
91 var _16k = 1024 * 16;
92 var data = new List<int>(_16k);
93 for (int i = 0; i < _16k; i++) { data[i] = i % 256; }
94
95 void testDone(int byteCount) {
96 Expect.equals(data.length, byteCount);
97 }
98
99 testWithChunkSize(data, 512, testDone);
100 testWithChunkSize(data, 1024, testDone);
101 testWithChunkSize(data, 2048, testDone);
102 }
103
104
105 void test2() {
106 InputStream s = new ListInputStream();
107 ChunkedInputStream stream = new ChunkedInputStream(s);
108 stream.chunkSize = 5;
109
110 var stage = 0;
111
112 void chunkData() {
113 var chunk;
114 if (stage == 0) {
115 Expect.equals(stream.chunkSize, 5);
116 chunk = stream.read(); // 5 bytes read from stream.
117 Expect.equals(stream.chunkSize, chunk.length);
118 chunk = stream.read(); // 5 bytes read from stream.
119 Expect.equals(null, chunk);
120 stage++;
121 s.write([7, 8, 9]); // 10 bytes written to stream.
122 } else if (stage == 1) {
123 Expect.equals(stream.chunkSize, 5);
124 chunk = stream.read(); // 10 bytes read from stream.
125 Expect.equals(stream.chunkSize, chunk.length);
126 chunk = stream.read(); // 10 bytes read from stream.
127 Expect.equals(null, chunk);
128 stage++;
129 s.write([10, 11, 12, 13, 14]); // 15 bytes written to stream.
130 s.write([15, 16, 17, 18]); // 19 bytes written to stream.
131 } else if (stage == 2) {
132 Expect.equals(stream.chunkSize, 5);
133 chunk = stream.read(); // 15 bytes read from stream.
134 Expect.equals(stream.chunkSize, chunk.length);
135 chunk = stream.read(); // 15 bytes read from stream.
136 Expect.equals(null, chunk);
137 stage++;
138 stream.chunkSize = 3;
139 } else if (stage == 3) {
140 Expect.equals(stream.chunkSize, 3);
141 chunk = stream.read(); // 18 bytes read from stream.
142 Expect.equals(stream.chunkSize, chunk.length);
143 chunk = stream.read(); // 18 bytes read from stream.
144 Expect.equals(null, chunk);
145 stage++;
146 s.close(); // 18 bytes written to stream.
147 } else if (stage == 4) {
148 chunk = stream.read(); // 19 bytes read from stream.
149 Expect.equals(1, chunk.length);
150 chunk = stream.read(); // 19 bytes read from stream.
151 Expect.equals(null, chunk);
152 stage++;
153 }
154 }
155
156 void streamClosed() {
157 Expect.equals(5, stage);
158 }
159
160 stream.dataHandler = chunkData;
161 stream.closeHandler = streamClosed;
162 s.write([0, 1, 2, 3]); // 4 bytes written to stream.
163 Expect.equals(0, stage);
164 s.write([4, 5, 6]); // 7 bytes written to stream.
165 Expect.equals(4, stage);
166 }
167
168
169 main() {
170 test1();
171 test2();
172 }
OLDNEW
« no previous file with comments | « runtime/bin/input_stream.dart ('k') | tests/standalone/src/FileInputStreamTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698