| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:collection'; | 9 import 'dart:collection'; |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| (...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 RandomAccessFile openedFile = file.openSync(mode: WRITE); | 709 RandomAccessFile openedFile = file.openSync(mode: WRITE); |
| 710 openedFile.writeFromSync(buffer, 0, 10); | 710 openedFile.writeFromSync(buffer, 0, 10); |
| 711 Expect.equals(10, openedFile.lengthSync()); | 711 Expect.equals(10, openedFile.lengthSync()); |
| 712 openedFile.truncateSync(5); | 712 openedFile.truncateSync(5); |
| 713 Expect.equals(5, openedFile.lengthSync()); | 713 Expect.equals(5, openedFile.lengthSync()); |
| 714 openedFile.closeSync(); | 714 openedFile.closeSync(); |
| 715 file.deleteSync(); | 715 file.deleteSync(); |
| 716 Expect.isFalse(file.existsSync()); | 716 Expect.isFalse(file.existsSync()); |
| 717 } | 717 } |
| 718 | 718 |
| 719 static void testReadInto() async { |
| 720 asyncTestStarted(); |
| 721 File file = new File(tempDirectory.path + "/out_read_into"); |
| 722 |
| 723 var openedFile = await file.open(mode: WRITE); |
| 724 await openedFile.writeFrom(const [1, 2, 3]); |
| 725 |
| 726 await openedFile.setPosition(0); |
| 727 var list = [null, null, null]; |
| 728 Expect.equals(3, await openedFile.readInto(list)); |
| 729 Expect.listEquals([1, 2, 3], list); |
| 730 |
| 731 read(start, end, length, expected) async { |
| 732 var list = [null, null, null]; |
| 733 await openedFile.setPosition(0); |
| 734 Expect.equals(length, await openedFile.readInto(list, start, end)); |
| 735 Expect.listEquals(expected, list); |
| 736 return list; |
| 737 } |
| 738 |
| 739 await read(0, 3, 3, [1, 2, 3]); |
| 740 await read(0, 2, 2, [1, 2, null]); |
| 741 await read(1, 2, 1, [null, 1, null]); |
| 742 await read(1, 3, 2, [null, 1, 2]); |
| 743 await read(2, 3, 1, [null, null, 1]); |
| 744 |
| 745 asyncTestDone("testReadInto"); |
| 746 } |
| 747 |
| 748 static void testReadIntoSync() { |
| 749 File file = new File(tempDirectory.path + "/out_read_into_sync"); |
| 750 |
| 751 var openedFile = file.openSync(mode: WRITE); |
| 752 openedFile.writeFromSync(const [1, 2, 3]); |
| 753 |
| 754 openedFile.setPositionSync(0); |
| 755 var list = [null, null, null]; |
| 756 Expect.equals(3, openedFile.readIntoSync(list)); |
| 757 Expect.listEquals([1, 2, 3], list); |
| 758 |
| 759 read(start, end, length, expected) { |
| 760 var list = [null, null, null]; |
| 761 openedFile.setPositionSync(0); |
| 762 Expect.equals(length, openedFile.readIntoSync(list, start, end)); |
| 763 Expect.listEquals(expected, list); |
| 764 return list; |
| 765 } |
| 766 |
| 767 read(0, 3, 3, [1, 2, 3]); |
| 768 read(0, 2, 2, [1, 2, null]); |
| 769 read(1, 2, 1, [null, 1, null]); |
| 770 read(1, 3, 2, [null, 1, 2]); |
| 771 read(2, 3, 1, [null, null, 1]); |
| 772 } |
| 773 |
| 774 static void testWriteFrom() async { |
| 775 asyncTestStarted(); |
| 776 File file = new File(tempDirectory.path + "/out_write_from"); |
| 777 |
| 778 var buffer = const [1, 2, 3]; |
| 779 var openedFile = await file.open(mode: WRITE); |
| 780 |
| 781 await openedFile.writeFrom(buffer); |
| 782 var result = []..addAll(buffer);; |
| 783 |
| 784 write([start, end]) async { |
| 785 await openedFile.writeFrom(buffer, start, end); |
| 786 result.addAll(buffer.sublist(start, end)); |
| 787 } |
| 788 await write(0, 3); |
| 789 await write(0, 2); |
| 790 await write(1, 2); |
| 791 await write(1, 3); |
| 792 await write(2, 3); |
| 793 |
| 794 var bytesFromFile = await file.readAsBytes(); |
| 795 Expect.listEquals(result, bytesFromFile); |
| 796 asyncTestDone("testWriteFrom"); |
| 797 } |
| 798 |
| 799 static void testWriteFromSync() { |
| 800 File file = new File(tempDirectory.path + "/out_write_from_sync"); |
| 801 |
| 802 var buffer = const [1, 2, 3]; |
| 803 var openedFile = file.openSync(mode: WRITE); |
| 804 |
| 805 openedFile.writeFromSync(buffer); |
| 806 var result = []..addAll(buffer);; |
| 807 |
| 808 write([start, end]) { |
| 809 openedFile.writeFromSync(buffer, start, end); |
| 810 result.addAll(buffer.sublist(start, end)); |
| 811 } |
| 812 write(0, 3); |
| 813 write(0, 2); |
| 814 write(1, 2); |
| 815 write(1, 3); |
| 816 write(2, 3); |
| 817 |
| 818 var bytesFromFile = file.readAsBytesSync(); |
| 819 Expect.listEquals(result, bytesFromFile); |
| 820 } |
| 821 |
| 719 // Tests exception handling after file was closed. | 822 // Tests exception handling after file was closed. |
| 720 static void testCloseException() { | 823 static void testCloseException() { |
| 721 bool exceptionCaught = false; | 824 bool exceptionCaught = false; |
| 722 bool wrongExceptionCaught = false; | 825 bool wrongExceptionCaught = false; |
| 723 File input = new File(tempDirectory.path + "/out_close_exception"); | 826 File input = new File(tempDirectory.path + "/out_close_exception"); |
| 724 RandomAccessFile openedFile = input.openSync(mode: WRITE); | 827 RandomAccessFile openedFile = input.openSync(mode: WRITE); |
| 725 openedFile.closeSync(); | 828 openedFile.closeSync(); |
| 726 try { | 829 try { |
| 727 openedFile.readByteSync(); | 830 openedFile.readByteSync(); |
| 728 } on FileSystemException catch (ex) { | 831 } on FileSystemException catch (ex) { |
| (...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1320 testReadWriteStreamLargeFile(); | 1423 testReadWriteStreamLargeFile(); |
| 1321 testReadAsBytes(); | 1424 testReadAsBytes(); |
| 1322 testReadAsBytesEmptyFile(); | 1425 testReadAsBytesEmptyFile(); |
| 1323 testReadAsText(); | 1426 testReadAsText(); |
| 1324 testReadAsTextEmptyFile(); | 1427 testReadAsTextEmptyFile(); |
| 1325 testReadAsLines(); | 1428 testReadAsLines(); |
| 1326 testReadAsErrors(); | 1429 testReadAsErrors(); |
| 1327 testPosition(); | 1430 testPosition(); |
| 1328 testTruncate(); | 1431 testTruncate(); |
| 1329 testTruncateSync(); | 1432 testTruncateSync(); |
| 1433 testReadInto(); |
| 1434 testReadIntoSync(); |
| 1435 testWriteFrom(); |
| 1436 testWriteFromSync(); |
| 1330 testCloseException(); | 1437 testCloseException(); |
| 1331 testCloseExceptionStream(); | 1438 testCloseExceptionStream(); |
| 1332 testBufferOutOfBoundsException(); | 1439 testBufferOutOfBoundsException(); |
| 1333 testAppend(); | 1440 testAppend(); |
| 1334 testAppendSync(); | 1441 testAppendSync(); |
| 1335 testWriteAppend(); | 1442 testWriteAppend(); |
| 1336 testOutputStreamWriteAppend(); | 1443 testOutputStreamWriteAppend(); |
| 1337 testOutputStreamWriteString(); | 1444 testOutputStreamWriteString(); |
| 1338 testWriteVariousLists(); | 1445 testWriteVariousLists(); |
| 1339 testDirectory(); | 1446 testDirectory(); |
| 1340 testDirectorySync(); | 1447 testDirectorySync(); |
| 1341 testWriteStringUtf8(); | 1448 testWriteStringUtf8(); |
| 1342 testWriteStringUtf8Sync(); | 1449 testWriteStringUtf8Sync(); |
| 1343 testRename(targetExists: false); | 1450 testRename(targetExists: false); |
| 1344 testRenameSync(targetExists: false); | 1451 testRenameSync(targetExists: false); |
| 1345 testRename(targetExists: true); | 1452 testRename(targetExists: true); |
| 1346 testRenameSync(targetExists: true); | 1453 testRenameSync(targetExists: true); |
| 1347 testLastModified(); | 1454 testLastModified(); |
| 1348 testDoubleAsyncOperation(); | 1455 testDoubleAsyncOperation(); |
| 1349 asyncEnd(); | 1456 asyncEnd(); |
| 1350 }); | 1457 }); |
| 1351 } | 1458 } |
| 1352 } | 1459 } |
| 1353 | 1460 |
| 1354 main() { | 1461 main() { |
| 1355 FileTest.testMain(); | 1462 FileTest.testMain(); |
| 1356 } | 1463 } |
| OLD | NEW |