Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: tests/standalone/io/file_test.dart

Issue 12441003: Rename String.concat to operator+. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 'dart:async'; 7 import 'dart:async';
8 import 'dart:io'; 8 import 'dart:io';
9 import 'dart:isolate'; 9 import 'dart:isolate';
10 10
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 var file1 = new File(inFilename); 81 var file1 = new File(inFilename);
82 List<int> buffer = new List<int>(); 82 List<int> buffer = new List<int>();
83 file1.openRead().listen( 83 file1.openRead().listen(
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 + "/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.writeBytes(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);
(...skipping 17 matching lines...) Expand all
119 // Test for file stream buffered handling of large files. 119 // Test for file stream buffered handling of large files.
120 static void testReadWriteStreamLargeFile() { 120 static void testReadWriteStreamLargeFile() {
121 asyncTestStarted(); 121 asyncTestStarted();
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 + "/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.writeBytes(buffer); 132 output.writeBytes(buffer);
133 output.writeBytes(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.
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 static void testReadWrite() { 261 static void testReadWrite() {
262 // Read a file. 262 // Read a file.
263 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 263 String inFilename = getFilename("tests/vm/data/fixed_length_file");
264 final File file = new File(inFilename); 264 final File file = new File(inFilename);
265 file.open(FileMode.READ).then((openedFile) { 265 file.open(FileMode.READ).then((openedFile) {
266 List<int> buffer1 = new List<int>(42); 266 List<int> buffer1 = new List<int>(42);
267 openedFile.readList(buffer1, 0, 42).then((bytes_read) { 267 openedFile.readList(buffer1, 0, 42).then((bytes_read) {
268 Expect.equals(42, bytes_read); 268 Expect.equals(42, bytes_read);
269 openedFile.close().then((ignore) { 269 openedFile.close().then((ignore) {
270 // Write the contents of the file just read into another file. 270 // Write the contents of the file just read into another file.
271 String outFilename = tempDirectory.path.concat("/out_read_write"); 271 String outFilename = tempDirectory.path + "/out_read_write";
272 final File file2 = new File(outFilename); 272 final File file2 = new File(outFilename);
273 file2.create().then((ignore) { 273 file2.create().then((ignore) {
274 file2.fullPath().then((s) { 274 file2.fullPath().then((s) {
275 Expect.isTrue(new File(s).existsSync()); 275 Expect.isTrue(new File(s).existsSync());
276 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { 276 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
277 Expect.fail("Not a full path"); 277 Expect.fail("Not a full path");
278 } 278 }
279 file2.open(FileMode.WRITE).then((openedFile2) { 279 file2.open(FileMode.WRITE).then((openedFile2) {
280 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) { 280 openedFile2.writeList(buffer1, 0, bytes_read).then((ignore) {
281 openedFile2.close().then((ignore) { 281 openedFile2.close().then((ignore) {
(...skipping 26 matching lines...) Expand all
308 }); 308 });
309 }); 309 });
310 }); 310 });
311 }); 311 });
312 }); 312 });
313 asyncTestStarted(); 313 asyncTestStarted();
314 } 314 }
315 315
316 static void testWriteAppend() { 316 static void testWriteAppend() {
317 String content = "foobar"; 317 String content = "foobar";
318 String filename = tempDirectory.path.concat("/write_append"); 318 String filename = tempDirectory.path + "/write_append";
319 File file = new File(filename); 319 File file = new File(filename);
320 file.createSync(); 320 file.createSync();
321 Expect.isTrue(new File(filename).existsSync()); 321 Expect.isTrue(new File(filename).existsSync());
322 List<int> buffer = content.codeUnits; 322 List<int> buffer = content.codeUnits;
323 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 323 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
324 openedFile.writeListSync(buffer, 0, buffer.length); 324 openedFile.writeListSync(buffer, 0, buffer.length);
325 openedFile.closeSync(); 325 openedFile.closeSync();
326 // Reopen the file in write mode to ensure that we overwrite the content. 326 // Reopen the file in write mode to ensure that we overwrite the content.
327 openedFile = (new File(filename)).openSync(FileMode.WRITE); 327 openedFile = (new File(filename)).openSync(FileMode.WRITE);
328 openedFile.writeListSync(buffer, 0, buffer.length); 328 openedFile.writeListSync(buffer, 0, buffer.length);
329 Expect.equals(content.length, openedFile.lengthSync()); 329 Expect.equals(content.length, openedFile.lengthSync());
330 openedFile.closeSync(); 330 openedFile.closeSync();
331 // Open the file in append mode and ensure that we do not overwrite 331 // Open the file in append mode and ensure that we do not overwrite
332 // the existing content. 332 // the existing content.
333 openedFile = (new File(filename)).openSync(FileMode.APPEND); 333 openedFile = (new File(filename)).openSync(FileMode.APPEND);
334 openedFile.writeListSync(buffer, 0, buffer.length); 334 openedFile.writeListSync(buffer, 0, buffer.length);
335 Expect.equals(content.length * 2, openedFile.lengthSync()); 335 Expect.equals(content.length * 2, openedFile.lengthSync());
336 openedFile.closeSync(); 336 openedFile.closeSync();
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 + "/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.writeBytes(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(mode: FileMode.APPEND); 351 var appendingOutput = file2.openWrite(mode: FileMode.APPEND);
352 appendingOutput.writeBytes(buffer); 352 appendingOutput.writeBytes(buffer);
(...skipping 11 matching lines...) Expand all
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 + "/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.write("abcdABCD"); 379 output.write("abcdABCD");
380 output.encoding = Encoding.UTF_8; 380 output.encoding = Encoding.UTF_8;
381 output.write("abcdABCD"); 381 output.write("abcdABCD");
382 output.encoding = Encoding.ISO_8859_1; 382 output.encoding = Encoding.ISO_8859_1;
383 output.write("abcdABCD"); 383 output.write("abcdABCD");
384 output.encoding = Encoding.ASCII; 384 output.encoding = Encoding.ASCII;
(...skipping 16 matching lines...) Expand all
401 // Read a file. 401 // Read a file.
402 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 402 String inFilename = getFilename("tests/vm/data/fixed_length_file");
403 RandomAccessFile file = (new File(inFilename)).openSync(); 403 RandomAccessFile file = (new File(inFilename)).openSync();
404 List<int> buffer1 = new List<int>(42); 404 List<int> buffer1 = new List<int>(42);
405 int bytes_read = 0; 405 int bytes_read = 0;
406 int bytes_written = 0; 406 int bytes_written = 0;
407 bytes_read = file.readListSync(buffer1, 0, 42); 407 bytes_read = file.readListSync(buffer1, 0, 42);
408 Expect.equals(42, bytes_read); 408 Expect.equals(42, bytes_read);
409 file.closeSync(); 409 file.closeSync();
410 // Write the contents of the file just read into another file. 410 // Write the contents of the file just read into another file.
411 String outFilename = tempDirectory.path.concat("/out_read_write_sync"); 411 String outFilename = tempDirectory.path + "/out_read_write_sync";
412 File outFile = new File(outFilename); 412 File outFile = new File(outFilename);
413 outFile.createSync(); 413 outFile.createSync();
414 String path = outFile.fullPathSync(); 414 String path = outFile.fullPathSync();
415 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { 415 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') {
416 Expect.fail("Not a full path"); 416 Expect.fail("Not a full path");
417 } 417 }
418 Expect.isTrue(new File(path).existsSync()); 418 Expect.isTrue(new File(path).existsSync());
419 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE); 419 RandomAccessFile openedFile = outFile.openSync(FileMode.WRITE);
420 openedFile.writeListSync(buffer1, 0, bytes_read); 420 openedFile.writeListSync(buffer1, 0, bytes_read);
421 openedFile.closeSync(); 421 openedFile.closeSync();
422 // Now read the contents of the file just written. 422 // Now read the contents of the file just written.
423 List<int> buffer2 = new List<int>(bytes_read); 423 List<int> buffer2 = new List<int>(bytes_read);
424 openedFile = (new File(outFilename)).openSync(); 424 openedFile = (new File(outFilename)).openSync();
425 bytes_read = openedFile.readListSync(buffer2, 0, 42); 425 bytes_read = openedFile.readListSync(buffer2, 0, 42);
426 Expect.equals(42, bytes_read); 426 Expect.equals(42, bytes_read);
427 openedFile.closeSync(); 427 openedFile.closeSync();
428 // Now compare the two buffers to check if they are identical. 428 // Now compare the two buffers to check if they are identical.
429 Expect.equals(buffer1.length, buffer2.length); 429 Expect.equals(buffer1.length, buffer2.length);
430 for (int i = 0; i < buffer1.length; i++) { 430 for (int i = 0; i < buffer1.length; i++) {
431 Expect.equals(buffer1[i], buffer2[i]); 431 Expect.equals(buffer1[i], buffer2[i]);
432 } 432 }
433 // Delete the output file. 433 // Delete the output file.
434 outFile.deleteSync(); 434 outFile.deleteSync();
435 Expect.isFalse(outFile.existsSync()); 435 Expect.isFalse(outFile.existsSync());
436 } 436 }
437 437
438 static void testReadEmptyFileSync() { 438 static void testReadEmptyFileSync() {
439 String fileName = tempDirectory.path.concat("/empty_file_sync"); 439 String fileName = tempDirectory.path + "/empty_file_sync";
440 File file = new File(fileName); 440 File file = new File(fileName);
441 file.createSync(); 441 file.createSync();
442 RandomAccessFile openedFile = file.openSync(); 442 RandomAccessFile openedFile = file.openSync();
443 Expect.equals(-1, openedFile.readByteSync()); 443 Expect.equals(-1, openedFile.readByteSync());
444 openedFile.closeSync(); 444 openedFile.closeSync();
445 file.deleteSync(); 445 file.deleteSync();
446 } 446 }
447 447
448 static void testReadEmptyFile() { 448 static void testReadEmptyFile() {
449 String fileName = tempDirectory.path.concat("/empty_file"); 449 String fileName = tempDirectory.path + "/empty_file";
450 File file = new File(fileName); 450 File file = new File(fileName);
451 asyncTestStarted(); 451 asyncTestStarted();
452 file.create().then((ignore) { 452 file.create().then((ignore) {
453 file.open(FileMode.READ).then((RandomAccessFile openedFile) { 453 file.open(FileMode.READ).then((RandomAccessFile openedFile) {
454 var readByteFuture = openedFile.readByte(); 454 var readByteFuture = openedFile.readByte();
455 readByteFuture.then((int byte) { 455 readByteFuture.then((int byte) {
456 Expect.equals(-1, byte); 456 Expect.equals(-1, byte);
457 openedFile.close().then((ignore) { 457 openedFile.close().then((ignore) {
458 asyncTestDone("testReadEmptyFile"); 458 asyncTestDone("testReadEmptyFile");
459 }); 459 });
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 input.readListSync(buffer, 0, 12); 620 input.readListSync(buffer, 0, 12);
621 Expect.equals(12, input.positionSync()); 621 Expect.equals(12, input.positionSync());
622 input.readListSync(buffer, 12, 6); 622 input.readListSync(buffer, 12, 6);
623 Expect.equals(18, input.positionSync()); 623 Expect.equals(18, input.positionSync());
624 input.setPositionSync(8); 624 input.setPositionSync(8);
625 Expect.equals(8, input.positionSync()); 625 Expect.equals(8, input.positionSync());
626 input.closeSync(); 626 input.closeSync();
627 } 627 }
628 628
629 static void testTruncate() { 629 static void testTruncate() {
630 File file = new File(tempDirectory.path.concat("/out_truncate")); 630 File file = new File(tempDirectory.path + "/out_truncate");
631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 631 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
632 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) { 632 file.open(FileMode.WRITE).then((RandomAccessFile openedFile) {
633 openedFile.writeList(buffer, 0, 10).then((ignore) { 633 openedFile.writeList(buffer, 0, 10).then((ignore) {
634 openedFile.length().then((length) { 634 openedFile.length().then((length) {
635 Expect.equals(10, length); 635 Expect.equals(10, length);
636 openedFile.truncate(5).then((ignore) { 636 openedFile.truncate(5).then((ignore) {
637 openedFile.length().then((length) { 637 openedFile.length().then((length) {
638 Expect.equals(5, length); 638 Expect.equals(5, length);
639 openedFile.close().then((ignore) { 639 openedFile.close().then((ignore) {
640 file.delete().then((ignore) { 640 file.delete().then((ignore) {
641 file.exists().then((exists) { 641 file.exists().then((exists) {
642 Expect.isFalse(exists); 642 Expect.isFalse(exists);
643 asyncTestDone("testTruncate"); 643 asyncTestDone("testTruncate");
644 }); 644 });
645 }); 645 });
646 }); 646 });
647 }); 647 });
648 }); 648 });
649 }); 649 });
650 }); 650 });
651 }); 651 });
652 asyncTestStarted(); 652 asyncTestStarted();
653 } 653 }
654 654
655 static void testTruncateSync() { 655 static void testTruncateSync() {
656 File file = new File(tempDirectory.path.concat("/out_truncate_sync")); 656 File file = new File(tempDirectory.path + "/out_truncate_sync");
657 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; 657 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
658 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 658 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
659 openedFile.writeListSync(buffer, 0, 10); 659 openedFile.writeListSync(buffer, 0, 10);
660 Expect.equals(10, openedFile.lengthSync()); 660 Expect.equals(10, openedFile.lengthSync());
661 openedFile.truncateSync(5); 661 openedFile.truncateSync(5);
662 Expect.equals(5, openedFile.lengthSync()); 662 Expect.equals(5, openedFile.lengthSync());
663 openedFile.closeSync(); 663 openedFile.closeSync();
664 file.deleteSync(); 664 file.deleteSync();
665 Expect.isFalse(file.existsSync()); 665 Expect.isFalse(file.existsSync());
666 } 666 }
667 667
668 // Tests exception handling after file was closed. 668 // Tests exception handling after file was closed.
669 static void testCloseException() { 669 static void testCloseException() {
670 bool exceptionCaught = false; 670 bool exceptionCaught = false;
671 bool wrongExceptionCaught = false; 671 bool wrongExceptionCaught = false;
672 File input = new File(tempDirectory.path.concat("/out_close_exception")); 672 File input = new File(tempDirectory.path + "/out_close_exception");
673 RandomAccessFile openedFile = input.openSync(FileMode.WRITE); 673 RandomAccessFile openedFile = input.openSync(FileMode.WRITE);
674 openedFile.closeSync(); 674 openedFile.closeSync();
675 try { 675 try {
676 openedFile.readByteSync(); 676 openedFile.readByteSync();
677 } on FileIOException catch (ex) { 677 } on FileIOException catch (ex) {
678 exceptionCaught = true; 678 exceptionCaught = true;
679 } on Exception catch (ex) { 679 } on Exception catch (ex) {
680 wrongExceptionCaught = true; 680 wrongExceptionCaught = true;
681 } 681 }
682 Expect.equals(true, exceptionCaught); 682 Expect.equals(true, exceptionCaught);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 Expect.equals(true, exceptionCaught); 754 Expect.equals(true, exceptionCaught);
755 Expect.equals(true, !wrongExceptionCaught); 755 Expect.equals(true, !wrongExceptionCaught);
756 input.deleteSync(); 756 input.deleteSync();
757 } 757 }
758 758
759 // Tests stream exception handling after file was closed. 759 // Tests stream exception handling after file was closed.
760 static void testCloseExceptionStream() { 760 static void testCloseExceptionStream() {
761 asyncTestStarted(); 761 asyncTestStarted();
762 List<int> buffer = new List<int>(42); 762 List<int> buffer = new List<int>(42);
763 File file = 763 File file =
764 new File(tempDirectory.path.concat("/out_close_exception_stream")); 764 new File(tempDirectory.path + "/out_close_exception_stream");
765 file.createSync(); 765 file.createSync();
766 var output = file.openWrite(); 766 var output = file.openWrite();
767 output.close(); 767 output.close();
768 Expect.throws(() => output.writeBytes(buffer)); 768 Expect.throws(() => output.writeBytes(buffer));
769 output.done.then((_) { 769 output.done.then((_) {
770 file.deleteSync(); 770 file.deleteSync();
771 asyncTestDone("testCloseExceptionStream"); 771 asyncTestDone("testCloseExceptionStream");
772 }); 772 });
773 } 773 }
774 774
775 // Tests buffer out of bounds exception. 775 // Tests buffer out of bounds exception.
776 static void testBufferOutOfBoundsException() { 776 static void testBufferOutOfBoundsException() {
777 bool exceptionCaught = false; 777 bool exceptionCaught = false;
778 bool wrongExceptionCaught = false; 778 bool wrongExceptionCaught = false;
779 File file = 779 File file =
780 new File(tempDirectory.path.concat("/out_buffer_out_of_bounds")); 780 new File(tempDirectory.path + "/out_buffer_out_of_bounds");
781 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 781 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
782 try { 782 try {
783 List<int> buffer = new List<int>(10); 783 List<int> buffer = new List<int>(10);
784 openedFile.readListSync(buffer, 0, 12); 784 openedFile.readListSync(buffer, 0, 12);
785 } on RangeError catch (ex) { 785 } on RangeError catch (ex) {
786 exceptionCaught = true; 786 exceptionCaught = true;
787 } on Exception catch (ex) { 787 } on Exception catch (ex) {
788 wrongExceptionCaught = true; 788 wrongExceptionCaught = true;
789 } 789 }
790 Expect.equals(true, exceptionCaught); 790 Expect.equals(true, exceptionCaught);
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
1236 testDirectorySync(); 1236 testDirectorySync();
1237 testWriteStringUtf8(); 1237 testWriteStringUtf8();
1238 testWriteStringUtf8Sync(); 1238 testWriteStringUtf8Sync();
1239 }); 1239 });
1240 } 1240 }
1241 } 1241 }
1242 1242
1243 main() { 1243 main() {
1244 FileTest.testMain(); 1244 FileTest.testMain();
1245 } 1245 }
OLDNEW
« no previous file with comments | « tests/standalone/io/file_invalid_arguments_test.dart ('k') | tests/standalone/io/pipe_server_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698