| 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 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 static void deleteTempDirectory() { | 26 static void deleteTempDirectory() { |
| 27 tempDirectory.deleteSync(); | 27 tempDirectory.deleteSync(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 // Test for file read functionality. | 30 // Test for file read functionality. |
| 31 static int testReadStream() { | 31 static int testReadStream() { |
| 32 // Read a file and check part of it's contents. | 32 // Read a file and check part of it's contents. |
| 33 String filename = getFilename("bin/file_test.cc"); | 33 String filename = getFilename("bin/file_test.cc"); |
| 34 File file = new File(filename); | 34 File file = new File(filename); |
| 35 FileInputStream input = file.openInputStream(); | 35 InputStream input = file.openInputStream(); |
| 36 List<int> buffer = new List<int>(42); | 36 List<int> buffer = new List<int>(42); |
| 37 int bytesRead = input.readInto(buffer, 0, 12); | 37 int bytesRead = input.readInto(buffer, 0, 12); |
| 38 Expect.equals(12, bytesRead); | 38 Expect.equals(12, bytesRead); |
| 39 bytesRead = input.readInto(buffer, 12, 30); | 39 bytesRead = input.readInto(buffer, 12, 30); |
| 40 input.close(); | 40 input.close(); |
| 41 Expect.equals(30, bytesRead); | 41 Expect.equals(30, bytesRead); |
| 42 Expect.equals(47, buffer[0]); // represents '/' in the file. | 42 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 43 Expect.equals(47, buffer[1]); // represents '/' in the file. | 43 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 44 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 44 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| 45 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 45 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| 46 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 46 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
| 47 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 47 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
| 48 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 48 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
| 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. | 59 // Read a file. |
| 60 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 60 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 61 File file = new File(inFilename); | 61 File file = new File(inFilename); |
| 62 FileInputStream input = file.openInputStream(); | 62 InputStream input = file.openInputStream(); |
| 63 List<int> buffer1 = new List<int>(42); | 63 List<int> buffer1 = new List<int>(42); |
| 64 int bytesRead = input.readInto(buffer1, 0, 42); | 64 int bytesRead = input.readInto(buffer1, 0, 42); |
| 65 Expect.equals(42, bytesRead); | 65 Expect.equals(42, bytesRead); |
| 66 input.close(); | 66 Expect.isTrue(input.closed); |
| 67 // Write the contents of the file just read into another file. | 67 // Write the contents of the file just read into another file. |
| 68 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 68 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 69 file = new File(outFilename); | 69 file = new File(outFilename); |
| 70 OutputStream output = file.openOutputStream(); | 70 OutputStream output = file.openOutputStream(); |
| 71 bool writeDone = output.writeFrom(buffer1, 0, 42); | 71 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 72 Expect.equals(true, writeDone); | 72 Expect.equals(true, writeDone); |
| 73 output.close(); | 73 output.close(); |
| 74 // Now read the contents of the file just written. | 74 // Now read the contents of the file just written. |
| 75 List<int> buffer2 = new List<int>(42); | 75 List<int> buffer2 = new List<int>(42); |
| 76 file = new File(outFilename); | 76 file = new File(outFilename); |
| 77 input = file.openInputStream(); | 77 input = file.openInputStream(); |
| 78 bytesRead = input.readInto(buffer2, 0, 42); | 78 bytesRead = input.readInto(buffer2, 0, 42); |
| 79 input.close(); | 79 Expect.isTrue(input.closed); |
| 80 Expect.equals(42, bytesRead); | 80 Expect.equals(42, bytesRead); |
| 81 // Now compare the two buffers to check if they are identical. | 81 // Now compare the two buffers to check if they are identical. |
| 82 for (int i = 0; i < buffer1.length; i++) { | 82 for (int i = 0; i < buffer1.length; i++) { |
| 83 Expect.equals(buffer1[i], buffer2[i]); | 83 Expect.equals(buffer1[i], buffer2[i]); |
| 84 } | 84 } |
| 85 // Delete the output file. | 85 // Delete the output file. |
| 86 file.deleteSync(); | 86 file.deleteSync(); |
| 87 Expect.isFalse(file.existsSync()); | 87 Expect.isFalse(file.existsSync()); |
| 88 return 1; | 88 return 1; |
| 89 } | 89 } |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 wrongExceptionCaught = true; | 478 wrongExceptionCaught = true; |
| 479 } | 479 } |
| 480 Expect.equals(true, exceptionCaught); | 480 Expect.equals(true, exceptionCaught); |
| 481 Expect.equals(true, !wrongExceptionCaught); | 481 Expect.equals(true, !wrongExceptionCaught); |
| 482 input.deleteSync(); | 482 input.deleteSync(); |
| 483 return 1; | 483 return 1; |
| 484 } | 484 } |
| 485 | 485 |
| 486 // Tests stream exception handling after file was closed. | 486 // Tests stream exception handling after file was closed. |
| 487 static int testCloseExceptionStream() { | 487 static int testCloseExceptionStream() { |
| 488 bool exceptionCaught = false; | 488 List<int> buffer = new List<int>(42); |
| 489 bool wrongExceptionCaught = false; | |
| 490 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 489 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 491 file.createSync(); | 490 file.createSync(); |
| 492 FileInputStream input = file.openInputStream(); | 491 InputStream input = file.openInputStream(); |
| 493 input.close(); | 492 Expect.isTrue(input.closed); |
| 494 try { | 493 Expect.throws(( ) => input.readInto(buffer, 0, 12), |
| 495 List<int> buffer = new List<int>(42); | 494 (e) => e is FileIOException); |
| 496 input.readInto(buffer, 0, 12); | |
| 497 } catch (FileIOException ex) { | |
| 498 exceptionCaught = true; | |
| 499 } catch (Exception ex) { | |
| 500 wrongExceptionCaught = true; | |
| 501 } | |
| 502 Expect.equals(true, exceptionCaught); | |
| 503 Expect.equals(true, !wrongExceptionCaught); | |
| 504 exceptionCaught = false; | |
| 505 OutputStream output = file.openOutputStream(); | 495 OutputStream output = file.openOutputStream(); |
| 506 output.close(); | 496 output.close(); |
| 507 try { | 497 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), |
| 508 List<int> buffer = new List<int>(42); | 498 (e) => e is FileIOException); |
| 509 bool readDone = output.writeFrom(buffer, 0, 12); | |
| 510 } catch (FileIOException ex) { | |
| 511 exceptionCaught = true; | |
| 512 } catch (Exception ex) { | |
| 513 wrongExceptionCaught = true; | |
| 514 } | |
| 515 Expect.equals(true, exceptionCaught); | |
| 516 Expect.equals(true, !wrongExceptionCaught); | |
| 517 file.deleteSync(); | 499 file.deleteSync(); |
| 518 return 1; | 500 return 1; |
| 519 } | 501 } |
| 520 | 502 |
| 521 // Tests buffer out of bounds exception. | 503 // Tests buffer out of bounds exception. |
| 522 static int testBufferOutOfBoundsException() { | 504 static int testBufferOutOfBoundsException() { |
| 523 bool exceptionCaught = false; | 505 bool exceptionCaught = false; |
| 524 bool wrongExceptionCaught = false; | 506 bool wrongExceptionCaught = false; |
| 525 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); | 507 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); |
| 526 RandomAccessFile openedFile = file.openSync(true); | 508 RandomAccessFile openedFile = file.openSync(true); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 Expect.equals(1, testCloseExceptionStream()); | 642 Expect.equals(1, testCloseExceptionStream()); |
| 661 Expect.equals(1, testBufferOutOfBoundsException()); | 643 Expect.equals(1, testBufferOutOfBoundsException()); |
| 662 asyncTestDone(); | 644 asyncTestDone(); |
| 663 }); | 645 }); |
| 664 } | 646 } |
| 665 } | 647 } |
| 666 | 648 |
| 667 main() { | 649 main() { |
| 668 FileTest.testMain(); | 650 FileTest.testMain(); |
| 669 } | 651 } |
| OLD | NEW |