| 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 MyListOfOneElement implements List { | 9 class MyListOfOneElement implements List { |
| 10 int _value; | 10 int _value; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 static void deleteTempDirectory() { | 37 static void deleteTempDirectory() { |
| 38 tempDirectory.deleteSync(recursive: true); | 38 tempDirectory.deleteSync(recursive: true); |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Test for file read functionality. | 41 // Test for file read functionality. |
| 42 static void testReadStream() { | 42 static void testReadStream() { |
| 43 // Read a file and check part of it's contents. | 43 // Read a file and check part of it's contents. |
| 44 String filename = getFilename("bin/file_test.cc"); | 44 String filename = getFilename("bin/file_test.cc"); |
| 45 File file = new File(filename); | 45 File file = new File(filename); |
| 46 InputStream input = file.openInputStreamSync(); | 46 InputStream input = file.openInputStreamSync(); |
| 47 List<int> buffer = new List<int>(42); | 47 input.dataHandler = () { |
| 48 int bytesRead = input.readInto(buffer, 0, 12); | 48 List<int> buffer = new List<int>(42); |
| 49 Expect.equals(12, bytesRead); | 49 int bytesRead = input.readInto(buffer, 0, 12); |
| 50 bytesRead = input.readInto(buffer, 12, 30); | 50 Expect.equals(12, bytesRead); |
| 51 input.close(); | 51 bytesRead = input.readInto(buffer, 12, 30); |
| 52 Expect.equals(30, bytesRead); | 52 input.close(); |
| 53 Expect.equals(47, buffer[0]); // represents '/' in the file. | 53 Expect.equals(30, bytesRead); |
| 54 Expect.equals(47, buffer[1]); // represents '/' in the file. | 54 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 55 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 55 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 56 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 56 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| 57 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 57 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| 58 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 58 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
| 59 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 59 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
| 60 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 60 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
| 61 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 61 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
| 62 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 62 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
| 63 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 63 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 64 Expect.equals(116, buffer[11]); // represents 't' in the file. | 64 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
| 65 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 66 }; |
| 65 } | 67 } |
| 66 | 68 |
| 67 // Test for file read and write functionality. | 69 // Test for file read and write functionality. |
| 68 static void testReadWriteStream() { | 70 static void testReadWriteStream() { |
| 71 asyncTestStarted(); |
| 72 |
| 69 // Read a file. | 73 // Read a file. |
| 70 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 74 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 71 File file; | 75 File file; |
| 72 InputStream input; | 76 InputStream input; |
| 73 int bytesRead; | 77 int bytesRead; |
| 74 | 78 |
| 75 // Test reading all using readInto. | 79 // Test reading all using readInto. |
| 76 file = new File(inFilename); | 80 file = new File(inFilename); |
| 77 input = file.openInputStreamSync(); | 81 input = file.openInputStreamSync(); |
| 78 List<int> buffer1 = new List<int>(42); | 82 input.dataHandler = () { |
| 79 bytesRead = input.readInto(buffer1, 0, 42); | 83 List<int> buffer1 = new List<int>(42); |
| 80 Expect.equals(42, bytesRead); | 84 bytesRead = input.readInto(buffer1, 0, 42); |
| 81 Expect.isTrue(input.closed); | 85 Expect.equals(42, bytesRead); |
| 86 Expect.isTrue(input.closed); |
| 82 | 87 |
| 83 // Test reading all using readInto and read. | 88 // Test reading all using readInto and read. |
| 84 file = new File(inFilename); | 89 file = new File(inFilename); |
| 85 input = file.openInputStreamSync(); | 90 input = file.openInputStreamSync(); |
| 86 bytesRead = input.readInto(buffer1, 0, 21); | 91 input.dataHandler = () { |
| 87 Expect.equals(21, bytesRead); | 92 bytesRead = input.readInto(buffer1, 0, 21); |
| 88 buffer1 = input.read(); | 93 Expect.equals(21, bytesRead); |
| 89 Expect.equals(21, buffer1.length); | 94 buffer1 = input.read(); |
| 90 Expect.isTrue(input.closed); | 95 Expect.equals(21, buffer1.length); |
| 96 Expect.isTrue(input.closed); |
| 91 | 97 |
| 92 // Test reading all using read and readInto. | 98 // Test reading all using read and readInto. |
| 93 file = new File(inFilename); | 99 file = new File(inFilename); |
| 94 input = file.openInputStreamSync(); | 100 input = file.openInputStreamSync(); |
| 95 buffer1 = input.read(21); | 101 input.dataHandler = () { |
| 96 Expect.equals(21, buffer1.length); | 102 buffer1 = input.read(21); |
| 97 bytesRead = input.readInto(buffer1, 0, 21); | 103 Expect.equals(21, buffer1.length); |
| 98 Expect.equals(21, bytesRead); | 104 bytesRead = input.readInto(buffer1, 0, 21); |
| 99 Expect.isTrue(input.closed); | 105 Expect.equals(21, bytesRead); |
| 106 Expect.isTrue(input.closed); |
| 100 | 107 |
| 101 // Test reading all using read. | 108 // Test reading all using read. |
| 102 file = new File(inFilename); | 109 file = new File(inFilename); |
| 103 input = file.openInputStreamSync(); | 110 input = file.openInputStreamSync(); |
| 104 buffer1 = input.read(); | 111 input.dataHandler = () { |
| 105 Expect.equals(42, buffer1.length); | 112 buffer1 = input.read(); |
| 106 Expect.isTrue(input.closed); | 113 Expect.equals(42, buffer1.length); |
| 114 Expect.isTrue(input.closed); |
| 107 | 115 |
| 108 // Write the contents of the file just read into another file. | 116 // Write the contents of the file just read into another file. |
| 109 String outFilename = tempDirectory.path + "/out_read_write_stream"; | 117 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 110 file = new File(outFilename); | 118 file = new File(outFilename); |
| 111 OutputStream output = file.openOutputStreamSync(); | 119 OutputStream output = file.openOutputStreamSync(); |
| 112 bool writeDone = output.writeFrom(buffer1, 0, 42); | 120 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 113 Expect.equals(true, writeDone); | 121 Expect.equals(false, writeDone); |
| 114 output.close(); | 122 output.noPendingWriteHandler = () { |
| 123 output.close(); |
| 115 | 124 |
| 116 // Now read the contents of the file just written. | 125 // Now read the contents of the file just written. |
| 117 List<int> buffer2 = new List<int>(42); | 126 List<int> buffer2 = new List<int>(42); |
| 118 file = new File(outFilename); | 127 file = new File(outFilename); |
| 119 input = file.openInputStreamSync(); | 128 input = file.openInputStreamSync(); |
| 120 bytesRead = input.readInto(buffer2, 0, 42); | 129 input.dataHandler = () { |
| 121 Expect.isTrue(input.closed); | 130 bytesRead = input.readInto(buffer2, 0, 42); |
| 122 Expect.equals(42, bytesRead); | 131 Expect.isTrue(input.closed); |
| 123 // Now compare the two buffers to check if they are identical. | 132 Expect.equals(42, bytesRead); |
| 124 for (int i = 0; i < buffer1.length; i++) { | 133 // Now compare the two buffers to check if they are identical. |
| 125 Expect.equals(buffer1[i], buffer2[i]); | 134 for (int i = 0; i < buffer1.length; i++) { |
| 126 } | 135 Expect.equals(buffer1[i], buffer2[i]); |
| 127 // Delete the output file. | 136 } |
| 128 file.deleteSync(); | 137 // Delete the output file. |
| 129 Expect.isFalse(file.existsSync()); | 138 file.deleteSync(); |
| 139 Expect.isFalse(file.existsSync()); |
| 140 asyncTestDone("testReadWriteStream"); |
| 141 }; |
| 142 }; |
| 143 }; |
| 144 }; |
| 145 }; |
| 146 }; |
| 130 } | 147 } |
| 131 | 148 |
| 132 static void testRead() { | 149 static void testRead() { |
| 133 // Read a file and check part of it's contents. | 150 // Read a file and check part of it's contents. |
| 134 String filename = getFilename("bin/file_test.cc"); | 151 String filename = getFilename("bin/file_test.cc"); |
| 135 File file = new File(filename); | 152 File file = new File(filename); |
| 136 file.errorHandler = (s) { | 153 file.errorHandler = (s) { |
| 137 Expect.fail("No errors expected : $s"); | 154 Expect.fail("No errors expected : $s"); |
| 138 }; | 155 }; |
| 139 file.openHandler = (RandomAccessFile file) { | 156 file.openHandler = (RandomAccessFile file) { |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 314 } |
| 298 | 315 |
| 299 static void testOutputStreamWriteAppend() { | 316 static void testOutputStreamWriteAppend() { |
| 300 String content = "foobar"; | 317 String content = "foobar"; |
| 301 String filename = tempDirectory.path + "/outstream_write_append"; | 318 String filename = tempDirectory.path + "/outstream_write_append"; |
| 302 File file = new File(filename); | 319 File file = new File(filename); |
| 303 file.createSync(); | 320 file.createSync(); |
| 304 List<int> buffer = content.charCodes(); | 321 List<int> buffer = content.charCodes(); |
| 305 OutputStream outStream = file.openOutputStreamSync(); | 322 OutputStream outStream = file.openOutputStreamSync(); |
| 306 outStream.write(buffer); | 323 outStream.write(buffer); |
| 307 outStream.close(); | 324 outStream.noPendingWriteHandler = () { |
| 308 File file2 = new File(filename); | 325 outStream.close(); |
| 309 OutputStream appendingOutput = file2.openOutputStreamSync(FileMode.APPEND); | 326 File file2 = new File(filename); |
| 310 appendingOutput.write(buffer); | 327 OutputStream appendingOutput = |
| 311 appendingOutput.close(); | 328 file2.openOutputStreamSync(FileMode.APPEND); |
| 312 File file3 = new File(filename); | 329 appendingOutput.write(buffer); |
| 313 file3.openHandler = (RandomAccessFile openedFile) { | 330 appendingOutput.noPendingWriteHandler = () { |
| 314 openedFile.lengthHandler = (int length) { | 331 appendingOutput.close(); |
| 315 Expect.equals(content.length * 2, length); | 332 File file3 = new File(filename); |
| 316 openedFile.closeHandler = () { | 333 file3.openHandler = (RandomAccessFile openedFile) { |
| 317 file3.deleteHandler = () { | 334 openedFile.lengthHandler = (int length) { |
| 318 asyncTestDone("testOutputStreamWriteAppend"); | 335 Expect.equals(content.length * 2, length); |
| 336 openedFile.closeHandler = () { |
| 337 file3.deleteHandler = () { |
| 338 asyncTestDone("testOutputStreamWriteAppend"); |
| 339 }; |
| 340 file3.delete(); |
| 341 }; |
| 342 openedFile.close(); |
| 319 }; | 343 }; |
| 320 file3.delete(); | 344 openedFile.length(); |
| 321 }; | 345 }; |
| 322 openedFile.close(); | 346 file3.open(); |
| 323 }; | 347 }; |
| 324 openedFile.length(); | |
| 325 }; | 348 }; |
| 326 file3.open(); | |
| 327 asyncTestStarted(); | 349 asyncTestStarted(); |
| 328 } | 350 } |
| 329 | 351 |
| 330 | 352 |
| 331 static void testReadWriteSync() { | 353 static void testReadWriteSync() { |
| 332 // Read a file. | 354 // Read a file. |
| 333 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 355 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 334 RandomAccessFile file = (new File(inFilename)).openSync(); | 356 RandomAccessFile file = (new File(inFilename)).openSync(); |
| 335 List<int> buffer1 = new List<int>(42); | 357 List<int> buffer1 = new List<int>(42); |
| 336 int bytes_read = 0; | 358 int bytes_read = 0; |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 Expect.equals(true, !wrongExceptionCaught); | 756 Expect.equals(true, !wrongExceptionCaught); |
| 735 input.deleteSync(); | 757 input.deleteSync(); |
| 736 } | 758 } |
| 737 | 759 |
| 738 // Tests stream exception handling after file was closed. | 760 // Tests stream exception handling after file was closed. |
| 739 static void testCloseExceptionStream() { | 761 static void testCloseExceptionStream() { |
| 740 List<int> buffer = new List<int>(42); | 762 List<int> buffer = new List<int>(42); |
| 741 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 763 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 742 file.createSync(); | 764 file.createSync(); |
| 743 InputStream input = file.openInputStreamSync(); | 765 InputStream input = file.openInputStreamSync(); |
| 744 Expect.isTrue(input.closed); | 766 input.closeHandler = () { |
| 745 Expect.isNull(input.readInto(buffer, 0, 12)); | 767 Expect.isTrue(input.closed); |
| 746 OutputStream output = file.openOutputStreamSync(); | 768 Expect.isNull(input.readInto(buffer, 0, 12)); |
| 747 output.close(); | 769 OutputStream output = file.openOutputStreamSync(); |
| 748 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), | 770 output.close(); |
| 749 (e) => e is FileIOException); | 771 Expect.throws(() => output.writeFrom(buffer, 0, 12)); |
| 750 file.deleteSync(); | 772 file.deleteSync(); |
| 773 }; |
| 751 } | 774 } |
| 752 | 775 |
| 753 // Tests buffer out of bounds exception. | 776 // Tests buffer out of bounds exception. |
| 754 static void testBufferOutOfBoundsException() { | 777 static void testBufferOutOfBoundsException() { |
| 755 bool exceptionCaught = false; | 778 bool exceptionCaught = false; |
| 756 bool wrongExceptionCaught = false; | 779 bool wrongExceptionCaught = false; |
| 757 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); | 780 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); |
| 758 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | 781 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
| 759 try { | 782 try { |
| 760 List<int> buffer = new List<int>(10); | 783 List<int> buffer = new List<int>(10); |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 984 var lines = new File(name).readAsLinesSync(); | 1007 var lines = new File(name).readAsLinesSync(); |
| 985 Expect.equals(1, lines.length); | 1008 Expect.equals(1, lines.length); |
| 986 var line = lines[0]; | 1009 var line = lines[0]; |
| 987 Expect.isTrue(line.endsWith("42 bytes.")); | 1010 Expect.isTrue(line.endsWith("42 bytes.")); |
| 988 Expect.equals(42, line.length); | 1011 Expect.equals(42, line.length); |
| 989 name = getDataFilename("tests/standalone/src/readline_test1.dat"); | 1012 name = getDataFilename("tests/standalone/src/readline_test1.dat"); |
| 990 lines = new File(name).readAsLinesSync(); | 1013 lines = new File(name).readAsLinesSync(); |
| 991 Expect.equals(10, lines.length); | 1014 Expect.equals(10, lines.length); |
| 992 } | 1015 } |
| 993 | 1016 |
| 1017 |
| 994 static void testReadAsErrors() { | 1018 static void testReadAsErrors() { |
| 1019 var port = new ReceivePort.singleShot(); |
| 1020 port.receive((message, _) { |
| 1021 Expect.equals(1, message); |
| 1022 }); |
| 995 var f = new File('.'); | 1023 var f = new File('.'); |
| 996 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 1024 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
| 997 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 1025 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
| 998 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 1026 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
| 999 // TODO(ager): Add tests of errors in readAsBytes when input | 1027 f.readAsBytes(); |
| 1000 // streams are truely async. | 1028 f.errorHandler = (e) { |
| 1029 f.readAsText(); |
| 1030 f.errorHandler = (e) { |
| 1031 f.readAsLines(); |
| 1032 f.errorHandler = (e) { |
| 1033 port.toSendPort().send(1); |
| 1034 }; |
| 1035 }; |
| 1036 }; |
| 1001 } | 1037 } |
| 1002 | 1038 |
| 1003 // Test that opens the same file for writing then for appending to test | 1039 // Test that opens the same file for writing then for appending to test |
| 1004 // that the file is not truncated when opened for appending. | 1040 // that the file is not truncated when opened for appending. |
| 1005 static void testAppend() { | 1041 static void testAppend() { |
| 1006 var file = new File('${tempDirectory.path}/out_append'); | 1042 var file = new File('${tempDirectory.path}/out_append'); |
| 1007 file.openHandler = (openedFile) { | 1043 file.openHandler = (openedFile) { |
| 1008 openedFile.noPendingWriteHandler = () { | 1044 openedFile.noPendingWriteHandler = () { |
| 1009 openedFile.closeHandler = () { | 1045 openedFile.closeHandler = () { |
| 1010 file.openHandler = (openedFile) { | 1046 file.openHandler = (openedFile) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 testWriteVariousLists(); | 1141 testWriteVariousLists(); |
| 1106 testDirectory(); | 1142 testDirectory(); |
| 1107 testDirectorySync(); | 1143 testDirectorySync(); |
| 1108 }); | 1144 }); |
| 1109 } | 1145 } |
| 1110 } | 1146 } |
| 1111 | 1147 |
| 1112 main() { | 1148 main() { |
| 1113 FileTest.testMain(); | 1149 FileTest.testMain(); |
| 1114 } | 1150 } |
| OLD | NEW |