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...) 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.errorHandler = (s) { |
| 72 Expect.fail("No errors expected"); |
| 73 }; |
| 74 file.openHandler = () { |
| 75 List<int> buffer = new List<int>(10); |
| 76 file.readListHandler = (bytes_read) { |
| 77 Expect.equals(5, bytes_read); |
| 78 file.readListHandler = (bytes_read) { |
| 79 Expect.equals(5, bytes_read); |
| 80 Expect.equals(47, buffer[0]); // represents '/' in the file. |
| 81 Expect.equals(47, buffer[1]); // represents '/' in the file. |
| 82 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
| 83 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
| 84 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
| 85 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
| 86 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
| 87 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
| 88 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
| 89 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
| 90 file.close(); |
| 91 }; |
| 92 file.readList(buffer, 5, 5); |
| 93 }; |
| 94 file.readList(buffer, 0, 5); |
| 95 }; |
| 96 file.open(); |
| 97 return 1; |
| 98 } |
| 99 |
| 100 static int testReadSync() { |
| 101 // Read a file and check part of it's contents. |
| 102 String filename = getFilename("bin/file_test.cc"); |
| 103 File file = new File(filename); |
71 file.openSync(); | 104 file.openSync(); |
72 List<int> buffer = new List<int>(42); | 105 List<int> buffer = new List<int>(42); |
73 int bytes_read = 0; | 106 int bytes_read = 0; |
74 bytes_read = file.readListSync(buffer, 0, 12); | 107 bytes_read = file.readListSync(buffer, 0, 12); |
75 Expect.equals(12, bytes_read); | 108 Expect.equals(12, bytes_read); |
76 bytes_read = file.readListSync(buffer, 12, 30); | 109 bytes_read = file.readListSync(buffer, 12, 30); |
77 Expect.equals(30, bytes_read); | 110 Expect.equals(30, bytes_read); |
78 Expect.equals(47, buffer[0]); // represents '/' in the file. | 111 Expect.equals(47, buffer[0]); // represents '/' in the file. |
79 Expect.equals(47, buffer[1]); // represents '/' in the file. | 112 Expect.equals(47, buffer[1]); // represents '/' in the file. |
80 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 113 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
81 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 114 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
82 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 115 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
83 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 116 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
84 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 117 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
85 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 118 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
86 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 119 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
87 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 120 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
88 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 121 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
89 Expect.equals(116, buffer[11]); // represents 't' in the file. | 122 Expect.equals(116, buffer[11]); // represents 't' in the file. |
90 return 1; | 123 return 1; |
91 } | 124 } |
92 | 125 |
93 // Test for file read and write functionality. | 126 // Test for file read and write functionality. |
94 static int testReadWrite() { | 127 static int testReadWrite() { |
95 // Read a file. | 128 // Read a file. |
96 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 129 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
97 File file = new File(inFilename); | 130 File file = new File(inFilename); |
| 131 file.errorHandler = (s) { |
| 132 Expect.fail("No errors expected"); |
| 133 }; |
| 134 file.openHandler = () { |
| 135 List<int> buffer1 = new List<int>(42); |
| 136 file.readListHandler = (bytes_read) { |
| 137 Expect.equals(42, bytes_read); |
| 138 file.closeHandler = () { |
| 139 // Write the contents of the file just read into another file. |
| 140 String outFilenameBase = |
| 141 getFilename("tests/vm/data/fixed_length_file_out"); |
| 142 file = new File(outFilenameBase); |
| 143 file.errorHandler = (s) { |
| 144 Expect.fail("No errors expected"); |
| 145 }; |
| 146 file.openHandler = () { |
| 147 file.noPendingWriteHandler = () { |
| 148 file.closeHandler = () { |
| 149 // Now read the contents of the file just written. |
| 150 List<int> buffer2 = new List<int>(bytes_read); |
| 151 file = new File(getFilename(outFilenameBase)); |
| 152 file.errorHandler = (s) { |
| 153 Expect.fail("No errors expected"); |
| 154 }; |
| 155 file.openHandler = () { |
| 156 file.readListHandler = (bytes_read) { |
| 157 Expect.equals(42, bytes_read); |
| 158 file.closeHandler = () { |
| 159 // Now compare the two buffers to check if they |
| 160 // are identical. |
| 161 Expect.equals(buffer1.length, buffer2.length); |
| 162 for (int i = 0; i < buffer1.length; i++) { |
| 163 Expect.equals(buffer1[i], buffer2[i]); |
| 164 } |
| 165 }; |
| 166 file.close(); |
| 167 }; |
| 168 file.readList(buffer2, 0, 42); |
| 169 }; |
| 170 file.open(); |
| 171 }; |
| 172 file.close(); |
| 173 }; |
| 174 file.writeList(buffer1, 0, bytes_read); |
| 175 }; |
| 176 file.open(true); |
| 177 }; |
| 178 file.close(); |
| 179 }; |
| 180 file.readList(buffer1, 0, 42); |
| 181 }; |
| 182 file.open(); |
| 183 return 1; |
| 184 |
| 185 } |
| 186 |
| 187 static int testReadWriteSync() { |
| 188 // Read a file. |
| 189 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 190 File file = new File(inFilename); |
98 file.openSync(); | 191 file.openSync(); |
99 List<int> buffer1 = new List<int>(42); | 192 List<int> buffer1 = new List<int>(42); |
100 int bytes_read = 0; | 193 int bytes_read = 0; |
101 int bytes_written = 0; | 194 int bytes_written = 0; |
102 bytes_read = file.readListSync(buffer1, 0, 42); | 195 bytes_read = file.readListSync(buffer1, 0, 42); |
103 Expect.equals(42, bytes_read); | 196 Expect.equals(42, bytes_read); |
104 file.closeSync(); | 197 file.closeSync(); |
105 // Write the contents of the file just read into another file. | 198 // Write the contents of the file just read into another file. |
106 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file_out"); | 199 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file_out"); |
107 file = new File(outFilenameBase); | 200 file = new File(outFilenameBase); |
(...skipping 12 matching lines...) Loading... |
120 for (int i = 0; i < buffer1.length; i++) { | 213 for (int i = 0; i < buffer1.length; i++) { |
121 Expect.equals(buffer1[i], buffer2[i]); | 214 Expect.equals(buffer1[i], buffer2[i]); |
122 } | 215 } |
123 return 1; | 216 return 1; |
124 } | 217 } |
125 | 218 |
126 // Test for file length functionality. | 219 // Test for file length functionality. |
127 static int testLength() { | 220 static int testLength() { |
128 String filename = getFilename("tests/vm/data/fixed_length_file"); | 221 String filename = getFilename("tests/vm/data/fixed_length_file"); |
129 File input = new File(filename); | 222 File input = new File(filename); |
| 223 input.errorHandler = (s) { |
| 224 Expect.fail("No errors expected"); |
| 225 }; |
| 226 input.openSync(); |
| 227 input.lengthHandler = (length) { |
| 228 Expect.equals(42, length); |
| 229 input.close(); |
| 230 }; |
| 231 input.length(); |
| 232 return 1; |
| 233 } |
| 234 |
| 235 static int testLengthSync() { |
| 236 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 237 File input = new File(filename); |
130 input.openSync(); | 238 input.openSync(); |
131 Expect.equals(42, input.lengthSync()); | 239 Expect.equals(42, input.lengthSync()); |
132 input.closeSync(); | 240 input.closeSync(); |
133 return 1; | 241 return 1; |
134 } | 242 } |
135 | 243 |
136 // Test for file position functionality. | 244 // Test for file position functionality. |
137 static int testPosition() { | 245 static int testPosition() { |
138 String filename = getFilename("tests/vm/data/fixed_length_file"); | 246 String filename = getFilename("tests/vm/data/fixed_length_file"); |
139 File input = new File(filename); | 247 File input = new File(filename); |
| 248 input.errorHandler = (s) { |
| 249 Expect.fail("No errors expected"); |
| 250 }; |
| 251 input.openSync(); |
| 252 input.positionHandler = (position) { |
| 253 Expect.equals(0, position); |
| 254 List<int> buffer = new List<int>(100); |
| 255 input.readListHandler = (bytes_read) { |
| 256 input.positionHandler = (position) { |
| 257 Expect.equals(12, position); |
| 258 input.readListHandler = (bytes_read) { |
| 259 input.positionHandler = (position) { |
| 260 Expect.equals(18, position); |
| 261 input.close(); |
| 262 }; |
| 263 }; |
| 264 input.readList(buffer, 12, 6); |
| 265 }; |
| 266 input.position(); |
| 267 }; |
| 268 input.readList(buffer, 0, 12); |
| 269 }; |
| 270 input.position(); |
| 271 return 1; |
| 272 } |
| 273 |
| 274 static int testPositionSync() { |
| 275 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 276 File input = new File(filename); |
140 input.openSync(); | 277 input.openSync(); |
141 Expect.equals(0, input.positionSync()); | 278 Expect.equals(0, input.positionSync()); |
142 List<int> buffer = new List<int>(100); | 279 List<int> buffer = new List<int>(100); |
143 input.readListSync(buffer, 0, 12); | 280 input.readListSync(buffer, 0, 12); |
144 Expect.equals(12, input.positionSync()); | 281 Expect.equals(12, input.positionSync()); |
145 input.readListSync(buffer, 12, 6); | 282 input.readListSync(buffer, 12, 6); |
146 Expect.equals(18, input.positionSync()); | 283 Expect.equals(18, input.positionSync()); |
147 input.closeSync(); | 284 input.closeSync(); |
148 return 1; | 285 return 1; |
149 } | 286 } |
(...skipping 215 matching lines...) Loading... |
365 exceptionCaught = true; | 502 exceptionCaught = true; |
366 } catch (Exception ex) { | 503 } catch (Exception ex) { |
367 wrongExceptionCaught = true; | 504 wrongExceptionCaught = true; |
368 } | 505 } |
369 Expect.equals(true, exceptionCaught); | 506 Expect.equals(true, exceptionCaught); |
370 Expect.equals(true, !wrongExceptionCaught); | 507 Expect.equals(true, !wrongExceptionCaught); |
371 file.closeSync(); | 508 file.closeSync(); |
372 return 1; | 509 return 1; |
373 } | 510 } |
374 | 511 |
| 512 static int testMixedSyncAndAsync() { |
| 513 var name = getFilename("tests/vm/data/fixed_length_file"); |
| 514 var f = new File(name); |
| 515 f.errorHandler = (s) { |
| 516 Expect.fail("No errors expected"); |
| 517 }; |
| 518 f.existsHandler = (exists) { |
| 519 try { |
| 520 f.existsSync(); |
| 521 Expect.fail("Expected exception"); |
| 522 } catch (var e) { |
| 523 Expect.isTrue(e is FileIOException); |
| 524 } |
| 525 }; |
| 526 f.exists(); |
| 527 return 1; |
| 528 } |
| 529 |
375 // Helper method to be able to run the test from the runtime | 530 // Helper method to be able to run the test from the runtime |
376 // directory, or the top directory. | 531 // directory, or the top directory. |
377 static String getFilename(String path) => | 532 static String getFilename(String path) => |
378 new File(path).existsSync() ? path : 'runtime/' + path; | 533 new File(path).existsSync() ? path : 'runtime/' + path; |
379 | 534 |
380 // Main test entrypoint. | 535 // Main test entrypoint. |
381 static testMain() { | 536 static testMain() { |
382 Expect.equals(1, testRead()); | 537 Expect.equals(1, testRead()); |
| 538 Expect.equals(1, testReadSync()); |
383 Expect.equals(1, testReadWrite()); | 539 Expect.equals(1, testReadWrite()); |
| 540 Expect.equals(1, testReadWriteSync()); |
384 Expect.equals(1, testReadStream()); | 541 Expect.equals(1, testReadStream()); |
385 Expect.equals(1, testReadWriteStream()); | 542 Expect.equals(1, testReadWriteStream()); |
386 Expect.equals(1, testLength()); | 543 Expect.equals(1, testLength()); |
| 544 Expect.equals(1, testLengthSync()); |
387 Expect.equals(1, testPosition()); | 545 Expect.equals(1, testPosition()); |
| 546 Expect.equals(1, testPositionSync()); |
388 Expect.equals(1, testCloseException()); | 547 Expect.equals(1, testCloseException()); |
389 Expect.equals(1, testCloseExceptionStream()); | 548 Expect.equals(1, testCloseExceptionStream()); |
390 Expect.equals(1, testBufferOutOfBoundsException()); | 549 Expect.equals(1, testBufferOutOfBoundsException()); |
| 550 Expect.equals(1, testMixedSyncAndAsync()); |
391 } | 551 } |
392 } | 552 } |
393 | 553 |
394 main() { | 554 main() { |
395 FileTest.testMain(); | 555 FileTest.testMain(); |
396 } | 556 } |
OLD | NEW |