1 /********************************************************************
3 * Copyright (c) 1999-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6 /************************************************************************
7 * Date Name Description
8 * 12/14/99 Madhu Creation.
9 * 01/12/2000 Madhu updated for changed API
10 ************************************************************************/
12 #include "unicode/utypes.h"
14 #if !UCONFIG_NO_BREAK_ITERATION
16 #include "unicode/uchar.h"
18 #include "unicode/rbbi.h"
19 #include "unicode/schriter.h"
24 #include "unicode/ustring.h"
25 #include "unicode/utext.h"
28 * API Test the RuleBasedBreakIterator class
32 #define TEST_ASSERT_SUCCESS(status) {if (U_FAILURE(status)) {\
33 errln("Failure at file %s, line %d, error = %s", __FILE__, __LINE__, u_errorName(status));}}
35 #define TEST_ASSERT(expr) {if ((expr)==FALSE) { \
36 errln("Test Failure at file %s, line %d", __FILE__, __LINE__);}}
38 void RBBIAPITest::TestCloneEquals()
41 UErrorCode status
=U_ZERO_ERROR
;
42 RuleBasedBreakIterator
* bi1
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
43 RuleBasedBreakIterator
* biequal
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
44 RuleBasedBreakIterator
* bi3
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
45 RuleBasedBreakIterator
* bi2
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status
);
46 if(U_FAILURE(status
)){
47 errln((UnicodeString
)"FAIL : in construction");
52 UnicodeString testString
="Testing word break iterators's clone() and equals()";
53 bi1
->setText(testString
);
54 bi2
->setText(testString
);
55 biequal
->setText(testString
);
57 bi3
->setText("hello");
59 logln((UnicodeString
)"Testing equals()");
61 logln((UnicodeString
)"Testing == and !=");
62 UBool b
= (*bi1
!= *biequal
);
66 errln((UnicodeString
)"ERROR:1 RBBI's == and != operator failed.");
69 if(*bi2
== *biequal
|| *bi2
== *bi1
|| *biequal
== *bi3
)
70 errln((UnicodeString
)"ERROR:2 RBBI's == and != operator failed.");
73 // Quick test of RulesBasedBreakIterator assignment -
75 // two different iterators are !=
76 // they are == after assignment
77 // source and dest iterator produce the same next() after assignment.
78 // deleting one doesn't disable the other.
79 logln("Testing assignment");
80 RuleBasedBreakIterator
*bix
= (RuleBasedBreakIterator
*)BreakIterator::createLineInstance(Locale::getDefault(), status
);
81 if(U_FAILURE(status
)){
82 errln((UnicodeString
)"FAIL : in construction");
86 RuleBasedBreakIterator biDefault
, biDefault2
;
87 if(U_FAILURE(status
)){
88 errln((UnicodeString
)"FAIL : in construction of default iterator");
91 if (biDefault
== *bix
) {
92 errln((UnicodeString
)"ERROR: iterators should not compare ==");
95 if (biDefault
!= biDefault2
) {
96 errln((UnicodeString
)"ERROR: iterators should compare ==");
101 UnicodeString
HelloString("Hello Kitty");
102 bix
->setText(HelloString
);
104 errln(UnicodeString("ERROR: strings should not be equal before assignment."));
108 errln(UnicodeString("ERROR: strings should be equal before assignment."));
111 int bixnext
= bix
->next();
112 int bi2next
= bi2
->next();
113 if (! (bixnext
== bi2next
&& bixnext
== 7)) {
114 errln(UnicodeString("ERROR: iterators behaved differently after assignment."));
117 if (bi2
->next() != 8) {
118 errln(UnicodeString("ERROR: iterator.next() failed after deleting copy."));
123 logln((UnicodeString
)"Testing clone()");
124 RuleBasedBreakIterator
* bi1clone
=(RuleBasedBreakIterator
*)bi1
->clone();
125 RuleBasedBreakIterator
* bi2clone
=(RuleBasedBreakIterator
*)bi2
->clone();
127 if(*bi1clone
!= *bi1
|| *bi1clone
!= *biequal
||
128 *bi1clone
== *bi3
|| *bi1clone
== *bi2
)
129 errln((UnicodeString
)"ERROR:1 RBBI's clone() method failed");
131 if(*bi2clone
== *bi1
|| *bi2clone
== *biequal
||
132 *bi2clone
== *bi3
|| *bi2clone
!= *bi2
)
133 errln((UnicodeString
)"ERROR:2 RBBI's clone() method failed");
135 if(bi1
->getText() != bi1clone
->getText() ||
136 bi2clone
->getText() != bi2
->getText() ||
137 *bi2clone
== *bi1clone
)
138 errln((UnicodeString
)"ERROR: RBBI's clone() method failed");
148 void RBBIAPITest::TestBoilerPlate()
150 UErrorCode status
= U_ZERO_ERROR
;
151 BreakIterator
* a
= BreakIterator::createWordInstance(Locale("hi"), status
);
152 BreakIterator
* b
= BreakIterator::createWordInstance(Locale("hi_IN"),status
);
153 if (U_FAILURE(status
)) {
154 errln("Creation of break iterator failed %s", u_errorName(status
));
158 errln("Failed: boilerplate method operator!= does not return correct results");
160 BreakIterator
* c
= BreakIterator::createWordInstance(Locale("ja"),status
);
163 errln("Failed: boilerplate method opertator== does not return correct results");
166 errln("creation of break iterator failed");
173 void RBBIAPITest::TestgetRules()
175 UErrorCode status
=U_ZERO_ERROR
;
177 RuleBasedBreakIterator
* bi1
=(RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
178 RuleBasedBreakIterator
* bi2
=(RuleBasedBreakIterator
*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status
);
179 if(U_FAILURE(status
)){
180 errln((UnicodeString
)"FAIL: in construction");
188 logln((UnicodeString
)"Testing toString()");
190 bi1
->setText((UnicodeString
)"Hello there");
192 RuleBasedBreakIterator
* bi3
=(RuleBasedBreakIterator
*)bi1
->clone();
194 UnicodeString temp
=bi1
->getRules();
195 UnicodeString temp2
=bi2
->getRules();
196 UnicodeString temp3
=bi3
->getRules();
197 if( temp2
.compare(temp3
) ==0 || temp
.compare(temp2
) == 0 || temp
.compare(temp3
) != 0)
198 errln((UnicodeString
)"ERROR: error in getRules() method");
204 void RBBIAPITest::TestHashCode()
206 UErrorCode status
=U_ZERO_ERROR
;
207 RuleBasedBreakIterator
* bi1
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
208 RuleBasedBreakIterator
* bi3
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
209 RuleBasedBreakIterator
* bi2
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status
);
210 if(U_FAILURE(status
)){
211 errln((UnicodeString
)"FAIL : in construction");
219 logln((UnicodeString
)"Testing hashCode()");
221 bi1
->setText((UnicodeString
)"Hash code");
222 bi2
->setText((UnicodeString
)"Hash code");
223 bi3
->setText((UnicodeString
)"Hash code");
225 RuleBasedBreakIterator
* bi1clone
= (RuleBasedBreakIterator
*)bi1
->clone();
226 RuleBasedBreakIterator
* bi2clone
= (RuleBasedBreakIterator
*)bi2
->clone();
228 if(bi1
->hashCode() != bi1clone
->hashCode() || bi1
->hashCode() != bi3
->hashCode() ||
229 bi1clone
->hashCode() != bi3
->hashCode() || bi2
->hashCode() != bi2clone
->hashCode())
230 errln((UnicodeString
)"ERROR: identical objects have different hashcodes");
232 if(bi1
->hashCode() == bi2
->hashCode() || bi2
->hashCode() == bi3
->hashCode() ||
233 bi1clone
->hashCode() == bi2clone
->hashCode() || bi1clone
->hashCode() == bi2
->hashCode())
234 errln((UnicodeString
)"ERROR: different objects have same hashcodes");
243 void RBBIAPITest::TestGetSetAdoptText()
245 logln((UnicodeString
)"Testing getText setText ");
246 UErrorCode status
=U_ZERO_ERROR
;
247 UnicodeString str1
="first string.";
248 UnicodeString str2
="Second string.";
249 RuleBasedBreakIterator
* charIter1
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
250 RuleBasedBreakIterator
* wordIter1
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status
);
251 if(U_FAILURE(status
)){
252 errln((UnicodeString
)"FAIL : in construction");
257 CharacterIterator
* text1
= new StringCharacterIterator(str1
);
258 CharacterIterator
* text1Clone
= text1
->clone();
259 CharacterIterator
* text2
= new StringCharacterIterator(str2
);
260 CharacterIterator
* text3
= new StringCharacterIterator(str2
, 3, 10, 3); // "ond str"
262 wordIter1
->setText(str1
);
263 CharacterIterator
*tci
= &wordIter1
->getText();
266 TEST_ASSERT(tstr
== str1
);
267 if(wordIter1
->current() != 0)
268 errln((UnicodeString
)"ERROR:1 setText did not set the iteration position to the beginning of the text, it is" + wordIter1
->current() + (UnicodeString
)"\n");
272 wordIter1
->setText(str2
);
273 if(wordIter1
->current() != 0)
274 errln((UnicodeString
)"ERROR:2 setText did not reset the iteration position to the beginning of the text, it is" + wordIter1
->current() + (UnicodeString
)"\n");
277 charIter1
->adoptText(text1Clone
);
278 TEST_ASSERT(wordIter1
->getText() != charIter1
->getText());
279 tci
= &wordIter1
->getText();
281 TEST_ASSERT(tstr
== str2
);
282 tci
= &charIter1
->getText();
284 TEST_ASSERT(tstr
== str1
);
287 RuleBasedBreakIterator
* rb
=(RuleBasedBreakIterator
*)wordIter1
->clone();
288 rb
->adoptText(text1
);
289 if(rb
->getText() != *text1
)
290 errln((UnicodeString
)"ERROR:1 error in adoptText ");
291 rb
->adoptText(text2
);
292 if(rb
->getText() != *text2
)
293 errln((UnicodeString
)"ERROR:2 error in adoptText ");
295 // Adopt where iterator range is less than the entire orignal source string.
296 // (With the change of the break engine to working with UText internally,
297 // CharacterIterators starting at positions other than zero are not supported)
298 rb
->adoptText(text3
);
299 TEST_ASSERT(rb
->preceding(2) == 0);
300 TEST_ASSERT(rb
->following(11) == BreakIterator::DONE
);
301 //if(rb->preceding(2) != 3) {
302 // errln((UnicodeString)"ERROR:3 error in adoptText ");
304 //if(rb->following(11) != BreakIterator::DONE) {
305 // errln((UnicodeString)"ERROR:4 error in adoptText ");
310 // Quick test to see if UText is working at all.
312 const char *s1
= "\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C\x64"; /* "hello world" in UTF-8 */
313 const char *s2
= "\x73\x65\x65\x20\x79\x61"; /* "see ya" in UTF-8 */
316 status
= U_ZERO_ERROR
;
317 UText
*ut
= utext_openUTF8(NULL
, s1
, -1, &status
);
318 wordIter1
->setText(ut
, status
);
319 TEST_ASSERT_SUCCESS(status
);
322 pos
= wordIter1
->first();
324 pos
= wordIter1
->next();
326 pos
= wordIter1
->next();
328 pos
= wordIter1
->next();
329 TEST_ASSERT(pos
==11);
330 pos
= wordIter1
->next();
331 TEST_ASSERT(pos
==UBRK_DONE
);
333 status
= U_ZERO_ERROR
;
334 UText
*ut2
= utext_openUTF8(NULL
, s2
, -1, &status
);
335 TEST_ASSERT_SUCCESS(status
);
336 wordIter1
->setText(ut2
, status
);
337 TEST_ASSERT_SUCCESS(status
);
339 pos
= wordIter1
->first();
341 pos
= wordIter1
->next();
343 pos
= wordIter1
->next();
346 pos
= wordIter1
->last();
348 pos
= wordIter1
->previous();
350 pos
= wordIter1
->previous();
352 pos
= wordIter1
->previous();
354 pos
= wordIter1
->previous();
355 TEST_ASSERT(pos
==UBRK_DONE
);
357 status
= U_ZERO_ERROR
;
358 UnicodeString sEmpty
;
359 UText
*gut2
= utext_openUnicodeString(NULL
, &sEmpty
, &status
);
360 wordIter1
->getUText(gut2
, status
);
361 TEST_ASSERT_SUCCESS(status
);
374 void RBBIAPITest::TestIteration()
376 // This test just verifies that the API is present.
377 // Testing for correct operation of the break rules happens elsewhere.
379 UErrorCode status
=U_ZERO_ERROR
;
380 RuleBasedBreakIterator
* bi
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
381 if (U_FAILURE(status
) || bi
== NULL
) {
382 errln("Failure creating character break iterator. Status = %s", u_errorName(status
));
387 bi
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createWordInstance(Locale::getDefault(), status
);
388 if (U_FAILURE(status
) || bi
== NULL
) {
389 errln("Failure creating Word break iterator. Status = %s", u_errorName(status
));
394 bi
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createLineInstance(Locale::getDefault(), status
);
395 if (U_FAILURE(status
) || bi
== NULL
) {
396 errln("Failure creating Line break iterator. Status = %s", u_errorName(status
));
401 bi
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createSentenceInstance(Locale::getDefault(), status
);
402 if (U_FAILURE(status
) || bi
== NULL
) {
403 errln("Failure creating Sentence break iterator. Status = %s", u_errorName(status
));
408 bi
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createTitleInstance(Locale::getDefault(), status
);
409 if (U_FAILURE(status
) || bi
== NULL
) {
410 errln("Failure creating Title break iterator. Status = %s", u_errorName(status
));
415 bi
= (RuleBasedBreakIterator
*)RuleBasedBreakIterator::createCharacterInstance(Locale::getDefault(), status
);
416 if (U_FAILURE(status
) || bi
== NULL
) {
417 errln("Failure creating character break iterator. Status = %s", u_errorName(status
));
418 return; // Skip the rest of these tests.
422 UnicodeString testString
="0123456789";
423 bi
->setText(testString
);
428 errln("Incorrect value from bi->first(). Expected 0, got %d.", i
);
433 errln("Incorrect value from bi->last(). Expected 10, got %d", i
);
442 errln("Incorrect value from bi->last() at line %d. Expected 9, got %d", __LINE__
, i
);
448 if (i
!= BreakIterator::DONE
) {
449 errln("Incorrect value from bi->previous() at line %d. Expected DONE, got %d", __LINE__
, i
);
458 errln("Incorrect value from bi->next() at line %d. Expected 1, got %d", __LINE__
, i
);
463 if (i
!= BreakIterator::DONE
) {
464 errln("Incorrect value from bi->next() at line %d. Expected DONE, got %d", __LINE__
, i
);
474 errln("Incorrect value from bi->previous() at line %d. Expected 0, got %d", __LINE__
, i
);
480 errln("Incorrect value from bi->previous() at line %d. Expected 1, got %d", __LINE__
, i
);
487 errln("Incorrect value from bi->previous() at line %d. Expected 10, got %d", __LINE__
, i
);
494 errln("Incorrect value from bi->previous() at line %d. Expected 0, got %d", __LINE__
, i
);
501 i
= bi
->following(4);
503 errln("Incorrect value from bi->following() at line %d. Expected 5, got %d", __LINE__
, i
);
506 i
= bi
->following(9);
508 errln("Incorrect value from bi->following() at line %d. Expected 10, got %d", __LINE__
, i
);
511 i
= bi
->following(10);
512 if (i
!= BreakIterator::DONE
) {
513 errln("Incorrect value from bi->following() at line %d. Expected DONE, got %d", __LINE__
, i
);
520 i
= bi
->preceding(4);
522 errln("Incorrect value from bi->preceding() at line %d. Expected 3, got %d", __LINE__
, i
);
525 i
= bi
->preceding(10);
527 errln("Incorrect value from bi->preceding() at line %d. Expected 9, got %d", __LINE__
, i
);
530 i
= bi
->preceding(1);
532 errln("Incorrect value from bi->preceding() at line %d. Expected 0, got %d", __LINE__
, i
);
535 i
= bi
->preceding(0);
536 if (i
!= BreakIterator::DONE
) {
537 errln("Incorrect value from bi->preceding() at line %d. Expected DONE, got %d", __LINE__
, i
);
545 if (bi
->isBoundary(3) != TRUE
) {
546 errln("Incorrect value from bi->isBoudary() at line %d. Expected TRUE, got FALSE", __LINE__
, i
);
550 errln("Incorrect value from bi->current() at line %d. Expected 3, got %d", __LINE__
, i
);
554 if (bi
->isBoundary(11) != FALSE
) {
555 errln("Incorrect value from bi->isBoudary() at line %d. Expected FALSE, got TRUE", __LINE__
, i
);
559 errln("Incorrect value from bi->current() at line %d. Expected 10, got %d", __LINE__
, i
);
568 errln("Incorrect value from bi->next() at line %d. Expected 4, got %d", __LINE__
, i
);
573 errln("Incorrect value from bi->next() at line %d. Expected 10, got %d", __LINE__
, i
);
578 if (i
!= BreakIterator::DONE
) {
579 errln("Incorrect value from bi->next() at line %d. Expected BreakIterator::DONE, got %d", __LINE__
, i
);
591 void RBBIAPITest::TestBuilder() {
592 UnicodeString rulesString1
= "$Letters = [:L:];\n"
593 "$Numbers = [:N:];\n"
596 "[^$Letters $Numbers];\n"
598 UnicodeString testString1
= "abc123..abc";
600 int32_t bounds1
[] = {0, 3, 6, 7, 8, 11};
601 UErrorCode status
=U_ZERO_ERROR
;
602 UParseError parseError
;
604 RuleBasedBreakIterator
*bi
= new RuleBasedBreakIterator(rulesString1
, parseError
, status
);
605 if(U_FAILURE(status
)) {
606 errln("FAIL : in construction");
608 bi
->setText(testString1
);
609 doBoundaryTest(*bi
, testString1
, bounds1
);
617 // Single quotes within rules imply a grouping, so that a modifier
618 // following the quoted text (* or +) applies to all of the quoted chars.
620 void RBBIAPITest::TestQuoteGrouping() {
621 UnicodeString rulesString1
= "#Here comes the rule...\n"
622 "'$@!'*;\n" // (\$\@\!)*
625 UnicodeString testString1
= "$@!$@!X$@!!X";
627 int32_t bounds1
[] = {0, 6, 7, 10, 11, 12};
628 UErrorCode status
=U_ZERO_ERROR
;
629 UParseError parseError
;
631 RuleBasedBreakIterator
*bi
= new RuleBasedBreakIterator(rulesString1
, parseError
, status
);
632 if(U_FAILURE(status
)) {
633 errln("FAIL : in construction");
635 bi
->setText(testString1
);
636 doBoundaryTest(*bi
, testString1
, bounds1
);
643 // Test word break rule status constants.
645 void RBBIAPITest::TestRuleStatus() {
647 u_unescape("plain word 123.45 \\u9160\\u9161 \\u30a1\\u30a2 \\u3041\\u3094",
648 // 012345678901234567 8 9 0 1 2 3 4 5 6
649 // Ideographic Katakana Hiragana
651 UnicodeString
testString1(str
);
652 int32_t bounds1
[] = {0, 5, 6, 10, 11, 17, 18, 19, 20, 21, 23, 24, 25, 26};
653 int32_t tag_lo
[] = {UBRK_WORD_NONE
, UBRK_WORD_LETTER
, UBRK_WORD_NONE
, UBRK_WORD_LETTER
,
654 UBRK_WORD_NONE
, UBRK_WORD_NUMBER
, UBRK_WORD_NONE
,
655 UBRK_WORD_IDEO
, UBRK_WORD_IDEO
, UBRK_WORD_NONE
,
656 UBRK_WORD_KANA
, UBRK_WORD_NONE
, UBRK_WORD_KANA
, UBRK_WORD_KANA
};
658 int32_t tag_hi
[] = {UBRK_WORD_NONE_LIMIT
, UBRK_WORD_LETTER_LIMIT
, UBRK_WORD_NONE_LIMIT
, UBRK_WORD_LETTER_LIMIT
,
659 UBRK_WORD_NONE_LIMIT
, UBRK_WORD_NUMBER_LIMIT
, UBRK_WORD_NONE_LIMIT
,
660 UBRK_WORD_IDEO_LIMIT
, UBRK_WORD_IDEO_LIMIT
, UBRK_WORD_NONE_LIMIT
,
661 UBRK_WORD_KANA_LIMIT
, UBRK_WORD_NONE_LIMIT
, UBRK_WORD_KANA_LIMIT
, UBRK_WORD_KANA_LIMIT
};
663 UErrorCode status
=U_ZERO_ERROR
;
665 RuleBasedBreakIterator
*bi
= (RuleBasedBreakIterator
*)BreakIterator::createWordInstance(Locale::getEnglish(), status
);
666 if(U_FAILURE(status
)) {
667 errln("FAIL : in construction");
669 bi
->setText(testString1
);
670 // First test that the breaks are in the right spots.
671 doBoundaryTest(*bi
, testString1
, bounds1
);
673 // Then go back and check tag values
676 for (pos
= bi
->first(); pos
!= BreakIterator::DONE
; pos
= bi
->next(), i
++) {
677 if (pos
!= bounds1
[i
]) {
678 errln("FAIL: unexpected word break at postion %d", pos
);
681 tag
= bi
->getRuleStatus();
682 if (tag
< tag_lo
[i
] || tag
>= tag_hi
[i
]) {
683 errln("FAIL: incorrect tag value %d at position %d", tag
, pos
);
687 // Check that we get the same tag values from getRuleStatusVec()
689 int t
= bi
->getRuleStatusVec(vec
, 10, status
);
690 TEST_ASSERT_SUCCESS(status
);
692 TEST_ASSERT(vec
[0] == tag
);
697 // Now test line break status. This test mostly is to confirm that the status constants
698 // are correctly declared in the header.
699 testString1
= "test line. \n";
702 bi
= (RuleBasedBreakIterator
*)
703 BreakIterator::createLineInstance(Locale::getEnglish(), status
);
704 if(U_FAILURE(status
)) {
705 errln("failed to create word break iterator.");
711 bi
->setText(testString1
);
713 tag
= bi
->getRuleStatus();
714 for (i
=0; i
<3; i
++) {
717 success
= pos
==0 && tag
==UBRK_LINE_SOFT
; break;
719 success
= pos
==5 && tag
==UBRK_LINE_SOFT
; break;
721 success
= pos
==12 && tag
==UBRK_LINE_HARD
; break;
723 success
= FALSE
; break;
725 if (success
== FALSE
) {
726 errln("Fail: incorrect word break status or position. i=%d, pos=%d, tag=%d",
731 tag
= bi
->getRuleStatus();
733 if (UBRK_LINE_SOFT
>= UBRK_LINE_SOFT_LIMIT
||
734 UBRK_LINE_HARD
>= UBRK_LINE_HARD_LIMIT
||
735 UBRK_LINE_HARD
> UBRK_LINE_SOFT
&& UBRK_LINE_HARD
< UBRK_LINE_SOFT_LIMIT
) {
736 errln("UBRK_LINE_* constants from header are inconsistent.");
746 // Test the vector form of break rule status.
748 void RBBIAPITest::TestRuleStatusVec() {
749 UnicodeString rulesString
= "[A-N]{100}; \n"
755 UnicodeString testString1
= "Aapz5?";
756 int32_t statusVals
[10];
760 UErrorCode status
=U_ZERO_ERROR
;
761 UParseError parseError
;
763 RuleBasedBreakIterator
*bi
= new RuleBasedBreakIterator(rulesString
, parseError
, status
);
764 TEST_ASSERT_SUCCESS(status
);
765 if (U_SUCCESS(status
)) {
766 bi
->setText(testString1
);
771 numStatuses
= bi
->getRuleStatusVec(statusVals
, 10, status
);
772 TEST_ASSERT_SUCCESS(status
);
773 TEST_ASSERT(numStatuses
== 2);
774 TEST_ASSERT(statusVals
[0] == 100);
775 TEST_ASSERT(statusVals
[1] == 300);
780 numStatuses
= bi
->getRuleStatusVec(statusVals
, 10, status
);
781 TEST_ASSERT_SUCCESS(status
);
782 TEST_ASSERT(numStatuses
== 2);
783 TEST_ASSERT(statusVals
[0] == 200);
784 TEST_ASSERT(statusVals
[1] == 300);
789 numStatuses
= bi
->getRuleStatusVec(statusVals
, 10, status
);
790 TEST_ASSERT_SUCCESS(status
);
791 TEST_ASSERT(numStatuses
== 2);
792 TEST_ASSERT(statusVals
[0] == 200);
793 TEST_ASSERT(statusVals
[1] == 300);
798 numStatuses
= bi
->getRuleStatusVec(statusVals
, 10, status
);
799 TEST_ASSERT_SUCCESS(status
);
800 TEST_ASSERT(numStatuses
== 1);
801 TEST_ASSERT(statusVals
[0] == 300);
806 numStatuses
= bi
->getRuleStatusVec(statusVals
, 10, status
);
807 TEST_ASSERT_SUCCESS(status
);
808 TEST_ASSERT(numStatuses
== 2);
809 TEST_ASSERT(statusVals
[0] == 400);
810 TEST_ASSERT(statusVals
[1] == 500);
815 numStatuses
= bi
->getRuleStatusVec(statusVals
, 10, status
);
816 TEST_ASSERT_SUCCESS(status
);
817 TEST_ASSERT(numStatuses
== 1);
818 TEST_ASSERT(statusVals
[0] == 0);
821 // Check buffer overflow error handling. Char == A
826 memset(statusVals
, -1, sizeof(statusVals
));
827 numStatuses
= bi
->getRuleStatusVec(statusVals
, 0, status
);
828 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
829 TEST_ASSERT(numStatuses
== 2);
830 TEST_ASSERT(statusVals
[0] == -1);
832 status
= U_ZERO_ERROR
;
833 memset(statusVals
, -1, sizeof(statusVals
));
834 numStatuses
= bi
->getRuleStatusVec(statusVals
, 1, status
);
835 TEST_ASSERT(status
== U_BUFFER_OVERFLOW_ERROR
);
836 TEST_ASSERT(numStatuses
== 2);
837 TEST_ASSERT(statusVals
[0] == 100);
838 TEST_ASSERT(statusVals
[1] == -1);
840 status
= U_ZERO_ERROR
;
841 memset(statusVals
, -1, sizeof(statusVals
));
842 numStatuses
= bi
->getRuleStatusVec(statusVals
, 2, status
);
843 TEST_ASSERT_SUCCESS(status
);
844 TEST_ASSERT(numStatuses
== 2);
845 TEST_ASSERT(statusVals
[0] == 100);
846 TEST_ASSERT(statusVals
[1] == 300);
847 TEST_ASSERT(statusVals
[2] == -1);
854 // Bug 2190 Regression test. Builder crash on rule consisting of only a
855 // $variable reference
856 void RBBIAPITest::TestBug2190() {
857 UnicodeString rulesString1
= "$aaa = abcd;\n"
860 UnicodeString testString1
= "abcdabcd";
862 int32_t bounds1
[] = {0, 4, 8};
863 UErrorCode status
=U_ZERO_ERROR
;
864 UParseError parseError
;
866 RuleBasedBreakIterator
*bi
= new RuleBasedBreakIterator(rulesString1
, parseError
, status
);
867 if(U_FAILURE(status
)) {
868 errln("FAIL : in construction");
870 bi
->setText(testString1
);
871 doBoundaryTest(*bi
, testString1
, bounds1
);
877 void RBBIAPITest::TestRegistration() {
878 #if !UCONFIG_NO_SERVICE
879 UErrorCode status
= U_ZERO_ERROR
;
880 BreakIterator
* ja_word
= BreakIterator::createWordInstance("ja_JP", status
);
882 // ok to not delete these if we exit because of error?
883 BreakIterator
* ja_char
= BreakIterator::createCharacterInstance("ja_JP", status
);
884 BreakIterator
* root_word
= BreakIterator::createWordInstance("", status
);
885 BreakIterator
* root_char
= BreakIterator::createCharacterInstance("", status
);
887 URegistryKey key
= BreakIterator::registerInstance(ja_word
, "xx", UBRK_WORD
, status
);
889 if (ja_word
&& *ja_word
== *root_word
) {
890 errln("japan not different from root");
895 BreakIterator
* result
= BreakIterator::createWordInstance("xx_XX", status
);
898 fail
= *result
!= *ja_word
;
902 errln("bad result for xx_XX/word");
907 BreakIterator
* result
= BreakIterator::createCharacterInstance("ja_JP", status
);
910 fail
= *result
!= *ja_char
;
914 errln("bad result for ja_JP/char");
919 BreakIterator
* result
= BreakIterator::createCharacterInstance("xx_XX", status
);
922 fail
= *result
!= *root_char
;
926 errln("bad result for xx_XX/char");
931 StringEnumeration
* avail
= BreakIterator::getAvailableLocales();
933 const UnicodeString
* p
;
934 while ((p
= avail
->snext(status
))) {
935 if (p
->compare("xx") == 0) {
942 errln("did not find test locale");
947 UBool unreg
= BreakIterator::unregister(key
, status
);
949 errln("unable to unregister");
954 BreakIterator
* result
= BreakIterator::createWordInstance("en_US", status
);
955 BreakIterator
* root
= BreakIterator::createWordInstance("", status
);
958 fail
= *root
!= *result
;
963 errln("did not get root break");
968 StringEnumeration
* avail
= BreakIterator::getAvailableLocales();
970 const UnicodeString
* p
;
971 while ((p
= avail
->snext(status
))) {
972 if (p
->compare("xx") == 0) {
979 errln("found test locale");
985 UBool foundLocale
= FALSE
;
986 const Locale
*avail
= BreakIterator::getAvailableLocales(count
);
987 for (int i
=0; i
<count
; i
++) {
988 if (avail
[i
] == Locale::getEnglish()) {
993 if (foundLocale
== FALSE
) {
994 errln("BreakIterator::getAvailableLocales(&count), failed to find EN.");
999 // ja_word was adopted by factory
1006 void RBBIAPITest::RoundtripRule(const char *dataFile
) {
1007 UErrorCode status
= U_ZERO_ERROR
;
1008 UParseError parseError
;
1009 parseError
.line
= 0;
1010 parseError
.offset
= 0;
1011 UDataMemory
*data
= udata_open(U_ICUDATA_BRKITR
, "brk", dataFile
, &status
);
1013 const UChar
*builtSource
;
1014 const uint8_t *rbbiRules
;
1015 const uint8_t *builtRules
;
1017 if (U_FAILURE(status
)) {
1018 errln("Can't open \"%s\"", dataFile
);
1022 builtRules
= (const uint8_t *)udata_getMemory(data
);
1023 builtSource
= (const UChar
*)(builtRules
+ ((RBBIDataHeader
*)builtRules
)->fRuleSource
);
1024 RuleBasedBreakIterator
*brkItr
= new RuleBasedBreakIterator(builtSource
, parseError
, status
);
1025 if (U_FAILURE(status
)) {
1026 errln("createRuleBasedBreakIterator: ICU Error \"%s\" at line %d, column %d\n",
1027 u_errorName(status
), parseError
.line
, parseError
.offset
);
1030 rbbiRules
= brkItr
->getBinaryRules(length
);
1031 logln("Comparing \"%s\" len=%d", dataFile
, length
);
1032 if (memcmp(builtRules
, rbbiRules
, (int32_t)length
) != 0) {
1033 errln("Built rules and rebuilt rules are different %s", dataFile
);
1040 void RBBIAPITest::TestRoundtripRules() {
1041 RoundtripRule("word");
1042 RoundtripRule("title");
1043 RoundtripRule("sent");
1044 RoundtripRule("line");
1045 RoundtripRule("char");
1047 RoundtripRule("word_ja");
1048 RoundtripRule("word_POSIX");
1052 //---------------------------------------------
1054 //---------------------------------------------
1056 void RBBIAPITest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
1058 if (exec
) logln((UnicodeString
)"TestSuite RuleBasedBreakIterator API ");
1060 // case 0: name = "TestConstruction"; if (exec) TestConstruction(); break;
1061 case 0: name
= "TestCloneEquals"; if (exec
) TestCloneEquals(); break;
1062 case 1: name
= "TestgetRules"; if (exec
) TestgetRules(); break;
1063 case 2: name
= "TestHashCode"; if (exec
) TestHashCode(); break;
1064 case 3: name
= "TestGetSetAdoptText"; if (exec
) TestGetSetAdoptText(); break;
1065 case 4: name
= "TestIteration"; if (exec
) TestIteration(); break;
1066 case 5: name
= "TestBuilder"; if (exec
) TestBuilder(); break;
1067 case 6: name
= "TestQuoteGrouping"; if (exec
) TestQuoteGrouping(); break;
1068 case 7: name
= "TestRuleStatus"; if (exec
) TestRuleStatus(); break;
1069 case 8: name
= "TestRuleStatusVec"; if (exec
) TestRuleStatusVec(); break;
1070 case 9: name
= "TestBug2190"; if (exec
) TestBug2190(); break;
1071 case 10: name
= "TestRegistration"; if (exec
) TestRegistration(); break;
1072 case 11: name
= "TestBoilerPlate"; if (exec
) TestBoilerPlate(); break;
1073 case 12: name
= "TestRoundtripRules"; if (exec
) TestRoundtripRules(); break;
1075 default: name
= ""; break; // needed to end loop
1079 //---------------------------------------------
1080 //Internal subroutines
1081 //---------------------------------------------
1083 void RBBIAPITest::doBoundaryTest(RuleBasedBreakIterator
& bi
, UnicodeString
& text
, int32_t *boundaries
){
1084 logln((UnicodeString
)"testIsBoundary():");
1087 for (int32_t i
= 0; i
< text
.length(); i
++) {
1088 isB
= bi
.isBoundary(i
);
1089 logln((UnicodeString
)"bi.isBoundary(" + i
+ ") -> " + isB
);
1091 if (i
== boundaries
[p
]) {
1093 errln((UnicodeString
)"Wrong result from isBoundary() for " + i
+ (UnicodeString
)": expected true, got false");
1098 errln((UnicodeString
)"Wrong result from isBoundary() for " + i
+ (UnicodeString
)": expected false, got true");
1102 void RBBIAPITest::doTest(UnicodeString
& testString
, int32_t start
, int32_t gotoffset
, int32_t expectedOffset
, const char* expectedString
){
1103 UnicodeString selected
;
1104 UnicodeString expected
=CharsToUnicodeString(expectedString
);
1106 if(gotoffset
!= expectedOffset
)
1107 errln((UnicodeString
)"ERROR:****returned #" + gotoffset
+ (UnicodeString
)" instead of #" + expectedOffset
);
1108 if(start
<= gotoffset
){
1109 testString
.extractBetween(start
, gotoffset
, selected
);
1112 testString
.extractBetween(gotoffset
, start
, selected
);
1114 if(selected
.compare(expected
) != 0)
1115 errln(prettify((UnicodeString
)"ERROR:****selected \"" + selected
+ "\" instead of \"" + expected
+ "\""));
1117 logln(prettify("****selected \"" + selected
+ "\""));
1120 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */