| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 #library("utf8_tests"); | 6 #library("utf8_tests"); |
| 7 #import("dunit.dart"); | 7 #import("dunit.dart"); |
| 8 #import("../../string_encoding/unicode.dart"); | 8 #import("../../string_encoding/unicode.dart"); |
| 9 #import("../../string_encoding/unicode_core.dart"); | 9 #import("../../string_encoding/unicode_core.dart"); |
| 10 #source("../../string_encoding/utf8_impl.dart"); | 10 #source("../../string_encoding/utf8_impl.dart"); |
| 11 | 11 |
| 12 void main() { | 12 void main() { |
| 13 TestSuite suite = new TestSuite(); | 13 TestSuite suite = new TestSuite(); |
| 14 suite.registerTestClass(new Utf8Tests()); | 14 suite.registerTestClass(new Utf8Tests()); |
| 15 suite.run(); | 15 suite.run(); |
| 16 } | 16 } |
| 17 | 17 |
| 18 class Utf8Tests extends TestClass { | 18 class Utf8Tests extends TestClass { |
| 19 static final String testEnglishPhrase = | 19 static final String testEnglishPhrase = |
| 20 "The quick brown fox jumps over the lazy dog."; | 20 "The quick brown fox jumps over the lazy dog."; |
| 21 | 21 |
| 22 static final List<int> testEnglishUtf8 = const<int> [ | 22 static final List<int> testEnglishUtf8 = const<int> [ |
| 23 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, | 23 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, |
| 24 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, | 24 0x6b, 0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, |
| 25 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, | 25 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, |
| 26 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, | 26 0x73, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x20, 0x74, |
| 27 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, | 27 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, |
| 28 0x64, 0x6f, 0x67, 0x2e]; | 28 0x64, 0x6f, 0x67, 0x2e]; |
| 29 | 29 |
| 30 static final String testDanishPhrase = "Quizdeltagerne spiste jordbær med " + | 30 static final String testDanishPhrase = "Quizdeltagerne spiste jordbær med " + |
| 31 "fløde mens cirkusklovnen Wolther spillede på xylofon."; | 31 "fløde mens cirkusklovnen Wolther spillede på xylofon."; |
| 32 | 32 |
| 33 static final List<int> testDanishUtf8 = const<int>[ | 33 static final List<int> testDanishUtf8 = const<int>[ |
| 34 0x51, 0x75, 0x69, 0x7a, 0x64, 0x65, 0x6c, 0x74, | 34 0x51, 0x75, 0x69, 0x7a, 0x64, 0x65, 0x6c, 0x74, |
| 35 0x61, 0x67, 0x65, 0x72, 0x6e, 0x65, 0x20, 0x73, | 35 0x61, 0x67, 0x65, 0x72, 0x6e, 0x65, 0x20, 0x73, |
| 36 0x70, 0x69, 0x73, 0x74, 0x65, 0x20, 0x6a, 0x6f, | 36 0x70, 0x69, 0x73, 0x74, 0x65, 0x20, 0x6a, 0x6f, |
| 37 0x72, 0x64, 0x62, 0xc3, 0xa6, 0x72, 0x20, 0x6d, | 37 0x72, 0x64, 0x62, 0xc3, 0xa6, 0x72, 0x20, 0x6d, |
| 38 0x65, 0x64, 0x20, 0x66, 0x6c, 0xc3, 0xb8, 0x64, | 38 0x65, 0x64, 0x20, 0x66, 0x6c, 0xc3, 0xb8, 0x64, |
| 39 0x65, 0x20, 0x6d, 0x65, 0x6e, 0x73, 0x20, 0x63, | 39 0x65, 0x20, 0x6d, 0x65, 0x6e, 0x73, 0x20, 0x63, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 0xe3, 0x83, 0xa4, 0xe3, 0x83, 0x9e, 0x20, 0xe3, | 117 0xe3, 0x83, 0xa4, 0xe3, 0x83, 0x9e, 0x20, 0xe3, |
| 118 0x82, 0xb1, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xb3, | 118 0x82, 0xb1, 0xe3, 0x83, 0x95, 0xe3, 0x82, 0xb3, |
| 119 0xe3, 0x82, 0xa8, 0xe3, 0x83, 0x86, 0x20, 0xe3, | 119 0xe3, 0x82, 0xa8, 0xe3, 0x83, 0x86, 0x20, 0xe3, |
| 120 0x82, 0xa2, 0xe3, 0x82, 0xb5, 0xe3, 0x82, 0xad, | 120 0x82, 0xa2, 0xe3, 0x82, 0xb5, 0xe3, 0x82, 0xad, |
| 121 0xe3, 0x83, 0xa6, 0xe3, 0x83, 0xa1, 0xe3, 0x83, | 121 0xe3, 0x83, 0xa6, 0xe3, 0x83, 0xa1, 0xe3, 0x83, |
| 122 0x9f, 0xe3, 0x82, 0xb7, 0x20, 0xe3, 0x83, 0xb1, | 122 0x9f, 0xe3, 0x82, 0xb7, 0x20, 0xe3, 0x83, 0xb1, |
| 123 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa2, 0xe3, 0x82, | 123 0xe3, 0x83, 0x92, 0xe3, 0x83, 0xa2, 0xe3, 0x82, |
| 124 0xbb, 0xe3, 0x82, 0xb9, 0xe3, 0x83, 0xb3]; | 124 0xbb, 0xe3, 0x82, 0xb9, 0xe3, 0x83, 0xb3]; |
| 125 | 125 |
| 126 void registerTests(TestSuite suite) { | 126 void registerTests(TestSuite suite) { |
| 127 register("testUtf8bytesToCodepoints", testUtf8bytesToCodepoints, suite); | 127 register("Utf8Tests.testUtf8bytesToCodepoints", testUtf8bytesToCodepoints, |
| 128 register("testUtf8BytesToString", testUtf8BytesToString, suite); | 128 suite); |
| 129 register("testEncodeToUtf8", testEncodeToUtf8, suite); | 129 register("Utf8Tests.testUtf8BytesToString", testUtf8BytesToString, suite); |
| 130 register("Utf8Tests.testEncodeToUtf8", testEncodeToUtf8, suite); |
| 131 register("Utf8Tests.testIterableMethods", testIterableMethods, suite); |
| 130 } | 132 } |
| 131 | 133 |
| 132 void testEncodeToUtf8() { | 134 void testEncodeToUtf8() { |
| 133 Expect.listEquals(testEnglishUtf8, encodeAsUtf8(testEnglishPhrase), | 135 Expect.listEquals(testEnglishUtf8, encodeUtf8(testEnglishPhrase), |
| 134 "english to utf8"); | 136 "english to utf8"); |
| 135 | 137 |
| 136 Expect.listEquals(testDanishUtf8, encodeAsUtf8(testDanishPhrase), | 138 Expect.listEquals(testDanishUtf8, encodeUtf8(testDanishPhrase), |
| 137 "encode danish to utf8"); | 139 "encode danish to utf8"); |
| 138 | 140 |
| 139 Expect.listEquals(testHebrewUtf8, encodeAsUtf8(testHebrewPhrase), | 141 Expect.listEquals(testHebrewUtf8, encodeUtf8(testHebrewPhrase), |
| 140 "Hebrew to utf8"); | 142 "Hebrew to utf8"); |
| 141 | 143 |
| 142 Expect.listEquals(testRussianUtf8, encodeAsUtf8(testRussianPhrase), | 144 Expect.listEquals(testRussianUtf8, encodeUtf8(testRussianPhrase), |
| 143 "Russian to utf8"); | 145 "Russian to utf8"); |
| 144 | 146 |
| 145 Expect.listEquals(testGreekUtf8, encodeAsUtf8(testGreekPhrase), | 147 Expect.listEquals(testGreekUtf8, encodeUtf8(testGreekPhrase), |
| 146 "Greek to utf8"); | 148 "Greek to utf8"); |
| 147 | 149 |
| 148 Expect.listEquals(testKatakanaUtf8, encodeAsUtf8(testKatakanaPhrase), | 150 Expect.listEquals(testKatakanaUtf8, encodeUtf8(testKatakanaPhrase), |
| 149 "Katakana to utf8"); | 151 "Katakana to utf8"); |
| 150 } | 152 } |
| 151 | 153 |
| 152 void testUtf8bytesToCodepoints() { | 154 void testUtf8bytesToCodepoints() { |
| 153 Expect.listEquals([954, 972, 963, 956, 949], | 155 Expect.listEquals([954, 972, 963, 956, 949], |
| 154 _utf8ToCodepoints([0xce, 0xba, 0xcf, 0x8c, 0xcf, | 156 _utf8ToCodepoints([0xce, 0xba, 0xcf, 0x8c, 0xcf, |
| 155 0x83, 0xce, 0xbc, 0xce, 0xb5]), "κόσμε"); | 157 0x83, 0xce, 0xbc, 0xce, 0xb5]), "κόσμε"); |
| 156 | 158 |
| 157 // boundary conditions: First possible sequence of a certain length | 159 // boundary conditions: First possible sequence of a certain length |
| 160 Expect.listEquals([], _utf8ToCodepoints([]), "no input"); |
| 158 Expect.listEquals([0x0], _utf8ToCodepoints([0x0]), "0"); | 161 Expect.listEquals([0x0], _utf8ToCodepoints([0x0]), "0"); |
| 159 Expect.listEquals([0x80], _utf8ToCodepoints([0xc2, 0x80]), "80"); | 162 Expect.listEquals([0x80], _utf8ToCodepoints([0xc2, 0x80]), "80"); |
| 160 Expect.listEquals([0x800], | 163 Expect.listEquals([0x800], |
| 161 _utf8ToCodepoints([0xe0, 0xa0, 0x80]), "800"); | 164 _utf8ToCodepoints([0xe0, 0xa0, 0x80]), "800"); |
| 162 Expect.listEquals([0x10000], | 165 Expect.listEquals([0x10000], |
| 163 _utf8ToCodepoints([0xf0, 0x90, 0x80, 0x80]), "10000"); | 166 _utf8ToCodepoints([0xf0, 0x90, 0x80, 0x80]), "10000"); |
| 164 Expect.listEquals([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT], | 167 Expect.listEquals([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT], |
| 165 _utf8ToCodepoints([0xf8, 0x88, 0x80, 0x80, 0x80]), "200000"); | 168 _utf8ToCodepoints([0xf8, 0x88, 0x80, 0x80, 0x80]), "200000"); |
| 166 Expect.listEquals([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT], | 169 Expect.listEquals([UNICODE_REPLACEMENT_CHARACTER_CODEPOINT], |
| 167 _utf8ToCodepoints([0xfc, 0x84, 0x80, 0x80, 0x80, 0x80]), | 170 _utf8ToCodepoints([0xfc, 0x84, 0x80, 0x80, 0x80, 0x80]), |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 | 433 |
| 431 // Other illegal code positions (???) | 434 // Other illegal code positions (???) |
| 432 Expect.listEquals([0xfffe], _utf8ToCodepoints([0xef, 0xbf, 0xbe]), | 435 Expect.listEquals([0xfffe], _utf8ToCodepoints([0xef, 0xbf, 0xbe]), |
| 433 "U+FFFE"); | 436 "U+FFFE"); |
| 434 Expect.listEquals([0xffff], _utf8ToCodepoints([0xef, 0xbf, 0xbf]), | 437 Expect.listEquals([0xffff], _utf8ToCodepoints([0xef, 0xbf, 0xbf]), |
| 435 "U+FFFF"); | 438 "U+FFFF"); |
| 436 } | 439 } |
| 437 | 440 |
| 438 void testUtf8BytesToString() { | 441 void testUtf8BytesToString() { |
| 439 Expect.stringEquals(testEnglishPhrase, | 442 Expect.stringEquals(testEnglishPhrase, |
| 440 decodeFromUtf8(testEnglishUtf8), "English"); | 443 decodeUtf8(testEnglishUtf8), "English"); |
| 441 | 444 |
| 442 Expect.stringEquals(testDanishPhrase, | 445 Expect.stringEquals(testDanishPhrase, |
| 443 decodeFromUtf8(testDanishUtf8), "Danish"); | 446 decodeUtf8(testDanishUtf8), "Danish"); |
| 444 | 447 |
| 445 Expect.stringEquals(testHebrewPhrase, | 448 Expect.stringEquals(testHebrewPhrase, |
| 446 decodeFromUtf8(testHebrewUtf8), "Hebrew"); | 449 decodeUtf8(testHebrewUtf8), "Hebrew"); |
| 447 | 450 |
| 448 Expect.stringEquals(testRussianPhrase, | 451 Expect.stringEquals(testRussianPhrase, |
| 449 decodeFromUtf8(testRussianUtf8), "Russian"); | 452 decodeUtf8(testRussianUtf8), "Russian"); |
| 450 | 453 |
| 451 Expect.stringEquals(testGreekPhrase, | 454 Expect.stringEquals(testGreekPhrase, |
| 452 decodeFromUtf8(testGreekUtf8), "Greek"); | 455 decodeUtf8(testGreekUtf8), "Greek"); |
| 453 | 456 |
| 454 Expect.stringEquals(testKatakanaPhrase, | 457 Expect.stringEquals(testKatakanaPhrase, |
| 455 decodeFromUtf8(testKatakanaUtf8), "Katakana"); | 458 decodeUtf8(testKatakanaUtf8), "Katakana"); |
| 459 } |
| 460 |
| 461 void testIterableMethods() { |
| 462 IterableUtf8Decoder englishDecoder = decodeUtf8AsIterable(testEnglishUtf8); |
| 463 // get the first character |
| 464 Expect.equals(testEnglishUtf8[0], englishDecoder.iterator().next()); |
| 465 // get the whole translation using the Iterable interface |
| 466 Expect.stringEquals(testEnglishPhrase, |
| 467 new String.fromCharCodes(new List<int>.from(englishDecoder))); |
| 468 |
| 469 IterableUtf8Decoder kataDecoder = decodeUtf8AsIterable(testKatakanaUtf8); |
| 470 // get the first character |
| 471 Expect.equals(testKatakanaPhrase.charCodes()[0], |
| 472 kataDecoder.iterator().next()); |
| 473 // get the whole translation using the Iterable interface |
| 474 Expect.stringEquals(testKatakanaPhrase, |
| 475 new String.fromCharCodes(new List<int>.from(kataDecoder))); |
| 456 } | 476 } |
| 457 } | 477 } |
| OLD | NEW |