OLD | NEW |
1 // Copyright (c) 2012, 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:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
11 class MyListOfOneElement implements List { | 11 class MyListOfOneElement implements List { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 (d) { | 84 (d) { |
85 buffer.addAll(d); | 85 buffer.addAll(d); |
86 }, | 86 }, |
87 onDone: () { | 87 onDone: () { |
88 Expect.equals(42, buffer.length); | 88 Expect.equals(42, buffer.length); |
89 // Write the contents of the file just read into another file. | 89 // Write the contents of the file just read into another file. |
90 String outFilename = | 90 String outFilename = |
91 tempDirectory.path.concat("/out_read_write_stream"); | 91 tempDirectory.path.concat("/out_read_write_stream"); |
92 var file2 = new File(outFilename); | 92 var file2 = new File(outFilename); |
93 var output = file2.openWrite(); | 93 var output = file2.openWrite(); |
94 output.add(buffer); | 94 output.writeBytes(buffer); |
95 output.close(); | 95 output.close(); |
96 output.done.then((_) { | 96 output.done.then((_) { |
97 // Now read the contents of the file just written. | 97 // Now read the contents of the file just written. |
98 List<int> buffer2 = new List<int>(); | 98 List<int> buffer2 = new List<int>(); |
99 new File(outFilename).openRead().listen( | 99 new File(outFilename).openRead().listen( |
100 (d) { | 100 (d) { |
101 buffer2.addAll(d); | 101 buffer2.addAll(d); |
102 }, | 102 }, |
103 onDone: () { | 103 onDone: () { |
104 Expect.equals(42, buffer2.length); | 104 Expect.equals(42, buffer2.length); |
(...skipping 17 matching lines...) Expand all Loading... |
122 | 122 |
123 // Create the test data - arbitrary binary data. | 123 // Create the test data - arbitrary binary data. |
124 List<int> buffer = new List<int>(100000); | 124 List<int> buffer = new List<int>(100000); |
125 for (var i = 0; i < buffer.length; ++i) { | 125 for (var i = 0; i < buffer.length; ++i) { |
126 buffer[i] = i % 256; | 126 buffer[i] = i % 256; |
127 } | 127 } |
128 String filename = | 128 String filename = |
129 tempDirectory.path.concat("/out_read_write_stream_large_file"); | 129 tempDirectory.path.concat("/out_read_write_stream_large_file"); |
130 File file = new File(filename); | 130 File file = new File(filename); |
131 IOSink output = file.openWrite(); | 131 IOSink output = file.openWrite(); |
132 output.add(buffer); | 132 output.writeBytes(buffer); |
133 output.add(buffer); | 133 output.writeBytes(buffer); |
134 output.close(); | 134 output.close(); |
135 output.done.then((_) { | 135 output.done.then((_) { |
136 Stream input = file.openRead(); | 136 Stream input = file.openRead(); |
137 int position = 0; | 137 int position = 0; |
138 final int expectedLength = 200000; | 138 final int expectedLength = 200000; |
139 // Start an independent asynchronous check on the length. | 139 // Start an independent asynchronous check on the length. |
140 asyncTestStarted(); | 140 asyncTestStarted(); |
141 file.length().then((len) { | 141 file.length().then((len) { |
142 Expect.equals(expectedLength, len); | 142 Expect.equals(expectedLength, len); |
143 asyncTestDone('testReadWriteStreamLargeFile: length check'); | 143 asyncTestDone('testReadWriteStreamLargeFile: length check'); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 file.deleteSync(); | 337 file.deleteSync(); |
338 } | 338 } |
339 | 339 |
340 static void testOutputStreamWriteAppend() { | 340 static void testOutputStreamWriteAppend() { |
341 String content = "foobar"; | 341 String content = "foobar"; |
342 String filename = tempDirectory.path.concat("/outstream_write_append"); | 342 String filename = tempDirectory.path.concat("/outstream_write_append"); |
343 File file = new File(filename); | 343 File file = new File(filename); |
344 file.createSync(); | 344 file.createSync(); |
345 List<int> buffer = content.codeUnits; | 345 List<int> buffer = content.codeUnits; |
346 var output = file.openWrite(); | 346 var output = file.openWrite(); |
347 output.add(buffer); | 347 output.writeBytes(buffer); |
348 output.close(); | 348 output.close(); |
349 output.done.then((_) { | 349 output.done.then((_) { |
350 File file2 = new File(filename); | 350 File file2 = new File(filename); |
351 var appendingOutput = file2.openWrite(FileMode.APPEND); | 351 var appendingOutput = file2.openWrite(mode: FileMode.APPEND); |
352 appendingOutput.add(buffer); | 352 appendingOutput.writeBytes(buffer); |
353 appendingOutput.close(); | 353 appendingOutput.close(); |
354 appendingOutput.done.then((_) { | 354 appendingOutput.done.then((_) { |
355 File file3 = new File(filename); | 355 File file3 = new File(filename); |
356 file3.open(FileMode.READ).then((RandomAccessFile openedFile) { | 356 file3.open(FileMode.READ).then((RandomAccessFile openedFile) { |
357 openedFile.length().then((int length) { | 357 openedFile.length().then((int length) { |
358 Expect.equals(content.length * 2, length); | 358 Expect.equals(content.length * 2, length); |
359 openedFile.close().then((ignore) { | 359 openedFile.close().then((ignore) { |
360 file3.delete().then((ignore) { | 360 file3.delete().then((ignore) { |
361 asyncTestDone("testOutputStreamWriteAppend"); | 361 asyncTestDone("testOutputStreamWriteAppend"); |
362 }); | 362 }); |
363 }); | 363 }); |
364 }); | 364 }); |
365 }); | 365 }); |
366 }); | 366 }); |
367 }); | 367 }); |
368 asyncTestStarted(); | 368 asyncTestStarted(); |
369 } | 369 } |
370 | 370 |
371 // Test for file read and write functionality. | 371 // Test for file read and write functionality. |
372 static void testOutputStreamWriteString() { | 372 static void testOutputStreamWriteString() { |
373 String content = "foobar"; | 373 String content = "foobar"; |
374 String filename = tempDirectory.path.concat("/outstream_write_string"); | 374 String filename = tempDirectory.path.concat("/outstream_write_string"); |
375 File file = new File(filename); | 375 File file = new File(filename); |
376 file.createSync(); | 376 file.createSync(); |
377 List<int> buffer = content.codeUnits; | 377 List<int> buffer = content.codeUnits; |
378 var output = file.openWrite(); | 378 var output = file.openWrite(); |
379 output.addString("abcdABCD"); | 379 output.write("abcdABCD"); |
380 output.addString("abcdABCD", Encoding.UTF_8); | 380 output.encoding = Encoding.UTF_8; |
381 output.addString("abcdABCD", Encoding.ISO_8859_1); | 381 output.write("abcdABCD"); |
382 output.addString("abcdABCD", Encoding.ASCII); | 382 output.encoding = Encoding.ISO_8859_1; |
383 output.addString("æøå", Encoding.UTF_8); | 383 output.write("abcdABCD"); |
| 384 output.encoding = Encoding.ASCII; |
| 385 output.write("abcdABCD"); |
| 386 output.encoding = Encoding.UTF_8; |
| 387 output.write("æøå"); |
384 output.close(); | 388 output.close(); |
385 output.done.then((_) { | 389 output.done.then((_) { |
386 RandomAccessFile raf = file.openSync(); | 390 RandomAccessFile raf = file.openSync(); |
387 Expect.equals(38, raf.lengthSync()); | 391 Expect.equals(38, raf.lengthSync()); |
388 raf.close().then((ignore) { | 392 raf.close().then((ignore) { |
389 asyncTestDone("testOutputStreamWriteString"); | 393 asyncTestDone("testOutputStreamWriteString"); |
390 }); | 394 }); |
391 }); | 395 }); |
392 asyncTestStarted(); | 396 asyncTestStarted(); |
393 } | 397 } |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 | 758 |
755 // Tests stream exception handling after file was closed. | 759 // Tests stream exception handling after file was closed. |
756 static void testCloseExceptionStream() { | 760 static void testCloseExceptionStream() { |
757 asyncTestStarted(); | 761 asyncTestStarted(); |
758 List<int> buffer = new List<int>(42); | 762 List<int> buffer = new List<int>(42); |
759 File file = | 763 File file = |
760 new File(tempDirectory.path.concat("/out_close_exception_stream")); | 764 new File(tempDirectory.path.concat("/out_close_exception_stream")); |
761 file.createSync(); | 765 file.createSync(); |
762 var output = file.openWrite(); | 766 var output = file.openWrite(); |
763 output.close(); | 767 output.close(); |
764 Expect.throws(() => output.add(buffer)); | 768 Expect.throws(() => output.writeBytes(buffer)); |
765 output.done.then((_) { | 769 output.done.then((_) { |
766 file.deleteSync(); | 770 file.deleteSync(); |
767 asyncTestDone("testCloseExceptionStream"); | 771 asyncTestDone("testCloseExceptionStream"); |
768 }); | 772 }); |
769 } | 773 } |
770 | 774 |
771 // Tests buffer out of bounds exception. | 775 // Tests buffer out of bounds exception. |
772 static void testBufferOutOfBoundsException() { | 776 static void testBufferOutOfBoundsException() { |
773 bool exceptionCaught = false; | 777 bool exceptionCaught = false; |
774 bool wrongExceptionCaught = false; | 778 bool wrongExceptionCaught = false; |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1232 testDirectorySync(); | 1236 testDirectorySync(); |
1233 testWriteStringUtf8(); | 1237 testWriteStringUtf8(); |
1234 testWriteStringUtf8Sync(); | 1238 testWriteStringUtf8Sync(); |
1235 }); | 1239 }); |
1236 } | 1240 } |
1237 } | 1241 } |
1238 | 1242 |
1239 main() { | 1243 main() { |
1240 FileTest.testMain(); | 1244 FileTest.testMain(); |
1241 } | 1245 } |
OLD | NEW |