| 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 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 class MyListOfOneElement implements List { | 10 class MyListOfOneElement implements List { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 static void deleteTempDirectory() { | 38 static void deleteTempDirectory() { |
| 39 tempDirectory.deleteSync(recursive: true); | 39 tempDirectory.deleteSync(recursive: true); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Test for file read functionality. | 42 // Test for file read functionality. |
| 43 static void testReadStream() { | 43 static void testReadStream() { |
| 44 // Read a file and check part of it's contents. | 44 // Read a file and check part of it's contents. |
| 45 String filename = getFilename("bin/file_test.cc"); | 45 String filename = getFilename("bin/file_test.cc"); |
| 46 File file = new File(filename); | 46 File file = new File(filename); |
| 47 InputStream input = file.openInputStreamSync(); | 47 InputStream input = file.openInputStream(); |
| 48 input.dataHandler = () { | 48 input.dataHandler = () { |
| 49 List<int> buffer = new List<int>(42); | 49 List<int> buffer = new List<int>(42); |
| 50 int bytesRead = input.readInto(buffer, 0, 12); | 50 int bytesRead = input.readInto(buffer, 0, 12); |
| 51 Expect.equals(12, bytesRead); | 51 Expect.equals(12, bytesRead); |
| 52 bytesRead = input.readInto(buffer, 12, 30); | 52 bytesRead = input.readInto(buffer, 12, 30); |
| 53 input.close(); | 53 input.close(); |
| 54 Expect.equals(30, bytesRead); | 54 Expect.equals(30, bytesRead); |
| 55 Expect.equals(47, buffer[0]); // represents '/' in the file. | 55 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 56 Expect.equals(47, buffer[1]); // represents '/' in the file. | 56 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 57 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 57 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 72 asyncTestStarted(); | 72 asyncTestStarted(); |
| 73 | 73 |
| 74 // Read a file. | 74 // Read a file. |
| 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 76 File file; | 76 File file; |
| 77 InputStream input; | 77 InputStream input; |
| 78 int bytesRead; | 78 int bytesRead; |
| 79 | 79 |
| 80 // Test reading all using readInto. | 80 // Test reading all using readInto. |
| 81 file = new File(inFilename); | 81 file = new File(inFilename); |
| 82 input = file.openInputStreamSync(); | 82 input = file.openInputStream(); |
| 83 input.dataHandler = () { | 83 input.dataHandler = () { |
| 84 List<int> buffer1 = new List<int>(42); | 84 List<int> buffer1 = new List<int>(42); |
| 85 bytesRead = input.readInto(buffer1, 0, 42); | 85 bytesRead = input.readInto(buffer1, 0, 42); |
| 86 Expect.equals(42, bytesRead); | 86 Expect.equals(42, bytesRead); |
| 87 Expect.isTrue(input.closed); | 87 Expect.isTrue(input.closed); |
| 88 | 88 |
| 89 // Test reading all using readInto and read. | 89 // Test reading all using readInto and read. |
| 90 file = new File(inFilename); | 90 file = new File(inFilename); |
| 91 input = file.openInputStreamSync(); | 91 input = file.openInputStream(); |
| 92 input.dataHandler = () { | 92 input.dataHandler = () { |
| 93 bytesRead = input.readInto(buffer1, 0, 21); | 93 bytesRead = input.readInto(buffer1, 0, 21); |
| 94 Expect.equals(21, bytesRead); | 94 Expect.equals(21, bytesRead); |
| 95 buffer1 = input.read(); | 95 buffer1 = input.read(); |
| 96 Expect.equals(21, buffer1.length); | 96 Expect.equals(21, buffer1.length); |
| 97 Expect.isTrue(input.closed); | 97 Expect.isTrue(input.closed); |
| 98 | 98 |
| 99 // Test reading all using read and readInto. | 99 // Test reading all using read and readInto. |
| 100 file = new File(inFilename); | 100 file = new File(inFilename); |
| 101 input = file.openInputStreamSync(); | 101 input = file.openInputStream(); |
| 102 input.dataHandler = () { | 102 input.dataHandler = () { |
| 103 buffer1 = input.read(21); | 103 buffer1 = input.read(21); |
| 104 Expect.equals(21, buffer1.length); | 104 Expect.equals(21, buffer1.length); |
| 105 bytesRead = input.readInto(buffer1, 0, 21); | 105 bytesRead = input.readInto(buffer1, 0, 21); |
| 106 Expect.equals(21, bytesRead); | 106 Expect.equals(21, bytesRead); |
| 107 Expect.isTrue(input.closed); | 107 Expect.isTrue(input.closed); |
| 108 | 108 |
| 109 // Test reading all using read. | 109 // Test reading all using read. |
| 110 file = new File(inFilename); | 110 file = new File(inFilename); |
| 111 input = file.openInputStreamSync(); | 111 input = file.openInputStream(); |
| 112 input.dataHandler = () { | 112 input.dataHandler = () { |
| 113 buffer1 = input.read(); | 113 buffer1 = input.read(); |
| 114 Expect.equals(42, buffer1.length); | 114 Expect.equals(42, buffer1.length); |
| 115 Expect.isTrue(input.closed); | 115 Expect.isTrue(input.closed); |
| 116 | 116 |
| 117 // Write the contents of the file just read into another file. | 117 // Write the contents of the file just read into another file. |
| 118 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 118 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 119 file = new File(outFilename); | 119 file = new File(outFilename); |
| 120 OutputStream output = file.openOutputStreamSync(); | 120 OutputStream output = file.openOutputStream(); |
| 121 bool writeDone = output.writeFrom(buffer1, 0, 42); | 121 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 122 Expect.equals(false, writeDone); | 122 Expect.equals(false, writeDone); |
| 123 output.noPendingWriteHandler = () { | 123 output.noPendingWriteHandler = () { |
| 124 output.close(); | 124 output.close(); |
| 125 | 125 |
| 126 // Now read the contents of the file just written. | 126 // Now read the contents of the file just written. |
| 127 List<int> buffer2 = new List<int>(42); | 127 List<int> buffer2 = new List<int>(42); |
| 128 file = new File(outFilename); | 128 file = new File(outFilename); |
| 129 input = file.openInputStreamSync(); | 129 input = file.openInputStream(); |
| 130 input.dataHandler = () { | 130 input.dataHandler = () { |
| 131 bytesRead = input.readInto(buffer2, 0, 42); | 131 bytesRead = input.readInto(buffer2, 0, 42); |
| 132 Expect.isTrue(input.closed); | 132 Expect.isTrue(input.closed); |
| 133 Expect.equals(42, bytesRead); | 133 Expect.equals(42, bytesRead); |
| 134 // Now compare the two buffers to check if they are identical. | 134 // Now compare the two buffers to check if they are identical. |
| 135 for (int i = 0; i < buffer1.length; i++) { | 135 for (int i = 0; i < buffer1.length; i++) { |
| 136 Expect.equals(buffer1[i], buffer2[i]); | 136 Expect.equals(buffer1[i], buffer2[i]); |
| 137 } | 137 } |
| 138 // Delete the output file. | 138 // Delete the output file. |
| 139 file.deleteSync(); | 139 file.deleteSync(); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 openedFile.closeSync(); | 313 openedFile.closeSync(); |
| 314 file.deleteSync(); | 314 file.deleteSync(); |
| 315 } | 315 } |
| 316 | 316 |
| 317 static void testOutputStreamWriteAppend() { | 317 static void testOutputStreamWriteAppend() { |
| 318 String content = "foobar"; | 318 String content = "foobar"; |
| 319 String filename = tempDirectory.path + "/outstream_write_append"; | 319 String filename = tempDirectory.path + "/outstream_write_append"; |
| 320 File file = new File(filename); | 320 File file = new File(filename); |
| 321 file.createSync(); | 321 file.createSync(); |
| 322 List<int> buffer = content.charCodes(); | 322 List<int> buffer = content.charCodes(); |
| 323 OutputStream outStream = file.openOutputStreamSync(); | 323 OutputStream outStream = file.openOutputStream(); |
| 324 outStream.write(buffer); | 324 outStream.write(buffer); |
| 325 outStream.noPendingWriteHandler = () { | 325 outStream.noPendingWriteHandler = () { |
| 326 outStream.close(); | 326 outStream.close(); |
| 327 File file2 = new File(filename); | 327 File file2 = new File(filename); |
| 328 OutputStream appendingOutput = | 328 OutputStream appendingOutput = |
| 329 file2.openOutputStreamSync(FileMode.APPEND); | 329 file2.openOutputStream(FileMode.APPEND); |
| 330 appendingOutput.write(buffer); | 330 appendingOutput.write(buffer); |
| 331 appendingOutput.noPendingWriteHandler = () { | 331 appendingOutput.noPendingWriteHandler = () { |
| 332 appendingOutput.close(); | 332 appendingOutput.close(); |
| 333 File file3 = new File(filename); | 333 File file3 = new File(filename); |
| 334 file3.openHandler = (RandomAccessFile openedFile) { | 334 file3.openHandler = (RandomAccessFile openedFile) { |
| 335 openedFile.lengthHandler = (int length) { | 335 openedFile.lengthHandler = (int length) { |
| 336 Expect.equals(content.length * 2, length); | 336 Expect.equals(content.length * 2, length); |
| 337 openedFile.closeHandler = () { | 337 openedFile.closeHandler = () { |
| 338 file3.deleteHandler = () { | 338 file3.deleteHandler = () { |
| 339 asyncTestDone("testOutputStreamWriteAppend"); | 339 asyncTestDone("testOutputStreamWriteAppend"); |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 Expect.equals(true, !wrongExceptionCaught); | 757 Expect.equals(true, !wrongExceptionCaught); |
| 758 input.deleteSync(); | 758 input.deleteSync(); |
| 759 } | 759 } |
| 760 | 760 |
| 761 // Tests stream exception handling after file was closed. | 761 // Tests stream exception handling after file was closed. |
| 762 static void testCloseExceptionStream() { | 762 static void testCloseExceptionStream() { |
| 763 asyncTestStarted(); | 763 asyncTestStarted(); |
| 764 List<int> buffer = new List<int>(42); | 764 List<int> buffer = new List<int>(42); |
| 765 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 765 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 766 file.createSync(); | 766 file.createSync(); |
| 767 InputStream input = file.openInputStreamSync(); | 767 InputStream input = file.openInputStream(); |
| 768 input.closeHandler = () { | 768 input.closeHandler = () { |
| 769 Expect.isTrue(input.closed); | 769 Expect.isTrue(input.closed); |
| 770 Expect.isNull(input.readInto(buffer, 0, 12)); | 770 Expect.isNull(input.readInto(buffer, 0, 12)); |
| 771 OutputStream output = file.openOutputStreamSync(); | 771 OutputStream output = file.openOutputStream(); |
| 772 output.close(); | 772 output.close(); |
| 773 Expect.throws(() => output.writeFrom(buffer, 0, 12)); | 773 Expect.throws(() => output.writeFrom(buffer, 0, 12)); |
| 774 output.closeHandler = () { | 774 output.closeHandler = () { |
| 775 file.deleteSync(); | 775 file.deleteSync(); |
| 776 asyncTestDone("testCloseExceptionStream"); | 776 asyncTestDone("testCloseExceptionStream"); |
| 777 }; | 777 }; |
| 778 }; | 778 }; |
| 779 } | 779 } |
| 780 | 780 |
| 781 // Tests buffer out of bounds exception. | 781 // Tests buffer out of bounds exception. |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1146 testWriteVariousLists(); | 1146 testWriteVariousLists(); |
| 1147 testDirectory(); | 1147 testDirectory(); |
| 1148 testDirectorySync(); | 1148 testDirectorySync(); |
| 1149 }); | 1149 }); |
| 1150 } | 1150 } |
| 1151 } | 1151 } |
| 1152 | 1152 |
| 1153 main() { | 1153 main() { |
| 1154 FileTest.testMain(); | 1154 FileTest.testMain(); |
| 1155 } | 1155 } |
| OLD | NEW |