| 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 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 static void deleteTempDirectory() { | 39 static void deleteTempDirectory() { |
| 40 tempDirectory.deleteSync(recursive: true); | 40 tempDirectory.deleteSync(recursive: true); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Test for file read functionality. | 43 // Test for file read functionality. |
| 44 static void testReadStream() { | 44 static void testReadStream() { |
| 45 // Read a file and check part of it's contents. | 45 // Read a file and check part of it's contents. |
| 46 String filename = getFilename("bin/file_test.cc"); | 46 String filename = getFilename("bin/file_test.cc"); |
| 47 File file = new File(filename); | 47 File file = new File(filename); |
| 48 Expect.isTrue('$file'.contains(file.name)); | 48 Expect.isTrue('$file'.contains(file.name)); |
| 49 InputStream input = file.openInputStream(); | 49 var subscription; |
| 50 input.onData = () { | 50 List<int> buffer = new List<int>(); |
| 51 List<int> buffer = new List<int>.fixedLength(42); | 51 subscription = file.openRead().listen( |
| 52 int bytesRead = input.readInto(buffer, 0, 12); | 52 (d) { |
| 53 Expect.equals(12, bytesRead); | 53 buffer.addAll(d); |
| 54 bytesRead = input.readInto(buffer, 12, 30); | 54 if (buffer.length >= 12) { |
| 55 input.close(); | 55 subscription.cancel(); |
| 56 Expect.equals(30, bytesRead); | 56 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 57 Expect.equals(47, buffer[0]); // represents '/' in the file. | 57 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 58 Expect.equals(47, buffer[1]); // represents '/' in the file. | 58 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| 59 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 59 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| 60 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 60 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
| 61 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 61 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
| 62 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 62 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
| 63 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 63 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
| 64 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 64 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
| 65 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 65 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 66 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 66 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
| 67 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 67 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 68 Expect.equals(116, buffer[11]); // represents 't' in the file. | 68 } |
| 69 }; | 69 }); |
| 70 } | 70 } |
| 71 | 71 |
| 72 // Test for file read and write functionality. | 72 // Test for file read and write functionality. |
| 73 static void testReadWriteStream() { | 73 static void testReadWriteStream() { |
| 74 asyncTestStarted(); | 74 asyncTestStarted(); |
| 75 | 75 |
| 76 // Read a file. | 76 // Read a file. |
| 77 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 77 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 78 File file; | 78 File file; |
| 79 InputStream input; | 79 InputStream input; |
| 80 int bytesRead; | 80 int bytesRead; |
| 81 | 81 |
| 82 // Test reading all using readInto. | |
| 83 var file1 = new File(inFilename); | 82 var file1 = new File(inFilename); |
| 84 var input1 = file1.openInputStream(); | 83 List<int> buffer = new List<int>(); |
| 85 List<int> buffer1; | 84 file1.openRead().listen( |
| 86 input1.onData = () { | 85 (d) { |
| 87 buffer1 = new List<int>.fixedLength(42); | 86 buffer.addAll(d); |
| 88 bytesRead = input1.readInto(buffer1, 0, 42); | 87 }, |
| 89 Expect.equals(42, bytesRead); | 88 onDone: () { |
| 90 }; | 89 Expect.equals(42, buffer.length); |
| 91 input1.onError = (e) { throw e; }; | 90 // Write the contents of the file just read into another file. |
| 92 input1.onClosed = () { | 91 String outFilename = |
| 93 Expect.isTrue(input1.closed); | 92 tempDirectory.path.concat("/out_read_write_stream"); |
| 94 | 93 var file2 = new File(outFilename); |
| 95 // Test reading all using readInto and read. | 94 var output = file2.openWrite(); |
| 96 var file2 = new File(inFilename); | 95 output.add(buffer); |
| 97 var input2 = file2.openInputStream(); | 96 output.close(); |
| 98 input2.onData = () { | 97 output.done.then((_) { |
| 99 bytesRead = input2.readInto(buffer1, 0, 21); | 98 // Now read the contents of the file just written. |
| 100 Expect.equals(21, bytesRead); | 99 List<int> buffer2 = new List<int>(); |
| 101 buffer1 = input2.read(); | 100 new File(outFilename).openRead().listen( |
| 102 Expect.equals(21, buffer1.length); | 101 (d) { |
| 103 }; | 102 buffer2.addAll(d); |
| 104 input2.onError = (e) { throw e; }; | 103 }, |
| 105 input2.onClosed = () { | 104 onDone: () { |
| 106 Expect.isTrue(input2.closed); | 105 Expect.equals(42, buffer2.length); |
| 107 | 106 // Now compare the two buffers to check if they are |
| 108 // Test reading all using read and readInto. | 107 // identical. |
| 109 var file3 = new File(inFilename); | 108 for (int i = 0; i < buffer.length; i++) { |
| 110 var input3 = file3.openInputStream(); | 109 Expect.equals(buffer[i], buffer2[i]); |
| 111 input3.onData = () { | 110 } |
| 112 buffer1 = input3.read(21); | 111 // Delete the output file. |
| 113 Expect.equals(21, buffer1.length); | 112 file2.deleteSync(); |
| 114 bytesRead = input3.readInto(buffer1, 0, 21); | 113 Expect.isFalse(file2.existsSync()); |
| 115 Expect.equals(21, bytesRead); | 114 asyncTestDone("testReadWriteStream"); |
| 116 }; | 115 }); |
| 117 input3.onError = (e) { throw e; }; | 116 }); |
| 118 input3.onClosed = () { | 117 }); |
| 119 Expect.isTrue(input3.closed); | |
| 120 | |
| 121 // Test reading all using read. | |
| 122 var file4 = new File(inFilename); | |
| 123 var input4 = file4.openInputStream(); | |
| 124 input4.onData = () { | |
| 125 buffer1 = input4.read(); | |
| 126 Expect.equals(42, buffer1.length); | |
| 127 }; | |
| 128 input4.onError = (e) { throw e; }; | |
| 129 input4.onClosed = () { | |
| 130 Expect.isTrue(input4.closed); | |
| 131 | |
| 132 // Write the contents of the file just read into another file. | |
| 133 String outFilename = | |
| 134 tempDirectory.path.concat("/out_read_write_stream"); | |
| 135 file = new File(outFilename); | |
| 136 OutputStream output = file.openOutputStream(); | |
| 137 bool writeDone = output.writeFrom(buffer1, 0, 42); | |
| 138 Expect.equals(false, writeDone); | |
| 139 output.onNoPendingWrites = () { | |
| 140 output.close(); | |
| 141 output.onClosed = () { | |
| 142 // Now read the contents of the file just written. | |
| 143 List<int> buffer2 = new List<int>.fixedLength(42); | |
| 144 var file6 = new File(outFilename); | |
| 145 var input6 = file6.openInputStream(); | |
| 146 input6.onData = () { | |
| 147 bytesRead = input6.readInto(buffer2, 0, 42); | |
| 148 Expect.equals(42, bytesRead); | |
| 149 // Now compare the two buffers to check if they are identical. | |
| 150 for (int i = 0; i < buffer1.length; i++) { | |
| 151 Expect.equals(buffer1[i], buffer2[i]); | |
| 152 } | |
| 153 }; | |
| 154 input6.onError = (e) { throw e; }; | |
| 155 input6.onClosed = () { | |
| 156 // Delete the output file. | |
| 157 file6.deleteSync(); | |
| 158 Expect.isFalse(file6.existsSync()); | |
| 159 asyncTestDone("testReadWriteStream"); | |
| 160 }; | |
| 161 }; | |
| 162 }; | |
| 163 }; | |
| 164 }; | |
| 165 }; | |
| 166 }; | |
| 167 } | 118 } |
| 168 | 119 |
| 169 // Test for file stream buffered handling of large files. | 120 // Test for file stream buffered handling of large files. |
| 170 static void testReadWriteStreamLargeFile() { | 121 static void testReadWriteStreamLargeFile() { |
| 171 asyncTestStarted(); | 122 asyncTestStarted(); |
| 172 | 123 |
| 173 // Create the test data - arbitrary binary data. | 124 // Create the test data - arbitrary binary data. |
| 174 List<int> buffer = new List<int>.fixedLength(100000); | 125 List<int> buffer = new List<int>.fixedLength(100000); |
| 175 for (var i = 0; i < buffer.length; ++i) { | 126 for (var i = 0; i < buffer.length; ++i) { |
| 176 buffer[i] = i % 256; | 127 buffer[i] = i % 256; |
| 177 } | 128 } |
| 178 String filename = | 129 String filename = |
| 179 tempDirectory.path.concat("/out_read_write_stream_large_file"); | 130 tempDirectory.path.concat("/out_read_write_stream_large_file"); |
| 180 File file = new File(filename); | 131 File file = new File(filename); |
| 181 OutputStream output = file.openOutputStream(); | 132 IOSink output = file.openWrite(); |
| 182 // Test a write immediately after the output stream is created. | 133 output.add(buffer); |
| 183 output.writeFrom(buffer, 0, 20000); | 134 output.add(buffer); |
| 184 | 135 output.close(); |
| 185 output.onNoPendingWrites = () { | 136 output.done.then((_) { |
| 186 output.writeFrom(buffer, 20000, 60000); | 137 Stream input = file.openRead(); |
| 187 output.writeFrom(buffer, 80000, 20000); | |
| 188 output.onNoPendingWrites = () { | |
| 189 output.writeFrom(buffer, 0, 0); | |
| 190 output.writeFrom(buffer, 0, 0); | |
| 191 output.writeFrom(buffer, 0, 100000); | |
| 192 output.close(); | |
| 193 }; | |
| 194 }; | |
| 195 output.onClosed = () { | |
| 196 InputStream input = file.openInputStream(); | |
| 197 int position = 0; | 138 int position = 0; |
| 198 final int expectedLength = 200000; | 139 final int expectedLength = 200000; |
| 199 // Start an independent asynchronous check on the length. | 140 // Start an independent asynchronous check on the length. |
| 200 asyncTestStarted(); | 141 asyncTestStarted(); |
| 201 file.length().then((len) { | 142 file.length().then((len) { |
| 202 Expect.equals(expectedLength, len); | 143 Expect.equals(expectedLength, len); |
| 203 asyncTestDone('testReadWriteStreamLargeFile: length check'); | 144 asyncTestDone('testReadWriteStreamLargeFile: length check'); |
| 204 }); | 145 }); |
| 205 | 146 |
| 206 List<int> inputBuffer = | |
| 207 new List<int>.fixedLength(expectedLength + 100000); | |
| 208 // Immediate read should read 0 bytes. | 147 // Immediate read should read 0 bytes. |
| 209 Expect.equals(0, input.available()); | 148 input.listen( |
| 210 Expect.equals(false, input.closed); | 149 (d) { |
| 211 int bytesRead = input.readInto(inputBuffer); | 150 for (int i = 0; i < d.length; ++i) { |
| 212 Expect.equals(0, bytesRead); | 151 Expect.equals(buffer[(i + position) % buffer.length], d[i]); |
| 213 Expect.equals(0, input.available()); | 152 } |
| 214 Expect.isFalse(input.closed); | 153 position += d.length; |
| 215 input.onError = (e) { | 154 }, |
| 216 print('Error handler called on input in testReadWriteStreamLargeFile'); | 155 onError: (e) { |
| 217 print('with error $e'); | 156 print('Error on input in testReadWriteStreamLargeFile'); |
| 218 throw e; | 157 print('with error $e'); |
| 219 }; | 158 throw e; |
| 220 input.onData = () { | 159 }, |
| 221 Expect.isFalse(input.closed); | 160 onDone: () { |
| 222 bytesRead = input.readInto(inputBuffer, position, | 161 Expect.equals(expectedLength, position); |
| 223 inputBuffer.length - position); | 162 testPipe(file, buffer) |
| 224 position += bytesRead; | 163 .then((_) => file.delete()) |
| 225 // The buffer is large enough to hold all available data. | 164 .then((_) { |
| 226 // So there should be no data left to read. | 165 asyncTestDone('testReadWriteStreamLargeFile: main test'); |
| 227 Expect.equals(0, input.available()); | 166 }) |
| 228 bytesRead = input.readInto(inputBuffer, position, | 167 .catchError((e) { |
| 229 expectedLength - position); | 168 print('Exception while deleting ReadWriteStreamLargeFile file'); |
| 230 Expect.equals(0, bytesRead); | 169 print('Exception $e'); |
| 231 Expect.equals(0, input.available()); | 170 }); |
| 232 Expect.isFalse(input.closed); | |
| 233 }; | |
| 234 input.onClosed = () { | |
| 235 Expect.equals(0, input.available()); | |
| 236 Expect.isTrue(input.closed); | |
| 237 input.close(); // This should be safe to call. | |
| 238 | |
| 239 Expect.equals(expectedLength, position); | |
| 240 for (int i = 0; i < position; ++i) { | |
| 241 Expect.equals(buffer[i % buffer.length], inputBuffer[i]); | |
| 242 } | |
| 243 | |
| 244 Future testPipeDone = testPipe(file, buffer); | |
| 245 | |
| 246 Future futureDeleted = testPipeDone.then((ignored) => file.delete()); | |
| 247 futureDeleted.then((ignored) { | |
| 248 asyncTestDone('testReadWriteStreamLargeFile: main test'); | |
| 249 }).catchError((e) { | |
| 250 print('Exception while deleting ReadWriteStreamLargeFile file'); | |
| 251 print('Exception $e'); | |
| 252 }); | 171 }); |
| 253 }; | 172 }); |
| 254 // Try a read again after handlers are set. | |
| 255 bytesRead = input.readInto(inputBuffer); | |
| 256 Expect.equals(0, bytesRead); | |
| 257 Expect.equals(0, input.available()); | |
| 258 Expect.isFalse(input.closed); | |
| 259 }; | |
| 260 } | 173 } |
| 261 | 174 |
| 262 static Future testPipe(File file, buffer) { | 175 static Future testPipe(File file, buffer) { |
| 263 String outputFilename = '${file.name}_copy'; | 176 String outputFilename = '${file.name}_copy'; |
| 264 File outputFile = new File(outputFilename); | 177 File outputFile = new File(outputFilename); |
| 265 InputStream input = file.openInputStream(); | 178 var input = file.openRead(); |
| 266 OutputStream output = outputFile.openOutputStream(); | 179 var output = outputFile.openWrite(); |
| 267 input.pipe(output); | |
| 268 Completer done = new Completer(); | 180 Completer done = new Completer(); |
| 269 output.onClosed = () { | 181 input.pipe(output).then((_) { |
| 270 InputStream copy = outputFile.openInputStream(); | 182 var copy = outputFile.openRead(); |
| 271 int position = 0; | 183 int position = 0; |
| 272 copy.onData = () { | 184 copy.listen( |
| 273 var data; | 185 (d) { |
| 274 while ((data = copy.read()) != null) { | 186 for (int i = 0; i < d.length; i++) { |
| 275 for (int value in data) { | 187 Expect.equals(buffer[(position + i) % buffer.length], d[i]); |
| 276 Expect.equals(buffer[position % buffer.length], value); | 188 } |
| 277 position++; | 189 position += d.length; |
| 278 } | 190 }, |
| 279 } | 191 onDone: () { |
| 280 }; | 192 Expect.equals(2 * buffer.length, position); |
| 281 copy.onClosed = () { | 193 outputFile.delete().then((ignore) { done.complete(); }); |
| 282 Expect.equals(2 * buffer.length, position); | 194 }); |
| 283 outputFile.delete().then((ignore) { done.complete(null); }); | 195 }); |
| 284 }; | |
| 285 }; | |
| 286 return done.future; | 196 return done.future; |
| 287 } | 197 } |
| 288 | 198 |
| 289 static void testRead() { | 199 static void testRead() { |
| 290 ReceivePort port = new ReceivePort(); | 200 ReceivePort port = new ReceivePort(); |
| 291 // Read a file and check part of it's contents. | 201 // Read a file and check part of it's contents. |
| 292 String filename = getFilename("bin/file_test.cc"); | 202 String filename = getFilename("bin/file_test.cc"); |
| 293 File file = new File(filename); | 203 File file = new File(filename); |
| 294 file.open(FileMode.READ).then((RandomAccessFile file) { | 204 file.open(FileMode.READ).then((RandomAccessFile file) { |
| 295 List<int> buffer = new List<int>.fixedLength(10); | 205 List<int> buffer = new List<int>.fixedLength(10); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 openedFile.closeSync(); | 326 openedFile.closeSync(); |
| 417 file.deleteSync(); | 327 file.deleteSync(); |
| 418 } | 328 } |
| 419 | 329 |
| 420 static void testOutputStreamWriteAppend() { | 330 static void testOutputStreamWriteAppend() { |
| 421 String content = "foobar"; | 331 String content = "foobar"; |
| 422 String filename = tempDirectory.path.concat("/outstream_write_append"); | 332 String filename = tempDirectory.path.concat("/outstream_write_append"); |
| 423 File file = new File(filename); | 333 File file = new File(filename); |
| 424 file.createSync(); | 334 file.createSync(); |
| 425 List<int> buffer = content.charCodes; | 335 List<int> buffer = content.charCodes; |
| 426 OutputStream outStream = file.openOutputStream(); | 336 var output = file.openWrite(); |
| 427 outStream.write(buffer); | 337 output.add(buffer); |
| 428 outStream.onNoPendingWrites = () { | 338 output.close(); |
| 429 outStream.close(); | 339 output.done.then((_) { |
| 430 outStream.onClosed = () { | 340 File file2 = new File(filename); |
| 431 File file2 = new File(filename); | 341 var appendingOutput = file2.openWrite(FileMode.APPEND); |
| 432 OutputStream appendingOutput = | 342 appendingOutput.add(buffer); |
| 433 file2.openOutputStream(FileMode.APPEND); | 343 appendingOutput.close(); |
| 434 appendingOutput.write(buffer); | 344 appendingOutput.done.then((_) { |
| 435 appendingOutput.onNoPendingWrites = () { | 345 File file3 = new File(filename); |
| 436 appendingOutput.close(); | 346 file3.open(FileMode.READ).then((RandomAccessFile openedFile) { |
| 437 appendingOutput.onClosed = () { | 347 openedFile.length().then((int length) { |
| 438 File file3 = new File(filename); | 348 Expect.equals(content.length * 2, length); |
| 439 file3.open(FileMode.READ).then((RandomAccessFile openedFile) { | 349 openedFile.close().then((ignore) { |
| 440 openedFile.length().then((int length) { | 350 file3.delete().then((ignore) { |
| 441 Expect.equals(content.length * 2, length); | 351 asyncTestDone("testOutputStreamWriteAppend"); |
| 442 openedFile.close().then((ignore) { | |
| 443 file3.delete().then((ignore) { | |
| 444 asyncTestDone("testOutputStreamWriteAppend"); | |
| 445 }); | |
| 446 }); | |
| 447 }); | 352 }); |
| 448 }); | 353 }); |
| 449 }; | 354 }); |
| 450 }; | 355 }); |
| 451 }; | 356 }); |
| 452 }; | 357 }); |
| 453 asyncTestStarted(); | 358 asyncTestStarted(); |
| 454 } | 359 } |
| 455 | 360 |
| 456 // Test for file read and write functionality. | 361 // Test for file read and write functionality. |
| 457 static void testOutputStreamWriteString() { | 362 static void testOutputStreamWriteString() { |
| 458 String content = "foobar"; | 363 String content = "foobar"; |
| 459 String filename = tempDirectory.path.concat("/outstream_write_string"); | 364 String filename = tempDirectory.path.concat("/outstream_write_string"); |
| 460 File file = new File(filename); | 365 File file = new File(filename); |
| 461 file.createSync(); | 366 file.createSync(); |
| 462 List<int> buffer = content.charCodes; | 367 List<int> buffer = content.charCodes; |
| 463 OutputStream outStream = file.openOutputStream(); | 368 var output = file.openWrite(); |
| 464 outStream.writeString("abcdABCD"); | 369 output.addString("abcdABCD"); |
| 465 outStream.writeString("abcdABCD", Encoding.UTF_8); | 370 output.addString("abcdABCD", Encoding.UTF_8); |
| 466 outStream.writeString("abcdABCD", Encoding.ISO_8859_1); | 371 output.addString("abcdABCD", Encoding.ISO_8859_1); |
| 467 outStream.writeString("abcdABCD", Encoding.ASCII); | 372 output.addString("abcdABCD", Encoding.ASCII); |
| 468 outStream.writeString("æøå", Encoding.UTF_8); | 373 output.addString("æøå", Encoding.UTF_8); |
| 469 outStream.onNoPendingWrites = () { | 374 output.close(); |
| 470 outStream.close(); | 375 output.done.then((_) { |
| 471 outStream.onClosed = () { | 376 RandomAccessFile raf = file.openSync(); |
| 472 RandomAccessFile raf = file.openSync(); | 377 Expect.equals(38, raf.lengthSync()); |
| 473 Expect.equals(38, raf.lengthSync()); | 378 raf.close().then((ignore) { |
| 474 raf.close().then((ignore) { | 379 asyncTestDone("testOutputStreamWriteString"); |
| 475 asyncTestDone("testOutputStreamWriteString"); | 380 }); |
| 476 }); | 381 }); |
| 477 }; | |
| 478 }; | |
| 479 asyncTestStarted(); | 382 asyncTestStarted(); |
| 480 } | 383 } |
| 481 | 384 |
| 482 | 385 |
| 483 static void testReadWriteSync() { | 386 static void testReadWriteSync() { |
| 484 // Read a file. | 387 // Read a file. |
| 485 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 388 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 486 RandomAccessFile file = (new File(inFilename)).openSync(); | 389 RandomAccessFile file = (new File(inFilename)).openSync(); |
| 487 List<int> buffer1 = new List<int>.fixedLength(42); | 390 List<int> buffer1 = new List<int>.fixedLength(42); |
| 488 int bytes_read = 0; | 391 int bytes_read = 0; |
| (...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 839 input.deleteSync(); | 742 input.deleteSync(); |
| 840 } | 743 } |
| 841 | 744 |
| 842 // Tests stream exception handling after file was closed. | 745 // Tests stream exception handling after file was closed. |
| 843 static void testCloseExceptionStream() { | 746 static void testCloseExceptionStream() { |
| 844 asyncTestStarted(); | 747 asyncTestStarted(); |
| 845 List<int> buffer = new List<int>.fixedLength(42); | 748 List<int> buffer = new List<int>.fixedLength(42); |
| 846 File file = | 749 File file = |
| 847 new File(tempDirectory.path.concat("/out_close_exception_stream")); | 750 new File(tempDirectory.path.concat("/out_close_exception_stream")); |
| 848 file.createSync(); | 751 file.createSync(); |
| 849 InputStream input = file.openInputStream(); | 752 var output = file.openWrite(); |
| 850 input.onClosed = () { | 753 output.close(); |
| 851 Expect.isTrue(input.closed); | 754 Expect.throws(() => output.add(buffer)); |
| 852 Expect.equals(0, input.readInto(buffer, 0, 12)); | 755 output.done.then((_) { |
| 853 OutputStream output = file.openOutputStream(); | 756 file.deleteSync(); |
| 854 output.close(); | 757 asyncTestDone("testCloseExceptionStream"); |
| 855 Expect.throws(() => output.writeFrom(buffer, 0, 12)); | 758 }); |
| 856 output.onClosed = () { | |
| 857 file.deleteSync(); | |
| 858 asyncTestDone("testCloseExceptionStream"); | |
| 859 }; | |
| 860 }; | |
| 861 } | 759 } |
| 862 | 760 |
| 863 // Tests buffer out of bounds exception. | 761 // Tests buffer out of bounds exception. |
| 864 static void testBufferOutOfBoundsException() { | 762 static void testBufferOutOfBoundsException() { |
| 865 bool exceptionCaught = false; | 763 bool exceptionCaught = false; |
| 866 bool wrongExceptionCaught = false; | 764 bool wrongExceptionCaught = false; |
| 867 File file = | 765 File file = |
| 868 new File(tempDirectory.path.concat("/out_buffer_out_of_bounds")); | 766 new File(tempDirectory.path.concat("/out_buffer_out_of_bounds")); |
| 869 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | 767 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
| 870 try { | 768 try { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1076 static void testReadAsTextSync() { | 974 static void testReadAsTextSync() { |
| 1077 var name = getFilename("tests/vm/data/fixed_length_file"); | 975 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 1078 var text = new File(name).readAsStringSync(); | 976 var text = new File(name).readAsStringSync(); |
| 1079 Expect.isTrue(text.endsWith("42 bytes.")); | 977 Expect.isTrue(text.endsWith("42 bytes.")); |
| 1080 Expect.equals(42, text.length); | 978 Expect.equals(42, text.length); |
| 1081 name = getDataFilename("tests/standalone/io/read_as_text.dat"); | 979 name = getDataFilename("tests/standalone/io/read_as_text.dat"); |
| 1082 text = new File(name).readAsStringSync(); | 980 text = new File(name).readAsStringSync(); |
| 1083 Expect.equals(6, text.length); | 981 Expect.equals(6, text.length); |
| 1084 var expected = [955, 120, 46, 32, 120, 10]; | 982 var expected = [955, 120, 46, 32, 120, 10]; |
| 1085 Expect.listEquals(expected, text.charCodes); | 983 Expect.listEquals(expected, text.charCodes); |
| 1086 Expect.throws(() { new File(name).readAsStringSync(Encoding.ASCII); }); | 984 text = new File(name).readAsStringSync(Encoding.ASCII); |
| 985 // Default replacement character is '?', char code 63. |
| 986 expected = [63, 63, 120, 46, 32, 120, 10]; |
| 987 Expect.listEquals(expected, text.charCodes); |
| 1087 text = new File(name).readAsStringSync(Encoding.ISO_8859_1); | 988 text = new File(name).readAsStringSync(Encoding.ISO_8859_1); |
| 1088 expected = [206, 187, 120, 46, 32, 120, 10]; | 989 expected = [206, 187, 120, 46, 32, 120, 10]; |
| 1089 Expect.equals(7, text.length); | 990 Expect.equals(7, text.length); |
| 1090 Expect.listEquals(expected, text.charCodes); | 991 Expect.listEquals(expected, text.charCodes); |
| 1091 } | 992 } |
| 1092 | 993 |
| 1093 static void testReadAsTextSyncEmptyFile() { | 994 static void testReadAsTextSyncEmptyFile() { |
| 1094 var name = getFilename("tests/vm/data/empty_file"); | 995 var name = getFilename("tests/vm/data/empty_file"); |
| 1095 var text = new File(name).readAsStringSync(); | 996 var text = new File(name).readAsStringSync(); |
| 1096 Expect.equals(0, text.length); | 997 Expect.equals(0, text.length); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1321 testDirectorySync(); | 1222 testDirectorySync(); |
| 1322 testWriteStringUtf8(); | 1223 testWriteStringUtf8(); |
| 1323 testWriteStringUtf8Sync(); | 1224 testWriteStringUtf8Sync(); |
| 1324 }); | 1225 }); |
| 1325 } | 1226 } |
| 1326 } | 1227 } |
| 1327 | 1228 |
| 1328 main() { | 1229 main() { |
| 1329 FileTest.testMain(); | 1230 FileTest.testMain(); |
| 1330 } | 1231 } |
| OLD | NEW |