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