Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(474)

Side by Side Diff: utils/tests/string_encoding/utf32_tests.dart

Issue 9410001: restructure string decoding to support iterable use and include benchmarks for UTF-8 decoding. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: "stop using introduced variable _length. Improve docs. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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("utf32_tests"); 6 #library("utf32_tests");
7 #import("dunit.dart"); 7 #import("dunit.dart");
8 #import("../../string_encoding/unicode_core.dart");
8 #import("../../string_encoding/utf32.dart"); 9 #import("../../string_encoding/utf32.dart");
9 10
10 void main() { 11 void main() {
11 TestSuite suite = new TestSuite(); 12 TestSuite suite = new TestSuite();
12 suite.registerTestClass(new Utf32Tests()); 13 suite.registerTestClass(new Utf32Tests());
13 suite.run(); 14 suite.run();
14 } 15 }
15 16
16 class Utf32Tests extends TestClass { 17 class Utf32Tests extends TestClass {
17 static final String testKoreanCharSubset = """ 18 static final String testKoreanCharSubset = """
18 가각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓갔강갖갗갘같갚갛 19 가각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓갔강갖갗갘같갚갛
19 개객갞갟갠갡갢갣갤갥갦갧갨갩갪갫갬갭갮갯갰갱갲갳갴갵갶갷 20 개객갞갟갠갡갢갣갤갥갦갧갨갩갪갫갬갭갮갯갰갱갲갳갴갵갶갷
20 갸갹갺갻갼갽갾갿걀걁걂걃걄걅걆걇걈걉걊걋걌걍걎걏걐걑걒걓"""; 21 갸갹갺갻갼갽갾갿걀걁걂걃걄걅걆걇걈걉걊걋걌걍걎걏걐걑걒걓""";
21 static final String testHanTwice = "二"; 22 static final String testHanTwice = "二";
22 23
23 static final List<int> testKoreanCharSubsetUtf32be = const<int>[ 24 static final List<int> testKoreanCharSubsetUtf32beBom = const<int>[
24 0x00, 0x00, 0xfe, 0xff, 0x00, 0x00, 0xac, 0x00, 25 0x00, 0x00, 0xfe, 0xff, 0x00, 0x00, 0xac, 0x00,
25 0x00, 0x00, 0xac, 0x01, 0x00, 0x00, 0xac, 0x02, 26 0x00, 0x00, 0xac, 0x01, 0x00, 0x00, 0xac, 0x02,
26 0x00, 0x00, 0xac, 0x03, 0x00, 0x00, 0xac, 0x04, 27 0x00, 0x00, 0xac, 0x03, 0x00, 0x00, 0xac, 0x04,
27 0x00, 0x00, 0xac, 0x05, 0x00, 0x00, 0xac, 0x06, 28 0x00, 0x00, 0xac, 0x05, 0x00, 0x00, 0xac, 0x06,
28 0x00, 0x00, 0xac, 0x07, 0x00, 0x00, 0xac, 0x08, 29 0x00, 0x00, 0xac, 0x07, 0x00, 0x00, 0xac, 0x08,
29 0x00, 0x00, 0xac, 0x09, 0x00, 0x00, 0xac, 0x0a, 30 0x00, 0x00, 0xac, 0x09, 0x00, 0x00, 0xac, 0x0a,
30 0x00, 0x00, 0xac, 0x0b, 0x00, 0x00, 0xac, 0x0c, 31 0x00, 0x00, 0xac, 0x0b, 0x00, 0x00, 0xac, 0x0c,
31 0x00, 0x00, 0xac, 0x0d, 0x00, 0x00, 0xac, 0x0e, 32 0x00, 0x00, 0xac, 0x0d, 0x00, 0x00, 0xac, 0x0e,
32 0x00, 0x00, 0xac, 0x0f, 0x00, 0x00, 0xac, 0x10, 33 0x00, 0x00, 0xac, 0x0f, 0x00, 0x00, 0xac, 0x10,
33 0x00, 0x00, 0xac, 0x11, 0x00, 0x00, 0xac, 0x12, 34 0x00, 0x00, 0xac, 0x11, 0x00, 0x00, 0xac, 0x12,
(...skipping 25 matching lines...) Expand all
59 0x00, 0x00, 0xac, 0x43, 0x00, 0x00, 0xac, 0x44, 60 0x00, 0x00, 0xac, 0x43, 0x00, 0x00, 0xac, 0x44,
60 0x00, 0x00, 0xac, 0x45, 0x00, 0x00, 0xac, 0x46, 61 0x00, 0x00, 0xac, 0x45, 0x00, 0x00, 0xac, 0x46,
61 0x00, 0x00, 0xac, 0x47, 0x00, 0x00, 0xac, 0x48, 62 0x00, 0x00, 0xac, 0x47, 0x00, 0x00, 0xac, 0x48,
62 0x00, 0x00, 0xac, 0x49, 0x00, 0x00, 0xac, 0x4a, 63 0x00, 0x00, 0xac, 0x49, 0x00, 0x00, 0xac, 0x4a,
63 0x00, 0x00, 0xac, 0x4b, 0x00, 0x00, 0xac, 0x4c, 64 0x00, 0x00, 0xac, 0x4b, 0x00, 0x00, 0xac, 0x4c,
64 0x00, 0x00, 0xac, 0x4d, 0x00, 0x00, 0xac, 0x4e, 65 0x00, 0x00, 0xac, 0x4d, 0x00, 0x00, 0xac, 0x4e,
65 0x00, 0x00, 0xac, 0x4f, 0x00, 0x00, 0xac, 0x50, 66 0x00, 0x00, 0xac, 0x4f, 0x00, 0x00, 0xac, 0x50,
66 0x00, 0x00, 0xac, 0x51, 0x00, 0x00, 0xac, 0x52, 67 0x00, 0x00, 0xac, 0x51, 0x00, 0x00, 0xac, 0x52,
67 0x00, 0x00, 0xac, 0x53]; 68 0x00, 0x00, 0xac, 0x53];
68 69
70 static final List<int> testKoreanCharSubsetUtf32le = const<int>[
71 0x00, 0xac, 0x00, 0x00, 0x01, 0xac, 0x00, 0x00,
72 0x02, 0xac, 0x00, 0x00, 0x03, 0xac, 0x00, 0x00,
73 0x04, 0xac, 0x00, 0x00, 0x05, 0xac, 0x00, 0x00,
74 0x06, 0xac, 0x00, 0x00, 0x07, 0xac, 0x00, 0x00,
75 0x08, 0xac, 0x00, 0x00, 0x09, 0xac, 0x00, 0x00,
76 0x0a, 0xac, 0x00, 0x00, 0x0b, 0xac, 0x00, 0x00,
77 0x0c, 0xac, 0x00, 0x00, 0x0d, 0xac, 0x00, 0x00,
78 0x0e, 0xac, 0x00, 0x00, 0x0f, 0xac, 0x00, 0x00,
79 0x10, 0xac, 0x00, 0x00, 0x11, 0xac, 0x00, 0x00,
80 0x12, 0xac, 0x00, 0x00, 0x13, 0xac, 0x00, 0x00,
81 0x14, 0xac, 0x00, 0x00, 0x15, 0xac, 0x00, 0x00,
82 0x16, 0xac, 0x00, 0x00, 0x17, 0xac, 0x00, 0x00,
83 0x18, 0xac, 0x00, 0x00, 0x19, 0xac, 0x00, 0x00,
84 0x1a, 0xac, 0x00, 0x00, 0x1b, 0xac, 0x00, 0x00,
85 0x0a, 0x00, 0x00, 0x00, 0x1c, 0xac, 0x00, 0x00,
86 0x1d, 0xac, 0x00, 0x00, 0x1e, 0xac, 0x00, 0x00,
87 0x1f, 0xac, 0x00, 0x00, 0x20, 0xac, 0x00, 0x00,
88 0x21, 0xac, 0x00, 0x00, 0x22, 0xac, 0x00, 0x00,
89 0x23, 0xac, 0x00, 0x00, 0x24, 0xac, 0x00, 0x00,
90 0x25, 0xac, 0x00, 0x00, 0x26, 0xac, 0x00, 0x00,
91 0x27, 0xac, 0x00, 0x00, 0x28, 0xac, 0x00, 0x00,
92 0x29, 0xac, 0x00, 0x00, 0x2a, 0xac, 0x00, 0x00,
93 0x2b, 0xac, 0x00, 0x00, 0x2c, 0xac, 0x00, 0x00,
94 0x2d, 0xac, 0x00, 0x00, 0x2e, 0xac, 0x00, 0x00,
95 0x2f, 0xac, 0x00, 0x00, 0x30, 0xac, 0x00, 0x00,
96 0x31, 0xac, 0x00, 0x00, 0x32, 0xac, 0x00, 0x00,
97 0x33, 0xac, 0x00, 0x00, 0x34, 0xac, 0x00, 0x00,
98 0x35, 0xac, 0x00, 0x00, 0x36, 0xac, 0x00, 0x00,
99 0x37, 0xac, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
100 0x38, 0xac, 0x00, 0x00, 0x39, 0xac, 0x00, 0x00,
101 0x3a, 0xac, 0x00, 0x00, 0x3b, 0xac, 0x00, 0x00,
102 0x3c, 0xac, 0x00, 0x00, 0x3d, 0xac, 0x00, 0x00,
103 0x3e, 0xac, 0x00, 0x00, 0x3f, 0xac, 0x00, 0x00,
104 0x40, 0xac, 0x00, 0x00, 0x41, 0xac, 0x00, 0x00,
105 0x42, 0xac, 0x00, 0x00, 0x43, 0xac, 0x00, 0x00,
106 0x44, 0xac, 0x00, 0x00, 0x45, 0xac, 0x00, 0x00,
107 0x46, 0xac, 0x00, 0x00, 0x47, 0xac, 0x00, 0x00,
108 0x48, 0xac, 0x00, 0x00, 0x49, 0xac, 0x00, 0x00,
109 0x4a, 0xac, 0x00, 0x00, 0x4b, 0xac, 0x00, 0x00,
110 0x4c, 0xac, 0x00, 0x00, 0x4d, 0xac, 0x00, 0x00,
111 0x4e, 0xac, 0x00, 0x00, 0x4f, 0xac, 0x00, 0x00,
112 0x50, 0xac, 0x00, 0x00, 0x51, 0xac, 0x00, 0x00,
113 0x52, 0xac, 0x00, 0x00, 0x53, 0xac, 0x00, 0x00];
114
69 void registerTests(TestSuite suite) { 115 void registerTests(TestSuite suite) {
70 register("testUtf32BytesToString", testUtf32BytesToString, suite); 116 register("Utf32Tests.testUtf32BytesToString", testUtf32BytesToString,
71 register("testEncodeToUtf32", testEncodeToUtf32, suite); 117 suite);
118 register("Utf32Tests.testEncodeToUtf32", testEncodeToUtf32, suite);
119 register("Utf32Tests.testIterableMethods", testIterableMethods, suite);
72 } 120 }
73 121
74 void testEncodeToUtf32() { 122 void testEncodeToUtf32() {
75 Expect.listEquals(testKoreanCharSubsetUtf32be, 123 Expect.listEquals([], encodeUtf32le(""), "no input"); // TODO(dcarlson) skip bom on empty?
76 encodeAsUtf32(testKoreanCharSubset), 124 Expect.listEquals(testKoreanCharSubsetUtf32beBom,
125 encodeUtf32(testKoreanCharSubset),
77 "encode UTF-32(BE by default) Korean"); 126 "encode UTF-32(BE by default) Korean");
78 Expect.listEquals([ 127 Expect.listEquals(testKoreanCharSubsetUtf32le,
79 0x00, 0xac, 0x00, 0x00, 0x01, 0xac, 0x00, 0x00, 128 encodeUtf32le(testKoreanCharSubset),
80 0x02, 0xac, 0x00, 0x00, 0x03, 0xac, 0x00, 0x00,
81 0x04, 0xac, 0x00, 0x00, 0x05, 0xac, 0x00, 0x00,
82 0x06, 0xac, 0x00, 0x00, 0x07, 0xac, 0x00, 0x00,
83 0x08, 0xac, 0x00, 0x00, 0x09, 0xac, 0x00, 0x00,
84 0x0a, 0xac, 0x00, 0x00, 0x0b, 0xac, 0x00, 0x00,
85 0x0c, 0xac, 0x00, 0x00, 0x0d, 0xac, 0x00, 0x00,
86 0x0e, 0xac, 0x00, 0x00, 0x0f, 0xac, 0x00, 0x00,
87 0x10, 0xac, 0x00, 0x00, 0x11, 0xac, 0x00, 0x00,
88 0x12, 0xac, 0x00, 0x00, 0x13, 0xac, 0x00, 0x00,
89 0x14, 0xac, 0x00, 0x00, 0x15, 0xac, 0x00, 0x00,
90 0x16, 0xac, 0x00, 0x00, 0x17, 0xac, 0x00, 0x00,
91 0x18, 0xac, 0x00, 0x00, 0x19, 0xac, 0x00, 0x00,
92 0x1a, 0xac, 0x00, 0x00, 0x1b, 0xac, 0x00, 0x00,
93 0x0a, 0x00, 0x00, 0x00, 0x1c, 0xac, 0x00, 0x00,
94 0x1d, 0xac, 0x00, 0x00, 0x1e, 0xac, 0x00, 0x00,
95 0x1f, 0xac, 0x00, 0x00, 0x20, 0xac, 0x00, 0x00,
96 0x21, 0xac, 0x00, 0x00, 0x22, 0xac, 0x00, 0x00,
97 0x23, 0xac, 0x00, 0x00, 0x24, 0xac, 0x00, 0x00,
98 0x25, 0xac, 0x00, 0x00, 0x26, 0xac, 0x00, 0x00,
99 0x27, 0xac, 0x00, 0x00, 0x28, 0xac, 0x00, 0x00,
100 0x29, 0xac, 0x00, 0x00, 0x2a, 0xac, 0x00, 0x00,
101 0x2b, 0xac, 0x00, 0x00, 0x2c, 0xac, 0x00, 0x00,
102 0x2d, 0xac, 0x00, 0x00, 0x2e, 0xac, 0x00, 0x00,
103 0x2f, 0xac, 0x00, 0x00, 0x30, 0xac, 0x00, 0x00,
104 0x31, 0xac, 0x00, 0x00, 0x32, 0xac, 0x00, 0x00,
105 0x33, 0xac, 0x00, 0x00, 0x34, 0xac, 0x00, 0x00,
106 0x35, 0xac, 0x00, 0x00, 0x36, 0xac, 0x00, 0x00,
107 0x37, 0xac, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00,
108 0x38, 0xac, 0x00, 0x00, 0x39, 0xac, 0x00, 0x00,
109 0x3a, 0xac, 0x00, 0x00, 0x3b, 0xac, 0x00, 0x00,
110 0x3c, 0xac, 0x00, 0x00, 0x3d, 0xac, 0x00, 0x00,
111 0x3e, 0xac, 0x00, 0x00, 0x3f, 0xac, 0x00, 0x00,
112 0x40, 0xac, 0x00, 0x00, 0x41, 0xac, 0x00, 0x00,
113 0x42, 0xac, 0x00, 0x00, 0x43, 0xac, 0x00, 0x00,
114 0x44, 0xac, 0x00, 0x00, 0x45, 0xac, 0x00, 0x00,
115 0x46, 0xac, 0x00, 0x00, 0x47, 0xac, 0x00, 0x00,
116 0x48, 0xac, 0x00, 0x00, 0x49, 0xac, 0x00, 0x00,
117 0x4a, 0xac, 0x00, 0x00, 0x4b, 0xac, 0x00, 0x00,
118 0x4c, 0xac, 0x00, 0x00, 0x4d, 0xac, 0x00, 0x00,
119 0x4e, 0xac, 0x00, 0x00, 0x4f, 0xac, 0x00, 0x00,
120 0x50, 0xac, 0x00, 0x00, 0x51, 0xac, 0x00, 0x00,
121 0x52, 0xac, 0x00, 0x00, 0x53, 0xac, 0x00, 0x00],
122 encodeAsUtf32le(testKoreanCharSubset),
123 "encode UTF-32(LE by default) Korean"); 129 "encode UTF-32(LE by default) Korean");
124 } 130 }
125 131
126 void testUtf32BytesToString() { 132 void testUtf32BytesToString() {
127 Expect.stringEquals("\ufffd", decodeFromUtf32([0]), "single byte"); 133 Expect.stringEquals("", decodeUtf32([]), "no input");
128 Expect.stringEquals("\ufffd", decodeFromUtf32([0, 0, 0x4e]), 134 Expect.stringEquals("\ufffd", decodeUtf32([0]), "single byte");
135 Expect.stringEquals("\ufffd", decodeUtf32([0, 0, 0x4e]),
129 "short a byte"); 136 "short a byte");
130 Expect.stringEquals("\u4e8c\ufffd", decodeFromUtf32([0, 0, 0x4e, 0x8c, 0]), 137 Expect.stringEquals("\u4e8c\ufffd", decodeUtf32([0, 0, 0x4e, 0x8c, 0]),
131 "extra byte"); 138 "extra byte");
132 139
133 Expect.stringEquals(testHanTwice, decodeFromUtf32([0, 0, 0x4e, 0x8c]), 140 Expect.stringEquals(testHanTwice, decodeUtf32([0, 0, 0x4e, 0x8c]),
134 "twice variation 1"); 141 "twice variation 1");
135 Expect.stringEquals(testHanTwice, 142 Expect.stringEquals(testHanTwice,
136 decodeFromUtf32([0, 0, 0xfe, 0xff, 0, 0, 0x4e, 0x8c]), 143 decodeUtf32([0, 0, 0xfe, 0xff, 0, 0, 0x4e, 0x8c]),
137 "twice variation 2"); 144 "twice variation 2");
138 Expect.stringEquals(testHanTwice, 145 Expect.stringEquals(testHanTwice,
139 decodeFromUtf32([0xff, 0xfe, 0, 0, 0x8c, 0x4e, 0, 0]), 146 decodeUtf32([0xff, 0xfe, 0, 0, 0x8c, 0x4e, 0, 0]),
140 "twice variation 3"); 147 "twice variation 3");
141 148
142 Expect.stringEquals(testHanTwice, decodeFromUtf32be([0, 0, 0x4e, 0x8c]), 149 Expect.stringEquals(testHanTwice, decodeUtf32be([0, 0, 0x4e, 0x8c]),
143 "twice variation 4"); 150 "twice variation 4");
144 Expect.stringEquals(testHanTwice, 151 Expect.stringEquals(testHanTwice,
145 decodeFromUtf32be([0, 0, 0xfe, 0xff, 0, 0, 0x4e, 0x8c]), 152 decodeUtf32be([0, 0, 0xfe, 0xff, 0, 0, 0x4e, 0x8c]),
146 "twice variation 5"); 153 "twice variation 5");
147 154
148 Expect.stringEquals(testHanTwice, decodeFromUtf32le([0x8c, 0x4e, 0, 0]), 155 Expect.stringEquals(testHanTwice, decodeUtf32le([0x8c, 0x4e, 0, 0]),
149 "twice variation 6"); 156 "twice variation 6");
150 Expect.stringEquals(testHanTwice, 157 Expect.stringEquals(testHanTwice,
151 decodeFromUtf32le([0xff, 0xfe, 0, 0, 0x8c, 0x4e, 0, 0]), 158 decodeUtf32le([0xff, 0xfe, 0, 0, 0x8c, 0x4e, 0, 0]),
152 "twice variation 7"); 159 "twice variation 7");
153 160
154 Expect.stringEquals(testKoreanCharSubset, 161 Expect.stringEquals(testKoreanCharSubset,
155 decodeFromUtf32(testKoreanCharSubsetUtf32be), 162 decodeUtf32(testKoreanCharSubsetUtf32beBom),
156 "UTF-32BE Korean"); 163 "UTF-32BE Korean");
157 } 164 }
165
166 void testIterableMethods() {
167 // empty input
168 Expect.isFalse(decodeUtf32AsIterable([]).iterator().hasNext());
169
170 IterableUtf32Decoder koreanDecoder =
171 decodeUtf32AsIterable(testKoreanCharSubsetUtf32beBom);
172 // get the first character
173 Expect.equals(testKoreanCharSubset.charCodes()[0],
174 koreanDecoder.iterator().next());
175 // get the whole translation using the Iterable interface
176 Expect.stringEquals(testKoreanCharSubset,
177 new String.fromCharCodes(new List<int>.from(koreanDecoder)));
178
179 // specify types
180 Expect.equals(44032, (new List<int>
181 .from(decodeUtf32beAsIterable(testKoreanCharSubsetUtf32beBom)))[0]);
182 Expect.equals(44032, (new List<int>
183 .from(decodeUtf32leAsIterable(testKoreanCharSubsetUtf32le)))[0]);
184 Expect.equals(UNICODE_BOM, (new List<int>
185 .from(decodeUtf32beAsIterable(testKoreanCharSubsetUtf32beBom,
186 stripBom: false)))[0]);
187 }
158 } 188 }
OLDNEW
« no previous file with comments | « utils/tests/string_encoding/utf16_tests.dart ('k') | utils/tests/string_encoding/utf8_benchmarks.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698