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

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

Issue 9474004: Make FileInputStream and FileOutputStream actually asynchronous. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove accidental edit Created 8 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) 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 8
9 class MyListOfOneElement implements List { 9 class MyListOfOneElement implements List {
10 int _value; 10 int _value;
(...skipping 26 matching lines...) Expand all
37 static void deleteTempDirectory() { 37 static void deleteTempDirectory() {
38 tempDirectory.deleteSync(recursive: true); 38 tempDirectory.deleteSync(recursive: true);
39 } 39 }
40 40
41 // Test for file read functionality. 41 // Test for file read functionality.
42 static void testReadStream() { 42 static void testReadStream() {
43 // Read a file and check part of it's contents. 43 // Read a file and check part of it's contents.
44 String filename = getFilename("bin/file_test.cc"); 44 String filename = getFilename("bin/file_test.cc");
45 File file = new File(filename); 45 File file = new File(filename);
46 InputStream input = file.openInputStreamSync(); 46 InputStream input = file.openInputStreamSync();
47 List<int> buffer = new List<int>(42); 47 input.dataHandler = () {
48 int bytesRead = input.readInto(buffer, 0, 12); 48 List<int> buffer = new List<int>(42);
49 Expect.equals(12, bytesRead); 49 int bytesRead = input.readInto(buffer, 0, 12);
50 bytesRead = input.readInto(buffer, 12, 30); 50 Expect.equals(12, bytesRead);
51 input.close(); 51 bytesRead = input.readInto(buffer, 12, 30);
52 Expect.equals(30, bytesRead); 52 input.close();
53 Expect.equals(47, buffer[0]); // represents '/' in the file. 53 Expect.equals(30, bytesRead);
54 Expect.equals(47, buffer[1]); // represents '/' in the file. 54 Expect.equals(47, buffer[0]); // represents '/' in the file.
55 Expect.equals(32, buffer[2]); // represents ' ' in the file. 55 Expect.equals(47, buffer[1]); // represents '/' in the file.
56 Expect.equals(67, buffer[3]); // represents 'C' in the file. 56 Expect.equals(32, buffer[2]); // represents ' ' in the file.
57 Expect.equals(111, buffer[4]); // represents 'o' in the file. 57 Expect.equals(67, buffer[3]); // represents 'C' in the file.
58 Expect.equals(112, buffer[5]); // represents 'p' in the file. 58 Expect.equals(111, buffer[4]); // represents 'o' in the file.
59 Expect.equals(121, buffer[6]); // represents 'y' in the file. 59 Expect.equals(112, buffer[5]); // represents 'p' in the file.
60 Expect.equals(114, buffer[7]); // represents 'r' in the file. 60 Expect.equals(121, buffer[6]); // represents 'y' in the file.
61 Expect.equals(105, buffer[8]); // represents 'i' in the file. 61 Expect.equals(114, buffer[7]); // represents 'r' in the file.
62 Expect.equals(103, buffer[9]); // represents 'g' in the file. 62 Expect.equals(105, buffer[8]); // represents 'i' in the file.
63 Expect.equals(104, buffer[10]); // represents 'h' in the file. 63 Expect.equals(103, buffer[9]); // represents 'g' in the file.
64 Expect.equals(116, buffer[11]); // represents 't' in the file. 64 Expect.equals(104, buffer[10]); // represents 'h' in the file.
65 Expect.equals(116, buffer[11]); // represents 't' in the file.
66 };
65 } 67 }
66 68
67 // Test for file read and write functionality. 69 // Test for file read and write functionality.
68 static void testReadWriteStream() { 70 static void testReadWriteStream() {
71 asyncTestStarted();
72
69 // Read a file. 73 // Read a file.
70 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 74 String inFilename = getFilename("tests/vm/data/fixed_length_file");
71 File file; 75 File file;
72 InputStream input; 76 InputStream input;
73 int bytesRead; 77 int bytesRead;
74 78
75 // Test reading all using readInto. 79 // Test reading all using readInto.
76 file = new File(inFilename); 80 file = new File(inFilename);
77 input = file.openInputStreamSync(); 81 input = file.openInputStreamSync();
78 List<int> buffer1 = new List<int>(42); 82 input.dataHandler = () {
79 bytesRead = input.readInto(buffer1, 0, 42); 83 List<int> buffer1 = new List<int>(42);
80 Expect.equals(42, bytesRead); 84 bytesRead = input.readInto(buffer1, 0, 42);
81 Expect.isTrue(input.closed); 85 Expect.equals(42, bytesRead);
86 Expect.isTrue(input.closed);
82 87
83 // Test reading all using readInto and read. 88 // Test reading all using readInto and read.
84 file = new File(inFilename); 89 file = new File(inFilename);
85 input = file.openInputStreamSync(); 90 input = file.openInputStreamSync();
86 bytesRead = input.readInto(buffer1, 0, 21); 91 input.dataHandler = () {
87 Expect.equals(21, bytesRead); 92 bytesRead = input.readInto(buffer1, 0, 21);
88 buffer1 = input.read(); 93 Expect.equals(21, bytesRead);
89 Expect.equals(21, buffer1.length); 94 buffer1 = input.read();
90 Expect.isTrue(input.closed); 95 Expect.equals(21, buffer1.length);
96 Expect.isTrue(input.closed);
91 97
92 // Test reading all using read and readInto. 98 // Test reading all using read and readInto.
93 file = new File(inFilename); 99 file = new File(inFilename);
94 input = file.openInputStreamSync(); 100 input = file.openInputStreamSync();
95 buffer1 = input.read(21); 101 input.dataHandler = () {
96 Expect.equals(21, buffer1.length); 102 buffer1 = input.read(21);
97 bytesRead = input.readInto(buffer1, 0, 21); 103 Expect.equals(21, buffer1.length);
98 Expect.equals(21, bytesRead); 104 bytesRead = input.readInto(buffer1, 0, 21);
99 Expect.isTrue(input.closed); 105 Expect.equals(21, bytesRead);
106 Expect.isTrue(input.closed);
100 107
101 // Test reading all using read. 108 // Test reading all using read.
102 file = new File(inFilename); 109 file = new File(inFilename);
103 input = file.openInputStreamSync(); 110 input = file.openInputStreamSync();
104 buffer1 = input.read(); 111 input.dataHandler = () {
105 Expect.equals(42, buffer1.length); 112 buffer1 = input.read();
106 Expect.isTrue(input.closed); 113 Expect.equals(42, buffer1.length);
114 Expect.isTrue(input.closed);
107 115
108 // Write the contents of the file just read into another file. 116 // Write the contents of the file just read into another file.
109 String outFilename = tempDirectory.path + "/out_read_write_stream"; 117 String outFilename = tempDirectory.path + "/out_read_write_stream";
110 file = new File(outFilename); 118 file = new File(outFilename);
111 OutputStream output = file.openOutputStreamSync(); 119 OutputStream output = file.openOutputStreamSync();
112 bool writeDone = output.writeFrom(buffer1, 0, 42); 120 bool writeDone = output.writeFrom(buffer1, 0, 42);
113 Expect.equals(true, writeDone); 121 Expect.equals(false, writeDone);
114 output.close(); 122 output.noPendingWriteHandler = () {
123 output.close();
115 124
116 // Now read the contents of the file just written. 125 // Now read the contents of the file just written.
117 List<int> buffer2 = new List<int>(42); 126 List<int> buffer2 = new List<int>(42);
118 file = new File(outFilename); 127 file = new File(outFilename);
119 input = file.openInputStreamSync(); 128 input = file.openInputStreamSync();
120 bytesRead = input.readInto(buffer2, 0, 42); 129 input.dataHandler = () {
121 Expect.isTrue(input.closed); 130 bytesRead = input.readInto(buffer2, 0, 42);
122 Expect.equals(42, bytesRead); 131 Expect.isTrue(input.closed);
123 // Now compare the two buffers to check if they are identical. 132 Expect.equals(42, bytesRead);
124 for (int i = 0; i < buffer1.length; i++) { 133 // Now compare the two buffers to check if they are identical.
125 Expect.equals(buffer1[i], buffer2[i]); 134 for (int i = 0; i < buffer1.length; i++) {
126 } 135 Expect.equals(buffer1[i], buffer2[i]);
127 // Delete the output file. 136 }
128 file.deleteSync(); 137 // Delete the output file.
129 Expect.isFalse(file.existsSync()); 138 file.deleteSync();
139 Expect.isFalse(file.existsSync());
140 asyncTestDone("testReadWriteStream");
141 };
142 };
143 };
144 };
145 };
146 };
130 } 147 }
131 148
132 static void testRead() { 149 static void testRead() {
133 // Read a file and check part of it's contents. 150 // Read a file and check part of it's contents.
134 String filename = getFilename("bin/file_test.cc"); 151 String filename = getFilename("bin/file_test.cc");
135 File file = new File(filename); 152 File file = new File(filename);
136 file.errorHandler = (s) { 153 file.errorHandler = (s) {
137 Expect.fail("No errors expected : $s"); 154 Expect.fail("No errors expected : $s");
138 }; 155 };
139 file.openHandler = (RandomAccessFile file) { 156 file.openHandler = (RandomAccessFile file) {
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 } 314 }
298 315
299 static void testOutputStreamWriteAppend() { 316 static void testOutputStreamWriteAppend() {
300 String content = "foobar"; 317 String content = "foobar";
301 String filename = tempDirectory.path + "/outstream_write_append"; 318 String filename = tempDirectory.path + "/outstream_write_append";
302 File file = new File(filename); 319 File file = new File(filename);
303 file.createSync(); 320 file.createSync();
304 List<int> buffer = content.charCodes(); 321 List<int> buffer = content.charCodes();
305 OutputStream outStream = file.openOutputStreamSync(); 322 OutputStream outStream = file.openOutputStreamSync();
306 outStream.write(buffer); 323 outStream.write(buffer);
307 outStream.close(); 324 outStream.noPendingWriteHandler = () {
308 File file2 = new File(filename); 325 outStream.close();
309 OutputStream appendingOutput = file2.openOutputStreamSync(FileMode.APPEND); 326 File file2 = new File(filename);
310 appendingOutput.write(buffer); 327 OutputStream appendingOutput = file2.openOutputStreamSync(FileMode.APPEND) ;
Søren Gjesse 2012/02/28 07:40:25 Long line.
Mads Ager (google) 2012/02/28 08:21:00 Done.
311 appendingOutput.close(); 328 appendingOutput.write(buffer);
312 File file3 = new File(filename); 329 appendingOutput.noPendingWriteHandler = () {
313 file3.openHandler = (RandomAccessFile openedFile) { 330 appendingOutput.close();
314 openedFile.lengthHandler = (int length) { 331 File file3 = new File(filename);
315 Expect.equals(content.length * 2, length); 332 file3.openHandler = (RandomAccessFile openedFile) {
316 openedFile.closeHandler = () { 333 openedFile.lengthHandler = (int length) {
317 file3.deleteHandler = () { 334 Expect.equals(content.length * 2, length);
318 asyncTestDone("testOutputStreamWriteAppend"); 335 openedFile.closeHandler = () {
336 file3.deleteHandler = () {
337 asyncTestDone("testOutputStreamWriteAppend");
338 };
339 file3.delete();
340 };
341 openedFile.close();
319 }; 342 };
320 file3.delete(); 343 openedFile.length();
321 }; 344 };
322 openedFile.close(); 345 file3.open();
323 }; 346 };
324 openedFile.length();
325 }; 347 };
326 file3.open();
327 asyncTestStarted(); 348 asyncTestStarted();
328 } 349 }
329 350
330 351
331 static void testReadWriteSync() { 352 static void testReadWriteSync() {
332 // Read a file. 353 // Read a file.
333 String inFilename = getFilename("tests/vm/data/fixed_length_file"); 354 String inFilename = getFilename("tests/vm/data/fixed_length_file");
334 RandomAccessFile file = (new File(inFilename)).openSync(); 355 RandomAccessFile file = (new File(inFilename)).openSync();
335 List<int> buffer1 = new List<int>(42); 356 List<int> buffer1 = new List<int>(42);
336 int bytes_read = 0; 357 int bytes_read = 0;
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 Expect.equals(true, !wrongExceptionCaught); 755 Expect.equals(true, !wrongExceptionCaught);
735 input.deleteSync(); 756 input.deleteSync();
736 } 757 }
737 758
738 // Tests stream exception handling after file was closed. 759 // Tests stream exception handling after file was closed.
739 static void testCloseExceptionStream() { 760 static void testCloseExceptionStream() {
740 List<int> buffer = new List<int>(42); 761 List<int> buffer = new List<int>(42);
741 File file = new File(tempDirectory.path + "/out_close_exception_stream"); 762 File file = new File(tempDirectory.path + "/out_close_exception_stream");
742 file.createSync(); 763 file.createSync();
743 InputStream input = file.openInputStreamSync(); 764 InputStream input = file.openInputStreamSync();
744 Expect.isTrue(input.closed); 765 input.closeHandler = () {
745 Expect.isNull(input.readInto(buffer, 0, 12)); 766 Expect.isTrue(input.closed);
746 OutputStream output = file.openOutputStreamSync(); 767 Expect.isNull(input.readInto(buffer, 0, 12));
747 output.close(); 768 OutputStream output = file.openOutputStreamSync();
748 Expect.throws(( ) => output.writeFrom(buffer, 0, 12), 769 output.close();
749 (e) => e is FileIOException); 770 Expect.throws(() => output.writeFrom(buffer, 0, 12));
750 file.deleteSync(); 771 file.deleteSync();
772 };
751 } 773 }
752 774
753 // Tests buffer out of bounds exception. 775 // Tests buffer out of bounds exception.
754 static void testBufferOutOfBoundsException() { 776 static void testBufferOutOfBoundsException() {
755 bool exceptionCaught = false; 777 bool exceptionCaught = false;
756 bool wrongExceptionCaught = false; 778 bool wrongExceptionCaught = false;
757 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); 779 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
758 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); 780 RandomAccessFile openedFile = file.openSync(FileMode.WRITE);
759 try { 781 try {
760 List<int> buffer = new List<int>(10); 782 List<int> buffer = new List<int>(10);
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 var lines = new File(name).readAsLinesSync(); 1006 var lines = new File(name).readAsLinesSync();
985 Expect.equals(1, lines.length); 1007 Expect.equals(1, lines.length);
986 var line = lines[0]; 1008 var line = lines[0];
987 Expect.isTrue(line.endsWith("42 bytes.")); 1009 Expect.isTrue(line.endsWith("42 bytes."));
988 Expect.equals(42, line.length); 1010 Expect.equals(42, line.length);
989 name = getDataFilename("tests/standalone/src/readline_test1.dat"); 1011 name = getDataFilename("tests/standalone/src/readline_test1.dat");
990 lines = new File(name).readAsLinesSync(); 1012 lines = new File(name).readAsLinesSync();
991 Expect.equals(10, lines.length); 1013 Expect.equals(10, lines.length);
992 } 1014 }
993 1015
1016
994 static void testReadAsErrors() { 1017 static void testReadAsErrors() {
1018 var port = new ReceivePort.singleShot();
1019 port.receive((message, _) {
1020 Expect.equals(1, message);
1021 });
995 var f = new File('.'); 1022 var f = new File('.');
996 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException); 1023 Expect.throws(f.readAsBytesSync, (e) => e is FileIOException);
997 Expect.throws(f.readAsTextSync, (e) => e is FileIOException); 1024 Expect.throws(f.readAsTextSync, (e) => e is FileIOException);
998 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException); 1025 Expect.throws(f.readAsLinesSync, (e) => e is FileIOException);
999 // TODO(ager): Add tests of errors in readAsBytes when input 1026 f.readAsBytes();
1000 // streams are truely async. 1027 f.errorHandler = (e) {
1028 f.readAsText();
1029 f.errorHandler = (e) {
1030 f.readAsLines();
1031 f.errorHandler = (e) {
1032 port.toSendPort().send(1);
1033 };
1034 };
1035 };
1001 } 1036 }
1002 1037
1003 // Test that opens the same file for writing then for appending to test 1038 // Test that opens the same file for writing then for appending to test
1004 // that the file is not truncated when opened for appending. 1039 // that the file is not truncated when opened for appending.
1005 static void testAppend() { 1040 static void testAppend() {
1006 var file = new File('${tempDirectory.path}/out_append'); 1041 var file = new File('${tempDirectory.path}/out_append');
1007 file.openHandler = (openedFile) { 1042 file.openHandler = (openedFile) {
1008 openedFile.noPendingWriteHandler = () { 1043 openedFile.noPendingWriteHandler = () {
1009 openedFile.closeHandler = () { 1044 openedFile.closeHandler = () {
1010 file.openHandler = (openedFile) { 1045 file.openHandler = (openedFile) {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 testWriteVariousLists(); 1140 testWriteVariousLists();
1106 testDirectory(); 1141 testDirectory();
1107 testDirectorySync(); 1142 testDirectorySync();
1108 }); 1143 });
1109 } 1144 }
1110 } 1145 }
1111 1146
1112 main() { 1147 main() {
1113 FileTest.testMain(); 1148 FileTest.testMain();
1114 } 1149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698