Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 | 7 |
| 8 class FileTest { | 8 class FileTest { |
| 9 // Test for file read functionality. | 9 // Test for file read functionality. |
| 10 static int testReadStream() { | 10 static int testReadStream() { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 // Read a file. | 38 // Read a file. |
| 39 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 39 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 40 File file = new File(inFilename); | 40 File file = new File(inFilename); |
| 41 FileInputStream input = file.openInputStream(); | 41 FileInputStream input = file.openInputStream(); |
| 42 List<int> buffer1 = new List<int>(42); | 42 List<int> buffer1 = new List<int>(42); |
| 43 int bytesRead = input.readInto(buffer1, 0, 42); | 43 int bytesRead = input.readInto(buffer1, 0, 42); |
| 44 Expect.equals(42, bytesRead); | 44 Expect.equals(42, bytesRead); |
| 45 input.close(); | 45 input.close(); |
| 46 // Write the contents of the file just read into another file. | 46 // Write the contents of the file just read into another file. |
| 47 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); | 47 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); |
| 48 file = new File(outFilenameBase + "_out"); | 48 String outFilename = outFilenameBase + "_out_read_write_stream"; |
| 49 file = new File(outFilename); | |
| 49 OutputStream output = file.openOutputStream(); | 50 OutputStream output = file.openOutputStream(); |
| 50 bool writeDone = output.writeFrom(buffer1, 0, 42); | 51 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 51 Expect.equals(true, writeDone); | 52 Expect.equals(true, writeDone); |
| 52 output.close(); | 53 output.close(); |
| 53 // Now read the contents of the file just written. | 54 // Now read the contents of the file just written. |
| 54 List<int> buffer2 = new List<int>(42); | 55 List<int> buffer2 = new List<int>(42); |
| 55 file = new File(outFilenameBase + "_out"); | 56 file = new File(outFilename); |
| 56 input = file.openInputStream(); | 57 input = file.openInputStream(); |
| 57 bytesRead = input.readInto(buffer2, 0, 42); | 58 bytesRead = input.readInto(buffer2, 0, 42); |
| 58 input.close(); | 59 input.close(); |
| 59 Expect.equals(42, bytesRead); | 60 Expect.equals(42, bytesRead); |
| 60 // Now compare the two buffers to check if they are identical. | 61 // Now compare the two buffers to check if they are identical. |
| 61 for (int i = 0; i < buffer1.length; i++) { | 62 for (int i = 0; i < buffer1.length; i++) { |
| 62 Expect.equals(buffer1[i], buffer2[i]); | 63 Expect.equals(buffer1[i], buffer2[i]); |
| 63 } | 64 } |
| 64 // Delete the output file. | 65 // Delete the output file. |
| 65 file.deleteSync(); | 66 file.deleteSync(); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 file.errorHandler = (s) { | 135 file.errorHandler = (s) { |
| 135 Expect.fail("No errors expected"); | 136 Expect.fail("No errors expected"); |
| 136 }; | 137 }; |
| 137 file.openHandler = () { | 138 file.openHandler = () { |
| 138 List<int> buffer1 = new List<int>(42); | 139 List<int> buffer1 = new List<int>(42); |
| 139 file.readListHandler = (bytes_read) { | 140 file.readListHandler = (bytes_read) { |
| 140 Expect.equals(42, bytes_read); | 141 Expect.equals(42, bytes_read); |
| 141 file.closeHandler = () { | 142 file.closeHandler = () { |
| 142 // Write the contents of the file just read into another file. | 143 // Write the contents of the file just read into another file. |
| 143 String outFilenameBase = | 144 String outFilenameBase = |
| 144 getFilename("tests/vm/data/fixed_length_file"); | 145 getFilename("tests/vm/data/fixed_length_file"); |
| 145 file = new File(outFilenameBase + "_out"); | 146 String outFilename = outFilenameBase + "_out_read_write"; |
| 147 file = new File(outFilename); | |
| 146 file.errorHandler = (s) { | 148 file.errorHandler = (s) { |
| 147 Expect.fail("No errors expected"); | 149 Expect.fail("No errors expected"); |
| 148 }; | 150 }; |
| 149 file.createHandler = () { | 151 file.createHandler = () { |
| 150 file.fullPathHandler = (s) { | 152 file.fullPathHandler = (s) { |
| 151 Expect.isTrue(new File(s).existsSync()); | 153 Expect.isTrue(new File(s).existsSync()); |
| 152 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 154 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
| 153 Expect.fail("Not a full path"); | 155 Expect.fail("Not a full path"); |
| 154 } | 156 } |
| 155 file.openHandler = () { | 157 file.openHandler = () { |
| 156 file.noPendingWriteHandler = () { | 158 file.noPendingWriteHandler = () { |
| 157 file.closeHandler = () { | 159 file.closeHandler = () { |
| 158 // Now read the contents of the file just written. | 160 // Now read the contents of the file just written. |
| 159 List<int> buffer2 = new List<int>(bytes_read); | 161 List<int> buffer2 = new List<int>(bytes_read); |
| 160 file = new File(getFilename(outFilenameBase + "_out")); | 162 file = new File(outFilename); |
| 161 file.errorHandler = (s) { | 163 file.errorHandler = (s) { |
| 162 Expect.fail("No errors expected"); | 164 Expect.fail("No errors expected"); |
| 163 }; | 165 }; |
| 164 file.openHandler = () { | 166 file.openHandler = () { |
| 165 file.readListHandler = (bytes_read) { | 167 file.readListHandler = (bytes_read) { |
| 166 Expect.equals(42, bytes_read); | 168 Expect.equals(42, bytes_read); |
| 167 file.closeHandler = () { | 169 file.closeHandler = () { |
| 168 // Now compare the two buffers to check if they | 170 // Now compare the two buffers to check if they |
| 169 // are identical. | 171 // are identical. |
| 170 Expect.equals(buffer1.length, buffer2.length); | 172 Expect.equals(buffer1.length, buffer2.length); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 File file = new File(inFilename); | 213 File file = new File(inFilename); |
| 212 file.openSync(); | 214 file.openSync(); |
| 213 List<int> buffer1 = new List<int>(42); | 215 List<int> buffer1 = new List<int>(42); |
| 214 int bytes_read = 0; | 216 int bytes_read = 0; |
| 215 int bytes_written = 0; | 217 int bytes_written = 0; |
| 216 bytes_read = file.readListSync(buffer1, 0, 42); | 218 bytes_read = file.readListSync(buffer1, 0, 42); |
| 217 Expect.equals(42, bytes_read); | 219 Expect.equals(42, bytes_read); |
| 218 file.closeSync(); | 220 file.closeSync(); |
| 219 // Write the contents of the file just read into another file. | 221 // Write the contents of the file just read into another file. |
| 220 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); | 222 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); |
| 221 file = new File(outFilenameBase + "_out"); | 223 String outFilename = outFilenameBase + "_out_read_write_sync"; |
| 224 file = new File(outFilename); | |
| 222 file.createSync(); | 225 file.createSync(); |
| 223 String path = file.fullPathSync(); | 226 String path = file.fullPathSync(); |
| 224 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { | 227 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { |
| 225 Expect.fail("Not a full path"); | 228 Expect.fail("Not a full path"); |
| 226 } | 229 } |
| 227 Expect.isTrue(new File(path).existsSync()); | 230 Expect.isTrue(new File(path).existsSync()); |
| 228 file.openSync(true); | 231 file.openSync(true); |
| 229 file.writeListSync(buffer1, 0, bytes_read); | 232 file.writeListSync(buffer1, 0, bytes_read); |
| 230 file.closeSync(); | 233 file.closeSync(); |
| 231 // Now read the contents of the file just written. | 234 // Now read the contents of the file just written. |
| 232 List<int> buffer2 = new List<int>(bytes_read); | 235 List<int> buffer2 = new List<int>(bytes_read); |
| 233 file = new File(getFilename(outFilenameBase + "_out")); | 236 file = new File(outFilename); |
| 234 file.openSync(); | 237 file.openSync(); |
| 235 bytes_read = file.readListSync(buffer2, 0, 42); | 238 bytes_read = file.readListSync(buffer2, 0, 42); |
| 236 Expect.equals(42, bytes_read); | 239 Expect.equals(42, bytes_read); |
| 237 file.closeSync(); | 240 file.closeSync(); |
| 238 // Now compare the two buffers to check if they are identical. | 241 // Now compare the two buffers to check if they are identical. |
| 239 Expect.equals(buffer1.length, buffer2.length); | 242 Expect.equals(buffer1.length, buffer2.length); |
| 240 for (int i = 0; i < buffer1.length; i++) { | 243 for (int i = 0; i < buffer1.length; i++) { |
| 241 Expect.equals(buffer1[i], buffer2[i]); | 244 Expect.equals(buffer1[i], buffer2[i]); |
| 242 } | 245 } |
| 243 // Delete the output file. | 246 // Delete the output file. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 Expect.equals(8, input.positionSync()); | 325 Expect.equals(8, input.positionSync()); |
| 323 input.closeSync(); | 326 input.closeSync(); |
| 324 return 1; | 327 return 1; |
| 325 } | 328 } |
| 326 | 329 |
| 327 // Tests exception handling after file was closed. | 330 // Tests exception handling after file was closed. |
| 328 static int testCloseException() { | 331 static int testCloseException() { |
| 329 bool exceptionCaught = false; | 332 bool exceptionCaught = false; |
| 330 bool wrongExceptionCaught = false; | 333 bool wrongExceptionCaught = false; |
| 331 String filename = getFilename("tests/vm/data/fixed_length_file"); | 334 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 332 File input = new File(filename + "_out"); | 335 File input = new File(filename + "_out_close_exception"); |
| 333 input.openSync(true); | 336 input.openSync(true); |
| 334 input.closeSync(); | 337 input.closeSync(); |
| 335 try { | 338 try { |
| 336 input.readByteSync(); | 339 input.readByteSync(); |
| 337 } catch (FileIOException ex) { | 340 } catch (FileIOException ex) { |
| 338 exceptionCaught = true; | 341 exceptionCaught = true; |
| 339 } catch (Exception ex) { | 342 } catch (Exception ex) { |
| 340 wrongExceptionCaught = true; | 343 wrongExceptionCaught = true; |
| 341 } | 344 } |
| 342 Expect.equals(true, exceptionCaught); | 345 Expect.equals(true, exceptionCaught); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 exceptionCaught = false; | 409 exceptionCaught = false; |
| 407 try { | 410 try { |
| 408 input.flushSync(); | 411 input.flushSync(); |
| 409 } catch (FileIOException ex) { | 412 } catch (FileIOException ex) { |
| 410 exceptionCaught = true; | 413 exceptionCaught = true; |
| 411 } catch (Exception ex) { | 414 } catch (Exception ex) { |
| 412 wrongExceptionCaught = true; | 415 wrongExceptionCaught = true; |
| 413 } | 416 } |
| 414 Expect.equals(true, exceptionCaught); | 417 Expect.equals(true, exceptionCaught); |
| 415 Expect.equals(true, !wrongExceptionCaught); | 418 Expect.equals(true, !wrongExceptionCaught); |
| 419 input.deleteSync(); | |
| 416 return 1; | 420 return 1; |
| 417 } | 421 } |
| 418 | 422 |
| 419 // Tests stream exception handling after file was closed. | 423 // Tests stream exception handling after file was closed. |
| 420 static int testCloseExceptionStream() { | 424 static int testCloseExceptionStream() { |
| 421 bool exceptionCaught = false; | 425 bool exceptionCaught = false; |
| 422 bool wrongExceptionCaught = false; | 426 bool wrongExceptionCaught = false; |
| 423 String filename = getFilename("tests/vm/data/fixed_length_file"); | 427 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 424 File file = new File(filename + "_out"); | 428 File file = new File(filename + "_out_close_exception_stream"); |
|
kasperl
2011/11/18 14:11:49
String interpolation for the win?
| |
| 429 file.createSync(); | |
| 425 FileInputStream input = file.openInputStream(); | 430 FileInputStream input = file.openInputStream(); |
| 426 input.close(); | 431 input.close(); |
| 427 try { | 432 try { |
| 428 List<int> buffer = new List<int>(42); | 433 List<int> buffer = new List<int>(42); |
| 429 input.readInto(buffer, 0, 12); | 434 input.readInto(buffer, 0, 12); |
| 430 } catch (FileIOException ex) { | 435 } catch (FileIOException ex) { |
| 431 exceptionCaught = true; | 436 exceptionCaught = true; |
| 432 } catch (Exception ex) { | 437 } catch (Exception ex) { |
| 433 wrongExceptionCaught = true; | 438 wrongExceptionCaught = true; |
| 434 } | 439 } |
| 435 Expect.equals(true, exceptionCaught); | 440 Expect.equals(true, exceptionCaught); |
| 436 Expect.equals(true, !wrongExceptionCaught); | 441 Expect.equals(true, !wrongExceptionCaught); |
| 437 exceptionCaught = false; | 442 exceptionCaught = false; |
| 438 OutputStream output = file.openOutputStream(); | 443 OutputStream output = file.openOutputStream(); |
| 439 output.close(); | 444 output.close(); |
| 440 try { | 445 try { |
| 441 List<int> buffer = new List<int>(42); | 446 List<int> buffer = new List<int>(42); |
| 442 bool readDone = output.writeFrom(buffer, 0, 12); | 447 bool readDone = output.writeFrom(buffer, 0, 12); |
| 443 } catch (FileIOException ex) { | 448 } catch (FileIOException ex) { |
| 444 exceptionCaught = true; | 449 exceptionCaught = true; |
| 445 } catch (Exception ex) { | 450 } catch (Exception ex) { |
| 446 wrongExceptionCaught = true; | 451 wrongExceptionCaught = true; |
| 447 } | 452 } |
| 448 Expect.equals(true, exceptionCaught); | 453 Expect.equals(true, exceptionCaught); |
| 449 Expect.equals(true, !wrongExceptionCaught); | 454 Expect.equals(true, !wrongExceptionCaught); |
| 455 file.deleteSync(); | |
| 450 return 1; | 456 return 1; |
| 451 } | 457 } |
| 452 | 458 |
| 453 // Tests buffer out of bounds exception. | 459 // Tests buffer out of bounds exception. |
| 454 static int testBufferOutOfBoundsException() { | 460 static int testBufferOutOfBoundsException() { |
| 455 bool exceptionCaught = false; | 461 bool exceptionCaught = false; |
| 456 bool wrongExceptionCaught = false; | 462 bool wrongExceptionCaught = false; |
| 457 String filename = getFilename("tests/vm/data/fixed_length_file"); | 463 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 458 File file = new File(filename + "_out"); | 464 File file = new File(filename + "_out_buffer_out_of_bounds"); |
| 459 file.openSync(true); | 465 file.openSync(true); |
| 460 try { | 466 try { |
| 461 List<int> buffer = new List<int>(10); | 467 List<int> buffer = new List<int>(10); |
| 462 bool readDone = file.readListSync(buffer, 0, 12); | 468 bool readDone = file.readListSync(buffer, 0, 12); |
| 463 } catch (IndexOutOfRangeException ex) { | 469 } catch (IndexOutOfRangeException ex) { |
| 464 exceptionCaught = true; | 470 exceptionCaught = true; |
| 465 } catch (Exception ex) { | 471 } catch (Exception ex) { |
| 466 wrongExceptionCaught = true; | 472 wrongExceptionCaught = true; |
| 467 } | 473 } |
| 468 Expect.equals(true, exceptionCaught); | 474 Expect.equals(true, exceptionCaught); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 List<int> buffer = new List<int>(10); | 544 List<int> buffer = new List<int>(10); |
| 539 bool readDone = file.writeListSync(buffer, 0, -1); | 545 bool readDone = file.writeListSync(buffer, 0, -1); |
| 540 } catch (IndexOutOfRangeException ex) { | 546 } catch (IndexOutOfRangeException ex) { |
| 541 exceptionCaught = true; | 547 exceptionCaught = true; |
| 542 } catch (Exception ex) { | 548 } catch (Exception ex) { |
| 543 wrongExceptionCaught = true; | 549 wrongExceptionCaught = true; |
| 544 } | 550 } |
| 545 Expect.equals(true, exceptionCaught); | 551 Expect.equals(true, exceptionCaught); |
| 546 Expect.equals(true, !wrongExceptionCaught); | 552 Expect.equals(true, !wrongExceptionCaught); |
| 547 file.closeSync(); | 553 file.closeSync(); |
| 554 file.deleteSync(); | |
| 548 return 1; | 555 return 1; |
| 549 } | 556 } |
| 550 | 557 |
| 551 static int testMixedSyncAndAsync() { | 558 static int testMixedSyncAndAsync() { |
| 552 var name = getFilename("tests/vm/data/fixed_length_file"); | 559 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 553 var f = new File(name); | 560 var f = new File(name); |
| 554 f.errorHandler = (s) { | 561 f.errorHandler = (s) { |
| 555 Expect.fail("No errors expected"); | 562 Expect.fail("No errors expected"); |
| 556 }; | 563 }; |
| 557 f.existsHandler = (exists) { | 564 f.existsHandler = (exists) { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 586 Expect.equals(1, testCloseException()); | 593 Expect.equals(1, testCloseException()); |
| 587 Expect.equals(1, testCloseExceptionStream()); | 594 Expect.equals(1, testCloseExceptionStream()); |
| 588 Expect.equals(1, testBufferOutOfBoundsException()); | 595 Expect.equals(1, testBufferOutOfBoundsException()); |
| 589 Expect.equals(1, testMixedSyncAndAsync()); | 596 Expect.equals(1, testMixedSyncAndAsync()); |
| 590 } | 597 } |
| 591 } | 598 } |
| 592 | 599 |
| 593 main() { | 600 main() { |
| 594 FileTest.testMain(); | 601 FileTest.testMain(); |
| 595 } | 602 } |
| OLD | NEW |