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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
base / i18n / break_iterator_unittest.cc [blame]
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/i18n/break_iterator.h"
#include <stddef.h>
#include <string_view>
#include <vector>
#include "base/ranges/algorithm.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace i18n {
TEST(BreakIteratorTest, BreakWordEmpty) {
std::u16string empty;
BreakIterator iter(empty, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakWord) {
std::u16string space(u" ");
std::u16string str(u" foo bar! \npouet boom");
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(space, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(u"foo", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(space, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(u"bar", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"!", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(space, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"\n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(u"pouet", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(space, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(u"boom", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakWordWide16) {
// Two greek words separated by space.
const std::u16string str(
u"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
u"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2");
const std::u16string word1(str.substr(0, 10));
const std::u16string word2(str.substr(11, 5));
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(word1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u" ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(word2, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakWordWide32) {
const std::u16string str = u"\U0001d49c a";
const std::u16string very_wide_word(str.substr(0, 2));
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(very_wide_word, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u" ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(u"a", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakWordThai) {
// Terms in Thai, without spaces in between.
const char16_t term1[] = u"พิมพ์";
const char16_t term2[] = u"น้อย";
const char16_t term3[] = u"ลง";
const std::u16string str(base::JoinString({term1, term2, term3}, u""));
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term2, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term3, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
}
// In some languages, the words are not broken by spaces. ICU provides a huge
// dictionary to detect word boundaries in Thai, Chinese, Japanese, Burmese,
// and Khmer. Due to the size of such a table, the part for Chinese and
// Japanese is not shipped on mobile.
#if !(BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID))
TEST(BreakIteratorTest, BreakWordChinese) {
// Terms in Traditional Chinese, without spaces in between.
const char16_t term1[] = u"瀏覽";
const char16_t term2[] = u"速度";
const char16_t term3[] = u"飛快";
const std::u16string str(base::JoinString({term1, term2, term3}, u""));
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term2, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term3, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakWordJapanese) {
// Terms in Japanese, without spaces in between.
const char16_t term1[] = u"モバイル";
const char16_t term2[] = u"でも";
const std::u16string str(base::JoinString({term1, term2}, u""));
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(term2, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakWordChineseEnglish) {
// Terms in Simplified Chinese mixed with English and wide punctuations.
std::u16string space(u" ");
const char16_t token1[] = u"下载";
const char16_t token2[] = u"Chrome";
const char16_t token3[] = u"(";
const char16_t token4[] = u"Mac";
const char16_t token5[] = u"版";
const char16_t token6[] = u")";
const std::u16string str(base::JoinString(
{token1, u" ", token2, token3, token4, u" ", token5, token6}, u""));
BreakIterator iter(str, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(token1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(space, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(token2, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(token3, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(token4, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(space, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.IsWord());
EXPECT_EQ(token5, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(token6, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
}
#endif // !(BUILDFLAG(IS_IOS) || BUILDFLAG(IS_ANDROID))
TEST(BreakIteratorTest, BreakSpaceEmpty) {
std::u16string empty;
BreakIterator iter(empty, BreakIterator::BREAK_SPACE);
ASSERT_TRUE(iter.Init());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakSpace) {
std::u16string str(u" foo bar! \npouet boom");
BreakIterator iter(str, BreakIterator::BREAK_SPACE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u" ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"foo ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"bar! \n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"pouet ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"boom", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakSpaceSP) {
std::u16string str(u" foo bar! \npouet boom ");
BreakIterator iter(str, BreakIterator::BREAK_SPACE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u" ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"foo ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"bar! \n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"pouet ", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"boom ", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakSpacekWide16) {
// Two Greek words.
const std::u16string str(
u"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
u"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2");
const std::u16string word1(str.substr(0, 11));
const std::u16string word2(str.substr(11, 5));
BreakIterator iter(str, BreakIterator::BREAK_SPACE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(word1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(word2, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakSpaceWide32) {
const std::u16string str = u"\U0001d49c a";
const std::u16string very_wide_word(str.substr(0, 3));
BreakIterator iter(str, BreakIterator::BREAK_SPACE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(very_wide_word, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"a", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakLineEmpty) {
std::u16string empty;
BreakIterator iter(empty, BreakIterator::BREAK_NEWLINE);
ASSERT_TRUE(iter.Init());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakLine) {
std::u16string nl(u"\n");
std::u16string str(u"\nfoo bar!\n\npouet boom");
BreakIterator iter(str, BreakIterator::BREAK_NEWLINE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(nl, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"foo bar!\n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(nl, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"pouet boom", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakSentence) {
std::u16string nl(u"\n");
std::u16string str(
u"\nFoo bar!\nOne sentence.\n\n\tAnother sentence?One more thing");
BreakIterator iter(str, BreakIterator::BREAK_SENTENCE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(nl, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"Foo bar!\n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"One sentence.\n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(nl, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"\tAnother sentence?", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"One more thing", iter.GetString());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, IsSentenceBoundary) {
std::u16string str(
u"Foo bar!\nOne sentence.\n\n\tAnother sentence?One more thing");
BreakIterator iter(str, BreakIterator::BREAK_SENTENCE);
ASSERT_TRUE(iter.Init());
std::vector<size_t> sentence_breaks;
sentence_breaks.push_back(0);
sentence_breaks.push_back(9);
sentence_breaks.push_back(23);
sentence_breaks.push_back(24);
sentence_breaks.push_back(42);
for (size_t i = 0; i < str.size(); i++) {
if (ranges::find(sentence_breaks, i) != sentence_breaks.end()) {
EXPECT_TRUE(iter.IsSentenceBoundary(i)) << " at index=" << i;
} else {
EXPECT_FALSE(iter.IsSentenceBoundary(i)) << " at index=" << i;
}
}
}
TEST(BreakIteratorTest, BreakLineNL) {
std::u16string nl(u"\n");
std::u16string str(u"\nfoo bar!\n\npouet boom\n");
BreakIterator iter(str, BreakIterator::BREAK_NEWLINE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(nl, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"foo bar!\n", iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(nl, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"pouet boom\n", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakLineWide16) {
// Two Greek words separated by newline.
const std::u16string str(
u"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9"
u"\x03bf\x03c2\x000a\x0399\x03c3\x03c4\x03cc\x03c2");
const std::u16string line1(str.substr(0, 11));
const std::u16string line2(str.substr(11, 5));
BreakIterator iter(str, BreakIterator::BREAK_NEWLINE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(line1, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(line2, iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakLineWide32) {
const std::u16string str = u"\U0001d49c\na";
const std::u16string very_wide_line(str.substr(0, 3));
BreakIterator iter(str, BreakIterator::BREAK_NEWLINE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(very_wide_line, iter.GetString());
EXPECT_TRUE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_EQ(u"a", iter.GetString());
EXPECT_FALSE(iter.Advance());
EXPECT_FALSE(iter.IsWord());
EXPECT_FALSE(iter.Advance()); // Test unexpected advance after end.
EXPECT_FALSE(iter.IsWord());
}
TEST(BreakIteratorTest, BreakCharacter) {
static const char16_t* const kCharacters[] = {
// An English word consisting of four ASCII characters.
u"w",
u"o",
u"r",
u"d",
u" ",
// A Hindi word (which means "Hindi") consisting of two Devanagari
// grapheme clusters.
u"हि",
u"न्दी",
u" ",
// A Thai word (which means "feel") consisting of three Thai grapheme
// clusters.
u"รู้",
u"สึ",
u"ก",
u" ",
};
std::vector<std::u16string> characters;
std::u16string text;
for (const auto* i : kCharacters) {
characters.push_back(i);
text.append(characters.back());
}
BreakIterator iter(text, BreakIterator::BREAK_CHARACTER);
ASSERT_TRUE(iter.Init());
for (size_t i = 0; i < std::size(kCharacters); ++i) {
EXPECT_TRUE(iter.Advance());
EXPECT_EQ(characters[i], iter.GetString());
}
}
// Test for https://code.google.com/p/chromium/issues/detail?id=411213
// We should be able to get valid substrings with GetString() function
// after setting new content by calling SetText().
TEST(BreakIteratorTest, GetStringAfterSetText) {
const std::u16string initial_string(u"str");
BreakIterator iter(initial_string, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
const std::u16string long_string(u"another,string");
EXPECT_TRUE(iter.SetText(long_string));
EXPECT_TRUE(iter.Advance());
EXPECT_TRUE(iter.Advance()); // Advance to ',' in |long_string|
// Check that the current position is out of bounds of the |initial_string|.
EXPECT_LT(initial_string.size(), iter.pos());
// Check that we can get a valid substring of |long_string|.
EXPECT_EQ(u",", iter.GetString());
}
// Make sure that when not in RULE_BASED or BREAK_WORD mode we're getting
// IS_LINE_OR_CHAR_BREAK.
TEST(BreakIteratorTest, GetWordBreakStatusBreakLine) {
// A string containing the English word "foo", followed by two Khmer
// characters, the English word "Can", and then two Russian characters and
// punctuation.
std::u16string text(u"foo \x1791\x17C1 \nCan \x041C\x0438...");
BreakIterator iter(text, BreakIterator::BREAK_LINE);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
// Finds "foo" and the space.
EXPECT_EQ(u"foo ", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_LINE_OR_CHAR_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds the Khmer characters, the next space, and the newline.
EXPECT_EQ(u"\x1791\x17C1 \n", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_LINE_OR_CHAR_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds "Can" and the space.
EXPECT_EQ(u"Can ", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_LINE_OR_CHAR_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds the Russian characters and periods.
EXPECT_EQ(u"\x041C\x0438...", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_LINE_OR_CHAR_BREAK);
EXPECT_FALSE(iter.Advance());
}
// Make sure that in BREAK_WORD mode we're getting IS_WORD_BREAK and
// IS_SKIPPABLE_WORD when we should be. IS_WORD_BREAK should be returned when we
// finish going over non-punctuation characters while IS_SKIPPABLE_WORD should
// be returned on punctuation and spaces.
TEST(BreakIteratorTest, GetWordBreakStatusBreakWord) {
// A string containing the English word "foo", followed by two Khmer
// characters, the English word "Can", and then two Russian characters and
// punctuation.
std::u16string text(u"foo \x1791\x17C1 \nCan \x041C\x0438...");
BreakIterator iter(text, BreakIterator::BREAK_WORD);
ASSERT_TRUE(iter.Init());
EXPECT_TRUE(iter.Advance());
// Finds "foo".
EXPECT_EQ(u"foo", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_WORD_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds the space, and the Khmer characters.
EXPECT_EQ(u" ", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_TRUE(iter.Advance());
EXPECT_EQ(u"\x1791\x17C1", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_WORD_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds the space and the newline.
EXPECT_EQ(u" ", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_TRUE(iter.Advance());
EXPECT_EQ(u"\n", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_TRUE(iter.Advance());
// Finds "Can".
EXPECT_EQ(u"Can", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_WORD_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds the space and the Russian characters.
EXPECT_EQ(u" ", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_TRUE(iter.Advance());
EXPECT_EQ(u"\x041C\x0438", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_WORD_BREAK);
EXPECT_TRUE(iter.Advance());
// Finds the trailing periods.
EXPECT_EQ(u".", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_TRUE(iter.Advance());
EXPECT_EQ(u".", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_TRUE(iter.Advance());
EXPECT_EQ(u".", iter.GetString());
EXPECT_EQ(iter.GetWordBreakStatus(), BreakIterator::IS_SKIPPABLE_WORD);
EXPECT_FALSE(iter.Advance());
}
} // namespace i18n
} // namespace base