OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:convert'; |
| 6 |
| 7 import 'package:expect/expect.dart'; |
| 8 |
| 9 class MyChunkedIntInterface extends ChunkedConversionInterface { |
| 10 const MyChunkedIntInterface(); |
| 11 |
| 12 ChunkedConversionSink adapt(ChunkedConversionSink sink) { |
| 13 if (sink.interface == MyChunkedIntSink.INTERFACE) return sink; |
| 14 Expect.fail("should only be called with correct sink"); |
| 15 } |
| 16 } |
| 17 |
| 18 abstract class MyChunkedIntSink extends ChunkedConversionSink<List<int>, int> { |
| 19 static const INTERFACE = const MyChunkedIntInterface(); |
| 20 |
| 21 addNonChunked(List<int> list); |
| 22 add(int i); |
| 23 close(); |
| 24 |
| 25 get interface => INTERFACE; |
| 26 } |
| 27 |
| 28 class MyChunkedCallbackIntSink extends MyChunkedIntSink { |
| 29 final Function _callback; |
| 30 final _accumulator = <int>[]; |
| 31 |
| 32 MyChunkedCallbackIntSink(void callback(List<int> value, bool wasChunked)) |
| 33 : this._callback = callback; |
| 34 |
| 35 addNonChunked(List<int> list) => _callback(list, false); |
| 36 add(int i) => _accumulator.add(i); |
| 37 close() => _callback(_accumulator, true); |
| 38 } |
| 39 |
| 40 class MyChunkedBoolInterface extends ChunkedConversionInterface { |
| 41 const MyChunkedBoolInterface(); |
| 42 |
| 43 ChunkedConversionSink adapt(ChunkedConversionSink sink) { |
| 44 if (sink.interface == MyChunkedBoolSink.INTERFACE) return sink; |
| 45 Expect.fail("should only be called with correct sink"); |
| 46 } |
| 47 } |
| 48 |
| 49 class MyChunkedBoolSink extends ChunkedConversionSink<List<bool>, bool> { |
| 50 static const INTERFACE = const MyChunkedBoolInterface(); |
| 51 |
| 52 addNonChunked(List<bool> list); |
| 53 add(bool b); |
| 54 close(); |
| 55 |
| 56 get interface => INTERFACE; |
| 57 } |
| 58 |
| 59 |
| 60 class MyChunkedCallbackBoolSink extends MyChunkedBoolSink { |
| 61 static const INTERFACE = const MyChunkedBoolInterface(); |
| 62 final Function _callback; |
| 63 final _accumulator = <bool>[]; |
| 64 |
| 65 MyChunkedCallbackBoolSink(void callback(List<bool> value, bool wasChunked)) |
| 66 : this._callback = callback; |
| 67 |
| 68 addNonChunked(List<bool> list) => _callback(list, false); |
| 69 add(bool b) => _accumulator.add(b); |
| 70 close() => _callback(_accumulator, true); |
| 71 |
| 72 get interface => INTERFACE; |
| 73 } |
| 74 |
| 75 |
| 76 class IntBoolConverter1 extends Converter<List<int>, List<bool>> { |
| 77 List<bool> convert(List<int> input) => input.map((x) => x > 0).toList(); |
| 78 |
| 79 startChunkedConversion(sink) { |
| 80 return new IntBoolConverter1Sink(outputInterface.adapt(sink)); |
| 81 } |
| 82 |
| 83 get outputInterface => MyChunkedBoolSink.INTERFACE; |
| 84 get inputInterface => MyChunkedIntSink.INTERFACE; |
| 85 } |
| 86 |
| 87 class BoolIntConverter1 extends Converter<List<bool>, List<int>> { |
| 88 List<int> convert(List<bool> input) => input.map((x) => x ? 1 : 0).toList(); |
| 89 |
| 90 startChunkedConversion(sink) { |
| 91 return new BoolIntConverter1Sink(outputInterface.adapt(sink)); |
| 92 } |
| 93 |
| 94 get outputInterface => MyChunkedIntSink.INTERFACE; |
| 95 get inputInterface => MyChunkedBoolSink.INTERFACE; |
| 96 } |
| 97 |
| 98 int chunkedConversionCounter = 0; |
| 99 |
| 100 class IntBoolConverter1Sink extends MyChunkedIntSink { |
| 101 var outSink; |
| 102 IntBoolConverter1Sink(this.outSink); |
| 103 |
| 104 add(int i) { |
| 105 chunkedConversionCounter++; |
| 106 outSink.add(i > 0); |
| 107 } |
| 108 close() => outSink.close(); |
| 109 addNonChunked(List<int> list) => list.forEach((x) => outSink.add(x > 0)); |
| 110 } |
| 111 |
| 112 class BoolIntConverter1Sink extends MyChunkedBoolSink { |
| 113 var outSink; |
| 114 BoolIntConverter1Sink(this.outSink); |
| 115 |
| 116 add(bool b) { |
| 117 chunkedConversionCounter++; |
| 118 outSink.add(b ? 1 : 0); |
| 119 } |
| 120 close() => outSink.close(); |
| 121 addNonChunked(List<bool> list) => list.forEach((x) => outSink.add(x ? 1 : 0)); |
| 122 } |
| 123 |
| 124 main() { |
| 125 var converter1, converter2, intSink, intSink2, hasExecuted, boolSink, fused; |
| 126 |
| 127 // Test int->bool converter individually. |
| 128 converter1 = new IntBoolConverter1(); |
| 129 Expect.listEquals([true, false, true], converter1.convert([2, -2, 2])); |
| 130 hasExecuted = false; |
| 131 boolSink = new MyChunkedCallbackBoolSink((value, wasChunked) { |
| 132 hasExecuted = true; |
| 133 Expect.listEquals([true, false, true], value); |
| 134 Expect.isTrue(wasChunked); |
| 135 }); |
| 136 intSink = converter1.startChunkedConversion(boolSink); |
| 137 intSink.add(3); |
| 138 intSink.add(-3); |
| 139 intSink.add(3); |
| 140 intSink.close(); |
| 141 Expect.isTrue(hasExecuted); |
| 142 Expect.equals(3, chunkedConversionCounter); |
| 143 chunkedConversionCounter = 0; |
| 144 hasExecuted = false; |
| 145 |
| 146 // Test bool->int converter individually. |
| 147 converter2 = new BoolIntConverter1(); |
| 148 Expect.listEquals([1, 0, 1], converter2.convert([true, false, true])); |
| 149 hasExecuted = false; |
| 150 intSink = new MyChunkedCallbackIntSink((value, wasChunked) { |
| 151 hasExecuted = true; |
| 152 Expect.listEquals([1, 0, 1], value); |
| 153 Expect.isTrue(wasChunked); |
| 154 }); |
| 155 boolSink = converter2.startChunkedConversion(intSink); |
| 156 boolSink.add(true); |
| 157 boolSink.add(false); |
| 158 boolSink.add(true); |
| 159 boolSink.close(); |
| 160 Expect.isTrue(hasExecuted); |
| 161 Expect.equals(3, chunkedConversionCounter); |
| 162 chunkedConversionCounter = 0; |
| 163 hasExecuted = false; |
| 164 |
| 165 // Test fused converters. |
| 166 fused = converter1.fuse(converter2); |
| 167 Expect.listEquals([1, 0, 1], fused.convert([2, -2, 2])); |
| 168 hasExecuted = false; |
| 169 intSink2 = new MyChunkedCallbackIntSink((value, wasChunked) { |
| 170 hasExecuted = true; |
| 171 Expect.listEquals([1, 0, 1], value); |
| 172 Expect.isTrue(wasChunked); |
| 173 }); |
| 174 intSink = fused.startChunkedConversion(intSink2); |
| 175 intSink.add(3); |
| 176 intSink.add(-3); |
| 177 intSink.add(3); |
| 178 intSink.close(); |
| 179 Expect.isTrue(hasExecuted); |
| 180 Expect.equals(6, chunkedConversionCounter); |
| 181 chunkedConversionCounter = 0; |
| 182 hasExecuted = false; |
| 183 } |
OLD | NEW |