Chromium Code Reviews| 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 26 matching lines...) Expand all Loading... | |
| 37 static int testReadWriteStream() { | 37 static int testReadWriteStream() { |
| 38 // Read a file. | 38 // Read a file. |
| 39 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 39 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 40 File file = new File(inFilename); | 40 File file = new File(inFilename); |
| 41 FileInputStream input = file.openInputStream(); | 41 FileInputStream input = file.openInputStream(); |
| 42 List<int> buffer1 = new List<int>(42); | 42 List<int> buffer1 = new List<int>(42); |
| 43 int bytesRead = input.readInto(buffer1, 0, 42); | 43 int bytesRead = input.readInto(buffer1, 0, 42); |
| 44 Expect.equals(42, bytesRead); | 44 Expect.equals(42, bytesRead); |
| 45 input.close(); | 45 input.close(); |
| 46 // Write the contents of the file just read into another file. | 46 // Write the contents of the file just read into another file. |
| 47 String outFilename = getFilename("tests/vm/data/fixed_length_file"); | 47 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); |
| 48 file = new File(outFilename + "_out"); | 48 file = new File(outFilenameBase + "_out"); |
| 49 OutputStream output = file.openOutputStream(); | 49 OutputStream output = file.openOutputStream(); |
| 50 bool writeDone = output.writeFrom(buffer1, 0, 42); | 50 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 51 Expect.equals(true, writeDone); | 51 Expect.equals(true, writeDone); |
| 52 output.close(); | 52 output.close(); |
| 53 // Now read the contents of the file just written. | 53 // Now read the contents of the file just written. |
| 54 List<int> buffer2 = new List<int>(42); | 54 List<int> buffer2 = new List<int>(42); |
| 55 file = new File(outFilename); | 55 file = new File(outFilenameBase + "_out"); |
|
Bill Hesse
2011/11/18 12:26:04
Why wasn't this test failing before? Has that pro
Mads Ager (google)
2011/11/18 12:27:24
Because the test copies input file to output file.
| |
| 56 input = file.openInputStream(); | 56 input = file.openInputStream(); |
| 57 bytesRead = input.readInto(buffer2, 0, 42); | 57 bytesRead = input.readInto(buffer2, 0, 42); |
| 58 input.close(); | 58 input.close(); |
| 59 Expect.equals(42, bytesRead); | 59 Expect.equals(42, bytesRead); |
| 60 // Now compare the two buffers to check if they are identical. | 60 // Now compare the two buffers to check if they are identical. |
| 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 // Delete the output file. | |
| 65 file.deleteSync(); | |
| 66 Expect.isFalse(file.existsSync()); | |
| 64 return 1; | 67 return 1; |
| 65 } | 68 } |
| 66 | 69 |
| 67 static int testRead() { | 70 static int testRead() { |
| 68 // Read a file and check part of it's contents. | 71 // Read a file and check part of it's contents. |
| 69 String filename = getFilename("bin/file_test.cc"); | 72 String filename = getFilename("bin/file_test.cc"); |
| 70 File file = new File(filename); | 73 File file = new File(filename); |
| 71 file.errorHandler = (s) { | 74 file.errorHandler = (s) { |
| 72 Expect.fail("No errors expected"); | 75 Expect.fail("No errors expected"); |
| 73 }; | 76 }; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 file.fullPathHandler = (s) { | 150 file.fullPathHandler = (s) { |
| 148 Expect.isTrue(new File(s).existsSync()); | 151 Expect.isTrue(new File(s).existsSync()); |
| 149 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 152 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
| 150 Expect.fail("Not a full path"); | 153 Expect.fail("Not a full path"); |
| 151 } | 154 } |
| 152 file.openHandler = () { | 155 file.openHandler = () { |
| 153 file.noPendingWriteHandler = () { | 156 file.noPendingWriteHandler = () { |
| 154 file.closeHandler = () { | 157 file.closeHandler = () { |
| 155 // Now read the contents of the file just written. | 158 // Now read the contents of the file just written. |
| 156 List<int> buffer2 = new List<int>(bytes_read); | 159 List<int> buffer2 = new List<int>(bytes_read); |
| 157 file = new File(getFilename(outFilenameBase)); | 160 file = new File(getFilename(outFilenameBase + "_out")); |
| 158 file.errorHandler = (s) { | 161 file.errorHandler = (s) { |
| 159 Expect.fail("No errors expected"); | 162 Expect.fail("No errors expected"); |
| 160 }; | 163 }; |
| 161 file.openHandler = () { | 164 file.openHandler = () { |
| 162 file.readListHandler = (bytes_read) { | 165 file.readListHandler = (bytes_read) { |
| 163 Expect.equals(42, bytes_read); | 166 Expect.equals(42, bytes_read); |
| 164 file.closeHandler = () { | 167 file.closeHandler = () { |
| 165 // Now compare the two buffers to check if they | 168 // Now compare the two buffers to check if they |
| 166 // are identical. | 169 // are identical. |
| 167 Expect.equals(buffer1.length, buffer2.length); | 170 Expect.equals(buffer1.length, buffer2.length); |
| 168 for (int i = 0; i < buffer1.length; i++) { | 171 for (int i = 0; i < buffer1.length; i++) { |
| 169 Expect.equals(buffer1[i], buffer2[i]); | 172 Expect.equals(buffer1[i], buffer2[i]); |
| 170 } | 173 } |
| 174 // Delete the output file. | |
| 175 file.deleteHandler = () { | |
| 176 file.existsHandler = (exists) { | |
| 177 Expect.isFalse(exists); | |
| 178 }; | |
| 179 file.exists(); | |
| 180 }; | |
| 181 file.delete(); | |
| 171 }; | 182 }; |
| 172 file.close(); | 183 file.close(); |
| 173 }; | 184 }; |
| 174 file.readList(buffer2, 0, 42); | 185 file.readList(buffer2, 0, 42); |
| 175 }; | 186 }; |
| 176 file.open(); | 187 file.open(); |
| 177 }; | 188 }; |
| 178 file.close(); | 189 file.close(); |
| 179 }; | 190 }; |
| 180 file.writeList(buffer1, 0, bytes_read); | 191 file.writeList(buffer1, 0, bytes_read); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 String path = file.fullPathSync(); | 223 String path = file.fullPathSync(); |
| 213 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { | 224 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { |
| 214 Expect.fail("Not a full path"); | 225 Expect.fail("Not a full path"); |
| 215 } | 226 } |
| 216 Expect.isTrue(new File(path).existsSync()); | 227 Expect.isTrue(new File(path).existsSync()); |
| 217 file.openSync(true); | 228 file.openSync(true); |
| 218 file.writeListSync(buffer1, 0, bytes_read); | 229 file.writeListSync(buffer1, 0, bytes_read); |
| 219 file.closeSync(); | 230 file.closeSync(); |
| 220 // Now read the contents of the file just written. | 231 // Now read the contents of the file just written. |
| 221 List<int> buffer2 = new List<int>(bytes_read); | 232 List<int> buffer2 = new List<int>(bytes_read); |
| 222 file = new File(getFilename(outFilenameBase)); | 233 file = new File(getFilename(outFilenameBase + "_out")); |
| 223 file.openSync(); | 234 file.openSync(); |
| 224 bytes_read = file.readListSync(buffer2, 0, 42); | 235 bytes_read = file.readListSync(buffer2, 0, 42); |
| 225 Expect.equals(42, bytes_read); | 236 Expect.equals(42, bytes_read); |
| 226 file.closeSync(); | 237 file.closeSync(); |
| 227 // Now compare the two buffers to check if they are identical. | 238 // Now compare the two buffers to check if they are identical. |
| 228 Expect.equals(buffer1.length, buffer2.length); | 239 Expect.equals(buffer1.length, buffer2.length); |
| 229 for (int i = 0; i < buffer1.length; i++) { | 240 for (int i = 0; i < buffer1.length; i++) { |
| 230 Expect.equals(buffer1[i], buffer2[i]); | 241 Expect.equals(buffer1[i], buffer2[i]); |
| 231 } | 242 } |
| 243 // Delete the output file. | |
| 244 file.deleteSync(); | |
| 245 Expect.isFalse(file.existsSync()); | |
| 232 return 1; | 246 return 1; |
| 233 } | 247 } |
| 234 | 248 |
| 235 // Test for file length functionality. | 249 // Test for file length functionality. |
| 236 static int testLength() { | 250 static int testLength() { |
| 237 String filename = getFilename("tests/vm/data/fixed_length_file"); | 251 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 238 File input = new File(filename); | 252 File input = new File(filename); |
| 239 input.errorHandler = (s) { | 253 input.errorHandler = (s) { |
| 240 Expect.fail("No errors expected"); | 254 Expect.fail("No errors expected"); |
| 241 }; | 255 }; |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 572 Expect.equals(1, testCloseException()); | 586 Expect.equals(1, testCloseException()); |
| 573 Expect.equals(1, testCloseExceptionStream()); | 587 Expect.equals(1, testCloseExceptionStream()); |
| 574 Expect.equals(1, testBufferOutOfBoundsException()); | 588 Expect.equals(1, testBufferOutOfBoundsException()); |
| 575 Expect.equals(1, testMixedSyncAndAsync()); | 589 Expect.equals(1, testMixedSyncAndAsync()); |
| 576 } | 590 } |
| 577 } | 591 } |
| 578 | 592 |
| 579 main() { | 593 main() { |
| 580 FileTest.testMain(); | 594 FileTest.testMain(); |
| 581 } | 595 } |
| OLD | NEW |