| 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 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 asyncTestDone("testReadWriteStream"); | 157 asyncTestDone("testReadWriteStream"); |
| 158 }; | 158 }; |
| 159 }; | 159 }; |
| 160 }; | 160 }; |
| 161 }; | 161 }; |
| 162 }; | 162 }; |
| 163 }; | 163 }; |
| 164 }; | 164 }; |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Test for file stream buffered handling of large files. |
| 168 static void testReadWriteStreamLargeFile() { |
| 169 asyncTestStarted(); |
| 170 |
| 171 // Create the test data - arbitrary binary data. |
| 172 List<int> buffer = new List<int>(100000); |
| 173 for (var i = 0; i < buffer.length; ++i) { |
| 174 buffer[i] = i % 256; |
| 175 } |
| 176 String filename = |
| 177 tempDirectory.path.concat("/out_read_write_stream_large_file"); |
| 178 File file = new File(filename); |
| 179 OutputStream output = file.openOutputStream(); |
| 180 // Test a write immediately after the output stream is created. |
| 181 output.writeFrom(buffer, 0, 20000); |
| 182 |
| 183 output.onNoPendingWrites = () { |
| 184 output.writeFrom(buffer, 20000, 60000); |
| 185 output.writeFrom(buffer, 80000, 20000); |
| 186 output.onNoPendingWrites = () { |
| 187 output.writeFrom(buffer, 0, 0); |
| 188 output.writeFrom(buffer, 0, 0); |
| 189 output.writeFrom(buffer, 0, 100000); |
| 190 output.close(); |
| 191 }; |
| 192 }; |
| 193 output.onClosed = () { |
| 194 InputStream input = file.openInputStream(); |
| 195 int position = 0; |
| 196 final int expectedLength = 200000; |
| 197 // Start an independent asynchronous check on the length. |
| 198 asyncTestStarted(); |
| 199 file.length().then((len) { |
| 200 Expect.equals(expectedLength, len); |
| 201 asyncTestDone('testReadWriteStreamLargeFile: length check'); |
| 202 }); |
| 203 |
| 204 List<int> inputBuffer = new List<int>(expectedLength + 100000); |
| 205 // Immediate read should read 0 bytes. |
| 206 Expect.equals(0, input.available()); |
| 207 Expect.equals(false, input.closed); |
| 208 int bytesRead = input.readInto(inputBuffer); |
| 209 Expect.equals(0, bytesRead); |
| 210 Expect.equals(0, input.available()); |
| 211 Expect.isFalse(input.closed); |
| 212 input.onError = (e) { |
| 213 print('Error handler called on input in testReadWriteStreamLargeFile'); |
| 214 print('with error $e'); |
| 215 throw e; |
| 216 }; |
| 217 input.onData = () { |
| 218 Expect.isFalse(input.closed); |
| 219 bytesRead = input.readInto(inputBuffer, offset: position, |
| 220 len: inputBuffer.length - position); |
| 221 position += bytesRead; |
| 222 // The buffer is large enough to hold all available data. |
| 223 // So there should be no data left to read. |
| 224 Expect.equals(0, input.available()); |
| 225 bytesRead = input.readInto(inputBuffer, offset: position, |
| 226 len: expectedLength - position); |
| 227 Expect.equals(0, bytesRead); |
| 228 Expect.equals(0, input.available()); |
| 229 }; |
| 230 input.onClosed = () { |
| 231 Expect.equals(0, input.available()); |
| 232 Expect.isTrue(input.closed); |
| 233 input.close(); // This should be safe to call. |
| 234 |
| 235 Expect.equals(expectedLength, position); |
| 236 for (int i = 0; i < position; ++i) { |
| 237 Expect.equals(buffer[i % buffer.length], inputBuffer[i]); |
| 238 } |
| 239 |
| 240 Future testPipeDone = testPipe(file, buffer); |
| 241 |
| 242 Future futureDeleted = testPipeDone.chain((ignored) => file.delete()); |
| 243 futureDeleted.handleException((e) { |
| 244 print('Exception while deleting ReadWriteStreamLargeFile file'); |
| 245 print('Exception $e'); |
| 246 return false; // Throw exception further. |
| 247 }); |
| 248 futureDeleted.then((ignored) { |
| 249 asyncTestDone('testReadWriteStreamLargeFile: main test'); |
| 250 }); |
| 251 }; |
| 252 // Try a read again after handlers are set. |
| 253 bytesRead = input.readInto(inputBuffer); |
| 254 Expect.equals(0, bytesRead); |
| 255 Expect.equals(0, input.available()); |
| 256 Expect.isFalse(input.closed); |
| 257 }; |
| 258 } |
| 259 |
| 260 static Future testPipe(File file, buffer) { |
| 261 String outputFilename = '${file.name}_copy'; |
| 262 File outputFile = new File(outputFilename); |
| 263 InputStream input = file.openInputStream(); |
| 264 OutputStream output = outputFile.openOutputStream(); |
| 265 input.pipe(output); |
| 266 Completer done = new Completer(); |
| 267 output.onClosed = () { |
| 268 InputStream copy = outputFile.openInputStream(); |
| 269 int position = 0; |
| 270 copy.onData = () { |
| 271 var data; |
| 272 while ((data = copy.read()) != null) { |
| 273 for (int value in data) { |
| 274 Expect.equals(buffer[position % buffer.length], value); |
| 275 position++; |
| 276 } |
| 277 } |
| 278 }; |
| 279 copy.onClosed = () { |
| 280 Expect.equals(2 * buffer.length, position); |
| 281 outputFile.delete().then((ignore) { done.complete(null); }); |
| 282 }; |
| 283 }; |
| 284 return done.future; |
| 285 } |
| 286 |
| 167 static void testRead() { | 287 static void testRead() { |
| 168 ReceivePort port = new ReceivePort(); | 288 ReceivePort port = new ReceivePort(); |
| 169 // Read a file and check part of it's contents. | 289 // Read a file and check part of it's contents. |
| 170 String filename = getFilename("bin/file_test.cc"); | 290 String filename = getFilename("bin/file_test.cc"); |
| 171 File file = new File(filename); | 291 File file = new File(filename); |
| 172 file.open(FileMode.READ).then((RandomAccessFile file) { | 292 file.open(FileMode.READ).then((RandomAccessFile file) { |
| 173 List<int> buffer = new List<int>(10); | 293 List<int> buffer = new List<int>(10); |
| 174 file.readList(buffer, 0, 5).then((bytes_read) { | 294 file.readList(buffer, 0, 5).then((bytes_read) { |
| 175 Expect.equals(5, bytes_read); | 295 Expect.equals(5, bytes_read); |
| 176 file.readList(buffer, 5, 5).then((bytes_read) { | 296 file.readList(buffer, 5, 5).then((bytes_read) { |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 // Tests stream exception handling after file was closed. | 843 // Tests stream exception handling after file was closed. |
| 724 static void testCloseExceptionStream() { | 844 static void testCloseExceptionStream() { |
| 725 asyncTestStarted(); | 845 asyncTestStarted(); |
| 726 List<int> buffer = new List<int>(42); | 846 List<int> buffer = new List<int>(42); |
| 727 File file = | 847 File file = |
| 728 new File(tempDirectory.path.concat("/out_close_exception_stream")); | 848 new File(tempDirectory.path.concat("/out_close_exception_stream")); |
| 729 file.createSync(); | 849 file.createSync(); |
| 730 InputStream input = file.openInputStream(); | 850 InputStream input = file.openInputStream(); |
| 731 input.onClosed = () { | 851 input.onClosed = () { |
| 732 Expect.isTrue(input.closed); | 852 Expect.isTrue(input.closed); |
| 733 Expect.isNull(input.readInto(buffer, 0, 12)); | 853 Expect.equals(0, input.readInto(buffer, 0, 12)); |
| 734 OutputStream output = file.openOutputStream(); | 854 OutputStream output = file.openOutputStream(); |
| 735 output.close(); | 855 output.close(); |
| 736 Expect.throws(() => output.writeFrom(buffer, 0, 12)); | 856 Expect.throws(() => output.writeFrom(buffer, 0, 12)); |
| 737 output.onClosed = () { | 857 output.onClosed = () { |
| 738 file.deleteSync(); | 858 file.deleteSync(); |
| 739 asyncTestDone("testCloseExceptionStream"); | 859 asyncTestDone("testCloseExceptionStream"); |
| 740 }; | 860 }; |
| 741 }; | 861 }; |
| 742 } | 862 } |
| 743 | 863 |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1075 testReadAsErrors(); | 1195 testReadAsErrors(); |
| 1076 testLastModified(); | 1196 testLastModified(); |
| 1077 testLastModifiedSync(); | 1197 testLastModifiedSync(); |
| 1078 | 1198 |
| 1079 createTempDirectory(() { | 1199 createTempDirectory(() { |
| 1080 testReadWrite(); | 1200 testReadWrite(); |
| 1081 testReadWriteSync(); | 1201 testReadWriteSync(); |
| 1082 testReadWriteStream(); | 1202 testReadWriteStream(); |
| 1083 testReadEmptyFileSync(); | 1203 testReadEmptyFileSync(); |
| 1084 testReadEmptyFile(); | 1204 testReadEmptyFile(); |
| 1205 testReadWriteStreamLargeFile(); |
| 1085 testTruncate(); | 1206 testTruncate(); |
| 1086 testTruncateSync(); | 1207 testTruncateSync(); |
| 1087 testCloseException(); | 1208 testCloseException(); |
| 1088 testCloseExceptionStream(); | 1209 testCloseExceptionStream(); |
| 1089 testBufferOutOfBoundsException(); | 1210 testBufferOutOfBoundsException(); |
| 1090 testAppend(); | 1211 testAppend(); |
| 1091 testAppendSync(); | 1212 testAppendSync(); |
| 1092 testWriteAppend(); | 1213 testWriteAppend(); |
| 1093 testOutputStreamWriteAppend(); | 1214 testOutputStreamWriteAppend(); |
| 1094 testOutputStreamWriteString(); | 1215 testOutputStreamWriteString(); |
| 1095 testWriteVariousLists(); | 1216 testWriteVariousLists(); |
| 1096 testDirectory(); | 1217 testDirectory(); |
| 1097 testDirectorySync(); | 1218 testDirectorySync(); |
| 1098 }); | 1219 }); |
| 1099 } | 1220 } |
| 1100 } | 1221 } |
| 1101 | 1222 |
| 1102 main() { | 1223 main() { |
| 1103 FileTest.testMain(); | 1224 FileTest.testMain(); |
| 1104 } | 1225 } |
| OLD | NEW |