| 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 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 // Delete the output file. | 392 // Delete the output file. |
| 393 outFile.deleteSync(); | 393 outFile.deleteSync(); |
| 394 Expect.isFalse(outFile.existsSync()); | 394 Expect.isFalse(outFile.existsSync()); |
| 395 } | 395 } |
| 396 | 396 |
| 397 static void testReadEmptyFileSync() { | 397 static void testReadEmptyFileSync() { |
| 398 String fileName = tempDirectory.path + "/empty_file_sync"; | 398 String fileName = tempDirectory.path + "/empty_file_sync"; |
| 399 File file = new File(fileName); | 399 File file = new File(fileName); |
| 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.equals(-1, openedFile.readByteSync()); |
| 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 = (e) { | 410 file.onError = (e) { |
| 411 Expect.fail("No errors expected : $e"); | 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.equals(-1, byte); |
| 418 }); | 418 }); |
| 419 openedFile.onError = (String err) { | 419 openedFile.onError = (e) { |
| 420 Expect.isTrue(err.indexOf("failed") != -1); | 420 Expect.isTrue(e is FileIOException); |
| 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 = (e) { | 436 file.onError = (e) => Expect.fail("No errors expected : $e"); |
| 437 Expect.fail("No errors expected : $e"); | |
| 438 }; | |
| 439 file.create(() { | 437 file.create(() { |
| 440 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | 438 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { |
| 441 // Write bytes from 0 to 7. | 439 // Write bytes from 0 to 7. |
| 442 openedFile.writeList([0], 0, 1); | 440 openedFile.writeList([0], 0, 1); |
| 443 openedFile.writeList(const [1], 0, 1); | 441 openedFile.writeList(const [1], 0, 1); |
| 444 openedFile.writeList(new MyListOfOneElement(2), 0, 1); | 442 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
| 445 var x = 12345678901234567890123456789012345678901234567890; | 443 var x = 12345678901234567890123456789012345678901234567890; |
| 446 var y = 12345678901234567890123456789012345678901234567893; | 444 var y = 12345678901234567890123456789012345678901234567893; |
| 447 openedFile.writeList([y - x], 0, 1); | 445 openedFile.writeList([y - x], 0, 1); |
| 448 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. | 446 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. |
| 449 openedFile.writeList(const [261], 0, 1); | 447 openedFile.writeList(const [261], 0, 1); |
| 450 openedFile.writeList(new MyListOfOneElement(262), 0, 1); | 448 openedFile.writeList(new MyListOfOneElement(262), 0, 1); |
| 451 x = 12345678901234567890123456789012345678901234567890; | 449 x = 12345678901234567890123456789012345678901234567890; |
| 452 y = 12345678901234567890123456789012345678901234568153; | 450 y = 12345678901234567890123456789012345678901234568153; |
| 453 openedFile.writeList([y - x], 0, 1); | 451 openedFile.writeList([y - x], 0, 1); |
| 454 | 452 |
| 455 openedFile.onError = (s) { | 453 openedFile.onError = (e) => Expect.fail("No errors expected : $e"); |
| 456 Expect.fail("No errors expected : $s"); | |
| 457 }; | |
| 458 openedFile.onNoPendingWrites = () { | 454 openedFile.onNoPendingWrites = () { |
| 459 openedFile.close(() { | 455 openedFile.close(() { |
| 460 // Check the written bytes. | 456 // Check the written bytes. |
| 461 final File file2 = new File(fileName); | 457 final File file2 = new File(fileName); |
| 462 var openedFile2 = file2.openSync(); | 458 var openedFile2 = file2.openSync(); |
| 463 var length = openedFile2.lengthSync(); | 459 var length = openedFile2.lengthSync(); |
| 464 Expect.equals(8, length); | 460 Expect.equals(8, length); |
| 465 List data = new List(length); | 461 List data = new List(length); |
| 466 openedFile2.readListSync(data, 0, length); | 462 openedFile2.readListSync(data, 0, length); |
| 467 for (var i = 0; i < data.length; i++) { | 463 for (var i = 0; i < data.length; i++) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 498 d.onError = (s) => Expect.fail("no error expected"); | 494 d.onError = (s) => Expect.fail("no error expected"); |
| 499 d.exists((exists) { | 495 d.exists((exists) { |
| 500 Expect.isTrue(exists); | 496 Expect.isTrue(exists); |
| 501 Expect.isTrue(d.path.endsWith(tempDir)); | 497 Expect.isTrue(d.path.endsWith(tempDir)); |
| 502 file.delete(() { | 498 file.delete(() { |
| 503 var file_dir = new File("."); | 499 var file_dir = new File("."); |
| 504 file_dir.directory((d) => Expect.fail("non-existing file")); | 500 file_dir.directory((d) => Expect.fail("non-existing file")); |
| 505 file_dir.onError = (e) { | 501 file_dir.onError = (e) { |
| 506 var file_dir = new File(tempDir); | 502 var file_dir = new File(tempDir); |
| 507 file_dir.directory((d) => Expect.fail("non-existing file")); | 503 file_dir.directory((d) => Expect.fail("non-existing file")); |
| 508 file_dir.onError = (e) { | 504 file_dir.onError = (e) => port.toSendPort().send(1); |
| 509 port.toSendPort().send(1); | |
| 510 }; | |
| 511 }; | 505 }; |
| 512 }); | 506 }); |
| 513 }); | 507 }); |
| 514 }); | 508 }); |
| 515 }); | 509 }); |
| 516 }; | 510 }; |
| 517 } | 511 } |
| 518 | 512 |
| 519 static void testDirectorySync() { | 513 static void testDirectorySync() { |
| 520 var tempDir = tempDirectory.path; | 514 var tempDir = tempDirectory.path; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 531 var file_dir = new File("."); | 525 var file_dir = new File("."); |
| 532 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 526 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
| 533 file_dir = new File(tempDir); | 527 file_dir = new File(tempDir); |
| 534 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); | 528 Expect.throws(file_dir.directorySync, (e) { return e is FileIOException; }); |
| 535 } | 529 } |
| 536 | 530 |
| 537 // Test for file length functionality. | 531 // Test for file length functionality. |
| 538 static void testLength() { | 532 static void testLength() { |
| 539 String filename = getFilename("tests/vm/data/fixed_length_file"); | 533 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 540 RandomAccessFile input = (new File(filename)).openSync(); | 534 RandomAccessFile input = (new File(filename)).openSync(); |
| 541 input.onError = (s) { | 535 input.onError = (e) => Expect.fail("No errors expected"); |
| 542 Expect.fail("No errors expected"); | |
| 543 }; | |
| 544 input.length((length) { | 536 input.length((length) { |
| 545 Expect.equals(42, length); | 537 Expect.equals(42, length); |
| 546 input.close(() => null); | 538 input.close(() => null); |
| 547 }); | 539 }); |
| 548 } | 540 } |
| 549 | 541 |
| 550 static void testLengthSync() { | 542 static void testLengthSync() { |
| 551 String filename = getFilename("tests/vm/data/fixed_length_file"); | 543 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 552 RandomAccessFile input = (new File(filename)).openSync(); | 544 RandomAccessFile input = (new File(filename)).openSync(); |
| 553 Expect.equals(42, input.lengthSync()); | 545 Expect.equals(42, input.lengthSync()); |
| 554 input.closeSync(); | 546 input.closeSync(); |
| 555 } | 547 } |
| 556 | 548 |
| 557 // Test for file position functionality. | 549 // Test for file position functionality. |
| 558 static void testPosition() { | 550 static void testPosition() { |
| 559 String filename = getFilename("tests/vm/data/fixed_length_file"); | 551 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 560 RandomAccessFile input = (new File(filename)).openSync(); | 552 RandomAccessFile input = (new File(filename)).openSync(); |
| 561 input.onError = (s) { | 553 input.onError = (e) => Expect.fail("No errors expected"); |
| 562 Expect.fail("No errors expected"); | |
| 563 }; | |
| 564 input.position((position) { | 554 input.position((position) { |
| 565 Expect.equals(0, position); | 555 Expect.equals(0, position); |
| 566 List<int> buffer = new List<int>(100); | 556 List<int> buffer = new List<int>(100); |
| 567 input.readList(buffer, 0, 12, (bytes_read) { | 557 input.readList(buffer, 0, 12, (bytes_read) { |
| 568 input.position((position) { | 558 input.position((position) { |
| 569 Expect.equals(12, position); | 559 Expect.equals(12, position); |
| 570 input.readList(buffer, 12, 6, (bytes_read) { | 560 input.readList(buffer, 12, 6, (bytes_read) { |
| 571 input.position((position) { | 561 input.position((position) { |
| 572 Expect.equals(18, position); | 562 Expect.equals(18, position); |
| 573 input.setPosition(8, () { | 563 input.setPosition(8, () { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 593 input.readListSync(buffer, 12, 6); | 583 input.readListSync(buffer, 12, 6); |
| 594 Expect.equals(18, input.positionSync()); | 584 Expect.equals(18, input.positionSync()); |
| 595 input.setPositionSync(8); | 585 input.setPositionSync(8); |
| 596 Expect.equals(8, input.positionSync()); | 586 Expect.equals(8, input.positionSync()); |
| 597 input.closeSync(); | 587 input.closeSync(); |
| 598 } | 588 } |
| 599 | 589 |
| 600 static void testTruncate() { | 590 static void testTruncate() { |
| 601 File file = new File(tempDirectory.path + "/out_truncate"); | 591 File file = new File(tempDirectory.path + "/out_truncate"); |
| 602 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 592 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
| 603 file.onError = (e) { | 593 file.onError = (e) => Expect.fail("No errors expected: $e"); |
| 604 Expect.fail("No errors expected: $e"); | |
| 605 }; | |
| 606 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { | 594 file.open(FileMode.WRITE, (RandomAccessFile openedFile) { |
| 607 openedFile.writeList(buffer, 0, 10); | 595 openedFile.writeList(buffer, 0, 10); |
| 608 openedFile.onNoPendingWrites = () { | 596 openedFile.onNoPendingWrites = () { |
| 609 openedFile.length((length) { | 597 openedFile.length((length) { |
| 610 Expect.equals(10, length); | 598 Expect.equals(10, length); |
| 611 openedFile.truncate(5, () { | 599 openedFile.truncate(5, () { |
| 612 openedFile.length((length) { | 600 openedFile.length((length) { |
| 613 Expect.equals(5, length); | 601 Expect.equals(5, length); |
| 614 openedFile.close(() { | 602 openedFile.close(() { |
| 615 file.delete(() { | 603 file.delete(() { |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 844 } | 832 } |
| 845 Expect.equals(true, exceptionCaught); | 833 Expect.equals(true, exceptionCaught); |
| 846 Expect.equals(true, !wrongExceptionCaught); | 834 Expect.equals(true, !wrongExceptionCaught); |
| 847 openedFile.closeSync(); | 835 openedFile.closeSync(); |
| 848 file.deleteSync(); | 836 file.deleteSync(); |
| 849 } | 837 } |
| 850 | 838 |
| 851 static void testMixedSyncAndAsync() { | 839 static void testMixedSyncAndAsync() { |
| 852 var name = getFilename("tests/vm/data/fixed_length_file"); | 840 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 853 var f = new File(name); | 841 var f = new File(name); |
| 854 f.onError = (e) { | 842 f.onError = (e) => Expect.fail("No errors expected: $e"); |
| 855 Expect.fail("No errors expected: $e"); | |
| 856 }; | |
| 857 f.exists((exists) { | 843 f.exists((exists) { |
| 858 try { | 844 try { |
| 859 f.existsSync(); | 845 f.existsSync(); |
| 860 Expect.fail("Expected exception"); | 846 Expect.fail("Expected exception"); |
| 861 } catch (var e) { | 847 } catch (var e) { |
| 862 Expect.isTrue(e is FileIOException); | 848 Expect.isTrue(e is FileIOException); |
| 863 } | 849 } |
| 864 }); | 850 }); |
| 865 } | 851 } |
| 866 | 852 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 884 port.receive((result, replyTo) { | 870 port.receive((result, replyTo) { |
| 885 port.close(); | 871 port.close(); |
| 886 Expect.equals(42, result); | 872 Expect.equals(42, result); |
| 887 }); | 873 }); |
| 888 var name = getFilename("tests/vm/data/fixed_length_file"); | 874 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 889 var f = new File(name); | 875 var f = new File(name); |
| 890 f.readAsBytes((bytes) { | 876 f.readAsBytes((bytes) { |
| 891 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 877 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 892 port.toSendPort().send(bytes.length); | 878 port.toSendPort().send(bytes.length); |
| 893 }); | 879 }); |
| 894 f.onError = (e) { | 880 f.onError = (e) => Expect.fail("No errors expected: $e"); |
| 895 Expect.fail("No errors expected: $e"); | |
| 896 }; | |
| 897 } | 881 } |
| 898 | 882 |
| 899 static void testReadAsBytesSync() { | 883 static void testReadAsBytesSync() { |
| 900 var name = getFilename("tests/vm/data/fixed_length_file"); | 884 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 901 var bytes = new File(name).readAsBytesSync(); | 885 var bytes = new File(name).readAsBytesSync(); |
| 902 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); | 886 Expect.isTrue(new String.fromCharCodes(bytes).endsWith("42 bytes.")); |
| 903 Expect.equals(bytes.length, 42); | 887 Expect.equals(bytes.length, 42); |
| 904 } | 888 } |
| 905 | 889 |
| 906 static void testReadAsText() { | 890 static void testReadAsText() { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 963 Expect.equals(42, result); | 947 Expect.equals(42, result); |
| 964 }); | 948 }); |
| 965 var name = getFilename("tests/vm/data/fixed_length_file"); | 949 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 966 var f = new File(name); | 950 var f = new File(name); |
| 967 f.readAsLines(Encoding.UTF_8, (lines) { | 951 f.readAsLines(Encoding.UTF_8, (lines) { |
| 968 Expect.equals(1, lines.length); | 952 Expect.equals(1, lines.length); |
| 969 var line = lines[0]; | 953 var line = lines[0]; |
| 970 Expect.isTrue(line.endsWith("42 bytes.")); | 954 Expect.isTrue(line.endsWith("42 bytes.")); |
| 971 port.toSendPort().send(line.length); | 955 port.toSendPort().send(line.length); |
| 972 }); | 956 }); |
| 973 f.onError = (e) { | 957 f.onError = (e) => Expect.fail("No errors expected: $e"); |
| 974 Expect.fail("No errors expected: $e"); | |
| 975 }; | |
| 976 } | 958 } |
| 977 | 959 |
| 978 static void testReadAsLinesSync() { | 960 static void testReadAsLinesSync() { |
| 979 var name = getFilename("tests/vm/data/fixed_length_file"); | 961 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 980 var lines = new File(name).readAsLinesSync(); | 962 var lines = new File(name).readAsLinesSync(); |
| 981 Expect.equals(1, lines.length); | 963 Expect.equals(1, lines.length); |
| 982 var line = lines[0]; | 964 var line = lines[0]; |
| 983 Expect.isTrue(line.endsWith("42 bytes.")); | 965 Expect.isTrue(line.endsWith("42 bytes.")); |
| 984 Expect.equals(42, line.length); | 966 Expect.equals(42, line.length); |
| 985 name = getDataFilename("tests/standalone/src/io/readline_test1.dat"); | 967 name = getDataFilename("tests/standalone/src/io/readline_test1.dat"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 997 var f = new File('.'); | 979 var f = new File('.'); |
| 998 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); | 980 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); |
| 999 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); | 981 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); |
| 1000 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); | 982 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); |
| 1001 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); | 983 f.readAsBytes((bytes) => Expect.fail("no bytes expected")); |
| 1002 f.onError = (e) { | 984 f.onError = (e) { |
| 1003 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); | 985 f.readAsText(Encoding.UTF_8, (text) => Expect.fail("no text expected")); |
| 1004 f.onError = (e) { | 986 f.onError = (e) { |
| 1005 f.readAsLines(Encoding.UTF_8, | 987 f.readAsLines(Encoding.UTF_8, |
| 1006 (lines) => Expect.fail("no lines expected")); | 988 (lines) => Expect.fail("no lines expected")); |
| 1007 f.onError = (e) { | 989 f.onError = (e) => port.toSendPort().send(1); |
| 1008 port.toSendPort().send(1); | |
| 1009 }; | |
| 1010 }; | 990 }; |
| 1011 }; | 991 }; |
| 1012 } | 992 } |
| 1013 | 993 |
| 1014 // Test that opens the same file for writing then for appending to test | 994 // Test that opens the same file for writing then for appending to test |
| 1015 // that the file is not truncated when opened for appending. | 995 // that the file is not truncated when opened for appending. |
| 1016 static void testAppend() { | 996 static void testAppend() { |
| 1017 var file = new File('${tempDirectory.path}/out_append'); | 997 var file = new File('${tempDirectory.path}/out_append'); |
| 1018 file.open(FileMode.WRITE, (openedFile) { | 998 file.open(FileMode.WRITE, (openedFile) { |
| 1019 openedFile.writeString("asdf"); | 999 openedFile.writeString("asdf"); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1106 testWriteVariousLists(); | 1086 testWriteVariousLists(); |
| 1107 testDirectory(); | 1087 testDirectory(); |
| 1108 testDirectorySync(); | 1088 testDirectorySync(); |
| 1109 }); | 1089 }); |
| 1110 } | 1090 } |
| 1111 } | 1091 } |
| 1112 | 1092 |
| 1113 main() { | 1093 main() { |
| 1114 FileTest.testMain(); | 1094 FileTest.testMain(); |
| 1115 } | 1095 } |
| OLD | NEW |