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 // This test implements a new special interface that can be used to |
| 10 // send data more efficiently between two converters. |
| 11 |
| 12 abstract class MyChunkedIntSink extends ChunkedConversionSink<int> { |
| 13 MyChunkedIntSink(); |
| 14 factory MyChunkedIntSink.from(sink) = IntAdapterSink; |
| 15 factory MyChunkedIntSink.withCallback(callback) { |
| 16 var sink = new ChunkedConversionSink.withCallback(callback); |
| 17 return new MyChunkedIntSink.from(sink); |
| 18 } |
| 19 |
| 20 add(int i); |
| 21 close(); |
| 22 |
| 23 // The special method. |
| 24 specialI(i); |
| 25 } |
| 26 |
| 27 class IntAdapterSink extends MyChunkedIntSink { |
| 28 final _sink; |
| 29 IntAdapterSink(this._sink); |
| 30 add(o) => _sink.add(o); |
| 31 close() => _sink.close(); |
| 32 specialI(o) => add(o); |
| 33 } |
| 34 |
| 35 class MyChunkedBoolSink extends ChunkedConversionSink<bool> { |
| 36 MyChunkedBoolSink(); |
| 37 factory MyChunkedBoolSink.from(sink) = BoolAdapterSink; |
| 38 factory MyChunkedBoolSink.withCallback(callback) { |
| 39 var sink = new ChunkedConversionSink.withCallback(callback); |
| 40 return new MyChunkedBoolSink.from(sink); |
| 41 } |
| 42 |
| 43 add(bool b); |
| 44 close(); |
| 45 |
| 46 specialB(bool b); |
| 47 } |
| 48 |
| 49 class BoolAdapterSink extends MyChunkedBoolSink { |
| 50 final _sink; |
| 51 BoolAdapterSink(this._sink); |
| 52 add(o) => _sink.add(o); |
| 53 close() => _sink.close(); |
| 54 specialB(o) => add(o); |
| 55 } |
| 56 |
| 57 class IntBoolConverter1 extends Converter<List<int>, List<bool>> { |
| 58 List<bool> convert(List<int> input) => input.map((x) => x > 0).toList(); |
| 59 |
| 60 startChunkedConversion(sink) { |
| 61 if (sink is! MyChunkedBoolSink) sink = new MyChunkedBoolSink.from(sink); |
| 62 return new IntBoolConverter1Sink(sink); |
| 63 } |
| 64 } |
| 65 |
| 66 class BoolIntConverter1 extends Converter<List<bool>, List<int>> { |
| 67 List<int> convert(List<bool> input) => input.map((x) => x ? 1 : 0).toList(); |
| 68 |
| 69 startChunkedConversion(sink) { |
| 70 if (sink is! MyChunkedIntSink) sink = new MyChunkedIntSink.from(sink); |
| 71 return new BoolIntConverter1Sink(sink); |
| 72 } |
| 73 } |
| 74 |
| 75 int specialICounter = 0; |
| 76 int specialBCounter = 0; |
| 77 |
| 78 class IntBoolConverter1Sink extends MyChunkedIntSink { |
| 79 var outSink; |
| 80 IntBoolConverter1Sink(this.outSink); |
| 81 |
| 82 add(int i) { |
| 83 outSink.specialB(i > 0); |
| 84 } |
| 85 specialI(int i) { |
| 86 specialICounter++; |
| 87 add(i); |
| 88 } |
| 89 close() => outSink.close(); |
| 90 } |
| 91 |
| 92 class BoolIntConverter1Sink extends MyChunkedBoolSink { |
| 93 var outSink; |
| 94 BoolIntConverter1Sink(this.outSink); |
| 95 |
| 96 add(bool b) { |
| 97 outSink.specialI(b ? 1 : 0); |
| 98 } |
| 99 |
| 100 specialB(bool b) { |
| 101 specialBCounter++; |
| 102 add(b); |
| 103 } |
| 104 close() => outSink.close(); |
| 105 } |
| 106 |
| 107 class IdentityConverter extends Converter { |
| 108 convert(x) => x; |
| 109 |
| 110 startChunkedConversion(sink) { |
| 111 return new IdentitySink(sink); |
| 112 } |
| 113 } |
| 114 |
| 115 class IdentitySink extends ChunkedConversionSink { |
| 116 final _sink; |
| 117 IdentitySink(this._sink); |
| 118 add(o) => _sink.add(o); |
| 119 close() => _sink.close(); |
| 120 } |
| 121 |
| 122 main() { |
| 123 var converter1, converter2, intSink, intSink2, hasExecuted, boolSink, fused; |
| 124 var converter3, fused2, sink, sink2; |
| 125 |
| 126 // Test int->bool converter individually. |
| 127 converter1 = new IntBoolConverter1(); |
| 128 Expect.listEquals([true, false, true], converter1.convert([2, -2, 2])); |
| 129 hasExecuted = false; |
| 130 boolSink = new MyChunkedBoolSink.withCallback((value) { |
| 131 hasExecuted = true; |
| 132 Expect.listEquals([true, false, true], value); |
| 133 }); |
| 134 intSink = converter1.startChunkedConversion(boolSink); |
| 135 intSink.add(3); |
| 136 intSink.specialI(-3); |
| 137 intSink.add(3); |
| 138 intSink.close(); |
| 139 Expect.isTrue(hasExecuted); |
| 140 Expect.equals(1, specialICounter); |
| 141 specialICounter = 0; |
| 142 hasExecuted = false; |
| 143 |
| 144 // Test bool->int converter individually. |
| 145 converter2 = new BoolIntConverter1(); |
| 146 Expect.listEquals([1, 0, 1], converter2.convert([true, false, true])); |
| 147 hasExecuted = false; |
| 148 intSink = new MyChunkedIntSink.withCallback((value) { |
| 149 hasExecuted = true; |
| 150 Expect.listEquals([1, 0, 1], value); |
| 151 }); |
| 152 boolSink = converter2.startChunkedConversion(intSink); |
| 153 boolSink.specialB(true); |
| 154 boolSink.add(false); |
| 155 boolSink.add(true); |
| 156 boolSink.close(); |
| 157 Expect.isTrue(hasExecuted); |
| 158 Expect.equals(1, specialBCounter); |
| 159 specialBCounter = 0; |
| 160 hasExecuted = false; |
| 161 |
| 162 // Test identity converter indidivually. |
| 163 converter3 = new IdentityConverter(); |
| 164 hasExecuted = false; |
| 165 sink = new ChunkedConversionSink.withCallback((value) { |
| 166 hasExecuted = true; |
| 167 Expect.listEquals([1, 2, 3], value); |
| 168 }); |
| 169 sink2 = converter3.startChunkedConversion(sink); |
| 170 [1, 2, 3].forEach(sink2.add); |
| 171 sink2.close(); |
| 172 Expect.isTrue(hasExecuted); |
| 173 hasExecuted = false; |
| 174 |
| 175 // Test fused converters. |
| 176 fused = converter1.fuse(converter2); |
| 177 Expect.listEquals([1, 0, 1], fused.convert([2, -2, 2])); |
| 178 hasExecuted = false; |
| 179 intSink2 = new MyChunkedIntSink.withCallback((value) { |
| 180 hasExecuted = true; |
| 181 Expect.listEquals([1, 0, 1], value); |
| 182 }); |
| 183 intSink = fused.startChunkedConversion(intSink2); |
| 184 intSink.specialI(3); |
| 185 intSink.add(-3); |
| 186 intSink.add(3); |
| 187 intSink.close(); |
| 188 Expect.isTrue(hasExecuted); |
| 189 Expect.equals(3, specialBCounter); |
| 190 specialBCounter = 0; |
| 191 Expect.equals(1, specialICounter); |
| 192 specialICounter = 0; |
| 193 hasExecuted = false; |
| 194 |
| 195 // With identity in front. |
| 196 fused2 = converter3.fuse(fused); |
| 197 hasExecuted = false; |
| 198 intSink2 = new MyChunkedIntSink.withCallback((value) { |
| 199 hasExecuted = true; |
| 200 Expect.listEquals([1, 0, 1], value); |
| 201 }); |
| 202 sink = fused2.startChunkedConversion(intSink2); |
| 203 Expect.isFalse(sink is MyChunkedIntSink); |
| 204 sink.add(3); |
| 205 sink.add(-3); |
| 206 sink.add(3); |
| 207 sink.close(); |
| 208 Expect.isTrue(hasExecuted); |
| 209 Expect.equals(3, specialBCounter); |
| 210 specialBCounter = 0; |
| 211 Expect.equals(0, specialICounter); |
| 212 specialICounter = 0; |
| 213 hasExecuted = false; |
| 214 |
| 215 // With identity at the end. |
| 216 fused2 = fused.fuse(converter3); |
| 217 hasExecuted = false; |
| 218 sink = new ChunkedConversionSink.withCallback((value) { |
| 219 hasExecuted = true; |
| 220 Expect.listEquals([1, 0, 1], value); |
| 221 }); |
| 222 intSink = fused2.startChunkedConversion(sink); |
| 223 Expect.isTrue(intSink is MyChunkedIntSink); |
| 224 intSink.specialI(3); |
| 225 intSink.add(-3); |
| 226 intSink.specialI(3); |
| 227 intSink.close(); |
| 228 Expect.isTrue(hasExecuted); |
| 229 Expect.equals(3, specialBCounter); |
| 230 specialBCounter = 0; |
| 231 Expect.equals(2, specialICounter); |
| 232 specialICounter = 0; |
| 233 hasExecuted = false; |
| 234 |
| 235 // With identity between the two converters. |
| 236 fused = converter1.fuse(converter3).fuse(converter2); |
| 237 Expect.listEquals([1, 0, 1], fused.convert([2, -2, 2])); |
| 238 hasExecuted = false; |
| 239 intSink2 = new MyChunkedIntSink.withCallback((value) { |
| 240 hasExecuted = true; |
| 241 Expect.listEquals([1, 0, 1], value); |
| 242 }); |
| 243 intSink = fused.startChunkedConversion(intSink2); |
| 244 intSink.specialI(3); |
| 245 intSink.add(-3); |
| 246 intSink.add(3); |
| 247 intSink.close(); |
| 248 Expect.isTrue(hasExecuted); |
| 249 Expect.equals(0, specialBCounter); |
| 250 specialBCounter = 0; |
| 251 Expect.equals(1, specialICounter); |
| 252 specialICounter = 0; |
| 253 hasExecuted = false; |
| 254 } |
OLD | NEW |