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

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: Added ChunkedStreamTest.dart 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
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;
Mads Ager (google) 2011/12/06 14:05:02 Is the underscore needed here?
Søren Gjesse 2011/12/07 07:58:29 Yes, a variable cannot start with a digit.
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 chunk = stream.read();
Mads Ager (google) 2011/12/06 14:05:02 Maybe add comments here static how many bytes are
Søren Gjesse 2011/12/07 07:58:29 Done.
116 Expect.equals(stream.chunkSize, chunk.length);
117 chunk = stream.read();
118 Expect.equals(null, chunk);
119 stage++;
120 s.write([7, 8, 9]);
121 } else if (stage == 1) {
122 chunk = stream.read();
123 Expect.equals(stream.chunkSize, chunk.length);
124 chunk = stream.read();
125 Expect.equals(null, chunk);
126 stage++;
127 s.write([10, 11, 12, 13, 14, 15, 16]);
128 } else if (stage == 2) {
129 chunk = stream.read();
130 Expect.equals(stream.chunkSize, chunk.length);
131 chunk = stream.read();
132 Expect.equals(null, chunk);
133 stage++;
134 s.close();
135 } else if (stage == 3) {
136 chunk = stream.read();
137 Expect.equals(2, chunk.length);
138 chunk = stream.read();
139 Expect.equals(null, chunk);
140 stage++;
141 }
142 }
143
144 void streamClosed() {
145 Expect.equals(4, stage);
146 }
147
148 stream.dataHandler = chunkData;
149 stream.closeHandler = streamClosed;
150 s.write([0, 1, 2, 3]);
151 Expect.equals(0, stage);
152 s.write([4, 5, 6]);
153 Expect.equals(3, stage);
154 }
155
156
Mads Ager (google) 2011/12/06 14:05:02 It would be nice to also have a test that changes
Søren Gjesse 2011/12/07 07:58:29 Good point. Done.
157 main() {
158 test1();
159 test2();
160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698