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 "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 (d) { | 85 (d) { |
86 buffer.addAll(d); | 86 buffer.addAll(d); |
87 }, | 87 }, |
88 onDone: () { | 88 onDone: () { |
89 Expect.equals(42, buffer.length); | 89 Expect.equals(42, buffer.length); |
90 // Write the contents of the file just read into another file. | 90 // Write the contents of the file just read into another file. |
91 String outFilename = | 91 String outFilename = |
92 tempDirectory.path + "/out_read_write_stream"; | 92 tempDirectory.path + "/out_read_write_stream"; |
93 var file2 = new File(outFilename); | 93 var file2 = new File(outFilename); |
94 var output = file2.openWrite(); | 94 var output = file2.openWrite(); |
95 output.writeBytes(buffer); | 95 output.add(buffer); |
96 output.close(); | 96 output.close(); |
97 output.done.then((_) { | 97 output.done.then((_) { |
98 // Now read the contents of the file just written. | 98 // Now read the contents of the file just written. |
99 List<int> buffer2 = new List<int>(); | 99 List<int> buffer2 = new List<int>(); |
100 new File(outFilename).openRead().listen( | 100 new File(outFilename).openRead().listen( |
101 (d) { | 101 (d) { |
102 buffer2.addAll(d); | 102 buffer2.addAll(d); |
103 }, | 103 }, |
104 onDone: () { | 104 onDone: () { |
105 Expect.equals(42, buffer2.length); | 105 Expect.equals(42, buffer2.length); |
(...skipping 17 matching lines...) Expand all Loading... |
123 | 123 |
124 // Create the test data - arbitrary binary data. | 124 // Create the test data - arbitrary binary data. |
125 List<int> buffer = new List<int>(100000); | 125 List<int> buffer = new List<int>(100000); |
126 for (var i = 0; i < buffer.length; ++i) { | 126 for (var i = 0; i < buffer.length; ++i) { |
127 buffer[i] = i % 256; | 127 buffer[i] = i % 256; |
128 } | 128 } |
129 String filename = | 129 String filename = |
130 tempDirectory.path + "/out_read_write_stream_large_file"; | 130 tempDirectory.path + "/out_read_write_stream_large_file"; |
131 File file = new File(filename); | 131 File file = new File(filename); |
132 IOSink output = file.openWrite(); | 132 IOSink output = file.openWrite(); |
133 output.writeBytes(buffer); | 133 output.add(buffer); |
134 output.writeBytes(buffer); | 134 output.add(buffer); |
135 output.close(); | 135 output.close(); |
136 output.done.then((_) { | 136 output.done.then((_) { |
137 Stream input = file.openRead(); | 137 Stream input = file.openRead(); |
138 int position = 0; | 138 int position = 0; |
139 final int expectedLength = 200000; | 139 final int expectedLength = 200000; |
140 // Start an independent asynchronous check on the length. | 140 // Start an independent asynchronous check on the length. |
141 asyncTestStarted(); | 141 asyncTestStarted(); |
142 file.length().then((len) { | 142 file.length().then((len) { |
143 Expect.equals(expectedLength, len); | 143 Expect.equals(expectedLength, len); |
144 asyncTestDone('testReadWriteStreamLargeFile: length check'); | 144 asyncTestDone('testReadWriteStreamLargeFile: length check'); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 file.deleteSync(); | 338 file.deleteSync(); |
339 } | 339 } |
340 | 340 |
341 static void testOutputStreamWriteAppend() { | 341 static void testOutputStreamWriteAppend() { |
342 String content = "foobar"; | 342 String content = "foobar"; |
343 String filename = tempDirectory.path + "/outstream_write_append"; | 343 String filename = tempDirectory.path + "/outstream_write_append"; |
344 File file = new File(filename); | 344 File file = new File(filename); |
345 file.createSync(); | 345 file.createSync(); |
346 List<int> buffer = content.codeUnits; | 346 List<int> buffer = content.codeUnits; |
347 var output = file.openWrite(); | 347 var output = file.openWrite(); |
348 output.writeBytes(buffer); | 348 output.add(buffer); |
349 output.close(); | 349 output.close(); |
350 output.done.then((_) { | 350 output.done.then((_) { |
351 File file2 = new File(filename); | 351 File file2 = new File(filename); |
352 var appendingOutput = file2.openWrite(mode: FileMode.APPEND); | 352 var appendingOutput = file2.openWrite(mode: FileMode.APPEND); |
353 appendingOutput.writeBytes(buffer); | 353 appendingOutput.add(buffer); |
354 appendingOutput.close(); | 354 appendingOutput.close(); |
355 appendingOutput.done.then((_) { | 355 appendingOutput.done.then((_) { |
356 File file3 = new File(filename); | 356 File file3 = new File(filename); |
357 file3.open(mode: FileMode.READ).then((RandomAccessFile openedFile) { | 357 file3.open(mode: FileMode.READ).then((RandomAccessFile openedFile) { |
358 openedFile.length().then((int length) { | 358 openedFile.length().then((int length) { |
359 Expect.equals(content.length * 2, length); | 359 Expect.equals(content.length * 2, length); |
360 openedFile.close().then((ignore) { | 360 openedFile.close().then((ignore) { |
361 file3.delete().then((ignore) { | 361 file3.delete().then((ignore) { |
362 asyncTestDone("testOutputStreamWriteAppend"); | 362 asyncTestDone("testOutputStreamWriteAppend"); |
363 }); | 363 }); |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 | 759 |
760 // Tests stream exception handling after file was closed. | 760 // Tests stream exception handling after file was closed. |
761 static void testCloseExceptionStream() { | 761 static void testCloseExceptionStream() { |
762 asyncTestStarted(); | 762 asyncTestStarted(); |
763 List<int> buffer = new List<int>(42); | 763 List<int> buffer = new List<int>(42); |
764 File file = | 764 File file = |
765 new File(tempDirectory.path + "/out_close_exception_stream"); | 765 new File(tempDirectory.path + "/out_close_exception_stream"); |
766 file.createSync(); | 766 file.createSync(); |
767 var output = file.openWrite(); | 767 var output = file.openWrite(); |
768 output.close(); | 768 output.close(); |
769 Expect.throws(() => output.writeBytes(buffer)); | 769 Expect.throws(() => output.add(buffer)); |
770 output.done.then((_) { | 770 output.done.then((_) { |
771 file.deleteSync(); | 771 file.deleteSync(); |
772 asyncTestDone("testCloseExceptionStream"); | 772 asyncTestDone("testCloseExceptionStream"); |
773 }); | 773 }); |
774 } | 774 } |
775 | 775 |
776 // Tests buffer out of bounds exception. | 776 // Tests buffer out of bounds exception. |
777 static void testBufferOutOfBoundsException() { | 777 static void testBufferOutOfBoundsException() { |
778 bool exceptionCaught = false; | 778 bool exceptionCaught = false; |
779 bool wrongExceptionCaught = false; | 779 bool wrongExceptionCaught = false; |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1237 testDirectorySync(); | 1237 testDirectorySync(); |
1238 testWriteStringUtf8(); | 1238 testWriteStringUtf8(); |
1239 testWriteStringUtf8Sync(); | 1239 testWriteStringUtf8Sync(); |
1240 }); | 1240 }); |
1241 } | 1241 } |
1242 } | 1242 } |
1243 | 1243 |
1244 main() { | 1244 main() { |
1245 FileTest.testMain(); | 1245 FileTest.testMain(); |
1246 } | 1246 } |
OLD | NEW |