forked from tensorflow/models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicodetext_unittest.cc
367 lines (319 loc) · 10.5 KB
/
unicodetext_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "util/utf8/unicodetext.h"
#include <iterator>
#include <set>
#include "gtest/gtest.h"
#include "third_party/utf/utf.h"
#include "util/utf8/unilib.h"
namespace {
class UnicodeTextTest : public testing::Test {
protected:
UnicodeTextTest() : empty_text_() {
const char32 text[] = {0x1C0, 0x4E8C, 0xD7DB, 0x34, 0x1D11E};
// Construct a UnicodeText from those codepoints.
text_.append(&text[0], text + arraysize(text));
}
UnicodeText empty_text_;
UnicodeText text_;
};
TEST(UnicodeTextTest, Ownership) {
const string src = "\u304A\u00B0\u106B";
{
string s = src;
char* sbuf = new char[s.size()];
memcpy(sbuf, s.data(), s.size());
UnicodeText owned;
owned.TakeOwnershipOfUTF8(sbuf, s.size(), s.size());
EXPECT_EQ(owned.utf8_data(), sbuf);
s.clear();
// owned should be OK even after s has been cleared.
UnicodeText::const_iterator it = owned.begin();
EXPECT_EQ(*it++, 0x304A);
EXPECT_EQ(*it++, 0x00B0);
EXPECT_EQ(*it++, 0x106B);
CHECK(it == owned.end());
}
{
UnicodeText owner;
{ // Create a new scope for s.
string s = src;
char* sbuf = new char[s.size()];
memcpy(sbuf, s.data(), s.size());
UnicodeText t;
t.TakeOwnershipOfUTF8(sbuf, s.size(), s.size());
EXPECT_EQ(t.utf8_data(), sbuf);
owner = t; // Copies the data
EXPECT_NE(owner.utf8_data(), sbuf);
}
// owner should be OK even after s has gone out of scope
UnicodeText::const_iterator it = owner.begin();
EXPECT_EQ(*it++, 0x304A);
EXPECT_EQ(*it++, 0x00B0);
EXPECT_EQ(*it++, 0x106B);
CHECK(it == owner.end());
}
{
UnicodeText alias;
alias.PointToUTF8(src.data(), src.size());
EXPECT_EQ(alias.utf8_data(), src.data());
UnicodeText::const_iterator it = alias.begin();
EXPECT_EQ(*it++, 0x304A);
EXPECT_EQ(*it++, 0x00B0);
EXPECT_EQ(*it++, 0x106B);
CHECK(it == alias.end());
UnicodeText t = alias; // Copy initialization copies the data.
EXPECT_NE(t.utf8_data(), alias.utf8_data());
UnicodeText t2;
t2 = alias; // Assignment copies the data.
EXPECT_NE(t2.utf8_data(), alias.utf8_data());
// Preserve an alias.
t.PointTo(alias); // This does not copy the data.
EXPECT_EQ(t.utf8_data(), alias.utf8_data());
t.push_back(0x0020); // Modify the alias
EXPECT_NE(t.utf8_data(), alias.utf8_data()); // It's no longer an alias.
}
}
class IteratorTest : public UnicodeTextTest {};
TEST_F(IteratorTest, Iterates) {
UnicodeText::const_iterator iter = text_.begin();
EXPECT_EQ(0x1C0, *iter);
EXPECT_EQ(&iter, &++iter); // operator++ returns *this.
EXPECT_EQ(0x4E8C, *iter++);
EXPECT_EQ(0xD7DB, *iter);
// Make sure you can dereference more than once.
EXPECT_EQ(0xD7DB, *iter);
EXPECT_EQ(0x34, *++iter);
EXPECT_EQ(0x1D11E, *++iter);
ASSERT_TRUE(iter != text_.end());
iter++;
EXPECT_TRUE(iter == text_.end());
}
TEST_F(IteratorTest, Reverse) {
UnicodeText::const_reverse_iterator iter = text_.rbegin();
EXPECT_EQ(0x1D11E, *iter);
EXPECT_EQ(&iter, &++iter); // operator++ returns *this.
EXPECT_EQ(0x34, *iter++);
EXPECT_EQ(0xD7DB, *iter);
// Make sure you can dereference more than once.
EXPECT_EQ(0xD7DB, *iter);
EXPECT_EQ(0x4E8C, *++iter);
EXPECT_EQ(0x1C0, *++iter);
ASSERT_TRUE(iter != text_.rend());
iter++;
EXPECT_TRUE(iter == text_.rend());
}
TEST_F(IteratorTest, MultiPass) {
// Also tests Default Constructible and Assignable.
UnicodeText::const_iterator i1, i2;
i1 = text_.begin();
i2 = i1;
EXPECT_EQ(0x4E8C, *++i1);
EXPECT_TRUE(i1 != i2);
EXPECT_EQ(0x1C0, *i2);
++i2;
EXPECT_TRUE(i1 == i2);
EXPECT_EQ(0x4E8C, *i2);
}
TEST_F(IteratorTest, ReverseIterates) {
UnicodeText::const_iterator iter = text_.end();
EXPECT_TRUE(iter == text_.end());
iter--;
ASSERT_TRUE(iter != text_.end());
EXPECT_EQ(0x1D11E, *iter--);
EXPECT_EQ(0x34, *iter);
EXPECT_EQ(0xD7DB, *--iter);
// Make sure you can dereference more than once.
EXPECT_EQ(0xD7DB, *iter);
--iter;
EXPECT_EQ(0x4E8C, *iter--);
EXPECT_EQ(0x1C0, *iter);
EXPECT_TRUE(iter == text_.begin());
}
TEST_F(IteratorTest, Comparable) {
UnicodeText::const_iterator i1, i2;
i1 = text_.begin();
i2 = i1;
++i2;
EXPECT_TRUE(i1 < i2);
EXPECT_TRUE(text_.begin() <= i1);
EXPECT_FALSE(i1 >= i2);
EXPECT_FALSE(i1 > text_.end());
}
TEST_F(IteratorTest, Advance) {
UnicodeText::const_iterator iter = text_.begin();
EXPECT_EQ(0x1C0, *iter);
std::advance(iter, 4);
EXPECT_EQ(0x1D11E, *iter);
++iter;
EXPECT_TRUE(iter == text_.end());
}
TEST_F(IteratorTest, Distance) {
UnicodeText::const_iterator iter = text_.begin();
EXPECT_EQ(0, distance(text_.begin(), iter));
EXPECT_EQ(5, distance(iter, text_.end()));
++iter;
++iter;
EXPECT_EQ(2, distance(text_.begin(), iter));
EXPECT_EQ(3, distance(iter, text_.end()));
++iter;
++iter;
EXPECT_EQ(4, distance(text_.begin(), iter));
++iter;
EXPECT_EQ(0, distance(iter, text_.end()));
}
TEST_F(IteratorTest, Encode) {
const string utf8 = "\xC7\x80"
"\xE4\xBA\x8C"
"\xED\x9F\x9B"
"\x34"
"\xF0\x9D\x84\x9E";
const int lengths[] = {2, 3, 3, 1, 4};
EXPECT_EQ(text_.size(), 5);
EXPECT_EQ(text_.utf8_length(), 13);
EXPECT_TRUE(memcmp(text_.utf8_data(), utf8.data(), text_.utf8_length())
== 0);
{
// Test the iterator
UnicodeText::const_iterator iter = text_.begin(), end = text_.end();
const char* u = utf8.data();
int i = 0;
while (iter != end) {
char buf[5];
int n = iter.get_utf8(buf);
buf[n] = '\0';
EXPECT_TRUE(strncmp(buf, u, n) == 0);
EXPECT_EQ(buf, iter.get_utf8_string());
EXPECT_EQ(lengths[i], iter.utf8_length());
u += n;
iter++;
i++;
}
}
{
// Test the reverse_iterator
UnicodeText::const_reverse_iterator iter = text_.rbegin();
UnicodeText::const_reverse_iterator end = text_.rend();
const char* u = utf8.data() + utf8.size();
int i = 0;
while (iter != end) {
char buf[5];
int n = iter.get_utf8(buf);
buf[n] = '\0';
u -= n;
EXPECT_TRUE(strncmp(buf, u, n) == 0);
EXPECT_EQ(buf, iter.get_utf8_string());
EXPECT_EQ(lengths[text_.size() - i - 1], iter.utf8_length());
iter++;
i++;
}
}
text_.push_back('$');
EXPECT_EQ(text_.size(), 6);
EXPECT_EQ(text_.utf8_length(), 14);
text_.push_back('\xAE'); // registered sign
EXPECT_EQ(text_.size(), 7);
EXPECT_EQ(text_.utf8_length(), 16); // 2 bytes long
}
TEST_F(IteratorTest, Decode) {
const char32 text[] = {0x1C0, 0x4E8C, 0xD7DB, 0x34, 0x1D11E};
UnicodeText::const_iterator iter = text_.begin();
for (int i = 0; i < 5; ++i)
EXPECT_EQ(text[i], *iter++);
string s = CodepointString(text_);
EXPECT_EQ(s, "1C0 4E8C D7DB 34 1D11E ");
}
class OperatorTest : public UnicodeTextTest {};
TEST_F(OperatorTest, Clear) {
UnicodeText empty_text(UTF8ToUnicodeText(""));
EXPECT_FALSE(text_ == empty_text);
text_.clear();
EXPECT_TRUE(text_ == empty_text);
}
TEST_F(OperatorTest, Empty) {
EXPECT_TRUE(empty_text_.empty());
EXPECT_FALSE(text_.empty());
text_.clear();
EXPECT_TRUE(text_.empty());
}
TEST(UnicodeTextTest, InterchangeValidity) {
char* FDD0 = new char[3];
memcpy(FDD0, "\xEF\xB7\x90", 3);
EXPECT_FALSE(UniLib::IsInterchangeValid(FDD0, 3));
UnicodeText a = MakeUnicodeTextWithoutAcceptingOwnership(FDD0, 3);
EXPECT_EQ(a.size(), 1);
EXPECT_EQ(*a.begin(), 0x20);
a.clear();
a.push_back(0xFDD0);
EXPECT_EQ(a.size(), 1);
EXPECT_EQ(*a.begin(), 0x20);
a = MakeUnicodeTextAcceptingOwnership(FDD0, 3, 3);
EXPECT_EQ(a.size(), 1);
EXPECT_EQ(*a.begin(), 0x20);
a.clear();
a.push_back(0xFDD0);
EXPECT_EQ(a.size(), 1);
EXPECT_EQ(*a.begin(), 0x20);
}
class SubstringSearchTest : public UnicodeTextTest {};
// TEST_F(SubstringSearchTest, FindEmpty) {
// EXPECT_TRUE(text_.find(empty_text_) == text_.begin());
// EXPECT_TRUE(empty_text_.find(text_) == empty_text_.end());
// }
// TEST_F(SubstringSearchTest, Find) {
// UnicodeText::const_iterator second_pos = text_.begin();
// ++second_pos;
// UnicodeText::const_iterator third_pos = second_pos;
// ++third_pos;
// UnicodeText::const_iterator fourth_pos = third_pos;
// ++fourth_pos;
// // same as text_
// const char32 text[] = {0x1C0, 0x4E8C, 0xD7DB, 0x34, 0x1D11E};
// UnicodeText prefix;
// prefix.append(&text[0], &text[2]);
// EXPECT_TRUE(text_.find(prefix) == text_.begin());
// EXPECT_TRUE(text_.find(prefix, second_pos) == text_.end());
// UnicodeText suffix;
// suffix.append(&text[2], text + arraysize(text));
// EXPECT_TRUE(text_.find(suffix) == third_pos);
// EXPECT_TRUE(text_.find(suffix, second_pos) == third_pos);
// EXPECT_TRUE(text_.find(suffix, third_pos) == third_pos);
// EXPECT_TRUE(text_.find(suffix, fourth_pos) == text_.end());
// }
// TEST_F(SubstringSearchTest, HasConversionError) {
// EXPECT_FALSE(text_.HasReplacementChar());
// const char32 beg[] = {0xFFFD, 0x1C0, 0x4E8C, 0xD7DB, 0x34, 0x1D11E};
// UnicodeText beg_uni;
// beg_uni.append(&beg[0], beg + arraysize(beg));
// EXPECT_TRUE(beg_uni.HasReplacementChar());
// const char32 mid[] = {0x1C0, 0x4E8C, 0xFFFD, 0xD7DB, 0x34, 0x1D11E};
// UnicodeText mid_uni;
// mid_uni.append(&mid[0], mid + arraysize(mid));
// EXPECT_TRUE(mid_uni.HasReplacementChar());
// const char32 end[] = {0x1C0, 0x4E8C, 0xD7DB, 0x34, 0x1D11E, 0xFFFD};
// UnicodeText end_uni;
// end_uni.append(&end[0], end + arraysize(end));
// EXPECT_TRUE(end_uni.HasReplacementChar());
// const char32 two[] = {0xFFFD, 0x1C0, 0x4E8C, 0xD7DB, 0x34, 0x1D11E, 0xFFFD};
// UnicodeText two_uni;
// two_uni.append(&two[0], two + arraysize(two));
// EXPECT_TRUE(two_uni.HasReplacementChar());
// const char32 adj[] = {0x1C0, 0xFFFD, 0xFFFD, 0x4E8C, 0xD7DB, 0x34, 0x1D11E};
// UnicodeText adj_uni;
// adj_uni.append(&adj[0], adj + arraysize(adj));
// EXPECT_TRUE(adj_uni.HasReplacementChar());
// }
} // namespace