| 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 "package:expect/expect.dart"; | |
| 6 import "dart:async"; | |
| 7 import "dart:io"; | |
| 8 | |
| 9 const UNICODE_REPLACEMENT_CHARACTER_RUNE = 0xFFFD; | |
| 10 | |
| 11 void testTransform() { | |
| 12 // Code point U+10FFFF is the largest code point supported by Dart. | |
| 13 var controller = new StreamController(sync: true); | |
| 14 controller.add([0xf0, 0x90, 0x80, 0x80]); // U+10000 | |
| 15 controller.add([0xf4, 0x8f, 0xbf, 0xbf]); // U+10FFFF | |
| 16 controller.add([0xf4, 0x90, 0x80, 0x80]); // U+110000 | |
| 17 controller.add([0xfa, 0x80, 0x80, 0x80, 0x80]); // U+2000000 | |
| 18 controller.add([0xfd, 0x80, 0x80, 0x80, 0x80, 0x80]); // U+40000000 | |
| 19 controller.close(); | |
| 20 | |
| 21 var decoder = new StringDecoder(Encoding.UTF_8); | |
| 22 var stream = controller.stream.transform(decoder); | |
| 23 stream.fold( | |
| 24 new StringBuffer(), | |
| 25 (b, e) { | |
| 26 b.write(e); | |
| 27 return b; | |
| 28 }) | |
| 29 .then((b) => b.toString()) | |
| 30 .then((decoded) { | |
| 31 Expect.equals(16, decoded.length); | |
| 32 | |
| 33 var replacementChar = UNICODE_REPLACEMENT_CHARACTER_RUNE; | |
| 34 Expect.equals(0xd800, decoded.codeUnitAt(0)); | |
| 35 Expect.equals(0xdc00, decoded.codeUnitAt(1)); | |
| 36 Expect.equals(0xdbff, decoded.codeUnitAt(2)); | |
| 37 Expect.equals(0xdfff, decoded.codeUnitAt(3)); | |
| 38 for (int i = 4; i < 16; i++) { | |
| 39 Expect.equals(replacementChar, decoded.codeUnitAt(i)); | |
| 40 } | |
| 41 }); | |
| 42 } | |
| 43 | |
| 44 void testDecode() { | |
| 45 // Code point U+10FFFF is the largest code point supported by Dart. | |
| 46 var controller = new StreamController(sync: true); | |
| 47 controller.add([0xf0, 0x90, 0x80, 0x80]); // U+10000 | |
| 48 controller.add([0xf4, 0x8f, 0xbf, 0xbf]); // U+10FFFF | |
| 49 controller.add([0xf4, 0x90, 0x80, 0x80]); // U+110000 | |
| 50 controller.add([0xfa, 0x80, 0x80, 0x80, 0x80]); // U+2000000 | |
| 51 controller.add([0xfd, 0x80, 0x80, 0x80, 0x80, 0x80]); // U+40000000 | |
| 52 controller.close(); | |
| 53 | |
| 54 StringDecoder.decode(controller.stream, Encoding.UTF_8) | |
| 55 .then((decoded) { | |
| 56 Expect.equals(16, decoded.length); | |
| 57 | |
| 58 var replacementChar = UNICODE_REPLACEMENT_CHARACTER_RUNE; | |
| 59 Expect.equals(0xd800, decoded.codeUnitAt(0)); | |
| 60 Expect.equals(0xdc00, decoded.codeUnitAt(1)); | |
| 61 Expect.equals(0xdbff, decoded.codeUnitAt(2)); | |
| 62 Expect.equals(0xdfff, decoded.codeUnitAt(3)); | |
| 63 for (int i = 4; i < 16; i++) { | |
| 64 Expect.equals(replacementChar, decoded.codeUnitAt(i)); | |
| 65 } | |
| 66 }); | |
| 67 } | |
| 68 | |
| 69 void testInvalid() { | |
| 70 void invalid(var bytes, var outputLength) { | |
| 71 var controller = new StreamController(sync: true); | |
| 72 controller.add(bytes); | |
| 73 controller.close(); | |
| 74 controller.stream.transform(new StringDecoder()).listen((string) { | |
| 75 Expect.equals(outputLength, string.length); | |
| 76 for (var i = 0; i < outputLength; i++) { | |
| 77 Expect.equals(UNICODE_REPLACEMENT_CHARACTER_RUNE, | |
| 78 string.codeUnitAt(i)); | |
| 79 } | |
| 80 }); | |
| 81 } | |
| 82 | |
| 83 invalid([0x80], 1); | |
| 84 invalid([0xff], 1); | |
| 85 invalid([0xf0, 0xc0], 1); | |
| 86 invalid([0xc0, 0x80], 1); | |
| 87 invalid([0xfd, 0x80, 0x80], 3); // Unfinished encoding. | |
| 88 } | |
| 89 | |
| 90 void main() { | |
| 91 testTransform(); | |
| 92 testDecode(); | |
| 93 testInvalid(); | |
| 94 } | |
| OLD | NEW |