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