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 // Testing FileInputStream, VM-only, standalone test. | 4 // Testing FileInputStream, VM-only, standalone test. |
5 | 5 |
6 // Helper method to be able to run the test from the runtime | 6 // Helper method to be able to run the test from the runtime |
7 // directory, or the top directory. | 7 // directory, or the top directory. |
8 String getFilename(String path) => | 8 String getFilename(String path) => |
9 new File(path).existsSync() ? path : '../' + path; | 9 new File(path).existsSync() ? path : '../' + path; |
10 | 10 |
11 void testStringInputStreamSync() { | 11 void testStringInputStreamSync() { |
12 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 12 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
13 // File contains "Hello Dart\nwassup!\n" | 13 // File contains "Hello Dart\nwassup!\n" |
14 File file = new File(fileName); | 14 File file = new File(fileName); |
15 file.openSync(); | |
16 StringInputStream x = new StringInputStream(file.openInputStream()); | 15 StringInputStream x = new StringInputStream(file.openInputStream()); |
17 String line = x.readLine(); | 16 String line = x.readLine(); |
18 Expect.equals("Hello Dart", line); | 17 Expect.equals("Hello Dart", line); |
19 file.closeSync(); | |
20 line = x.readLine(); | 18 line = x.readLine(); |
21 Expect.equals("wassup!", line); | 19 Expect.equals("wassup!", line); |
22 } | 20 } |
23 | 21 |
24 void testInputStreamAsync() { | 22 void testInputStreamAsync() { |
25 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 23 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
26 // File contains "Hello Dart\nwassup!\n" | 24 // File contains "Hello Dart\nwassup!\n" |
27 var expected = "Hello Dart\nwassup!\n".charCodes(); | 25 var expected = "Hello Dart\nwassup!\n".charCodes(); |
28 File file = new File(fileName); | 26 InputStream x = (new File(fileName)).openInputStream(); |
29 file.openSync(); | |
30 InputStream x = file.openInputStream(); | |
31 var byteCount = 0; | 27 var byteCount = 0; |
32 x.dataHandler = () { | 28 x.dataHandler = () { |
33 Expect.equals(expected[byteCount], x.read(1)[0]); | 29 Expect.equals(expected[byteCount], x.read(1)[0]); |
34 byteCount++; | 30 byteCount++; |
35 }; | 31 }; |
36 x.closeHandler = () { | 32 x.closeHandler = () { |
37 Expect.equals(expected.length, byteCount); | 33 Expect.equals(expected.length, byteCount); |
38 }; | 34 }; |
39 } | 35 } |
40 | 36 |
41 | 37 |
42 void testStringInputStreamAsync1() { | 38 void testStringInputStreamAsync1() { |
43 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 39 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
44 // File contains "Hello Dart\nwassup!\n" | 40 // File contains "Hello Dart\nwassup!\n" |
45 File file = new File(fileName); | 41 File file = new File(fileName); |
46 file.openSync(); | |
47 StringInputStream x = new StringInputStream(file.openInputStream()); | 42 StringInputStream x = new StringInputStream(file.openInputStream()); |
48 var result = ""; | 43 var result = ""; |
49 x.dataHandler = () { | 44 x.dataHandler = () { |
50 result += x.read(); | 45 result += x.read(); |
51 }; | 46 }; |
52 x.closeHandler = () { | 47 x.closeHandler = () { |
53 Expect.equals("Hello Dart\nwassup!\n", result); | 48 Expect.equals("Hello Dart\nwassup!\n", result); |
54 }; | 49 }; |
55 } | 50 } |
56 | 51 |
57 | 52 |
58 void testStringInputStreamAsync2() { | 53 void testStringInputStreamAsync2() { |
59 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 54 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
60 // File contains "Hello Dart\nwassup!\n" | 55 // File contains "Hello Dart\nwassup!\n" |
61 File file = new File(fileName); | 56 File file = new File(fileName); |
62 file.openSync(); | |
63 StringInputStream x = new StringInputStream(file.openInputStream()); | 57 StringInputStream x = new StringInputStream(file.openInputStream()); |
64 int lineCount = 0; | 58 int lineCount = 0; |
65 x.lineHandler = () { | 59 x.lineHandler = () { |
66 var line = x.readLine(); | 60 var line = x.readLine(); |
67 Expect.isTrue(lineCount == 0 || lineCount == 1); | 61 Expect.isTrue(lineCount == 0 || lineCount == 1); |
68 if (lineCount == 0) Expect.equals("Hello Dart", line); | 62 if (lineCount == 0) Expect.equals("Hello Dart", line); |
69 if (lineCount == 1) Expect.equals("wassup!", line); | 63 if (lineCount == 1) Expect.equals("wassup!", line); |
70 lineCount++; | 64 lineCount++; |
71 }; | 65 }; |
72 x.closeHandler = () { | 66 x.closeHandler = () { |
73 Expect.equals(2, lineCount); | 67 Expect.equals(2, lineCount); |
74 }; | 68 }; |
75 } | 69 } |
76 | 70 |
77 | 71 |
78 void testChunkedInputStream() { | 72 void testChunkedInputStream() { |
79 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); | 73 String fileName = getFilename("tests/standalone/src/readuntil_test.dat"); |
80 // File contains 19 bytes ("Hello Dart\nwassup!") | 74 // File contains 19 bytes ("Hello Dart\nwassup!") |
81 File file = new File(fileName); | 75 File file = new File(fileName); |
82 file.openSync(); | |
83 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); | 76 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); |
84 x.chunkSize = 9; | 77 x.chunkSize = 9; |
85 List<int> chunk = x.read(); | 78 List<int> chunk = x.read(); |
86 Expect.equals(9, chunk.length); | 79 Expect.equals(9, chunk.length); |
87 file.closeSync(); | |
88 x.chunkSize = 5; | 80 x.chunkSize = 5; |
89 chunk = x.read(); | 81 chunk = x.read(); |
90 Expect.equals(5, chunk.length); | 82 Expect.equals(5, chunk.length); |
91 chunk = x.read(); | 83 chunk = x.read(); |
92 Expect.equals(5, chunk.length); | 84 Expect.equals(5, chunk.length); |
93 chunk = x.read(); | 85 chunk = x.read(); |
94 Expect.equals(null, chunk); | 86 Expect.equals(null, chunk); |
95 } | 87 } |
96 | 88 |
97 | 89 |
98 main() { | 90 main() { |
99 testStringInputStreamSync(); | 91 testStringInputStreamSync(); |
100 testInputStreamAsync(); | 92 testInputStreamAsync(); |
101 testStringInputStreamAsync1(); | 93 testStringInputStreamAsync1(); |
102 testStringInputStreamAsync2(); | 94 testStringInputStreamAsync2(); |
103 testChunkedInputStream(); | 95 testChunkedInputStream(); |
104 } | 96 } |
OLD | NEW |