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() { |
11 // Read a file and check part of it's contents. | 11 // Read a file and check part of it's contents. |
12 String filename = getFilename("bin/file_test.cc"); | 12 String filename = getFilename("bin/file_test.cc"); |
13 File file = new File(filename, false); | 13 File file = new File(filename, false); |
14 InputStream input = file.inputStream; | 14 InputStream input = file.inputStream; |
15 List<int> buffer = new List<int>(42); | 15 List<int> buffer = new List<int>(42); |
16 bool readDone = input.read(buffer, 0, 12, null); | 16 int bytesRead = input.readInto(buffer, 0, 12); |
17 Expect.equals(true, readDone); | 17 Expect.equals(12, bytesRead); |
18 readDone = input.read(buffer, 12, 30, null); | 18 bytesRead = input.readInto(buffer, 12, 30); |
19 Expect.equals(true, readDone); | 19 Expect.equals(30, bytesRead); |
20 Expect.equals(47, buffer[0]); // represents '/' in the file. | 20 Expect.equals(47, buffer[0]); // represents '/' in the file. |
21 Expect.equals(47, buffer[1]); // represents '/' in the file. | 21 Expect.equals(47, buffer[1]); // represents '/' in the file. |
22 Expect.equals(32, buffer[2]); // represents ' ' in the file. | 22 Expect.equals(32, buffer[2]); // represents ' ' in the file. |
23 Expect.equals(67, buffer[3]); // represents 'C' in the file. | 23 Expect.equals(67, buffer[3]); // represents 'C' in the file. |
24 Expect.equals(111, buffer[4]); // represents 'o' in the file. | 24 Expect.equals(111, buffer[4]); // represents 'o' in the file. |
25 Expect.equals(112, buffer[5]); // represents 'p' in the file. | 25 Expect.equals(112, buffer[5]); // represents 'p' in the file. |
26 Expect.equals(121, buffer[6]); // represents 'y' in the file. | 26 Expect.equals(121, buffer[6]); // represents 'y' in the file. |
27 Expect.equals(114, buffer[7]); // represents 'r' in the file. | 27 Expect.equals(114, buffer[7]); // represents 'r' in the file. |
28 Expect.equals(105, buffer[8]); // represents 'i' in the file. | 28 Expect.equals(105, buffer[8]); // represents 'i' in the file. |
29 Expect.equals(103, buffer[9]); // represents 'g' in the file. | 29 Expect.equals(103, buffer[9]); // represents 'g' in the file. |
30 Expect.equals(104, buffer[10]); // represents 'h' in the file. | 30 Expect.equals(104, buffer[10]); // represents 'h' in the file. |
31 Expect.equals(116, buffer[11]); // represents 't' in the file. | 31 Expect.equals(116, buffer[11]); // represents 't' in the file. |
32 return 1; | 32 return 1; |
33 } | 33 } |
34 // Test for file read and write functionality. | 34 // Test for file read and write functionality. |
35 static int testReadWriteStream() { | 35 static int testReadWriteStream() { |
36 // Read a file. | 36 // Read a file. |
37 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 37 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
38 File file = new File(inFilename, false); | 38 File file = new File(inFilename, false); |
39 InputStream input = file.inputStream; | 39 InputStream input = file.inputStream; |
40 List<int> buffer1 = new List<int>(42); | 40 List<int> buffer1 = new List<int>(42); |
41 bool readDone = input.read(buffer1, 0, 42, null); | 41 int bytesRead = input.readInto(buffer1, 0, 42); |
42 Expect.equals(true, readDone); | 42 Expect.equals(42, bytesRead); |
43 file.close(); | 43 file.close(); |
44 // Write the contents of the file just read into another file. | 44 // Write the contents of the file just read into another file. |
45 String outFilename = getFilename("tests/vm/data/fixed_length_file_out"); | 45 String outFilename = getFilename("tests/vm/data/fixed_length_file_out"); |
46 file = new File(outFilename, true); | 46 file = new File(outFilename, true); |
47 OutputStream output = file.outputStream; | 47 OutputStream output = file.outputStream; |
48 bool writeDone = output.write(buffer1, 0, 42, null); | 48 bool writeDone = output.write(buffer1, 0, 42, null); |
49 Expect.equals(true, writeDone); | 49 Expect.equals(true, writeDone); |
50 file.close(); | 50 file.close(); |
51 // Now read the contents of the file just written. | 51 // Now read the contents of the file just written. |
52 List<int> buffer2 = new List<int>(42); | 52 List<int> buffer2 = new List<int>(42); |
53 file = new File(outFilename, false); | 53 file = new File(outFilename, false); |
54 input = file.inputStream; | 54 input = file.inputStream; |
55 readDone = input.read(buffer2, 0, 42, null); | 55 bytesRead = input.readInto(buffer2, 0, 42); |
56 Expect.equals(true, readDone); | 56 Expect.equals(42, bytesRead); |
57 file.close(); | 57 file.close(); |
58 // Now compare the two buffers to check if they are identical. | 58 // Now compare the two buffers to check if they are identical. |
59 for (int i = 0; i < buffer1.length; i++) { | 59 for (int i = 0; i < buffer1.length; i++) { |
60 Expect.equals(buffer1[i], buffer2[i]); | 60 Expect.equals(buffer1[i], buffer2[i]); |
61 } | 61 } |
62 return 1; | 62 return 1; |
63 } | 63 } |
64 static int testRead() { | 64 static int testRead() { |
65 // Read a file and check part of it's contents. | 65 // Read a file and check part of it's contents. |
66 String filename = getFilename("bin/file_test.cc"); | 66 String filename = getFilename("bin/file_test.cc"); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 static int testCloseExceptionStream() { | 235 static int testCloseExceptionStream() { |
236 bool exceptionCaught = false; | 236 bool exceptionCaught = false; |
237 bool wrongExceptionCaught = false; | 237 bool wrongExceptionCaught = false; |
238 String filename = getFilename("tests/vm/data/fixed_length_file_out"); | 238 String filename = getFilename("tests/vm/data/fixed_length_file_out"); |
239 File file = new File(filename, true); | 239 File file = new File(filename, true); |
240 assert(file != null); | 240 assert(file != null); |
241 file.close(); | 241 file.close(); |
242 InputStream input = file.inputStream; | 242 InputStream input = file.inputStream; |
243 try { | 243 try { |
244 List<int> buffer = new List<int>(42); | 244 List<int> buffer = new List<int>(42); |
245 bool readDone = input.read(buffer, 0, 12, null); | 245 input.readInto(buffer, 0, 12); |
246 } catch (FileIOException ex) { | 246 } catch (FileIOException ex) { |
247 exceptionCaught = true; | 247 exceptionCaught = true; |
248 } catch (Exception ex) { | 248 } catch (Exception ex) { |
249 wrongExceptionCaught = true; | 249 wrongExceptionCaught = true; |
250 } | 250 } |
251 Expect.equals(true, exceptionCaught); | 251 Expect.equals(true, exceptionCaught); |
252 Expect.equals(true, !wrongExceptionCaught); | 252 Expect.equals(true, !wrongExceptionCaught); |
253 exceptionCaught = false; | 253 exceptionCaught = false; |
254 OutputStream output = file.outputStream; | 254 OutputStream output = file.outputStream; |
255 try { | 255 try { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 Expect.equals(1, testPosition()); | 385 Expect.equals(1, testPosition()); |
386 Expect.equals(1, testCloseException()); | 386 Expect.equals(1, testCloseException()); |
387 Expect.equals(1, testCloseExceptionStream()); | 387 Expect.equals(1, testCloseExceptionStream()); |
388 Expect.equals(1, testBufferOutOfBoundsException()); | 388 Expect.equals(1, testBufferOutOfBoundsException()); |
389 } | 389 } |
390 } | 390 } |
391 | 391 |
392 main() { | 392 main() { |
393 FileTest.testMain(); | 393 FileTest.testMain(); |
394 } | 394 } |
OLD | NEW |