| 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:io"); | 7 #import("dart:io"); |
| 8 | 8 |
| 9 class FileTest { | 9 class FileTest { |
| 10 static Directory tempDirectory; | 10 static Directory tempDirectory; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 static void deleteTempDirectory() { | 27 static void deleteTempDirectory() { |
| 28 tempDirectory.deleteSync(); | 28 tempDirectory.deleteSync(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Test for file read functionality. | 31 // Test for file read functionality. |
| 32 static void testReadStream() { | 32 static void testReadStream() { |
| 33 // Read a file and check part of it's contents. | 33 // Read a file and check part of it's contents. |
| 34 String filename = getFilename("bin/file_test.cc"); | 34 String filename = getFilename("bin/file_test.cc"); |
| 35 File file = new File(filename); | 35 File file = new File(filename); |
| 36 InputStream input = file.openInputStream(); | 36 InputStream input = file.openInputStreamSync(); |
| 37 List<int> buffer = new List<int>(42); | 37 List<int> buffer = new List<int>(42); |
| 38 int bytesRead = input.readInto(buffer, 0, 12); | 38 int bytesRead = input.readInto(buffer, 0, 12); |
| 39 Expect.equals(12, bytesRead); | 39 Expect.equals(12, bytesRead); |
| 40 bytesRead = input.readInto(buffer, 12, 30); | 40 bytesRead = input.readInto(buffer, 12, 30); |
| 41 input.close(); | 41 input.close(); |
| 42 Expect.equals(30, bytesRead); | 42 Expect.equals(30, bytesRead); |
| 43 Expect.equals(47, buffer[0]); // represents '/' in the file. | 43 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 44 Expect.equals(47, buffer[1]); // represents '/' in the file. | 44 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 45 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 45 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| 46 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 46 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 57 // Test for file read and write functionality. | 57 // Test for file read and write functionality. |
| 58 static void testReadWriteStream() { | 58 static void 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; | 61 File file; |
| 62 InputStream input; | 62 InputStream input; |
| 63 int bytesRead; | 63 int bytesRead; |
| 64 | 64 |
| 65 // Test reading all using readInto. | 65 // Test reading all using readInto. |
| 66 file = new File(inFilename); | 66 file = new File(inFilename); |
| 67 input = file.openInputStream(); | 67 input = file.openInputStreamSync(); |
| 68 List<int> buffer1 = new List<int>(42); | 68 List<int> buffer1 = new List<int>(42); |
| 69 bytesRead = input.readInto(buffer1, 0, 42); | 69 bytesRead = input.readInto(buffer1, 0, 42); |
| 70 Expect.equals(42, bytesRead); | 70 Expect.equals(42, bytesRead); |
| 71 Expect.isTrue(input.closed); | 71 Expect.isTrue(input.closed); |
| 72 | 72 |
| 73 // Test reading all using readInto and read. | 73 // Test reading all using readInto and read. |
| 74 file = new File(inFilename); | 74 file = new File(inFilename); |
| 75 input = file.openInputStream(); | 75 input = file.openInputStreamSync(); |
| 76 bytesRead = input.readInto(buffer1, 0, 21); | 76 bytesRead = input.readInto(buffer1, 0, 21); |
| 77 Expect.equals(21, bytesRead); | 77 Expect.equals(21, bytesRead); |
| 78 buffer1 = input.read(); | 78 buffer1 = input.read(); |
| 79 Expect.equals(21, buffer1.length); | 79 Expect.equals(21, buffer1.length); |
| 80 Expect.isTrue(input.closed); | 80 Expect.isTrue(input.closed); |
| 81 | 81 |
| 82 // Test reading all using read and readInto. | 82 // Test reading all using read and readInto. |
| 83 file = new File(inFilename); | 83 file = new File(inFilename); |
| 84 input = file.openInputStream(); | 84 input = file.openInputStreamSync(); |
| 85 buffer1 = input.read(21); | 85 buffer1 = input.read(21); |
| 86 Expect.equals(21, buffer1.length); | 86 Expect.equals(21, buffer1.length); |
| 87 bytesRead = input.readInto(buffer1, 0, 21); | 87 bytesRead = input.readInto(buffer1, 0, 21); |
| 88 Expect.equals(21, bytesRead); | 88 Expect.equals(21, bytesRead); |
| 89 Expect.isTrue(input.closed); | 89 Expect.isTrue(input.closed); |
| 90 | 90 |
| 91 // Test reading all using read. | 91 // Test reading all using read. |
| 92 file = new File(inFilename); | 92 file = new File(inFilename); |
| 93 input = file.openInputStream(); | 93 input = file.openInputStreamSync(); |
| 94 buffer1 = input.read(); | 94 buffer1 = input.read(); |
| 95 Expect.equals(42, buffer1.length); | 95 Expect.equals(42, buffer1.length); |
| 96 Expect.isTrue(input.closed); | 96 Expect.isTrue(input.closed); |
| 97 | 97 |
| 98 // Write the contents of the file just read into another file. | 98 // Write the contents of the file just read into another file. |
| 99 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 99 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 100 file = new File(outFilename); | 100 file = new File(outFilename); |
| 101 OutputStream output = file.openOutputStream(); | 101 OutputStream output = file.openOutputStreamSync(); |
| 102 bool writeDone = output.writeFrom(buffer1, 0, 42); | 102 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 103 Expect.equals(true, writeDone); | 103 Expect.equals(true, writeDone); |
| 104 output.close(); | 104 output.close(); |
| 105 | 105 |
| 106 // Now read the contents of the file just written. | 106 // Now read the contents of the file just written. |
| 107 List<int> buffer2 = new List<int>(42); | 107 List<int> buffer2 = new List<int>(42); |
| 108 file = new File(outFilename); | 108 file = new File(outFilename); |
| 109 input = file.openInputStream(); | 109 input = file.openInputStreamSync(); |
| 110 bytesRead = input.readInto(buffer2, 0, 42); | 110 bytesRead = input.readInto(buffer2, 0, 42); |
| 111 Expect.isTrue(input.closed); | 111 Expect.isTrue(input.closed); |
| 112 Expect.equals(42, bytesRead); | 112 Expect.equals(42, bytesRead); |
| 113 // Now compare the two buffers to check if they are identical. | 113 // Now compare the two buffers to check if they are identical. |
| 114 for (int i = 0; i < buffer1.length; i++) { | 114 for (int i = 0; i < buffer1.length; i++) { |
| 115 Expect.equals(buffer1[i], buffer2[i]); | 115 Expect.equals(buffer1[i], buffer2[i]); |
| 116 } | 116 } |
| 117 // Delete the output file. | 117 // Delete the output file. |
| 118 file.deleteSync(); | 118 file.deleteSync(); |
| 119 Expect.isFalse(file.existsSync()); | 119 Expect.isFalse(file.existsSync()); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 openedFile.closeSync(); | 285 openedFile.closeSync(); |
| 286 file.deleteSync(); | 286 file.deleteSync(); |
| 287 } | 287 } |
| 288 | 288 |
| 289 static void testOutputStreamWriteAppend() { | 289 static void testOutputStreamWriteAppend() { |
| 290 String content = "foobar"; | 290 String content = "foobar"; |
| 291 String filename = tempDirectory.path + "/outstream_write_append"; | 291 String filename = tempDirectory.path + "/outstream_write_append"; |
| 292 File file = new File(filename); | 292 File file = new File(filename); |
| 293 file.createSync(); | 293 file.createSync(); |
| 294 List<int> buffer = content.charCodes(); | 294 List<int> buffer = content.charCodes(); |
| 295 OutputStream outStream = file.openOutputStream(); | 295 OutputStream outStream = file.openOutputStreamSync(); |
| 296 outStream.write(buffer); | 296 outStream.write(buffer); |
| 297 outStream.close(); | 297 outStream.close(); |
| 298 File file2 = new File(filename); | 298 File file2 = new File(filename); |
| 299 OutputStream appendingOutput = file2.openOutputStream(FileMode.APPEND); | 299 OutputStream appendingOutput = file2.openOutputStreamSync(FileMode.APPEND); |
| 300 appendingOutput.write(buffer); | 300 appendingOutput.write(buffer); |
| 301 appendingOutput.close(); | 301 appendingOutput.close(); |
| 302 File file3 = new File(filename); | 302 File file3 = new File(filename); |
| 303 file3.openHandler = (RandomAccessFile openedFile) { | 303 file3.openHandler = (RandomAccessFile openedFile) { |
| 304 openedFile.lengthHandler = (int length) { | 304 openedFile.lengthHandler = (int length) { |
| 305 Expect.equals(content.length * 2, length); | 305 Expect.equals(content.length * 2, length); |
| 306 openedFile.closeHandler = () { | 306 openedFile.closeHandler = () { |
| 307 file3.deleteHandler = () { | 307 file3.deleteHandler = () { |
| 308 asyncTestDone("testOutputStreamWriteAppend"); | 308 asyncTestDone("testOutputStreamWriteAppend"); |
| 309 }; | 309 }; |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 Expect.equals(true, exceptionCaught); | 604 Expect.equals(true, exceptionCaught); |
| 605 Expect.equals(true, !wrongExceptionCaught); | 605 Expect.equals(true, !wrongExceptionCaught); |
| 606 input.deleteSync(); | 606 input.deleteSync(); |
| 607 } | 607 } |
| 608 | 608 |
| 609 // Tests stream exception handling after file was closed. | 609 // Tests stream exception handling after file was closed. |
| 610 static void testCloseExceptionStream() { | 610 static void testCloseExceptionStream() { |
| 611 List<int> buffer = new List<int>(42); | 611 List<int> buffer = new List<int>(42); |
| 612 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 612 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 613 file.createSync(); | 613 file.createSync(); |
| 614 InputStream input = file.openInputStream(); | 614 InputStream input = file.openInputStreamSync(); |
| 615 Expect.isTrue(input.closed); | 615 Expect.isTrue(input.closed); |
| 616 Expect.isNull(input.readInto(buffer, 0, 12)); | 616 Expect.isNull(input.readInto(buffer, 0, 12)); |
| 617 OutputStream output = file.openOutputStream(); | 617 OutputStream output = file.openOutputStreamSync(); |
| 618 output.close(); | 618 output.close(); |
| 619 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), | 619 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), |
| 620 (e) => e is FileIOException); | 620 (e) => e is FileIOException); |
| 621 file.deleteSync(); | 621 file.deleteSync(); |
| 622 } | 622 } |
| 623 | 623 |
| 624 // Tests buffer out of bounds exception. | 624 // Tests buffer out of bounds exception. |
| 625 static void testBufferOutOfBoundsException() { | 625 static void testBufferOutOfBoundsException() { |
| 626 bool exceptionCaught = false; | 626 bool exceptionCaught = false; |
| 627 bool wrongExceptionCaught = false; | 627 bool wrongExceptionCaught = false; |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 843 testAppendSync(); | 843 testAppendSync(); |
| 844 testWriteAppend(); | 844 testWriteAppend(); |
| 845 testOutputStreamWriteAppend(); | 845 testOutputStreamWriteAppend(); |
| 846 }); | 846 }); |
| 847 } | 847 } |
| 848 } | 848 } |
| 849 | 849 |
| 850 main() { | 850 main() { |
| 851 FileTest.testMain(); | 851 FileTest.testMain(); |
| 852 } | 852 } |
| OLD | NEW |