Chromium Code Reviews| 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() { | |
|
Anders Johnsen
2012/06/13 12:51:31
We should also test the case of using pipe:
Input
| |
| 169 asyncTestStarted(); | |
| 170 | |
| 171 String filename = | |
| 172 tempDirectory.path.concat("/out_read_write_stream_large_file"); | |
|
Anders Johnsen
2012/06/13 12:51:31
What about using Options.executable?
Bill Hesse
2012/06/13 14:18:46
This also tests outputStream, and fits in with the
| |
| 173 File file = new File(filename); | |
| 174 OutputStream output = file.openOutputStream(); | |
| 175 List<int> buffer = new List<int>(100000); | |
| 176 for (var i = 0; i < buffer.length; ++i) { | |
| 177 buffer[i] = i % 25 + 97; // The letters a through y. | |
|
Anders Johnsen
2012/06/13 12:51:31
I don't think we should restrict it to normal char
Bill Hesse
2012/06/13 14:18:46
Done.
| |
| 178 } | |
| 179 bool writeDone = output.writeFrom(buffer, 0, 20000); | |
| 180 Expect.equals(false, writeDone); | |
|
Anders Johnsen
2012/06/13 12:51:31
We can not assume this.
Bill Hesse
2012/06/13 14:18:46
I guess not. Though it is true in this case.
| |
| 181 output.onNoPendingWrites = () { | |
| 182 output.writeFrom(buffer, 20000, 60000); | |
| 183 output.writeFrom(buffer, 80000, 20000); | |
| 184 output.onNoPendingWrites = () { | |
| 185 output.writeFrom(buffer, 0, 0); | |
| 186 output.writeFrom(buffer, 0, 0); | |
| 187 output.writeFrom(buffer, 0, 100000); | |
| 188 output.close(); | |
| 189 }; | |
| 190 }; | |
| 191 output.onClosed = () { | |
| 192 InputStream input = file.openInputStream(); | |
| 193 int position = 0; | |
| 194 final int expectedLength = 200000; | |
| 195 // Start an independent asynchronous check on the length. | |
| 196 asyncTestStarted(); | |
| 197 file.length().then((len) { | |
| 198 Expect.equals(expectedLength, len); | |
| 199 asyncTestDone('testReadWriteStreamLargeFile: length check'); | |
| 200 }); | |
| 201 | |
| 202 List<int> inputBuffer = new List<int>(expectedLength + 100000); | |
| 203 // Immediate read should read 0 bytes. | |
| 204 Expect.equals(0, input.available()); | |
|
Anders Johnsen
2012/06/13 12:51:31
The tests here, and below, could be moved to a iso
Bill Hesse
2012/06/13 14:18:46
It is really not a natural test, "All read but not
| |
| 205 Expect.equals(false, input.closed); | |
| 206 int bytesRead = input.readInto(inputBuffer); | |
| 207 Expect.equals(0, bytesRead); | |
| 208 Expect.equals(0, input.available()); | |
| 209 Expect.isFalse(input.closed); | |
| 210 input.onError = (e) { | |
| 211 print('Error handler called on input in testReadWriteStreamLargeFile'); | |
| 212 print('with error $e'); | |
| 213 throw e; | |
| 214 }; | |
| 215 input.onData = () { | |
| 216 Expect.isFalse(input.closed); | |
| 217 bytesRead = input.readInto(inputBuffer, offset: position, | |
| 218 len: inputBuffer.length - position); | |
| 219 position += bytesRead; | |
| 220 // The buffer is large enough to hold all available data. | |
| 221 // So there should be no data left to read. | |
| 222 Expect.equals(0, input.available()); | |
| 223 bytesRead = input.readInto(inputBuffer, offset: position, | |
| 224 len: expectedLength - position); | |
| 225 Expect.equals(0, bytesRead); | |
| 226 Expect.equals(0, input.available()); | |
| 227 Expect.isFalse(input.closed); | |
| 228 }; | |
| 229 input.onClosed = () { | |
| 230 Expect.equals(0, input.available()); | |
| 231 Expect.isTrue(input.closed); | |
| 232 input.close(); // This should be safe to call. | |
| 233 | |
| 234 Expect.equals(expectedLength, position); | |
| 235 for (int i = 0; i < position; ++i) { | |
| 236 Expect.equals(buffer[i % buffer.length], inputBuffer[i]); | |
| 237 } | |
| 238 | |
| 239 Future futureDeleted = file.delete(); | |
| 240 futureDeleted.handleException((e) { | |
| 241 print('Exception while deleting ReadWriteStreamLargeFile file'); | |
| 242 print('Exception $e'); | |
| 243 return false; // Throw exception further. | |
| 244 }); | |
| 245 futureDeleted.then((ignored) { | |
| 246 asyncTestDone('testReadWriteStreamLargeFile: main test'); | |
| 247 }); | |
| 248 }; | |
| 249 // Try a read again after handlers are set. | |
| 250 bytesRead = input.readInto(inputBuffer); | |
| 251 Expect.equals(0, bytesRead); | |
| 252 Expect.equals(0, input.available()); | |
| 253 Expect.isFalse(input.closed); | |
| 254 }; | |
| 255 } | |
| 256 | |
| 167 static void testRead() { | 257 static void testRead() { |
| 168 ReceivePort port = new ReceivePort(); | 258 ReceivePort port = new ReceivePort(); |
| 169 // Read a file and check part of it's contents. | 259 // Read a file and check part of it's contents. |
| 170 String filename = getFilename("bin/file_test.cc"); | 260 String filename = getFilename("bin/file_test.cc"); |
| 171 File file = new File(filename); | 261 File file = new File(filename); |
| 172 file.open(FileMode.READ).then((RandomAccessFile file) { | 262 file.open(FileMode.READ).then((RandomAccessFile file) { |
| 173 List<int> buffer = new List<int>(10); | 263 List<int> buffer = new List<int>(10); |
| 174 file.readList(buffer, 0, 5).then((bytes_read) { | 264 file.readList(buffer, 0, 5).then((bytes_read) { |
| 175 Expect.equals(5, bytes_read); | 265 Expect.equals(5, bytes_read); |
| 176 file.readList(buffer, 5, 5).then((bytes_read) { | 266 file.readList(buffer, 5, 5).then((bytes_read) { |
| (...skipping 898 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1075 testReadAsErrors(); | 1165 testReadAsErrors(); |
| 1076 testLastModified(); | 1166 testLastModified(); |
| 1077 testLastModifiedSync(); | 1167 testLastModifiedSync(); |
| 1078 | 1168 |
| 1079 createTempDirectory(() { | 1169 createTempDirectory(() { |
| 1080 testReadWrite(); | 1170 testReadWrite(); |
| 1081 testReadWriteSync(); | 1171 testReadWriteSync(); |
| 1082 testReadWriteStream(); | 1172 testReadWriteStream(); |
| 1083 testReadEmptyFileSync(); | 1173 testReadEmptyFileSync(); |
| 1084 testReadEmptyFile(); | 1174 testReadEmptyFile(); |
| 1175 testReadWriteStreamLargeFile(); | |
| 1085 testTruncate(); | 1176 testTruncate(); |
| 1086 testTruncateSync(); | 1177 testTruncateSync(); |
| 1087 testCloseException(); | 1178 testCloseException(); |
| 1088 testCloseExceptionStream(); | 1179 testCloseExceptionStream(); |
| 1089 testBufferOutOfBoundsException(); | 1180 testBufferOutOfBoundsException(); |
| 1090 testAppend(); | 1181 testAppend(); |
| 1091 testAppendSync(); | 1182 testAppendSync(); |
| 1092 testWriteAppend(); | 1183 testWriteAppend(); |
| 1093 testOutputStreamWriteAppend(); | 1184 testOutputStreamWriteAppend(); |
| 1094 testOutputStreamWriteString(); | 1185 testOutputStreamWriteString(); |
| 1095 testWriteVariousLists(); | 1186 testWriteVariousLists(); |
| 1096 testDirectory(); | 1187 testDirectory(); |
| 1097 testDirectorySync(); | 1188 testDirectorySync(); |
| 1098 }); | 1189 }); |
| 1099 } | 1190 } |
| 1100 } | 1191 } |
| 1101 | 1192 |
| 1102 main() { | 1193 main() { |
| 1103 FileTest.testMain(); | 1194 FileTest.testMain(); |
| 1104 } | 1195 } |
| OLD | NEW |