| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 file input stream, VM-only, standalone test. | 4 // Testing file input stream, VM-only, standalone test. |
| 5 | 5 |
| 6 #import("dart:io"); | 6 #import("dart:io"); |
| 7 #import("dart:isolate"); | 7 #import("dart:isolate"); |
| 8 | 8 |
| 9 // Helper method to be able to run the test from the runtime | 9 // Helper method to be able to run the test from the runtime |
| 10 // directory, or the top directory. | 10 // directory, or the top directory. |
| 11 String getFilename(String path) => | 11 String getFilename(String path) => |
| 12 new File(path).existsSync() ? path : '../$path'; | 12 new File(path).existsSync() ? path : '../$path'; |
| 13 | 13 |
| 14 void testStringInputStreamSync() { | 14 void testStringInputStreamSync() { |
| 15 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); | 15 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); |
| 16 // File contains "Hello Dart\nwassup!\n" | 16 // File contains "Hello Dart\nwassup!\n" |
| 17 File file = new File(fileName); | 17 File file = new File(fileName); |
| 18 int linesRead = 0; |
| 18 StringInputStream x = new StringInputStream(file.openInputStream()); | 19 StringInputStream x = new StringInputStream(file.openInputStream()); |
| 19 x.onLine = () { | 20 x.onLine = () { |
| 20 // The file input stream is known (for now) to have read the whole | |
| 21 // file when the data handler is called. | |
| 22 String line = x.readLine(); | 21 String line = x.readLine(); |
| 23 Expect.equals("Hello Dart", line); | 22 linesRead++; |
| 24 line = x.readLine(); | 23 if (linesRead == 1) { |
| 25 Expect.equals("wassup!", line); | 24 Expect.equals("Hello Dart", line); |
| 25 } else if (linesRead == 2) { |
| 26 Expect.equals("wassup!", line); |
| 27 } else { |
| 28 Expect.fail("More or less than 2 lines read ($linesRead lines read)."); |
| 29 } |
| 26 }; | 30 }; |
| 27 } | 31 } |
| 28 | 32 |
| 29 void testInputStreamAsync() { | 33 void testInputStreamAsync() { |
| 30 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); | 34 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); |
| 31 // File contains "Hello Dart\nwassup!\n" | 35 // File contains "Hello Dart\nwassup!\n" |
| 32 var expected = "Hello Dart\nwassup!\n".charCodes(); | 36 var expected = "Hello Dart\nwassup!\n".charCodes(); |
| 33 InputStream x = (new File(fileName)).openInputStream(); | 37 InputStream x = (new File(fileName)).openInputStream(); |
| 34 var byteCount = 0; | 38 var byteCount = 0; |
| 35 x.onData = () { | 39 x.onData = () { |
| 36 Expect.equals(expected[byteCount], x.read(1)[0]); | 40 Expect.equals(expected[byteCount], x.read(1)[0]); |
| 37 byteCount++; | 41 byteCount++; |
| 38 }; | 42 }; |
| 39 x.onClosed = () { | 43 x.onClosed = () { |
| 40 Expect.equals(expected.length, byteCount); | 44 Expect.equals(expected.length, byteCount); |
| 41 }; | 45 }; |
| 42 } | 46 } |
| 43 | 47 |
| 44 | |
| 45 void testStringInputStreamAsync(String name, int length) { | 48 void testStringInputStreamAsync(String name, int length) { |
| 46 String fileName = getFilename("tests/standalone/io/$name"); | 49 String fileName = getFilename("tests/standalone/io/$name"); |
| 47 // File contains 10 lines. | 50 // File contains 10 lines. |
| 48 File file = new File(fileName); | 51 File file = new File(fileName); |
| 49 Expect.equals(length, file.openSync().lengthSync()); | 52 Expect.equals(length, file.openSync().lengthSync()); |
| 50 StringInputStream x = new StringInputStream(file.openInputStream()); | 53 StringInputStream x = new StringInputStream(file.openInputStream()); |
| 51 int lineCount = 0; | 54 int lineCount = 0; |
| 52 x.onLine = () { | 55 x.onLine = () { |
| 53 var line = x.readLine(); | 56 var line = x.readLine(); |
| 54 lineCount++; | 57 lineCount++; |
| 55 Expect.isTrue(lineCount <= 10); | 58 Expect.isTrue(lineCount <= 10); |
| 56 if (line[0] != "#") { | 59 if (line[0] != "#") { |
| 57 Expect.equals("Line $lineCount", line); | 60 Expect.equals("Line $lineCount", line); |
| 58 } | 61 } |
| 59 }; | 62 }; |
| 60 x.onClosed = () { | 63 x.onClosed = () { |
| 61 Expect.equals(10, lineCount); | 64 Expect.equals(10, lineCount); |
| 62 }; | 65 }; |
| 63 } | 66 } |
| 64 | 67 |
| 65 | |
| 66 void testChunkedInputStream() { | 68 void testChunkedInputStream() { |
| 67 // Force the test to timeout if it does not finish. | 69 // Force the test to timeout if it does not finish. |
| 68 ReceivePort done = new ReceivePort(); | 70 ReceivePort done = new ReceivePort(); |
| 69 done.receive((message, replyTo) { done.close(); }); | 71 done.receive((message, replyTo) { done.close(); }); |
| 70 | 72 |
| 71 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); | 73 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); |
| 72 // File contains 19 bytes ("Hello Dart\nwassup!") | 74 // File contains 19 bytes ("Hello Dart\nwassup!") |
| 73 File file = new File(fileName); | 75 File file = new File(fileName); |
| 74 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); | 76 ChunkedInputStream x = new ChunkedInputStream(file.openInputStream()); |
| 75 x.chunkSize = 9; | 77 x.chunkSize = 9; |
| 76 x.onData = () { | 78 x.onData = () { |
| 77 List<int> chunk = x.read(); | 79 List<int> chunk = x.read(); |
| 78 Expect.equals(9, chunk.length); | 80 Expect.equals(9, chunk.length); |
| 79 x.chunkSize = 5; | 81 x.chunkSize = 5; |
| 80 x.onData = () { | 82 x.onData = () { |
| 81 chunk = x.read(); | 83 chunk = x.read(); |
| 82 Expect.equals(5, chunk.length); | 84 Expect.equals(5, chunk.length); |
| 83 x.onData = () { | 85 x.onData = () { |
| 84 chunk = x.read(); | 86 chunk = x.read(); |
| 85 Expect.equals(5, chunk.length); | 87 Expect.equals(5, chunk.length); |
| 86 chunk = x.read(); | 88 chunk = x.read(); |
| 87 Expect.equals(null, chunk); | 89 Expect.equals(null, chunk); |
| 88 done.toSendPort().send(null); | 90 done.toSendPort().send(null); |
| 89 }; | 91 }; |
| 90 }; | 92 }; |
| 91 }; | 93 }; |
| 92 } | 94 } |
| 93 | 95 |
| 96 void testUnreadyInputStream() { |
| 97 String fileName = getFilename("tests/standalone/io/readuntil_test.dat"); |
| 98 var expected = "Hello Dart\nwassup!\n".charCodes(); |
| 99 InputStream x = (new File(fileName)).openInputStream(); |
| 100 List<int> buffer = new List<int>(100); |
| 101 |
| 102 x.onData = () { |
| 103 Expect.fail("Input stream closed before opening called onData handler."); |
| 104 }; |
| 105 |
| 106 x.onClosed = () { }; |
| 107 |
| 108 // Called before stream is ready. |
| 109 int read = x.readInto(buffer); |
| 110 Expect.equals(0, read); |
| 111 |
| 112 // Called before stream is ready. |
| 113 x.close(); |
| 114 } |
| 115 |
| 94 | 116 |
| 95 main() { | 117 main() { |
| 96 testStringInputStreamSync(); | 118 testStringInputStreamSync(); |
| 97 testInputStreamAsync(); | 119 testInputStreamAsync(); |
| 98 // Check the length of these files as both are text files where one | 120 // Check the length of these files as both are text files where one |
| 99 // is without a terminating line separator which can easily be added | 121 // is without a terminating line separator which can easily be added |
| 100 // back if accidentally opened in a text editor. | 122 // back if accidentally opened in a text editor. |
| 101 testStringInputStreamAsync("readline_test1.dat", 111); | 123 testStringInputStreamAsync("readline_test1.dat", 111); |
| 102 testStringInputStreamAsync("readline_test2.dat", 114); | 124 testStringInputStreamAsync("readline_test2.dat", 114); |
| 103 testChunkedInputStream(); | 125 testChunkedInputStream(); |
| 126 testUnreadyInputStream(); |
| 104 } | 127 } |
| OLD | NEW |