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

Side by Side Diff: tests/standalone/src/FileTest.dart

Issue 9029001: Add close to input stream and cleanup socket and streams (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years 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) 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 14 matching lines...) Expand all
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 int 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 FileInputStream 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; 54 return 1;
55 } 55 }
56 56
57 // Test for file read and write functionality. 57 // Test for file read and write functionality.
58 static int testReadWriteStream() { 58 static int testReadWriteStream() {
59 // Read a file. 59 // Read a file.
60 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 60 String inFilename = getFilename("tests/vm/data/fixed_length_file");
61 File file = new File(inFilename); 61 File file = new File(inFilename);
62 FileInputStream input = file.openInputStream(); 62 InputStream input = file.openInputStream();
63 List<int> buffer1 = new List<int>(42); 63 List<int> buffer1 = new List<int>(42);
64 int bytesRead = input.readInto(buffer1, 0, 42); 64 int bytesRead = input.readInto(buffer1, 0, 42);
65 Expect.equals(42, bytesRead); 65 Expect.equals(42, bytesRead);
66 input.close(); 66 Expect.isTrue(input.closed);
67 // Write the contents of the file just read into another file. 67 // Write the contents of the file just read into another file.
68 String outFilename = tempDirectory.path + "/out_read_write_stream"; 68 String outFilename = tempDirectory.path + "/out_read_write_stream";
69 file = new File(outFilename); 69 file = new File(outFilename);
70 OutputStream output = file.openOutputStream(); 70 OutputStream output = file.openOutputStream();
71 bool writeDone = output.writeFrom(buffer1, 0, 42); 71 bool writeDone = output.writeFrom(buffer1, 0, 42);
72 Expect.equals(true, writeDone); 72 Expect.equals(true, writeDone);
73 output.close(); 73 output.close();
74 // Now read the contents of the file just written. 74 // Now read the contents of the file just written.
75 List<int> buffer2 = new List<int>(42); 75 List<int> buffer2 = new List<int>(42);
76 file = new File(outFilename); 76 file = new File(outFilename);
77 input = file.openInputStream(); 77 input = file.openInputStream();
78 bytesRead = input.readInto(buffer2, 0, 42); 78 bytesRead = input.readInto(buffer2, 0, 42);
79 input.close(); 79 Expect.isTrue(input.closed);
80 //input.close();
Mads Ager (google) 2011/12/23 08:35:40 Code in comment.
Søren Gjesse 2012/01/02 11:51:17 Done.
80 Expect.equals(42, bytesRead); 81 Expect.equals(42, bytesRead);
81 // Now compare the two buffers to check if they are identical. 82 // Now compare the two buffers to check if they are identical.
82 for (int i = 0; i < buffer1.length; i++) { 83 for (int i = 0; i < buffer1.length; i++) {
83 Expect.equals(buffer1[i], buffer2[i]); 84 Expect.equals(buffer1[i], buffer2[i]);
84 } 85 }
85 // Delete the output file. 86 // Delete the output file.
86 file.deleteSync(); 87 file.deleteSync();
87 Expect.isFalse(file.existsSync()); 88 Expect.isFalse(file.existsSync());
88 return 1; 89 return 1;
89 } 90 }
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 wrongExceptionCaught = true; 479 wrongExceptionCaught = true;
479 } 480 }
480 Expect.equals(true, exceptionCaught); 481 Expect.equals(true, exceptionCaught);
481 Expect.equals(true, !wrongExceptionCaught); 482 Expect.equals(true, !wrongExceptionCaught);
482 input.deleteSync(); 483 input.deleteSync();
483 return 1; 484 return 1;
484 } 485 }
485 486
486 // Tests stream exception handling after file was closed. 487 // Tests stream exception handling after file was closed.
487 static int testCloseExceptionStream() { 488 static int testCloseExceptionStream() {
488 bool exceptionCaught = false; 489 List<int> buffer = new List<int>(42);
489 bool wrongExceptionCaught = false;
490 File file = new File(tempDirectory.path + "/out_close_exception_stream"); 490 File file = new File(tempDirectory.path + "/out_close_exception_stream");
491 file.createSync(); 491 file.createSync();
492 FileInputStream input = file.openInputStream(); 492 InputStream input = file.openInputStream();
493 input.close(); 493 Expect.isTrue(input.closed);
494 try { 494 Expect.throws(( ) => input.readInto(buffer, 0, 12),
495 List<int> buffer = new List<int>(42); 495 (e) => e is FileIOException);
496 input.readInto(buffer, 0, 12);
497 } catch (FileIOException ex) {
498 exceptionCaught = true;
499 } catch (Exception ex) {
500 wrongExceptionCaught = true;
501 }
502 Expect.equals(true, exceptionCaught);
503 Expect.equals(true, !wrongExceptionCaught);
504 exceptionCaught = false;
505 OutputStream output = file.openOutputStream(); 496 OutputStream output = file.openOutputStream();
506 output.close(); 497 output.close();
507 try { 498 Expect.throws(( ) => output.writeFrom(buffer, 0, 12),
508 List<int> buffer = new List<int>(42); 499 (e) => e is FileIOException);
509 bool readDone = output.writeFrom(buffer, 0, 12);
510 } catch (FileIOException ex) {
511 exceptionCaught = true;
512 } catch (Exception ex) {
513 wrongExceptionCaught = true;
514 }
515 Expect.equals(true, exceptionCaught);
516 Expect.equals(true, !wrongExceptionCaught);
517 file.deleteSync(); 500 file.deleteSync();
518 return 1; 501 return 1;
519 } 502 }
520 503
521 // Tests buffer out of bounds exception. 504 // Tests buffer out of bounds exception.
522 static int testBufferOutOfBoundsException() { 505 static int testBufferOutOfBoundsException() {
523 bool exceptionCaught = false; 506 bool exceptionCaught = false;
524 bool wrongExceptionCaught = false; 507 bool wrongExceptionCaught = false;
525 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); 508 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
526 RandomAccessFile openedFile = file.openSync(true); 509 RandomAccessFile openedFile = file.openSync(true);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 Expect.equals(1, testCloseExceptionStream()); 643 Expect.equals(1, testCloseExceptionStream());
661 Expect.equals(1, testBufferOutOfBoundsException()); 644 Expect.equals(1, testBufferOutOfBoundsException());
662 asyncTestDone(); 645 asyncTestDone();
663 }); 646 });
664 } 647 }
665 } 648 }
666 649
667 main() { 650 main() {
668 FileTest.testMain(); 651 FileTest.testMain();
669 } 652 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698