Index: tests/lib/convert/chunked_conversion1_test.dart |
diff --git a/tests/lib/convert/chunked_conversion1_test.dart b/tests/lib/convert/chunked_conversion1_test.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e563b5e1fdefc7a63b5384eaa57f142356ea69e9 |
--- /dev/null |
+++ b/tests/lib/convert/chunked_conversion1_test.dart |
@@ -0,0 +1,183 @@ |
+// Copyright (c) 2013, 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. |
+ |
+import 'dart:convert'; |
+ |
+import 'package:expect/expect.dart'; |
+ |
+class MyChunkedIntInterface extends ChunkedConversionInterface { |
+ const MyChunkedIntInterface(); |
+ |
+ ChunkedConversionSink adapt(ChunkedConversionSink sink) { |
+ if (sink.interface == MyChunkedIntSink.INTERFACE) return sink; |
+ Expect.fail("should only be called with correct sink"); |
+ } |
+} |
+ |
+abstract class MyChunkedIntSink extends ChunkedConversionSink<List<int>, int> { |
+ static const INTERFACE = const MyChunkedIntInterface(); |
+ |
+ addNonChunked(List<int> list); |
+ add(int i); |
+ close(); |
+ |
+ get interface => INTERFACE; |
+} |
+ |
+class MyChunkedCallbackIntSink extends MyChunkedIntSink { |
+ final Function _callback; |
+ final _accumulator = <int>[]; |
+ |
+ MyChunkedCallbackIntSink(void callback(List<int> value, bool wasChunked)) |
+ : this._callback = callback; |
+ |
+ addNonChunked(List<int> list) => _callback(list, false); |
+ add(int i) => _accumulator.add(i); |
+ close() => _callback(_accumulator, true); |
+} |
+ |
+class MyChunkedBoolInterface extends ChunkedConversionInterface { |
+ const MyChunkedBoolInterface(); |
+ |
+ ChunkedConversionSink adapt(ChunkedConversionSink sink) { |
+ if (sink.interface == MyChunkedBoolSink.INTERFACE) return sink; |
+ Expect.fail("should only be called with correct sink"); |
+ } |
+} |
+ |
+class MyChunkedBoolSink extends ChunkedConversionSink<List<bool>, bool> { |
+ static const INTERFACE = const MyChunkedBoolInterface(); |
+ |
+ addNonChunked(List<bool> list); |
+ add(bool b); |
+ close(); |
+ |
+ get interface => INTERFACE; |
+} |
+ |
+ |
+class MyChunkedCallbackBoolSink extends MyChunkedBoolSink { |
+ static const INTERFACE = const MyChunkedBoolInterface(); |
+ final Function _callback; |
+ final _accumulator = <bool>[]; |
+ |
+ MyChunkedCallbackBoolSink(void callback(List<bool> value, bool wasChunked)) |
+ : this._callback = callback; |
+ |
+ addNonChunked(List<bool> list) => _callback(list, false); |
+ add(bool b) => _accumulator.add(b); |
+ close() => _callback(_accumulator, true); |
+ |
+ get interface => INTERFACE; |
+} |
+ |
+ |
+class IntBoolConverter1 extends Converter<List<int>, List<bool>> { |
+ List<bool> convert(List<int> input) => input.map((x) => x > 0).toList(); |
+ |
+ startChunkedConversion(sink) { |
+ return new IntBoolConverter1Sink(outputInterface.adapt(sink)); |
+ } |
+ |
+ get outputInterface => MyChunkedBoolSink.INTERFACE; |
+ get inputInterface => MyChunkedIntSink.INTERFACE; |
+} |
+ |
+class BoolIntConverter1 extends Converter<List<bool>, List<int>> { |
+ List<int> convert(List<bool> input) => input.map((x) => x ? 1 : 0).toList(); |
+ |
+ startChunkedConversion(sink) { |
+ return new BoolIntConverter1Sink(outputInterface.adapt(sink)); |
+ } |
+ |
+ get outputInterface => MyChunkedIntSink.INTERFACE; |
+ get inputInterface => MyChunkedBoolSink.INTERFACE; |
+} |
+ |
+int chunkedConversionCounter = 0; |
+ |
+class IntBoolConverter1Sink extends MyChunkedIntSink { |
+ var outSink; |
+ IntBoolConverter1Sink(this.outSink); |
+ |
+ add(int i) { |
+ chunkedConversionCounter++; |
+ outSink.add(i > 0); |
+ } |
+ close() => outSink.close(); |
+ addNonChunked(List<int> list) => list.forEach((x) => outSink.add(x > 0)); |
+} |
+ |
+class BoolIntConverter1Sink extends MyChunkedBoolSink { |
+ var outSink; |
+ BoolIntConverter1Sink(this.outSink); |
+ |
+ add(bool b) { |
+ chunkedConversionCounter++; |
+ outSink.add(b ? 1 : 0); |
+ } |
+ close() => outSink.close(); |
+ addNonChunked(List<bool> list) => list.forEach((x) => outSink.add(x ? 1 : 0)); |
+} |
+ |
+main() { |
+ var converter1, converter2, intSink, intSink2, hasExecuted, boolSink, fused; |
+ |
+ // Test int->bool converter individually. |
+ converter1 = new IntBoolConverter1(); |
+ Expect.listEquals([true, false, true], converter1.convert([2, -2, 2])); |
+ hasExecuted = false; |
+ boolSink = new MyChunkedCallbackBoolSink((value, wasChunked) { |
+ hasExecuted = true; |
+ Expect.listEquals([true, false, true], value); |
+ Expect.isTrue(wasChunked); |
+ }); |
+ intSink = converter1.startChunkedConversion(boolSink); |
+ intSink.add(3); |
+ intSink.add(-3); |
+ intSink.add(3); |
+ intSink.close(); |
+ Expect.isTrue(hasExecuted); |
+ Expect.equals(3, chunkedConversionCounter); |
+ chunkedConversionCounter = 0; |
+ hasExecuted = false; |
+ |
+ // Test bool->int converter individually. |
+ converter2 = new BoolIntConverter1(); |
+ Expect.listEquals([1, 0, 1], converter2.convert([true, false, true])); |
+ hasExecuted = false; |
+ intSink = new MyChunkedCallbackIntSink((value, wasChunked) { |
+ hasExecuted = true; |
+ Expect.listEquals([1, 0, 1], value); |
+ Expect.isTrue(wasChunked); |
+ }); |
+ boolSink = converter2.startChunkedConversion(intSink); |
+ boolSink.add(true); |
+ boolSink.add(false); |
+ boolSink.add(true); |
+ boolSink.close(); |
+ Expect.isTrue(hasExecuted); |
+ Expect.equals(3, chunkedConversionCounter); |
+ chunkedConversionCounter = 0; |
+ hasExecuted = false; |
+ |
+ // Test fused converters. |
+ fused = converter1.fuse(converter2); |
+ Expect.listEquals([1, 0, 1], fused.convert([2, -2, 2])); |
+ hasExecuted = false; |
+ intSink2 = new MyChunkedCallbackIntSink((value, wasChunked) { |
+ hasExecuted = true; |
+ Expect.listEquals([1, 0, 1], value); |
+ Expect.isTrue(wasChunked); |
+ }); |
+ intSink = fused.startChunkedConversion(intSink2); |
+ intSink.add(3); |
+ intSink.add(-3); |
+ intSink.add(3); |
+ intSink.close(); |
+ Expect.isTrue(hasExecuted); |
+ Expect.equals(6, chunkedConversionCounter); |
+ chunkedConversionCounter = 0; |
+ hasExecuted = false; |
+} |