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 // Test for file read functionality. | 9 // Test for file read functionality. |
10 static int testReadStream() { | 10 static int testReadStream() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 for (int i = 0; i < buffer1.length; i++) { | 61 for (int i = 0; i < buffer1.length; i++) { |
62 Expect.equals(buffer1[i], buffer2[i]); | 62 Expect.equals(buffer1[i], buffer2[i]); |
63 } | 63 } |
64 return 1; | 64 return 1; |
65 } | 65 } |
66 | 66 |
67 static int testRead() { | 67 static int testRead() { |
68 // Read a file and check part of it's contents. | 68 // Read a file and check part of it's contents. |
69 String filename = getFilename("bin/file_test.cc"); | 69 String filename = getFilename("bin/file_test.cc"); |
70 File file = new File(filename); | 70 File file = new File(filename); |
71 file.openHandler = () { | |
72 List<int> buffer = new List<int>(10); | |
73 file.readListHandler = (bytes_read) { | |
74 Expect.equals(5, bytes_read); | |
75 file.readListHandler = (bytes_read) { | |
76 Expect.equals(5, bytes_read); | |
77 Expect.equals(47, buffer[0]); // represents '/' in the file. | |
78 Expect.equals(47, buffer[1]); // represents '/' in the file. | |
79 Expect.equals(32, buffer[2]); // represents ' ' in the file. | |
80 Expect.equals(67, buffer[3]); // represents 'C' in the file. | |
81 Expect.equals(111, buffer[4]); // represents 'o' in the file. | |
82 Expect.equals(112, buffer[5]); // represents 'p' in the file. | |
83 Expect.equals(121, buffer[6]); // represents 'y' in the file. | |
84 Expect.equals(114, buffer[7]); // represents 'r' in the file. | |
85 Expect.equals(105, buffer[8]); // represents 'i' in the file. | |
86 Expect.equals(103, buffer[9]); // represents 'g' in the file. | |
87 file.close(); | |
88 }; | |
89 file.readList(buffer, 5, 5); | |
90 }; | |
91 file.readList(buffer, 0, 5); | |
92 }; | |
93 file.open(); | |
94 return 1; | |
95 } | |
96 | |
97 static int testReadSync() { | |
98 // Read a file and check part of it's contents. | |
99 String filename = getFilename("bin/file_test.cc"); | |
100 File file = new File(filename); | |
71 file.openSync(); | 101 file.openSync(); |
72 List<int> buffer = new List<int>(42); | 102 List<int> buffer = new List<int>(42); |
73 int bytes_read = 0; | 103 int bytes_read = 0; |
74 bytes_read = file.readListSync(buffer, 0, 12); | 104 bytes_read = file.readListSync(buffer, 0, 12); |
75 Expect.equals(12, bytes_read); | 105 Expect.equals(12, bytes_read); |
76 bytes_read = file.readListSync(buffer, 12, 30); | 106 bytes_read = file.readListSync(buffer, 12, 30); |
77 Expect.equals(30, bytes_read); | 107 Expect.equals(30, bytes_read); |
78 Expect.equals(47, buffer[0]); // represents '/' in the file. | 108 Expect.equals(47, buffer[0]); // represents '/' in the file. |
79 Expect.equals(47, buffer[1]); // represents '/' in the file. | 109 Expect.equals(47, buffer[1]); // represents '/' in the file. |
80 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 110 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
81 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 111 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
82 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 112 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
83 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 113 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
84 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 114 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
85 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 115 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
86 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 116 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
87 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 117 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
88 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 118 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
89 Expect.equals(116, buffer[11]); // represents 't' in the file. | 119 Expect.equals(116, buffer[11]); // represents 't' in the file. |
90 return 1; | 120 return 1; |
91 } | 121 } |
92 | 122 |
93 // Test for file read and write functionality. | 123 // Test for file read and write functionality. |
94 static int testReadWrite() { | 124 static int testReadWrite() { |
Søren Gjesse
2011/11/01 13:22:49
As discussed offline add an error handler to ensur
Mads Ager (google)
2011/11/01 14:04:31
Done.
| |
95 // Read a file. | 125 // Read a file. |
96 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 126 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
97 File file = new File(inFilename); | 127 File file = new File(inFilename); |
128 file.openHandler = () { | |
129 List<int> buffer1 = new List<int>(42); | |
130 file.readListHandler = (bytes_read) { | |
131 Expect.equals(42, bytes_read); | |
132 file.closeHandler = () { | |
133 // Write the contents of the file just read into another file. | |
134 String outFilenameBase = | |
135 getFilename("tests/vm/data/fixed_length_file_out"); | |
136 file = new File(outFilenameBase); | |
137 file.openHandler = () { | |
138 file.noPendingWriteHandler = () { | |
139 file.closeHandler = () { | |
140 // Now read the contents of the file just written. | |
141 List<int> buffer2 = new List<int>(bytes_read); | |
142 file = new File(getFilename(outFilenameBase)); | |
143 file.openHandler = () { | |
144 file.readListHandler = (bytes_read) { | |
145 Expect.equals(42, bytes_read); | |
146 file.closeHandler = () { | |
147 // Now compare the two buffers to check if they | |
148 // are identical. | |
149 Expect.equals(buffer1.length, buffer2.length); | |
150 for (int i = 0; i < buffer1.length; i++) { | |
151 Expect.equals(buffer1[i], buffer2[i]); | |
152 } | |
153 }; | |
154 file.close(); | |
155 }; | |
156 file.readList(buffer2, 0, 42); | |
157 }; | |
158 file.open(); | |
159 }; | |
160 file.close(); | |
161 }; | |
162 file.writeList(buffer1, 0, bytes_read); | |
163 }; | |
164 file.open(true); | |
165 }; | |
166 file.close(); | |
167 }; | |
168 file.readList(buffer1, 0, 42); | |
169 }; | |
170 file.open(); | |
171 return 1; | |
172 | |
173 } | |
174 | |
175 static int testReadWriteSync() { | |
176 // Read a file. | |
177 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | |
178 File file = new File(inFilename); | |
98 file.openSync(); | 179 file.openSync(); |
99 List<int> buffer1 = new List<int>(42); | 180 List<int> buffer1 = new List<int>(42); |
100 int bytes_read = 0; | 181 int bytes_read = 0; |
101 int bytes_written = 0; | 182 int bytes_written = 0; |
102 bytes_read = file.readListSync(buffer1, 0, 42); | 183 bytes_read = file.readListSync(buffer1, 0, 42); |
103 Expect.equals(42, bytes_read); | 184 Expect.equals(42, bytes_read); |
104 file.closeSync(); | 185 file.closeSync(); |
105 // Write the contents of the file just read into another file. | 186 // Write the contents of the file just read into another file. |
106 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file_out"); | 187 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file_out"); |
107 file = new File(outFilenameBase); | 188 file = new File(outFilenameBase); |
(...skipping 13 matching lines...) Expand all Loading... | |
121 Expect.equals(buffer1[i], buffer2[i]); | 202 Expect.equals(buffer1[i], buffer2[i]); |
122 } | 203 } |
123 return 1; | 204 return 1; |
124 } | 205 } |
125 | 206 |
126 // Test for file length functionality. | 207 // Test for file length functionality. |
127 static int testLength() { | 208 static int testLength() { |
128 String filename = getFilename("tests/vm/data/fixed_length_file"); | 209 String filename = getFilename("tests/vm/data/fixed_length_file"); |
129 File input = new File(filename); | 210 File input = new File(filename); |
130 input.openSync(); | 211 input.openSync(); |
212 input.lengthHandler = (length) { | |
213 Expect.equals(42, length); | |
214 input.close(); | |
215 }; | |
216 input.length(); | |
217 return 1; | |
218 } | |
219 | |
220 static int testLengthSync() { | |
221 String filename = getFilename("tests/vm/data/fixed_length_file"); | |
222 File input = new File(filename); | |
223 input.openSync(); | |
131 Expect.equals(42, input.lengthSync()); | 224 Expect.equals(42, input.lengthSync()); |
132 input.closeSync(); | 225 input.closeSync(); |
133 return 1; | 226 return 1; |
134 } | 227 } |
135 | 228 |
136 // Test for file position functionality. | 229 // Test for file position functionality. |
137 static int testPosition() { | 230 static int testPosition() { |
138 String filename = getFilename("tests/vm/data/fixed_length_file"); | 231 String filename = getFilename("tests/vm/data/fixed_length_file"); |
139 File input = new File(filename); | 232 File input = new File(filename); |
140 input.openSync(); | 233 input.openSync(); |
234 input.positionHandler = (position) { | |
235 Expect.equals(0, position); | |
236 List<int> buffer = new List<int>(100); | |
237 input.readListHandler = (bytes_read) { | |
238 input.positionHandler = (position) { | |
239 Expect.equals(12, position); | |
240 input.readListHandler = (bytes_read) { | |
241 input.positionHandler = (position) { | |
242 Expect.equals(18, position); | |
243 input.close(); | |
244 }; | |
245 }; | |
246 input.readList(buffer, 12, 6); | |
247 }; | |
248 input.position(); | |
249 }; | |
250 input.readList(buffer, 0, 12); | |
251 }; | |
252 input.position(); | |
253 return 1; | |
254 } | |
255 | |
256 static int testPositionSync() { | |
257 String filename = getFilename("tests/vm/data/fixed_length_file"); | |
258 File input = new File(filename); | |
259 input.openSync(); | |
141 Expect.equals(0, input.positionSync()); | 260 Expect.equals(0, input.positionSync()); |
142 List<int> buffer = new List<int>(100); | 261 List<int> buffer = new List<int>(100); |
143 input.readListSync(buffer, 0, 12); | 262 input.readListSync(buffer, 0, 12); |
144 Expect.equals(12, input.positionSync()); | 263 Expect.equals(12, input.positionSync()); |
145 input.readListSync(buffer, 12, 6); | 264 input.readListSync(buffer, 12, 6); |
146 Expect.equals(18, input.positionSync()); | 265 Expect.equals(18, input.positionSync()); |
147 input.closeSync(); | 266 input.closeSync(); |
148 return 1; | 267 return 1; |
149 } | 268 } |
150 | 269 |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 exceptionCaught = true; | 484 exceptionCaught = true; |
366 } catch (Exception ex) { | 485 } catch (Exception ex) { |
367 wrongExceptionCaught = true; | 486 wrongExceptionCaught = true; |
368 } | 487 } |
369 Expect.equals(true, exceptionCaught); | 488 Expect.equals(true, exceptionCaught); |
370 Expect.equals(true, !wrongExceptionCaught); | 489 Expect.equals(true, !wrongExceptionCaught); |
371 file.closeSync(); | 490 file.closeSync(); |
372 return 1; | 491 return 1; |
373 } | 492 } |
374 | 493 |
494 static int testMixedSyncAndAsync() { | |
495 var name = getFilename("tests/vm/data/fixed_length_file"); | |
496 var f = new File(name); | |
497 f.existsHandler = (exists) { | |
498 try { | |
499 f.existsSync(); | |
500 Expect.fail("Expected exception"); | |
501 } catch (var e) { | |
502 Expect.isTrue(e is FileIOException); | |
503 } | |
504 }; | |
505 f.exists(); | |
506 return 1; | |
507 } | |
508 | |
375 // Helper method to be able to run the test from the runtime | 509 // Helper method to be able to run the test from the runtime |
376 // directory, or the top directory. | 510 // directory, or the top directory. |
377 static String getFilename(String path) => | 511 static String getFilename(String path) => |
378 new File(path).existsSync() ? path : 'runtime/' + path; | 512 new File(path).existsSync() ? path : 'runtime/' + path; |
379 | 513 |
380 // Main test entrypoint. | 514 // Main test entrypoint. |
381 static testMain() { | 515 static testMain() { |
382 Expect.equals(1, testRead()); | 516 Expect.equals(1, testRead()); |
517 Expect.equals(1, testReadSync()); | |
383 Expect.equals(1, testReadWrite()); | 518 Expect.equals(1, testReadWrite()); |
519 Expect.equals(1, testReadWriteSync()); | |
384 Expect.equals(1, testReadStream()); | 520 Expect.equals(1, testReadStream()); |
385 Expect.equals(1, testReadWriteStream()); | 521 Expect.equals(1, testReadWriteStream()); |
386 Expect.equals(1, testLength()); | 522 Expect.equals(1, testLength()); |
523 Expect.equals(1, testLengthSync()); | |
387 Expect.equals(1, testPosition()); | 524 Expect.equals(1, testPosition()); |
525 Expect.equals(1, testPositionSync()); | |
388 Expect.equals(1, testCloseException()); | 526 Expect.equals(1, testCloseException()); |
389 Expect.equals(1, testCloseExceptionStream()); | 527 Expect.equals(1, testCloseExceptionStream()); |
390 Expect.equals(1, testBufferOutOfBoundsException()); | 528 Expect.equals(1, testBufferOutOfBoundsException()); |
529 Expect.equals(1, testMixedSyncAndAsync()); | |
391 } | 530 } |
392 } | 531 } |
393 | 532 |
394 main() { | 533 main() { |
395 FileTest.testMain(); | 534 FileTest.testMain(); |
396 } | 535 } |
OLD | NEW |