| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 }; | 145 }; |
| 146 }; | 146 }; |
| 147 }; | 147 }; |
| 148 }; | 148 }; |
| 149 } | 149 } |
| 150 | 150 |
| 151 static void testRead() { | 151 static void testRead() { |
| 152 // Read a file and check part of it's contents. | 152 // Read a file and check part of it's contents. |
| 153 String filename = getFilename("bin/file_test.cc"); | 153 String filename = getFilename("bin/file_test.cc"); |
| 154 File file = new File(filename); | 154 File file = new File(filename); |
| 155 file.onError = (s) { | 155 file.onError = (e) { |
| 156 Expect.fail("No errors expected : $s"); | 156 Expect.fail("No errors expected : $e"); |
| 157 }; | 157 }; |
| 158 file.open(FileMode.READ, (RandomAccessFile file) { | 158 file.open(FileMode.READ, (RandomAccessFile file) { |
| 159 List<int> buffer = new List<int>(10); | 159 List<int> buffer = new List<int>(10); |
| 160 file.readList(buffer, 0, 5, (bytes_read) { | 160 file.readList(buffer, 0, 5, (bytes_read) { |
| 161 Expect.equals(5, bytes_read); | 161 Expect.equals(5, bytes_read); |
| 162 file.readList(buffer, 5, 5, (bytes_read) { | 162 file.readList(buffer, 5, 5, (bytes_read) { |
| 163 Expect.equals(5, bytes_read); | 163 Expect.equals(5, bytes_read); |
| 164 Expect.equals(47, buffer[0]); // represents '/' in the file. | 164 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 165 Expect.equals(47, buffer[1]); // represents '/' in the file. | 165 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 166 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 166 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 199 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 200 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 200 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
| 201 Expect.equals(116, buffer[11]); // represents 't' in the file. | 201 Expect.equals(116, buffer[11]); // represents 't' in the file. |
| 202 } | 202 } |
| 203 | 203 |
| 204 // Test for file read and write functionality. | 204 // Test for file read and write functionality. |
| 205 static void testReadWrite() { | 205 static void testReadWrite() { |
| 206 // Read a file. | 206 // Read a file. |
| 207 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 207 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 208 final File file = new File(inFilename); | 208 final File file = new File(inFilename); |
| 209 file.onError = (s) { | 209 file.onError = (e) { |
| 210 Expect.fail("No errors expected : $s"); | 210 Expect.fail("No errors expected : $e"); |
| 211 }; | 211 }; |
| 212 file.open(FileMode.READ, (RandomAccessFile openedFile) { | 212 file.open(FileMode.READ, (RandomAccessFile openedFile) { |
| 213 openedFile.onError = (s) { | 213 openedFile.onError = (s) { |
| 214 Expect.fail("No errors expected : $s"); | 214 Expect.fail("No errors expected : $s"); |
| 215 }; | 215 }; |
| 216 List<int> buffer1 = new List<int>(42); | 216 List<int> buffer1 = new List<int>(42); |
| 217 openedFile.readList(buffer1, 0, 42, (bytes_read) { | 217 openedFile.readList(buffer1, 0, 42, (bytes_read) { |
| 218 Expect.equals(42, bytes_read); | 218 Expect.equals(42, bytes_read); |
| 219 openedFile.close(() { | 219 openedFile.close(() { |
| 220 // Write the contents of the file just read into another file. | 220 // Write the contents of the file just read into another file. |
| 221 String outFilename = tempDirectory.path + "/out_read_write"; | 221 String outFilename = tempDirectory.path + "/out_read_write"; |
| 222 final File file2 = new File(outFilename); | 222 final File file2 = new File(outFilename); |
| 223 file2.onError = (s) { | 223 file2.onError = (e) { |
| 224 Expect.fail("No errors expected : $s"); | 224 Expect.fail("No errors expected : $e"); |
| 225 }; | 225 }; |
| 226 file2.create(() { | 226 file2.create(() { |
| 227 file2.fullPath((s) { | 227 file2.fullPath((s) { |
| 228 Expect.isTrue(new File(s).existsSync()); | 228 Expect.isTrue(new File(s).existsSync()); |
| 229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 229 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
| 230 Expect.fail("Not a full path"); | 230 Expect.fail("Not a full path"); |
| 231 } | 231 } |
| 232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) { | 232 file2.open(FileMode.WRITE, (RandomAccessFile openedFile2) { |
| 233 openedFile2.onError = (s) { | 233 openedFile2.onError = (s) { |
| 234 Expect.fail("No errors expected : $s"); | 234 Expect.fail("No errors expected : $s"); |
| 235 }; | 235 }; |
| 236 openedFile2.writeList(buffer1, 0, bytes_read); | 236 openedFile2.writeList(buffer1, 0, bytes_read); |
| 237 openedFile2.onNoPendingWrites = () { | 237 openedFile2.onNoPendingWrites = () { |
| 238 openedFile2.close(() { | 238 openedFile2.close(() { |
| 239 List<int> buffer2 = new List<int>(bytes_read); | 239 List<int> buffer2 = new List<int>(bytes_read); |
| 240 final File file3 = new File(outFilename); | 240 final File file3 = new File(outFilename); |
| 241 file3.onError = (s) { | 241 file3.onError = (e) { |
| 242 Expect.fail("No errors expected : $s"); | 242 Expect.fail("No errors expected : $e"); |
| 243 }; | 243 }; |
| 244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) { | 244 file3.open(FileMode.READ, (RandomAccessFile openedFile3) { |
| 245 openedFile3.onError = (s) { | 245 openedFile3.onError = (s) { |
| 246 Expect.fail("No errors expected : $s"); | 246 Expect.fail("No errors expected : $s"); |
| 247 }; | 247 }; |
| 248 openedFile3.readList(buffer2, 0, 42, (bytes_read) { | 248 openedFile3.readList(buffer2, 0, 42, (bytes_read) { |
| 249 Expect.equals(42, bytes_read); | 249 Expect.equals(42, bytes_read); |
| 250 openedFile3.close(() { | 250 openedFile3.close(() { |
| 251 // Now compare the two buffers to check if they | 251 // Now compare the two buffers to check if they |
| 252 // are identical. | 252 // are identical. |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 file.createSync(); | 400 file.createSync(); |
| 401 RandomAccessFile openedFile = file.openSync(); | 401 RandomAccessFile openedFile = file.openSync(); |
| 402 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); | 402 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); |
| 403 openedFile.closeSync(); | 403 openedFile.closeSync(); |
| 404 file.deleteSync(); | 404 file.deleteSync(); |
| 405 } | 405 } |
| 406 | 406 |
| 407 static void testReadEmptyFile() { | 407 static void testReadEmptyFile() { |
| 408 String fileName = tempDirectory.path + "/empty_file"; | 408 String fileName = tempDirectory.path + "/empty_file"; |
| 409 File file = new File(fileName); | 409 File file = new File(fileName); |
| 410 file.onError = (s) { | 410 file.onError = (e) { |
| 411 Expect.fail("No errors expected : $s"); | 411 Expect.fail("No errors expected : $e"); |
| 412 }; | 412 }; |
| 413 asyncTestStarted(); | 413 asyncTestStarted(); |
| 414 file.create(() { | 414 file.create(() { |
| 415 file.open(FileMode.READ, (RandomAccessFile openedFile) { | 415 file.open(FileMode.READ, (RandomAccessFile openedFile) { |
| 416 openedFile.readByte((int byte) { | 416 openedFile.readByte((int byte) { |
| 417 Expect.fail("Read byte from empty file"); | 417 Expect.fail("Read byte from empty file"); |
| 418 }); | 418 }); |
| 419 openedFile.onError = (String err) { | 419 openedFile.onError = (String err) { |
| 420 Expect.isTrue(err.indexOf("failed") != -1); | 420 Expect.isTrue(err.indexOf("failed") != -1); |
| 421 openedFile.close(() { | 421 openedFile.close(() { |
| 422 file.delete(() { | 422 file.delete(() { |
| 423 asyncTestDone("testReadEmptyFile"); | 423 asyncTestDone("testReadEmptyFile"); |
| 424 }); | 424 }); |
| 425 }); | 425 }); |
| 426 }; | 426 }; |
| 427 }); | 427 }); |
| 428 }); | 428 }); |
| 429 } | 429 } |
| 430 | 430 |
| 431 // Test for file write of different types of lists. | 431 // Test for file write of different types of lists. |
| 432 static void testWriteVariousLists() { | 432 static void testWriteVariousLists() { |
| 433 asyncTestStarted(); | 433 asyncTestStarted(); |
| 434 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; | 434 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; |
| 435 final File file = new File(fileName); | 435 final File file = new File(fileName); |
| 436 file.onError = (s) { | 436 file.onError = (e) { |
| 437 Expect.fail("No errors expected : $s"); | 437 Expect.fail("No errors expected : $e"); |
| 438 }; | 438 }; |
| 439 file.create(() { | 439 file.create(() { |
| 440 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | 440 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { |
| 441 // Write bytes from 0 to 7. | 441 // Write bytes from 0 to 7. |
| 442 openedFile.writeList([0], 0, 1); | 442 openedFile.writeList([0], 0, 1); |
| 443 openedFile.writeList(const [1], 0, 1); | 443 openedFile.writeList(const [1], 0, 1); |
| 444 openedFile.writeList(new MyListOfOneElement(2), 0, 1); | 444 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
| 445 var x = 12345678901234567890123456789012345678901234567890; | 445 var x = 12345678901234567890123456789012345678901234567890; |
| 446 var y = 12345678901234567890123456789012345678901234567893; | 446 var y = 12345678901234567890123456789012345678901234567893; |
| 447 openedFile.writeList([y - x], 0, 1); | 447 openedFile.writeList([y - x], 0, 1); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 port.receive((message, replyTo) { | 484 port.receive((message, replyTo) { |
| 485 port.close(); | 485 port.close(); |
| 486 Expect.equals(1, message); | 486 Expect.equals(1, message); |
| 487 asyncTestDone("testDirectory"); | 487 asyncTestDone("testDirectory"); |
| 488 }); | 488 }); |
| 489 | 489 |
| 490 var tempDir = tempDirectory.path; | 490 var tempDir = tempDirectory.path; |
| 491 var file = new File("${tempDir}/testDirectory"); | 491 var file = new File("${tempDir}/testDirectory"); |
| 492 var errors = 0; | 492 var errors = 0; |
| 493 file.directory((d) => Expect.fail("non-existing file")); | 493 file.directory((d) => Expect.fail("non-existing file")); |
| 494 file.onError = (s) { | 494 file.onError = (e) { |
| 495 file.onError = (s) => Expect.fail("no error expected"); | 495 file.onError = (e) => Expect.fail("no error expected"); |
| 496 file.create(() { | 496 file.create(() { |
| 497 file.directory((Directory d) { | 497 file.directory((Directory d) { |
| 498 d.onError = (s) => Expect.fail("no error expected"); | 498 d.onError = (s) => Expect.fail("no error expected"); |
| 499 d.exists((exists) { | 499 d.exists((exists) { |
| 500 Expect.isTrue(exists); | 500 Expect.isTrue(exists); |
| 501 Expect.isTrue(d.path.endsWith(tempDir)); | 501 Expect.isTrue(d.path.endsWith(tempDir)); |
| 502 file.delete(() { | 502 file.delete(() { |
| 503 var file_dir = new File("."); | 503 var file_dir = new File("."); |
| 504 file_dir.directory((d) => Expect.fail("non-existing file")); | 504 file_dir.directory((d) => Expect.fail("non-existing file")); |
| 505 file_dir.onError = (s) { | 505 file_dir.onError = (e) { |
| 506 var file_dir = new File(tempDir); | 506 var file_dir = new File(tempDir); |
| 507 file_dir.directory((d) => Expect.fail("non-existing file")); | 507 file_dir.directory((d) => Expect.fail("non-existing file")); |
| 508 file_dir.onError = (s) { | 508 file_dir.onError = (e) { |
| 509 port.toSendPort().send(1); | 509 port.toSendPort().send(1); |
| 510 }; | 510 }; |
| 511 }; | 511 }; |
| 512 }); | 512 }); |
| 513 }); | 513 }); |
| 514 }); | 514 }); |
| 515 }); | 515 }); |
| 516 }; | 516 }; |
| 517 } | 517 } |
| 518 | 518 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 input.readListSync(buffer, 12, 6); | 593 input.readListSync(buffer, 12, 6); |
| 594 Expect.equals(18, input.positionSync()); | 594 Expect.equals(18, input.positionSync()); |
| 595 input.setPositionSync(8); | 595 input.setPositionSync(8); |
| 596 Expect.equals(8, input.positionSync()); | 596 Expect.equals(8, input.positionSync()); |
| 597 input.closeSync(); | 597 input.closeSync(); |
| 598 } | 598 } |
| 599 | 599 |
| 600 static void testTruncate() { | 600 static void testTruncate() { |
| 601 File file = new File(tempDirectory.path + "/out_truncate"); | 601 File file = new File(tempDirectory.path + "/out_truncate"); |
| 602 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 602 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
| 603 file.onError = (error) { | 603 file.onError = (e) { |
| 604 Expect.fail("testTruncate: No errors expected"); | 604 Expect.fail("No errors expected: $e"); |
| 605 }; | 605 }; |
| 606 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | 606 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { |
| 607 openedFile.writeList(buffer, 0, 10); | 607 openedFile.writeList(buffer, 0, 10); |
| 608 openedFile.onNoPendingWrites = () { | 608 openedFile.onNoPendingWrites = () { |
| 609 openedFile.length((length) { | 609 openedFile.length((length) { |
| 610 Expect.equals(10, length); | 610 Expect.equals(10, length); |
| 611 openedFile.truncate(5, () { | 611 openedFile.truncate(5, () { |
| 612 openedFile.length((length) { | 612 openedFile.length((length) { |
| 613 Expect.equals(5, length); | 613 Expect.equals(5, length); |
| 614 openedFile.close(() { | 614 openedFile.close(() { |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 } | 844 } |
| 845 Expect.equals(true, exceptionCaught); | 845 Expect.equals(true, exceptionCaught); |
| 846 Expect.equals(true, !wrongExceptionCaught); | 846 Expect.equals(true, !wrongExceptionCaught); |
| 847 openedFile.closeSync(); | 847 openedFile.closeSync(); |
| 848 file.deleteSync(); | 848 file.deleteSync(); |
| 849 } | 849 } |
| 850 | 850 |
| 851 static void testMixedSyncAndAsync() { | 851 static void testMixedSyncAndAsync() { |
| 852 var name = getFilename("tests/vm/data/fixed_length_file"); | 852 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 853 var f = new File(name); | 853 var f = new File(name); |
| 854 f.onError = (s) { | 854 f.onError = (e) { |
| 855 Expect.fail("No errors expected"); | 855 Expect.fail("No errors expected: $e"); |
| 856 }; | 856 }; |
| 857 f.exists((exists) { | 857 f.exists((exists) { |
| 858 try { | 858 try { |
| 859 f.existsSync(); | 859 f.existsSync(); |
| 860 Expect.fail("Expected exception"); | 860 Expect.fail("Expected exception"); |
| 861 } catch (var e) { | 861 } catch (var e) { |
| 862 Expect.isTrue(e is FileIOException); | 862 Expect.isTrue(e is FileIOException); |
| 863 } | 863 } |
| 864 }); | 864 }); |
| 865 } | 865 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 port.close(); | 909 port.close(); |
| 910 Expect.equals(1, result); | 910 Expect.equals(1, result); |
| 911 }); | 911 }); |
| 912 var name = getFilename("tests/vm/data/fixed_length_file"); | 912 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 913 var f = new File(name); | 913 var f = new File(name); |
| 914 f.readAsText(Encoding.UTF_8, (text) { | 914 f.readAsText(Encoding.UTF_8, (text) { |
| 915 Expect.isTrue(text.endsWith("42 bytes.")); | 915 Expect.isTrue(text.endsWith("42 bytes.")); |
| 916 Expect.equals(42, text.length); | 916 Expect.equals(42, text.length); |
| 917 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); | 917 var name = getDataFilename("tests/standalone/src/io/read_as_text.dat"); |
| 918 var f = new File(name); | 918 var f = new File(name); |
| 919 f.onError = (e) => Expect.fail("No errors expected"); | 919 f.onError = (e) => Expect.fail("No errors expected: $e"); |
| 920 f.readAsText(Encoding.UTF_8, (text) { | 920 f.readAsText(Encoding.UTF_8, (text) { |
| 921 Expect.equals(6, text.length); | 921 Expect.equals(6, text.length); |
| 922 var expected = [955, 120, 46, 32, 120, 10]; | 922 var expected = [955, 120, 46, 32, 120, 10]; |
| 923 Expect.listEquals(expected, text.charCodes()); | 923 Expect.listEquals(expected, text.charCodes()); |
| 924 f.readAsText(Encoding.ISO_8859_1, (text) { | 924 f.readAsText(Encoding.ISO_8859_1, (text) { |
| 925 Expect.equals(7, text.length); | 925 Expect.equals(7, text.length); |
| 926 var expected = [206, 187, 120, 46, 32, 120, 10]; | 926 var expected = [206, 187, 120, 46, 32, 120, 10]; |
| 927 Expect.listEquals(expected, text.charCodes()); | 927 Expect.listEquals(expected, text.charCodes()); |
| 928 f.onError = (e) { | 928 f.onError = (e) { |
| 929 port.toSendPort().send(1); | 929 port.toSendPort().send(1); |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1106 testWriteVariousLists(); | 1106 testWriteVariousLists(); |
| 1107 testDirectory(); | 1107 testDirectory(); |
| 1108 testDirectorySync(); | 1108 testDirectorySync(); |
| 1109 }); | 1109 }); |
| 1110 } | 1110 } |
| 1111 } | 1111 } |
| 1112 | 1112 |
| 1113 main() { | 1113 main() { |
| 1114 FileTest.testMain(); | 1114 FileTest.testMain(); |
| 1115 } | 1115 } |
| OLD | NEW |