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