OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #import("dart:io"); | 5 #import("dart:io"); |
6 #import("dart:isolate"); | 6 #import("dart:isolate"); |
7 | 7 |
8 void testEmptyListInputStream() { | 8 void testEmptyListInputStream() { |
9 InputStream stream = new ListInputStream(); | 9 InputStream stream = new ListInputStream(); |
10 stream.write([]); | 10 stream.write([]); |
11 stream.markEndOfStream(); | 11 stream.markEndOfStream(); |
12 ReceivePort donePort = new ReceivePort(); | 12 ReceivePort donePort = new ReceivePort(); |
13 | 13 |
14 void onData() { | 14 void onData() { |
15 throw "No data expected"; | 15 throw "No data expected"; |
16 } | 16 } |
17 | 17 |
18 void onClose() { | 18 void onClosed() { |
19 donePort.toSendPort().send(null); | 19 donePort.toSendPort().send(null); |
20 } | 20 } |
21 | 21 |
22 stream.dataHandler = onData; | 22 stream.onData = onData; |
23 stream.closeHandler = onClose; | 23 stream.onClosed = onClosed; |
24 | 24 |
25 donePort.receive((x,y) => donePort.close()); | 25 donePort.receive((x,y) => donePort.close()); |
26 } | 26 } |
27 | 27 |
28 void testEmptyDynamicListInputStream() { | 28 void testEmptyDynamicListInputStream() { |
29 InputStream stream = new ListInputStream(); | 29 InputStream stream = new ListInputStream(); |
30 ReceivePort donePort = new ReceivePort(); | 30 ReceivePort donePort = new ReceivePort(); |
31 | 31 |
32 void onData() { | 32 void onData() { |
33 throw "No data expected"; | 33 throw "No data expected"; |
34 } | 34 } |
35 | 35 |
36 void onClose() { | 36 void onClosed() { |
37 donePort.toSendPort().send(null); | 37 donePort.toSendPort().send(null); |
38 } | 38 } |
39 | 39 |
40 stream.dataHandler = onData; | 40 stream.onData = onData; |
41 stream.closeHandler = onClose; | 41 stream.onClosed = onClosed; |
42 stream.markEndOfStream(); | 42 stream.markEndOfStream(); |
43 | 43 |
44 donePort.receive((x,y) => donePort.close()); | 44 donePort.receive((x,y) => donePort.close()); |
45 } | 45 } |
46 | 46 |
47 void testListInputStream1() { | 47 void testListInputStream1() { |
48 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; | 48 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; |
49 InputStream stream = new ListInputStream(); | 49 InputStream stream = new ListInputStream(); |
50 stream.write(data); | 50 stream.write(data); |
51 stream.markEndOfStream(); | 51 stream.markEndOfStream(); |
52 int count = 0; | 52 int count = 0; |
53 ReceivePort donePort = new ReceivePort(); | 53 ReceivePort donePort = new ReceivePort(); |
54 | 54 |
55 void onData() { | 55 void onData() { |
56 List<int> x = stream.read(1); | 56 List<int> x = stream.read(1); |
57 Expect.equals(1, x.length); | 57 Expect.equals(1, x.length); |
58 Expect.equals(data[count++], x[0]); | 58 Expect.equals(data[count++], x[0]); |
59 } | 59 } |
60 | 60 |
61 void onClose() { | 61 void onClosed() { |
62 Expect.equals(data.length, count); | 62 Expect.equals(data.length, count); |
63 donePort.toSendPort().send(count); | 63 donePort.toSendPort().send(count); |
64 } | 64 } |
65 | 65 |
66 stream.dataHandler = onData; | 66 stream.onData = onData; |
67 stream.closeHandler = onClose; | 67 stream.onClosed = onClosed; |
68 | 68 |
69 donePort.receive((x,y) => donePort.close()); | 69 donePort.receive((x,y) => donePort.close()); |
70 } | 70 } |
71 | 71 |
72 void testListInputStream2() { | 72 void testListInputStream2() { |
73 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; | 73 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; |
74 InputStream stream = new ListInputStream(); | 74 InputStream stream = new ListInputStream(); |
75 stream.write(data); | 75 stream.write(data); |
76 stream.markEndOfStream(); | 76 stream.markEndOfStream(); |
77 int count = 0; | 77 int count = 0; |
78 ReceivePort donePort = new ReceivePort(); | 78 ReceivePort donePort = new ReceivePort(); |
79 | 79 |
80 void onData() { | 80 void onData() { |
81 List<int> x = new List<int>(2); | 81 List<int> x = new List<int>(2); |
82 var bytesRead = stream.readInto(x); | 82 var bytesRead = stream.readInto(x); |
83 Expect.equals(2, bytesRead); | 83 Expect.equals(2, bytesRead); |
84 Expect.equals(data[count++], x[0]); | 84 Expect.equals(data[count++], x[0]); |
85 Expect.equals(data[count++], x[1]); | 85 Expect.equals(data[count++], x[1]); |
86 } | 86 } |
87 | 87 |
88 void onClose() { | 88 void onClosed() { |
89 Expect.equals(data.length, count); | 89 Expect.equals(data.length, count); |
90 donePort.toSendPort().send(count); | 90 donePort.toSendPort().send(count); |
91 } | 91 } |
92 | 92 |
93 stream.dataHandler = onData; | 93 stream.onData = onData; |
94 stream.closeHandler = onClose; | 94 stream.onClosed = onClosed; |
95 | 95 |
96 donePort.receive((x,y) => donePort.close()); | 96 donePort.receive((x,y) => donePort.close()); |
97 } | 97 } |
98 | 98 |
99 void testListInputStreamPipe1() { | 99 void testListInputStreamPipe1() { |
100 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; | 100 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; |
101 InputStream input = new ListInputStream(); | 101 InputStream input = new ListInputStream(); |
102 input.write(data); | 102 input.write(data); |
103 input.markEndOfStream(); | 103 input.markEndOfStream(); |
104 OutputStream output = new ListOutputStream(); | 104 OutputStream output = new ListOutputStream(); |
105 ReceivePort donePort = new ReceivePort(); | 105 ReceivePort donePort = new ReceivePort(); |
106 | 106 |
107 void onClose() { | 107 void onClosed() { |
108 var contents = output.contents(); | 108 var contents = output.contents(); |
109 Expect.equals(data.length, contents.length); | 109 Expect.equals(data.length, contents.length); |
110 donePort.toSendPort().send(null); | 110 donePort.toSendPort().send(null); |
111 } | 111 } |
112 | 112 |
113 input.closeHandler = onClose; | 113 input.onClosed = onClosed; |
114 input.pipe(output); | 114 input.pipe(output); |
115 | 115 |
116 donePort.receive((x,y) => donePort.close()); | 116 donePort.receive((x,y) => donePort.close()); |
117 } | 117 } |
118 | 118 |
119 void testListInputStreamPipe2() { | 119 void testListInputStreamPipe2() { |
120 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; | 120 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; |
121 OutputStream output = new ListOutputStream(); | 121 OutputStream output = new ListOutputStream(); |
122 ReceivePort donePort = new ReceivePort(); | 122 ReceivePort donePort = new ReceivePort(); |
123 int count = 0; | 123 int count = 0; |
124 | 124 |
125 void onClose() { | 125 void onClosed() { |
126 if (count < 10) { | 126 if (count < 10) { |
127 InputStream input = new ListInputStream(); | 127 InputStream input = new ListInputStream(); |
128 input.write(data); | 128 input.write(data); |
129 input.markEndOfStream(); | 129 input.markEndOfStream(); |
130 input.closeHandler = onClose; | 130 input.onClosed = onClosed; |
131 if (count < 9) { | 131 if (count < 9) { |
132 input.pipe(output, close: false); | 132 input.pipe(output, close: false); |
133 } else { | 133 } else { |
134 input.pipe(output); | 134 input.pipe(output); |
135 } | 135 } |
136 count++; | 136 count++; |
137 } else { | 137 } else { |
138 var contents = output.contents(); | 138 var contents = output.contents(); |
139 Expect.equals(data.length * 10, contents.length); | 139 Expect.equals(data.length * 10, contents.length); |
140 donePort.toSendPort().send(null); | 140 donePort.toSendPort().send(null); |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 InputStream input = new ListInputStream(); | 144 InputStream input = new ListInputStream(); |
145 input.write(data); | 145 input.write(data); |
146 input.markEndOfStream(); | 146 input.markEndOfStream(); |
147 input.closeHandler = onClose; | 147 input.onClosed = onClosed; |
148 input.pipe(output, close: false); | 148 input.pipe(output, close: false); |
149 count++; | 149 count++; |
150 | 150 |
151 donePort.receive((x,y) => donePort.close()); | 151 donePort.receive((x,y) => donePort.close()); |
152 } | 152 } |
153 | 153 |
154 void testListInputClose1() { | 154 void testListInputClose1() { |
155 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 155 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
156 InputStream stream = new ListInputStream(); | 156 InputStream stream = new ListInputStream(); |
157 stream.write(data); | 157 stream.write(data); |
158 stream.markEndOfStream(); | 158 stream.markEndOfStream(); |
159 ReceivePort donePort = new ReceivePort(); | 159 ReceivePort donePort = new ReceivePort(); |
160 | 160 |
161 void onData() { | 161 void onData() { |
162 throw "No data expected"; | 162 throw "No data expected"; |
163 } | 163 } |
164 | 164 |
165 void onClose() { | 165 void onClosed() { |
166 donePort.toSendPort().send(null); | 166 donePort.toSendPort().send(null); |
167 } | 167 } |
168 | 168 |
169 stream.dataHandler = onData; | 169 stream.onData = onData; |
170 stream.closeHandler = onClose; | 170 stream.onClosed = onClosed; |
171 stream.close(); | 171 stream.close(); |
172 | 172 |
173 donePort.receive((x,y) => donePort.close()); | 173 donePort.receive((x,y) => donePort.close()); |
174 } | 174 } |
175 | 175 |
176 void testListInputClose2() { | 176 void testListInputClose2() { |
177 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 177 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
178 InputStream stream = new ListInputStream(); | 178 InputStream stream = new ListInputStream(); |
179 stream.write(data); | 179 stream.write(data); |
180 stream.markEndOfStream(); | 180 stream.markEndOfStream(); |
181 ReceivePort donePort = new ReceivePort(); | 181 ReceivePort donePort = new ReceivePort(); |
182 int count = 0; | 182 int count = 0; |
183 | 183 |
184 void onData() { | 184 void onData() { |
185 count += stream.read(2).length; | 185 count += stream.read(2).length; |
186 stream.close(); | 186 stream.close(); |
187 } | 187 } |
188 | 188 |
189 void onClose() { | 189 void onClosed() { |
190 Expect.equals(2, count); | 190 Expect.equals(2, count); |
191 donePort.toSendPort().send(count); | 191 donePort.toSendPort().send(count); |
192 } | 192 } |
193 | 193 |
194 stream.dataHandler = onData; | 194 stream.onData = onData; |
195 stream.closeHandler = onClose; | 195 stream.onClosed = onClosed; |
196 | 196 |
197 donePort.receive((x,y) => donePort.close()); | 197 donePort.receive((x,y) => donePort.close()); |
198 } | 198 } |
199 | 199 |
200 void testDynamicListInputStream() { | 200 void testDynamicListInputStream() { |
201 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; | 201 List<int> data = [0x00, 0x01, 0x10, 0x11, 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff]; |
202 InputStream stream = new ListInputStream(); | 202 InputStream stream = new ListInputStream(); |
203 int count = 0; | 203 int count = 0; |
204 ReceivePort donePort = new ReceivePort(); | 204 ReceivePort donePort = new ReceivePort(); |
205 | 205 |
206 void onData() { | 206 void onData() { |
207 List<int> x = stream.read(1); | 207 List<int> x = stream.read(1); |
208 Expect.equals(1, x.length); | 208 Expect.equals(1, x.length); |
209 x = stream.read(); | 209 x = stream.read(); |
210 Expect.equals(9, x.length); | 210 Expect.equals(9, x.length); |
211 count++; | 211 count++; |
212 if (count < 10) { | 212 if (count < 10) { |
213 stream.write(data); | 213 stream.write(data); |
214 } else { | 214 } else { |
215 stream.markEndOfStream(); | 215 stream.markEndOfStream(); |
216 } | 216 } |
217 } | 217 } |
218 | 218 |
219 void onClose() { | 219 void onClosed() { |
220 Expect.equals(data.length, count); | 220 Expect.equals(data.length, count); |
221 donePort.toSendPort().send(count); | 221 donePort.toSendPort().send(count); |
222 } | 222 } |
223 | 223 |
224 stream.write(data); | 224 stream.write(data); |
225 stream.dataHandler = onData; | 225 stream.onData = onData; |
226 stream.closeHandler = onClose; | 226 stream.onClosed = onClosed; |
227 | 227 |
228 donePort.receive((x,y) => donePort.close()); | 228 donePort.receive((x,y) => donePort.close()); |
229 } | 229 } |
230 | 230 |
231 void testDynamicListInputClose1() { | 231 void testDynamicListInputClose1() { |
232 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 232 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
233 InputStream stream = new ListInputStream(); | 233 InputStream stream = new ListInputStream(); |
234 ReceivePort donePort = new ReceivePort(); | 234 ReceivePort donePort = new ReceivePort(); |
235 | 235 |
236 void onData() { | 236 void onData() { |
237 throw "No data expected"; | 237 throw "No data expected"; |
238 } | 238 } |
239 | 239 |
240 void onClose() { | 240 void onClosed() { |
241 donePort.toSendPort().send(null); | 241 donePort.toSendPort().send(null); |
242 } | 242 } |
243 | 243 |
244 stream.write(data); | 244 stream.write(data); |
245 stream.dataHandler = onData; | 245 stream.onData = onData; |
246 stream.closeHandler = onClose; | 246 stream.onClosed = onClosed; |
247 stream.close(); | 247 stream.close(); |
248 Expect.throws(() => stream.write(data), (e) => e is StreamException); | 248 Expect.throws(() => stream.write(data), (e) => e is StreamException); |
249 | 249 |
250 donePort.receive((x,y) => donePort.close()); | 250 donePort.receive((x,y) => donePort.close()); |
251 } | 251 } |
252 | 252 |
253 void testDynamicListInputClose2() { | 253 void testDynamicListInputClose2() { |
254 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 254 List<int> data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
255 InputStream stream = new ListInputStream(); | 255 InputStream stream = new ListInputStream(); |
256 ReceivePort donePort = new ReceivePort(); | 256 ReceivePort donePort = new ReceivePort(); |
257 int count = 0; | 257 int count = 0; |
258 | 258 |
259 void onData() { | 259 void onData() { |
260 count += stream.read(15).length; | 260 count += stream.read(15).length; |
261 stream.close(); | 261 stream.close(); |
262 Expect.throws(() => stream.write(data), (e) => e is StreamException); | 262 Expect.throws(() => stream.write(data), (e) => e is StreamException); |
263 } | 263 } |
264 | 264 |
265 void onClose() { | 265 void onClosed() { |
266 Expect.equals(15, count); | 266 Expect.equals(15, count); |
267 donePort.toSendPort().send(null); | 267 donePort.toSendPort().send(null); |
268 } | 268 } |
269 | 269 |
270 stream.write(data); | 270 stream.write(data); |
271 stream.write(data); | 271 stream.write(data); |
272 stream.write(data); | 272 stream.write(data); |
273 stream.dataHandler = onData; | 273 stream.onData = onData; |
274 stream.closeHandler = onClose; | 274 stream.onClosed = onClosed; |
275 | 275 |
276 donePort.receive((x,y) => donePort.close()); | 276 donePort.receive((x,y) => donePort.close()); |
277 } | 277 } |
278 | 278 |
279 main() { | 279 main() { |
280 testEmptyListInputStream(); | 280 testEmptyListInputStream(); |
281 testEmptyDynamicListInputStream(); | 281 testEmptyDynamicListInputStream(); |
282 testListInputStream1(); | 282 testListInputStream1(); |
283 testListInputStream2(); | 283 testListInputStream2(); |
284 testListInputStreamPipe1(); | 284 testListInputStreamPipe1(); |
285 testListInputStreamPipe2(); | 285 testListInputStreamPipe2(); |
286 testListInputClose1(); | 286 testListInputClose1(); |
287 testListInputClose2(); | 287 testListInputClose2(); |
288 testDynamicListInputStream(); | 288 testDynamicListInputStream(); |
289 testDynamicListInputClose1(); | 289 testDynamicListInputClose1(); |
290 testDynamicListInputClose2(); | 290 testDynamicListInputClose2(); |
291 } | 291 } |
OLD | NEW |