| 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 static Directory tempDirectory; | 9 static Directory tempDirectory; |
| 10 static int numLiveAsyncTests = 0; | 10 static int numLiveAsyncTests = 0; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 49 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
| 50 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 50 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
| 51 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 51 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 52 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 52 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
| 53 Expect.equals(116, buffer[11]); // represents 't' in the file. | 53 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 54 return 1; | 54 return 1; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Test for file read and write functionality. | 57 // Test for file read and write functionality. |
| 58 static int testReadWriteStream() { | 58 static int testReadWriteStream() { |
| 59 // Read a file. | |
| 60 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 59 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 61 File file = new File(inFilename); | 60 File file; |
| 62 InputStream input = file.openInputStream(); | 61 InputStream input; |
| 62 int bytesRead; |
| 63 |
| 64 // Test reading all using readInto. |
| 65 file = new File(inFilename); |
| 66 input = file.openInputStream(); |
| 63 List<int> buffer1 = new List<int>(42); | 67 List<int> buffer1 = new List<int>(42); |
| 64 int bytesRead = input.readInto(buffer1, 0, 42); | 68 bytesRead = input.readInto(buffer1, 0, 42); |
| 65 Expect.equals(42, bytesRead); | 69 Expect.equals(42, bytesRead); |
| 66 Expect.isTrue(input.closed); | 70 Expect.isTrue(input.closed); |
| 71 |
| 72 // Test reading all using readInto and read. |
| 73 file = new File(inFilename); |
| 74 input = file.openInputStream(); |
| 75 bytesRead = input.readInto(buffer1, 0, 21); |
| 76 Expect.equals(21, bytesRead); |
| 77 buffer1 = input.read(); |
| 78 Expect.equals(21, buffer1.length); |
| 79 Expect.isTrue(input.closed); |
| 80 |
| 81 // Test reading all using read and readInto. |
| 82 file = new File(inFilename); |
| 83 input = file.openInputStream(); |
| 84 buffer1 = input.read(21); |
| 85 Expect.equals(21, buffer1.length); |
| 86 bytesRead = input.readInto(buffer1, 0, 21); |
| 87 Expect.equals(21, bytesRead); |
| 88 Expect.isTrue(input.closed); |
| 89 |
| 90 // Test reading all using read. |
| 91 file = new File(inFilename); |
| 92 input = file.openInputStream(); |
| 93 buffer1 = input.read(); |
| 94 Expect.equals(42, buffer1.length); |
| 95 Expect.isTrue(input.closed); |
| 96 |
| 67 // Write the contents of the file just read into another file. | 97 // Write the contents of the file just read into another file. |
| 68 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 98 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 69 file = new File(outFilename); | 99 file = new File(outFilename); |
| 70 OutputStream output = file.openOutputStream(); | 100 OutputStream output = file.openOutputStream(); |
| 71 bool writeDone = output.writeFrom(buffer1, 0, 42); | 101 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 72 Expect.equals(true, writeDone); | 102 Expect.equals(true, writeDone); |
| 73 output.close(); | 103 output.close(); |
| 104 |
| 74 // Now read the contents of the file just written. | 105 // Now read the contents of the file just written. |
| 75 List<int> buffer2 = new List<int>(42); | 106 List<int> buffer2 = new List<int>(42); |
| 76 file = new File(outFilename); | 107 file = new File(outFilename); |
| 77 input = file.openInputStream(); | 108 input = file.openInputStream(); |
| 78 bytesRead = input.readInto(buffer2, 0, 42); | 109 bytesRead = input.readInto(buffer2, 0, 42); |
| 79 Expect.isTrue(input.closed); | 110 Expect.isTrue(input.closed); |
| 80 Expect.equals(42, bytesRead); | 111 Expect.equals(42, bytesRead); |
| 81 // Now compare the two buffers to check if they are identical. | 112 // Now compare the two buffers to check if they are identical. |
| 82 for (int i = 0; i < buffer1.length; i++) { | 113 for (int i = 0; i < buffer1.length; i++) { |
| 83 Expect.equals(buffer1[i], buffer2[i]); | 114 Expect.equals(buffer1[i], buffer2[i]); |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 return 1; | 514 return 1; |
| 484 } | 515 } |
| 485 | 516 |
| 486 // Tests stream exception handling after file was closed. | 517 // Tests stream exception handling after file was closed. |
| 487 static int testCloseExceptionStream() { | 518 static int testCloseExceptionStream() { |
| 488 List<int> buffer = new List<int>(42); | 519 List<int> buffer = new List<int>(42); |
| 489 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 520 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 490 file.createSync(); | 521 file.createSync(); |
| 491 InputStream input = file.openInputStream(); | 522 InputStream input = file.openInputStream(); |
| 492 Expect.isTrue(input.closed); | 523 Expect.isTrue(input.closed); |
| 493 Expect.throws(( ) => input.readInto(buffer, 0, 12), | 524 Expect.isNull(input.readInto(buffer, 0, 12)); |
| 494 (e) => e is FileIOException); | |
| 495 OutputStream output = file.openOutputStream(); | 525 OutputStream output = file.openOutputStream(); |
| 496 output.close(); | 526 output.close(); |
| 497 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), | 527 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), |
| 498 (e) => e is FileIOException); | 528 (e) => e is FileIOException); |
| 499 file.deleteSync(); | 529 file.deleteSync(); |
| 500 return 1; | 530 return 1; |
| 501 } | 531 } |
| 502 | 532 |
| 503 // Tests buffer out of bounds exception. | 533 // Tests buffer out of bounds exception. |
| 504 static int testBufferOutOfBoundsException() { | 534 static int testBufferOutOfBoundsException() { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 Expect.equals(1, testCloseExceptionStream()); | 672 Expect.equals(1, testCloseExceptionStream()); |
| 643 Expect.equals(1, testBufferOutOfBoundsException()); | 673 Expect.equals(1, testBufferOutOfBoundsException()); |
| 644 asyncTestDone(); | 674 asyncTestDone(); |
| 645 }); | 675 }); |
| 646 } | 676 } |
| 647 } | 677 } |
| 648 | 678 |
| 649 main() { | 679 main() { |
| 650 FileTest.testMain(); | 680 FileTest.testMain(); |
| 651 } | 681 } |
| OLD | NEW |