Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1527)

Unified Diff: tests/standalone/src/ChunkedStreamTest.dart

Issue 8818009: Add chunked input stream (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added ChunkedStreamTest.dart Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tests/standalone/src/ChunkedStreamTest.dart
diff --git a/tests/standalone/src/ChunkedStreamTest.dart b/tests/standalone/src/ChunkedStreamTest.dart
new file mode 100644
index 0000000000000000000000000000000000000000..1d91c168222a9eb7e0bcce26c5af3f38f64001b7
--- /dev/null
+++ b/tests/standalone/src/ChunkedStreamTest.dart
@@ -0,0 +1,160 @@
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+class ListInputStream implements InputStream {
+ List<int> read() {
+ var result = _data;
+ _data = null;
+ return result;
+ }
+
+ void set dataHandler(void callback()) {
+ _dataHandler = callback;
+ }
+
+ void set closeHandler(void callback()) {
+ _closeHandler = callback;
+ }
+
+ void set errorHandler(void callback(int error)) {
+ }
+
+ void write(List<int> data) {
+ Expect.equals(null, _data);
+ _data = data;
+ if (_dataHandler != null) {
+ // Data handler is not called through the event loop here.
+ _dataHandler();
+ }
+ }
+
+ void close() {
+ Expect.equals(null, _data);
+ // Close handler is not called through the event loop here.
+ if (_closeHandler != null) _closeHandler();
+ }
+
+ List<int> _data;
+ var _dataHandler;
+ var _closeHandler;
+}
+
+void test1() {
+ void testWithChunkSize(var data, int chunkSize, Function testDone) {
+ InputStream s = new ListInputStream();
+ ChunkedInputStream stream = new ChunkedInputStream(s);
+ int chunkCount = 0;
+ int byteCount = 0;
+ void chunkData() {
+ List<int> chunk = stream.read();
+ if (byteCount + chunkSize < data.length) {
+ Expect.equals(chunkSize, chunk.length);
+ } else {
+ if (byteCount == data.length) {
+ Expect.equals(null, chunk);
+ } else {
+ Expect.equals(data.length - byteCount, chunk.length);
+ }
+ }
+ if (chunk != null) {
+ for (int i = 0; i < chunk.length;i++) {
+ Expect.equals(data[byteCount], chunk[i]);
+ byteCount++;
+ }
+ chunkCount++;
+ }
+ }
+
+ void closeHandler() {
+ Expect.equals(data.length, byteCount);
+ testDone(byteCount);
+ }
+
+ stream.dataHandler = chunkData;
+ stream.closeHandler = closeHandler;
+ stream.chunkSize = chunkSize;
+ s.write(data);
+ s.close();
+ }
+
+ for (int i = 1; i <= 10; i++) {
+ var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
+
+ void testDone(int byteCount) {
+ Expect.equals(data.length, byteCount);
+ }
+
+ testWithChunkSize(data, i, testDone);
+ }
+
+ var _16k = 1024 * 16;
Mads Ager (google) 2011/12/06 14:05:02 Is the underscore needed here?
Søren Gjesse 2011/12/07 07:58:29 Yes, a variable cannot start with a digit.
+ var data = new List<int>(_16k);
+ for (int i = 0; i < _16k; i++) { data[i] = i % 256; }
+
+ void testDone(int byteCount) {
+ Expect.equals(data.length, byteCount);
+ }
+
+ testWithChunkSize(data, 512, testDone);
+ testWithChunkSize(data, 1024, testDone);
+ testWithChunkSize(data, 2048, testDone);
+}
+
+
+void test2() {
+ InputStream s = new ListInputStream();
+ ChunkedInputStream stream = new ChunkedInputStream(s);
+ stream.chunkSize = 5;
+
+ var stage = 0;
+
+ void chunkData() {
+ var chunk;
+ if (stage == 0) {
+ chunk = stream.read();
Mads Ager (google) 2011/12/06 14:05:02 Maybe add comments here static how many bytes are
Søren Gjesse 2011/12/07 07:58:29 Done.
+ Expect.equals(stream.chunkSize, chunk.length);
+ chunk = stream.read();
+ Expect.equals(null, chunk);
+ stage++;
+ s.write([7, 8, 9]);
+ } else if (stage == 1) {
+ chunk = stream.read();
+ Expect.equals(stream.chunkSize, chunk.length);
+ chunk = stream.read();
+ Expect.equals(null, chunk);
+ stage++;
+ s.write([10, 11, 12, 13, 14, 15, 16]);
+ } else if (stage == 2) {
+ chunk = stream.read();
+ Expect.equals(stream.chunkSize, chunk.length);
+ chunk = stream.read();
+ Expect.equals(null, chunk);
+ stage++;
+ s.close();
+ } else if (stage == 3) {
+ chunk = stream.read();
+ Expect.equals(2, chunk.length);
+ chunk = stream.read();
+ Expect.equals(null, chunk);
+ stage++;
+ }
+ }
+
+ void streamClosed() {
+ Expect.equals(4, stage);
+ }
+
+ stream.dataHandler = chunkData;
+ stream.closeHandler = streamClosed;
+ s.write([0, 1, 2, 3]);
+ Expect.equals(0, stage);
+ s.write([4, 5, 6]);
+ Expect.equals(3, stage);
+}
+
+
Mads Ager (google) 2011/12/06 14:05:02 It would be nice to also have a test that changes
Søren Gjesse 2011/12/07 07:58:29 Good point. Done.
+main() {
+ test1();
+ test2();
+}

Powered by Google App Engine
This is Rietveld 408576698