| 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 { |
| 10 int _value; |
| 11 MyListOfOneElement(this._value); |
| 12 int get length() => 1; |
| 13 operator [](int index) => _value; |
| 14 } |
| 15 |
| 9 class FileTest { | 16 class FileTest { |
| 10 static Directory tempDirectory; | 17 static Directory tempDirectory; |
| 11 static int numLiveAsyncTests = 0; | 18 static int numLiveAsyncTests = 0; |
| 12 | 19 |
| 13 static void asyncTestStarted() { ++numLiveAsyncTests; } | 20 static void asyncTestStarted() { ++numLiveAsyncTests; } |
| 14 static void asyncTestDone(String name) { | 21 static void asyncTestDone(String name) { |
| 15 --numLiveAsyncTests; | 22 --numLiveAsyncTests; |
| 16 if (numLiveAsyncTests == 0) { | 23 if (numLiveAsyncTests == 0) { |
| 17 deleteTempDirectory(); | 24 deleteTempDirectory(); |
| 18 } | 25 } |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 openedFile.close(); | 395 openedFile.close(); |
| 389 }; | 396 }; |
| 390 openedFile.readByte(); | 397 openedFile.readByte(); |
| 391 }; | 398 }; |
| 392 file.open(); | 399 file.open(); |
| 393 }; | 400 }; |
| 394 asyncTestStarted(); | 401 asyncTestStarted(); |
| 395 file.create(); | 402 file.create(); |
| 396 } | 403 } |
| 397 | 404 |
| 405 // Test for file write of different types of lists. |
| 406 static void testWriteVariousLists() { |
| 407 final String fileName = "${tempDirectory.path}/testWriteVariousLists"; |
| 408 final File file = new File(fileName); |
| 409 file.create(); |
| 410 file.createHandler = () { |
| 411 file.open(FileMode.WRITE); |
| 412 file.openHandler = (RandomAccessFile openedFile) { |
| 413 // Write bytes from 0 to 7. |
| 414 openedFile.writeList([0], 0, 1); |
| 415 openedFile.writeList(const [1], 0, 1); |
| 416 openedFile.writeList(new MyListOfOneElement(2), 0, 1); |
| 417 var x = 12345678901234567890123456789012345678901234567890; |
| 418 var y = 12345678901234567890123456789012345678901234567893; |
| 419 openedFile.writeList([y - x], 0, 1); |
| 420 openedFile.writeList([260], 0, 1); // 260 = 256 + 4 = 0x104. |
| 421 openedFile.writeList(const [261], 0, 1); |
| 422 openedFile.writeList(new MyListOfOneElement(262), 0, 1); |
| 423 x = 12345678901234567890123456789012345678901234567890; |
| 424 y = 12345678901234567890123456789012345678901234568153; |
| 425 openedFile.writeList([y - x], 0, 1); |
| 426 |
| 427 openedFile.errorHandler = (s) { |
| 428 Expect.fail("No errors expected : $s"); |
| 429 }; |
| 430 openedFile.noPendingWriteHandler = () { |
| 431 openedFile.close(); |
| 432 }; |
| 433 openedFile.closeHandler = () { |
| 434 // Check the written bytes. |
| 435 final File file2 = new File(fileName); |
| 436 var openedFile2 = file2.openSync(); |
| 437 vat length = openedFile2.lengthSync(); |
| 438 Expect.equals(8, length); |
| 439 List data = new List(length); |
| 440 var readLength = openedFile2.readListSync(data, 0, length); |
| 441 Expect.equals(length, readLength); |
| 442 for (var i = 0; i < data.length; i++) { |
| 443 Expect.equals(i, data[i]); |
| 444 } |
| 445 openedFile2.closeSync(); |
| 446 file2.deleteSync(); |
| 447 }; |
| 448 }; |
| 449 file.errorHandler = (s) { |
| 450 Expect.fail("No errors expected : $s"); |
| 451 }; |
| 452 }; |
| 453 } |
| 454 |
| 398 // Test for file length functionality. | 455 // Test for file length functionality. |
| 399 static void testLength() { | 456 static void testLength() { |
| 400 String filename = getFilename("tests/vm/data/fixed_length_file"); | 457 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 401 RandomAccessFile input = (new File(filename)).openSync(); | 458 RandomAccessFile input = (new File(filename)).openSync(); |
| 402 input.errorHandler = (s) { | 459 input.errorHandler = (s) { |
| 403 Expect.fail("No errors expected"); | 460 Expect.fail("No errors expected"); |
| 404 }; | 461 }; |
| 405 input.lengthHandler = (length) { | 462 input.lengthHandler = (length) { |
| 406 Expect.equals(42, length); | 463 Expect.equals(42, length); |
| 407 input.close(); | 464 input.close(); |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 testReadEmptyFile(); | 893 testReadEmptyFile(); |
| 837 testTruncate(); | 894 testTruncate(); |
| 838 testTruncateSync(); | 895 testTruncateSync(); |
| 839 testCloseException(); | 896 testCloseException(); |
| 840 testCloseExceptionStream(); | 897 testCloseExceptionStream(); |
| 841 testBufferOutOfBoundsException(); | 898 testBufferOutOfBoundsException(); |
| 842 testAppend(); | 899 testAppend(); |
| 843 testAppendSync(); | 900 testAppendSync(); |
| 844 testWriteAppend(); | 901 testWriteAppend(); |
| 845 testOutputStreamWriteAppend(); | 902 testOutputStreamWriteAppend(); |
| 903 testWriteVariousLists(); |
| 846 }); | 904 }); |
| 847 } | 905 } |
| 848 } | 906 } |
| 849 | 907 |
| 850 main() { | 908 main() { |
| 851 FileTest.testMain(); | 909 FileTest.testMain(); |
| 852 } | 910 } |
| OLD | NEW |