OLD | NEW |
---|---|
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 | 7 |
8 class FileTest { | 8 class FileTest { |
9 static Directory tempDirectory; | 9 static Directory tempDirectory; |
10 static int numLiveAsyncTests = 0; | 10 static int numLiveAsyncTests = 0; |
(...skipping 10 matching lines...) Expand all Loading... | |
21 tempDirectory = new Directory(''); | 21 tempDirectory = new Directory(''); |
22 tempDirectory.createTempHandler = doNext; | 22 tempDirectory.createTempHandler = doNext; |
23 tempDirectory.createTemp(); | 23 tempDirectory.createTemp(); |
24 } | 24 } |
25 | 25 |
26 static void deleteTempDirectory() { | 26 static void deleteTempDirectory() { |
27 tempDirectory.deleteSync(); | 27 tempDirectory.deleteSync(); |
28 } | 28 } |
29 | 29 |
30 // Test for file read functionality. | 30 // Test for file read functionality. |
31 static int testReadStream() { | 31 static void testReadStream() { |
32 // Read a file and check part of it's contents. | 32 // Read a file and check part of it's contents. |
33 String filename = getFilename("bin/file_test.cc"); | 33 String filename = getFilename("bin/file_test.cc"); |
34 File file = new File(filename); | 34 File file = new File(filename); |
35 InputStream input = file.openInputStream(); | 35 InputStream input = file.openInputStream(); |
36 List<int> buffer = new List<int>(42); | 36 List<int> buffer = new List<int>(42); |
37 int bytesRead = input.readInto(buffer, 0, 12); | 37 int bytesRead = input.readInto(buffer, 0, 12); |
38 Expect.equals(12, bytesRead); | 38 Expect.equals(12, bytesRead); |
39 bytesRead = input.readInto(buffer, 12, 30); | 39 bytesRead = input.readInto(buffer, 12, 30); |
40 input.close(); | 40 input.close(); |
41 Expect.equals(30, bytesRead); | 41 Expect.equals(30, bytesRead); |
42 Expect.equals(47, buffer[0]); // represents '/' in the file. | 42 Expect.equals(47, buffer[0]); // represents '/' in the file. |
43 Expect.equals(47, buffer[1]); // represents '/' in the file. | 43 Expect.equals(47, buffer[1]); // represents '/' in the file. |
44 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 44 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
45 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 45 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
46 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 46 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
47 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 47 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
48 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 48 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
49 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 49 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
50 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 50 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
51 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 51 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
52 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 52 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
53 Expect.equals(116, buffer[11]); // represents 't' in the file. | 53 Expect.equals(116, buffer[11]); // represents 't' in the file. |
54 return 1; | |
55 } | 54 } |
56 | 55 |
57 // Test for file read and write functionality. | 56 // Test for file read and write functionality. |
58 static int testReadWriteStream() { | 57 static void testReadWriteStream() { |
58 // Read a file. | |
59 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 59 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
60 File file; | 60 File file; |
61 InputStream input; | 61 InputStream input; |
62 int bytesRead; | 62 int bytesRead; |
63 | 63 |
64 // Test reading all using readInto. | 64 // Test reading all using readInto. |
65 file = new File(inFilename); | 65 file = new File(inFilename); |
66 input = file.openInputStream(); | 66 input = file.openInputStream(); |
67 List<int> buffer1 = new List<int>(42); | 67 List<int> buffer1 = new List<int>(42); |
68 bytesRead = input.readInto(buffer1, 0, 42); | 68 bytesRead = input.readInto(buffer1, 0, 42); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 bytesRead = input.readInto(buffer2, 0, 42); | 109 bytesRead = input.readInto(buffer2, 0, 42); |
110 Expect.isTrue(input.closed); | 110 Expect.isTrue(input.closed); |
111 Expect.equals(42, bytesRead); | 111 Expect.equals(42, bytesRead); |
112 // Now compare the two buffers to check if they are identical. | 112 // Now compare the two buffers to check if they are identical. |
113 for (int i = 0; i < buffer1.length; i++) { | 113 for (int i = 0; i < buffer1.length; i++) { |
114 Expect.equals(buffer1[i], buffer2[i]); | 114 Expect.equals(buffer1[i], buffer2[i]); |
115 } | 115 } |
116 // Delete the output file. | 116 // Delete the output file. |
117 file.deleteSync(); | 117 file.deleteSync(); |
118 Expect.isFalse(file.existsSync()); | 118 Expect.isFalse(file.existsSync()); |
119 return 1; | |
120 } | 119 } |
121 | 120 |
122 static int testRead() { | 121 static void testRead() { |
123 // Read a file and check part of it's contents. | 122 // Read a file and check part of it's contents. |
124 String filename = getFilename("bin/file_test.cc"); | 123 String filename = getFilename("bin/file_test.cc"); |
125 File file = new File(filename); | 124 File file = new File(filename); |
126 file.errorHandler = (s) { | 125 file.errorHandler = (s) { |
127 Expect.fail("No errors expected"); | 126 Expect.fail("No errors expected"); |
128 }; | 127 }; |
129 file.openHandler = (RandomAccessFile file) { | 128 file.openHandler = (RandomAccessFile file) { |
130 List<int> buffer = new List<int>(10); | 129 List<int> buffer = new List<int>(10); |
131 file.readListHandler = (bytes_read) { | 130 file.readListHandler = (bytes_read) { |
132 Expect.equals(5, bytes_read); | 131 Expect.equals(5, bytes_read); |
133 file.readListHandler = (bytes_read) { | 132 file.readListHandler = (bytes_read) { |
134 Expect.equals(5, bytes_read); | 133 Expect.equals(5, bytes_read); |
135 Expect.equals(47, buffer[0]); // represents '/' in the file. | 134 Expect.equals(47, buffer[0]); // represents '/' in the file. |
136 Expect.equals(47, buffer[1]); // represents '/' in the file. | 135 Expect.equals(47, buffer[1]); // represents '/' in the file. |
137 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 136 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
138 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 137 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
139 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 138 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
140 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 139 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
141 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 140 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
142 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 141 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
143 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 142 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
144 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 143 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
145 file.close(); | 144 file.close(); |
146 }; | 145 }; |
147 file.readList(buffer, 5, 5); | 146 file.readList(buffer, 5, 5); |
148 }; | 147 }; |
149 file.readList(buffer, 0, 5); | 148 file.readList(buffer, 0, 5); |
150 }; | 149 }; |
151 file.open(); | 150 file.open(); |
152 return 1; | |
153 } | 151 } |
154 | 152 |
155 static int testReadSync() { | 153 static void testReadSync() { |
156 // Read a file and check part of it's contents. | 154 // Read a file and check part of it's contents. |
157 String filename = getFilename("bin/file_test.cc"); | 155 String filename = getFilename("bin/file_test.cc"); |
158 RandomAccessFile file = (new File(filename)).openSync(); | 156 RandomAccessFile file = (new File(filename)).openSync(); |
159 List<int> buffer = new List<int>(42); | 157 List<int> buffer = new List<int>(42); |
160 int bytes_read = 0; | 158 int bytes_read = 0; |
161 bytes_read = file.readListSync(buffer, 0, 12); | 159 bytes_read = file.readListSync(buffer, 0, 12); |
162 Expect.equals(12, bytes_read); | 160 Expect.equals(12, bytes_read); |
163 bytes_read = file.readListSync(buffer, 12, 30); | 161 bytes_read = file.readListSync(buffer, 12, 30); |
164 Expect.equals(30, bytes_read); | 162 Expect.equals(30, bytes_read); |
165 Expect.equals(47, buffer[0]); // represents '/' in the file. | 163 Expect.equals(47, buffer[0]); // represents '/' in the file. |
166 Expect.equals(47, buffer[1]); // represents '/' in the file. | 164 Expect.equals(47, buffer[1]); // represents '/' in the file. |
167 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 165 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
168 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 166 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
169 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 167 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
170 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 168 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
171 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 169 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
172 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 170 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
173 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 171 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
174 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 172 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
175 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 173 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
176 Expect.equals(116, buffer[11]); // represents 't' in the file. | 174 Expect.equals(116, buffer[11]); // represents 't' in the file. |
177 return 1; | |
178 } | 175 } |
179 | 176 |
180 // Test for file read and write functionality. | 177 // Test for file read and write functionality. |
181 static int testReadWrite() { | 178 static void testReadWrite() { |
182 // Read a file. | 179 // Read a file. |
183 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 180 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
184 File file = new File(inFilename); | 181 File file = new File(inFilename); |
185 file.errorHandler = (s) { | 182 file.errorHandler = (s) { |
186 Expect.fail("No errors expected"); | 183 Expect.fail("No errors expected"); |
187 }; | 184 }; |
188 file.openHandler = (RandomAccessFile openedFile) { | 185 file.openHandler = (RandomAccessFile openedFile) { |
189 List<int> buffer1 = new List<int>(42); | 186 List<int> buffer1 = new List<int>(42); |
190 openedFile.readListHandler = (bytes_read) { | 187 openedFile.readListHandler = (bytes_read) { |
191 Expect.equals(42, bytes_read); | 188 Expect.equals(42, bytes_read); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 openedFile.close(); | 231 openedFile.close(); |
235 }; | 232 }; |
236 openedFile.readList(buffer2, 0, 42); | 233 openedFile.readList(buffer2, 0, 42); |
237 }; | 234 }; |
238 file.open(); | 235 file.open(); |
239 }; | 236 }; |
240 openedFile.close(); | 237 openedFile.close(); |
241 }; | 238 }; |
242 openedFile.writeList(buffer1, 0, bytes_read); | 239 openedFile.writeList(buffer1, 0, bytes_read); |
243 }; | 240 }; |
244 file.open(true); | 241 file.open(FileMode.WRITE); |
245 }; | 242 }; |
246 file.fullPath(); | 243 file.fullPath(); |
247 }; | 244 }; |
248 file.create(); | 245 file.create(); |
249 }; | 246 }; |
250 openedFile.close(); | 247 openedFile.close(); |
251 }; | 248 }; |
252 openedFile.readList(buffer1, 0, 42); | 249 openedFile.readList(buffer1, 0, 42); |
253 }; | 250 }; |
254 asyncTestStarted(); | 251 asyncTestStarted(); |
255 file.open(); | 252 file.open(); |
256 return 1; | |
257 | |
258 } | 253 } |
259 | 254 |
260 static int testReadWriteSync() { | 255 static void testReadWriteSync() { |
261 // Read a file. | 256 // Read a file. |
262 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 257 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
263 RandomAccessFile file = (new File(inFilename)).openSync(); | 258 RandomAccessFile file = (new File(inFilename)).openSync(); |
264 List<int> buffer1 = new List<int>(42); | 259 List<int> buffer1 = new List<int>(42); |
265 int bytes_read = 0; | 260 int bytes_read = 0; |
266 int bytes_written = 0; | 261 int bytes_written = 0; |
267 bytes_read = file.readListSync(buffer1, 0, 42); | 262 bytes_read = file.readListSync(buffer1, 0, 42); |
268 Expect.equals(42, bytes_read); | 263 Expect.equals(42, bytes_read); |
269 file.closeSync(); | 264 file.closeSync(); |
270 // Write the contents of the file just read into another file. | 265 // Write the contents of the file just read into another file. |
271 String outFilename = tempDirectory.path + "/out_read_write_sync"; | 266 String outFilename = tempDirectory.path + "/out_read_write_sync"; |
272 File outFile = new File(outFilename); | 267 File outFile = new File(outFilename); |
273 outFile.createSync(); | 268 outFile.createSync(); |
274 String path = outFile.fullPathSync(); | 269 String path = outFile.fullPathSync(); |
275 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { | 270 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { |
276 Expect.fail("Not a full path"); | 271 Expect.fail("Not a full path"); |
277 } | 272 } |
278 Expect.isTrue(new File(path).existsSync()); | 273 Expect.isTrue(new File(path).existsSync()); |
279 RandomAccessFile openedFile = outFile.openSync(true); | 274 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE); |
280 openedFile.writeListSync(buffer1, 0, bytes_read); | 275 openedFile.writeListSync(buffer1, 0, bytes_read); |
281 openedFile.closeSync(); | 276 openedFile.closeSync(); |
282 // Now read the contents of the file just written. | 277 // Now read the contents of the file just written. |
283 List<int> buffer2 = new List<int>(bytes_read); | 278 List<int> buffer2 = new List<int>(bytes_read); |
284 openedFile = (new File(outFilename)).openSync(); | 279 openedFile = (new File(outFilename)).openSync(); |
285 bytes_read = openedFile.readListSync(buffer2, 0, 42); | 280 bytes_read = openedFile.readListSync(buffer2, 0, 42); |
286 Expect.equals(42, bytes_read); | 281 Expect.equals(42, bytes_read); |
287 openedFile.closeSync(); | 282 openedFile.closeSync(); |
288 // Now compare the two buffers to check if they are identical. | 283 // Now compare the two buffers to check if they are identical. |
289 Expect.equals(buffer1.length, buffer2.length); | 284 Expect.equals(buffer1.length, buffer2.length); |
290 for (int i = 0; i < buffer1.length; i++) { | 285 for (int i = 0; i < buffer1.length; i++) { |
291 Expect.equals(buffer1[i], buffer2[i]); | 286 Expect.equals(buffer1[i], buffer2[i]); |
292 } | 287 } |
293 // Delete the output file. | 288 // Delete the output file. |
294 outFile.deleteSync(); | 289 outFile.deleteSync(); |
295 Expect.isFalse(outFile.existsSync()); | 290 Expect.isFalse(outFile.existsSync()); |
296 return 1; | |
297 } | 291 } |
298 | 292 |
299 static int testReadEmptyFileSync() { | 293 static void testReadEmptyFileSync() { |
300 String fileName = tempDirectory.path + "/empty_file_sync"; | 294 String fileName = tempDirectory.path + "/empty_file_sync"; |
301 File file = new File(fileName); | 295 File file = new File(fileName); |
302 file.createSync(); | 296 file.createSync(); |
303 RandomAccessFile openedFile = file.openSync(); | 297 RandomAccessFile openedFile = file.openSync(); |
304 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); | 298 Expect.throws(() => openedFile.readByteSync(), (e) => e is FileIOException); |
305 return 1; | |
306 } | 299 } |
307 | 300 |
308 static int testReadEmptyFile() { | 301 static void testReadEmptyFile() { |
309 String fileName = tempDirectory.path + "/empty_file"; | 302 String fileName = tempDirectory.path + "/empty_file"; |
310 File file = new File(fileName); | 303 File file = new File(fileName); |
311 file.createHandler = () { | 304 file.createHandler = () { |
312 file.openHandler = (RandomAccessFile openedFile) { | 305 file.openHandler = (RandomAccessFile openedFile) { |
313 openedFile.readByteHandler = (int byte) { | 306 openedFile.readByteHandler = (int byte) { |
314 Expect.fail("Read byte from empty file"); | 307 Expect.fail("Read byte from empty file"); |
315 }; | 308 }; |
316 openedFile.errorHandler = (String err) { | 309 openedFile.errorHandler = (String err) { |
317 Expect.isTrue(err.indexOf("failed") != -1); | 310 Expect.isTrue(err.indexOf("failed") != -1); |
318 }; | 311 }; |
319 openedFile.readByte(); | 312 openedFile.readByte(); |
320 }; | 313 }; |
321 file.open(); | 314 file.open(); |
322 }; | 315 }; |
323 asyncTestStarted(); | 316 asyncTestStarted(); |
324 file.create(); | 317 file.create(); |
325 return 1; | |
326 } | 318 } |
327 | 319 |
328 // Test for file length functionality. | 320 // Test for file length functionality. |
329 static int testLength() { | 321 static void testLength() { |
330 String filename = getFilename("tests/vm/data/fixed_length_file"); | 322 String filename = getFilename("tests/vm/data/fixed_length_file"); |
331 RandomAccessFile input = (new File(filename)).openSync(); | 323 RandomAccessFile input = (new File(filename)).openSync(); |
332 input.errorHandler = (s) { | 324 input.errorHandler = (s) { |
333 Expect.fail("No errors expected"); | 325 Expect.fail("No errors expected"); |
334 }; | 326 }; |
335 input.lengthHandler = (length) { | 327 input.lengthHandler = (length) { |
336 Expect.equals(42, length); | 328 Expect.equals(42, length); |
337 input.close(); | 329 input.close(); |
338 }; | 330 }; |
339 input.length(); | 331 input.length(); |
340 return 1; | |
341 } | 332 } |
342 | 333 |
343 static int testLengthSync() { | 334 static void testLengthSync() { |
344 String filename = getFilename("tests/vm/data/fixed_length_file"); | 335 String filename = getFilename("tests/vm/data/fixed_length_file"); |
345 RandomAccessFile input = (new File(filename)).openSync(); | 336 RandomAccessFile input = (new File(filename)).openSync(); |
346 Expect.equals(42, input.lengthSync()); | 337 Expect.equals(42, input.lengthSync()); |
347 input.closeSync(); | 338 input.closeSync(); |
348 return 1; | |
349 } | 339 } |
350 | 340 |
351 // Test for file position functionality. | 341 // Test for file position functionality. |
352 static int testPosition() { | 342 static void testPosition() { |
353 String filename = getFilename("tests/vm/data/fixed_length_file"); | 343 String filename = getFilename("tests/vm/data/fixed_length_file"); |
354 RandomAccessFile input = (new File(filename)).openSync(); | 344 RandomAccessFile input = (new File(filename)).openSync(); |
355 input.errorHandler = (s) { | 345 input.errorHandler = (s) { |
356 Expect.fail("No errors expected"); | 346 Expect.fail("No errors expected"); |
357 }; | 347 }; |
358 input.positionHandler = (position) { | 348 input.positionHandler = (position) { |
359 Expect.equals(0, position); | 349 Expect.equals(0, position); |
360 List<int> buffer = new List<int>(100); | 350 List<int> buffer = new List<int>(100); |
361 input.readListHandler = (bytes_read) { | 351 input.readListHandler = (bytes_read) { |
362 input.positionHandler = (position) { | 352 input.positionHandler = (position) { |
(...skipping 11 matching lines...) Expand all Loading... | |
374 input.setPosition(8); | 364 input.setPosition(8); |
375 }; | 365 }; |
376 }; | 366 }; |
377 input.readList(buffer, 12, 6); | 367 input.readList(buffer, 12, 6); |
378 }; | 368 }; |
379 input.position(); | 369 input.position(); |
380 }; | 370 }; |
381 input.readList(buffer, 0, 12); | 371 input.readList(buffer, 0, 12); |
382 }; | 372 }; |
383 input.position(); | 373 input.position(); |
384 return 1; | |
385 } | 374 } |
386 | 375 |
387 static int testPositionSync() { | 376 static void testPositionSync() { |
388 String filename = getFilename("tests/vm/data/fixed_length_file"); | 377 String filename = getFilename("tests/vm/data/fixed_length_file"); |
389 RandomAccessFile input = (new File(filename)).openSync(); | 378 RandomAccessFile input = (new File(filename)).openSync(); |
390 Expect.equals(0, input.positionSync()); | 379 Expect.equals(0, input.positionSync()); |
391 List<int> buffer = new List<int>(100); | 380 List<int> buffer = new List<int>(100); |
392 input.readListSync(buffer, 0, 12); | 381 input.readListSync(buffer, 0, 12); |
393 Expect.equals(12, input.positionSync()); | 382 Expect.equals(12, input.positionSync()); |
394 input.readListSync(buffer, 12, 6); | 383 input.readListSync(buffer, 12, 6); |
395 Expect.equals(18, input.positionSync()); | 384 Expect.equals(18, input.positionSync()); |
396 input.setPositionSync(8); | 385 input.setPositionSync(8); |
397 Expect.equals(8, input.positionSync()); | 386 Expect.equals(8, input.positionSync()); |
398 input.closeSync(); | 387 input.closeSync(); |
399 return 1; | |
400 } | 388 } |
401 | 389 |
402 static int testTruncate() { | 390 static void testTruncate() { |
403 File file = new File(tempDirectory.path + "/out_truncate"); | 391 File file = new File(tempDirectory.path + "/out_truncate"); |
404 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 392 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
405 file.errorHandler = (error) { | 393 file.errorHandler = (error) { |
406 Expect.fail("testTruncate: No errors expected"); | 394 Expect.fail("testTruncate: No errors expected"); |
407 }; | 395 }; |
408 file.openHandler = (RandomAccessFile openedFile) { | 396 file.openHandler = (RandomAccessFile openedFile) { |
409 openedFile.noPendingWriteHandler = () { | 397 openedFile.noPendingWriteHandler = () { |
410 openedFile.lengthHandler = (length) { | 398 openedFile.lengthHandler = (length) { |
411 Expect.equals(10, length); | 399 Expect.equals(10, length); |
412 openedFile.truncateHandler = () { | 400 openedFile.truncateHandler = () { |
(...skipping 13 matching lines...) Expand all Loading... | |
426 }; | 414 }; |
427 openedFile.length(); | 415 openedFile.length(); |
428 }; | 416 }; |
429 openedFile.truncate(5); | 417 openedFile.truncate(5); |
430 }; | 418 }; |
431 openedFile.length(); | 419 openedFile.length(); |
432 }; | 420 }; |
433 openedFile.writeList(buffer, 0, 10); | 421 openedFile.writeList(buffer, 0, 10); |
434 }; | 422 }; |
435 asyncTestStarted(); | 423 asyncTestStarted(); |
436 file.open(true); | 424 file.open(FileMode.WRITE); |
437 return 1; | |
438 } | 425 } |
439 | 426 |
440 static int testTruncateSync() { | 427 static void testTruncateSync() { |
441 File file = new File(tempDirectory.path + "/out_truncate_sync"); | 428 File file = new File(tempDirectory.path + "/out_truncate_sync"); |
442 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 429 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
443 RandomAccessFile openedFile = file.openSync(true); | 430 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
444 openedFile.writeListSync(buffer, 0, 10); | 431 openedFile.writeListSync(buffer, 0, 10); |
445 Expect.equals(10, openedFile.lengthSync()); | 432 Expect.equals(10, openedFile.lengthSync()); |
446 openedFile.truncateSync(5); | 433 openedFile.truncateSync(5); |
447 Expect.equals(5, openedFile.lengthSync()); | 434 Expect.equals(5, openedFile.lengthSync()); |
448 openedFile.closeSync(); | 435 openedFile.closeSync(); |
449 file.deleteSync(); | 436 file.deleteSync(); |
450 Expect.isFalse(file.existsSync()); | 437 Expect.isFalse(file.existsSync()); |
451 return 1; | |
452 } | 438 } |
453 | 439 |
454 // Tests exception handling after file was closed. | 440 // Tests exception handling after file was closed. |
455 static int testCloseException() { | 441 static void testCloseException() { |
456 bool exceptionCaught = false; | 442 bool exceptionCaught = false; |
457 bool wrongExceptionCaught = false; | 443 bool wrongExceptionCaught = false; |
458 File input = new File(tempDirectory.path + "/out_close_exception"); | 444 File input = new File(tempDirectory.path + "/out_close_exception"); |
459 RandomAccessFile openedFile = input.openSync(true); | 445 RandomAccessFile openedFile = input.openSync(FileMode.WRITE); |
460 openedFile.closeSync(); | 446 openedFile.closeSync(); |
461 try { | 447 try { |
462 openedFile.readByteSync(); | 448 openedFile.readByteSync(); |
463 } catch (FileIOException ex) { | 449 } catch (FileIOException ex) { |
464 exceptionCaught = true; | 450 exceptionCaught = true; |
465 } catch (Exception ex) { | 451 } catch (Exception ex) { |
466 wrongExceptionCaught = true; | 452 wrongExceptionCaught = true; |
467 } | 453 } |
468 Expect.equals(true, exceptionCaught); | 454 Expect.equals(true, exceptionCaught); |
469 Expect.equals(true, !wrongExceptionCaught); | 455 Expect.equals(true, !wrongExceptionCaught); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
533 try { | 519 try { |
534 openedFile.flushSync(); | 520 openedFile.flushSync(); |
535 } catch (FileIOException ex) { | 521 } catch (FileIOException ex) { |
536 exceptionCaught = true; | 522 exceptionCaught = true; |
537 } catch (Exception ex) { | 523 } catch (Exception ex) { |
538 wrongExceptionCaught = true; | 524 wrongExceptionCaught = true; |
539 } | 525 } |
540 Expect.equals(true, exceptionCaught); | 526 Expect.equals(true, exceptionCaught); |
541 Expect.equals(true, !wrongExceptionCaught); | 527 Expect.equals(true, !wrongExceptionCaught); |
542 input.deleteSync(); | 528 input.deleteSync(); |
543 return 1; | |
544 } | 529 } |
545 | 530 |
546 // Tests stream exception handling after file was closed. | 531 // Tests stream exception handling after file was closed. |
547 static int testCloseExceptionStream() { | 532 static void testCloseExceptionStream() { |
548 List<int> buffer = new List<int>(42); | 533 List<int> buffer = new List<int>(42); |
549 File file = new File(tempDirectory.path + "/out_close_exception_stream"); | 534 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
550 file.createSync(); | 535 file.createSync(); |
551 InputStream input = file.openInputStream(); | 536 InputStream input = file.openInputStream(); |
552 Expect.isTrue(input.closed); | 537 Expect.isTrue(input.closed); |
553 Expect.isNull(input.readInto(buffer, 0, 12)); | 538 Expect.isNull(input.readInto(buffer, 0, 12)); |
554 OutputStream output = file.openOutputStream(); | 539 OutputStream output = file.openOutputStream(); |
555 output.close(); | 540 output.close(); |
556 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), | 541 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), |
557 (e) => e is FileIOException); | 542 (e) => e is FileIOException); |
558 file.deleteSync(); | 543 file.deleteSync(); |
559 return 1; | |
560 } | 544 } |
561 | 545 |
562 // Tests buffer out of bounds exception. | 546 // Tests buffer out of bounds exception. |
563 static int testBufferOutOfBoundsException() { | 547 static void testBufferOutOfBoundsException() { |
564 bool exceptionCaught = false; | 548 bool exceptionCaught = false; |
565 bool wrongExceptionCaught = false; | 549 bool wrongExceptionCaught = false; |
566 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); | 550 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); |
567 RandomAccessFile openedFile = file.openSync(true); | 551 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); |
568 try { | 552 try { |
569 List<int> buffer = new List<int>(10); | 553 List<int> buffer = new List<int>(10); |
570 bool readDone = openedFile.readListSync(buffer, 0, 12); | 554 bool readDone = openedFile.readListSync(buffer, 0, 12); |
571 } catch (IndexOutOfRangeException ex) { | 555 } catch (IndexOutOfRangeException ex) { |
572 exceptionCaught = true; | 556 exceptionCaught = true; |
573 } catch (Exception ex) { | 557 } catch (Exception ex) { |
574 wrongExceptionCaught = true; | 558 wrongExceptionCaught = true; |
575 } | 559 } |
576 Expect.equals(true, exceptionCaught); | 560 Expect.equals(true, exceptionCaught); |
577 Expect.equals(true, !wrongExceptionCaught); | 561 Expect.equals(true, !wrongExceptionCaught); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
647 bool readDone = openedFile.writeListSync(buffer, 0, -1); | 631 bool readDone = openedFile.writeListSync(buffer, 0, -1); |
648 } catch (IndexOutOfRangeException ex) { | 632 } catch (IndexOutOfRangeException ex) { |
649 exceptionCaught = true; | 633 exceptionCaught = true; |
650 } catch (Exception ex) { | 634 } catch (Exception ex) { |
651 wrongExceptionCaught = true; | 635 wrongExceptionCaught = true; |
652 } | 636 } |
653 Expect.equals(true, exceptionCaught); | 637 Expect.equals(true, exceptionCaught); |
654 Expect.equals(true, !wrongExceptionCaught); | 638 Expect.equals(true, !wrongExceptionCaught); |
655 openedFile.closeSync(); | 639 openedFile.closeSync(); |
656 file.deleteSync(); | 640 file.deleteSync(); |
657 return 1; | |
658 } | 641 } |
659 | 642 |
660 static int testMixedSyncAndAsync() { | 643 static void testMixedSyncAndAsync() { |
661 var name = getFilename("tests/vm/data/fixed_length_file"); | 644 var name = getFilename("tests/vm/data/fixed_length_file"); |
662 var f = new File(name); | 645 var f = new File(name); |
663 f.errorHandler = (s) { | 646 f.errorHandler = (s) { |
664 Expect.fail("No errors expected"); | 647 Expect.fail("No errors expected"); |
665 }; | 648 }; |
666 f.existsHandler = (exists) { | 649 f.existsHandler = (exists) { |
667 try { | 650 try { |
668 f.existsSync(); | 651 f.existsSync(); |
669 Expect.fail("Expected exception"); | 652 Expect.fail("Expected exception"); |
670 } catch (var e) { | 653 } catch (var e) { |
671 Expect.isTrue(e is FileIOException); | 654 Expect.isTrue(e is FileIOException); |
672 } | 655 } |
673 }; | 656 }; |
674 f.exists(); | 657 f.exists(); |
675 return 1; | 658 } |
659 | |
660 // Test that opens the same file for writing twice to test that | |
Bill Hesse
2012/01/03 18:28:47
I wouldn't say "writing twice" but "for writing, t
Mads Ager (google)
2012/01/04 10:27:52
Done.
| |
661 // the file is not truncated when opened for writing. | |
Bill Hesse
2012/01/03 18:28:47
for appending.
Mads Ager (google)
2012/01/04 10:27:52
Done.
| |
662 static void testAppend() { | |
663 var file = new File('${tempDirectory.path}/out_append'); | |
664 file.openHandler = (openedFile) { | |
665 openedFile.noPendingWriteHandler = () { | |
666 openedFile.closeHandler = () { | |
667 file.openHandler = (openedFile) { | |
668 openedFile.lengthHandler = (length) { | |
669 Expect.equals(4, length); | |
670 openedFile.setPositionHandler = () { | |
671 openedFile.noPendingWriteHandler = () { | |
672 openedFile.lengthHandler = (length) { | |
673 Expect.equals(8, length); | |
674 openedFile.closeHandler = () { | |
675 file.deleteHandler = () { | |
676 file.existsHandler = (exists) { | |
677 Expect.isFalse(exists); | |
678 asyncTestDone(); | |
679 }; | |
680 file.exists(); | |
681 }; | |
682 file.delete(); | |
683 }; | |
684 openedFile.close(); | |
685 }; | |
686 openedFile.length(); | |
687 }; | |
688 openedFile.writeString("asdf"); | |
689 }; | |
690 openedFile.setPosition(4); | |
691 }; | |
692 openedFile.length(); | |
693 }; | |
694 file.open(FileMode.APPEND); | |
695 }; | |
696 openedFile.close(); | |
697 }; | |
698 openedFile.writeString("asdf"); | |
699 }; | |
700 asyncTestStarted(); | |
701 file.open(FileMode.WRITE); | |
702 } | |
703 | |
704 static void testAppendSync() { | |
705 var file = new File('${tempDirectory.path}/out_append_sync'); | |
706 var openedFile = file.openSync(FileMode.WRITE); | |
707 openedFile.writeStringSync("asdf"); | |
708 Expect.equals(4, openedFile.lengthSync()); | |
709 openedFile.closeSync(); | |
710 openedFile = file.openSync(FileMode.WRITE); | |
711 openedFile.setPositionSync(4); | |
712 openedFile.writeStringSync("asdf"); | |
713 Expect.equals(8, openedFile.lengthSync()); | |
714 openedFile.closeSync(); | |
715 file.deleteSync(); | |
716 Expect.isFalse(file.existsSync()); | |
676 } | 717 } |
677 | 718 |
678 // Helper method to be able to run the test from the runtime | 719 // Helper method to be able to run the test from the runtime |
679 // directory, or the top directory. | 720 // directory, or the top directory. |
680 static String getFilename(String path) => | 721 static String getFilename(String path) => |
681 new File(path).existsSync() ? path : 'runtime/' + path; | 722 new File(path).existsSync() ? path : 'runtime/' + path; |
682 | 723 |
683 // Main test entrypoint. | 724 // Main test entrypoint. |
684 static testMain() { | 725 static testMain() { |
685 Expect.equals(1, testRead()); | 726 testRead(); |
686 Expect.equals(1, testReadSync()); | 727 testReadSync(); |
687 Expect.equals(1, testReadStream()); | 728 testReadStream(); |
688 Expect.equals(1, testLength()); | 729 testLength(); |
689 Expect.equals(1, testLengthSync()); | 730 testLengthSync(); |
690 Expect.equals(1, testPosition()); | 731 testPosition(); |
691 Expect.equals(1, testPositionSync()); | 732 testPositionSync(); |
692 Expect.equals(1, testMixedSyncAndAsync()); | 733 testMixedSyncAndAsync(); |
693 asyncTestStarted(); | 734 asyncTestStarted(); |
694 createTempDirectory(() { | 735 createTempDirectory(() { |
695 Expect.equals(1, testReadWrite()); | 736 testReadWrite(); |
696 Expect.equals(1, testReadWriteSync()); | 737 testReadWriteSync(); |
697 Expect.equals(1, testReadWriteStream()); | 738 testReadWriteStream(); |
698 Expect.equals(1, testReadEmptyFileSync()); | 739 testReadEmptyFileSync(); |
699 Expect.equals(1, testReadEmptyFile()); | 740 testReadEmptyFile(); |
700 Expect.equals(1, testTruncate()); | 741 testTruncate(); |
701 Expect.equals(1, testTruncateSync()); | 742 testTruncateSync(); |
702 Expect.equals(1, testCloseException()); | 743 testCloseException(); |
703 Expect.equals(1, testCloseExceptionStream()); | 744 testCloseExceptionStream(); |
704 Expect.equals(1, testBufferOutOfBoundsException()); | 745 testBufferOutOfBoundsException(); |
746 testAppend(); | |
747 testAppendSync(); | |
705 asyncTestDone(); | 748 asyncTestDone(); |
706 }); | 749 }); |
707 } | 750 } |
708 } | 751 } |
709 | 752 |
710 main() { | 753 main() { |
711 FileTest.testMain(); | 754 FileTest.testMain(); |
712 } | 755 } |
OLD | NEW |