OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "dart:io"; | 7 import "dart:io"; |
8 import "dart:isolate"; | 8 import "dart:isolate"; |
9 | 9 |
10 // Helper method to be able to run the test from the runtime | 10 // Helper method to be able to run the test from the runtime |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 onDone: () { | 162 onDone: () { |
163 Expect.equals(2 * originalLength, streamedBytes); | 163 Expect.equals(2 * originalLength, streamedBytes); |
164 temp.delete(recursive: true).then((_) => keepAlive.close()); | 164 temp.delete(recursive: true).then((_) => keepAlive.close()); |
165 }, | 165 }, |
166 onError: (e) { | 166 onError: (e) { |
167 Expect.fail("Unexpected error"); | 167 Expect.fail("Unexpected error"); |
168 }); | 168 }); |
169 } | 169 } |
170 | 170 |
171 | 171 |
| 172 void testInputStreamOffset() { |
| 173 void test(int start, int end, int expectedBytes) { |
| 174 var keepAlive = new ReceivePort(); |
| 175 var temp = new Directory('').createTempSync(); |
| 176 var file = new File('${temp.path}/input_stream_offset.txt'); |
| 177 var originalLength = writeLongFileSync(file); |
| 178 var streamedBytes = 0; |
| 179 if (expectedBytes < 0) expectedBytes = originalLength + expectedBytes; |
| 180 file.openRead(start, end).listen( |
| 181 (d) { |
| 182 streamedBytes += d.length; |
| 183 }, |
| 184 onDone: () { |
| 185 Expect.equals(expectedBytes, streamedBytes); |
| 186 temp.delete(recursive: true).then((_) => keepAlive.close()); |
| 187 }, |
| 188 onError: (e) { |
| 189 Expect.fail("Unexpected error"); |
| 190 }); |
| 191 } |
| 192 test(10, 20, 10); |
| 193 test(10, 11, 1); |
| 194 test(10, 10, 0); |
| 195 test(100000000, null, 0); |
| 196 test(null, 0, 0); |
| 197 test(null, 1, 1); |
| 198 test(1, null, -1); |
| 199 test(20, null, -20); |
| 200 } |
| 201 |
| 202 |
| 203 void testInputStreamBadOffset() { |
| 204 void test(int start, int end) { |
| 205 var keepAlive = new ReceivePort(); |
| 206 var temp = new Directory('').createTempSync(); |
| 207 var file = new File('${temp.path}/input_stream_bad_offset.txt'); |
| 208 var originalLength = writeLongFileSync(file); |
| 209 var streamedBytes = 0; |
| 210 file.openRead(start, end).listen( |
| 211 (d) { |
| 212 streamedBytes += d.length; |
| 213 }, |
| 214 onDone: () { |
| 215 }, |
| 216 onError: (e) { |
| 217 temp.delete(recursive: true).then((_) => keepAlive.close()); |
| 218 }); |
| 219 } |
| 220 test(-1, null); |
| 221 test(100, 99); |
| 222 test(null, -1); |
| 223 } |
| 224 |
| 225 |
172 void testStringLineTransformerEnding(String name, int length) { | 226 void testStringLineTransformerEnding(String name, int length) { |
173 String fileName = getFilename("tests/standalone/io/$name"); | 227 String fileName = getFilename("tests/standalone/io/$name"); |
174 // File contains 10 lines. | 228 // File contains 10 lines. |
175 File file = new File(fileName); | 229 File file = new File(fileName); |
176 Expect.equals(length, file.openSync().lengthSync()); | 230 Expect.equals(length, file.openSync().lengthSync()); |
177 var lineStream = file.openRead() | 231 var lineStream = file.openRead() |
178 .transform(new StringDecoder()) | 232 .transform(new StringDecoder()) |
179 .transform(new LineTransformer()); | 233 .transform(new LineTransformer()); |
180 int lineCount = 0; | 234 int lineCount = 0; |
181 lineStream.listen( | 235 lineStream.listen( |
182 (line) { | 236 (line) { |
183 lineCount++; | 237 lineCount++; |
184 Expect.isTrue(lineCount <= 10); | 238 Expect.isTrue(lineCount <= 10); |
185 if (line[0] != "#") { | 239 if (line[0] != "#") { |
186 Expect.equals("Line $lineCount", line); | 240 Expect.equals("Line $lineCount", line); |
187 } | 241 } |
188 }, | 242 }, |
189 onDone: () { | 243 onDone: () { |
190 Expect.equals(10, lineCount); | 244 Expect.equals(10, lineCount); |
191 }); | 245 }); |
192 } | 246 } |
193 | 247 |
194 | 248 |
195 main() { | 249 main() { |
196 testStringLineTransformer(); | 250 testStringLineTransformer(); |
197 testOpenStreamAsync(); | 251 testOpenStreamAsync(); |
198 testInputStreamTruncate(); | 252 testInputStreamTruncate(); |
199 testInputStreamDelete(); | 253 testInputStreamDelete(); |
200 testInputStreamAppend(); | 254 testInputStreamAppend(); |
| 255 testInputStreamOffset(); |
| 256 testInputStreamBadOffset(); |
201 // Check the length of these files as both are text files where one | 257 // Check the length of these files as both are text files where one |
202 // is without a terminating line separator which can easily be added | 258 // is without a terminating line separator which can easily be added |
203 // back if accidentally opened in a text editor. | 259 // back if accidentally opened in a text editor. |
204 testStringLineTransformerEnding("readline_test1.dat", 111); | 260 testStringLineTransformerEnding("readline_test1.dat", 111); |
205 testStringLineTransformerEnding("readline_test2.dat", 114); | 261 testStringLineTransformerEnding("readline_test2.dat", 114); |
206 } | 262 } |
OLD | NEW |