2 *****************************************************************************
3 * Copyright (C) 2001-2008, International Business Machines orporation
4 * and others. All Rights Reserved.
5 ****************************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_COLLATION
12 #include "../cintltst/usrchdat.c"
13 #include "unicode/stsearch.h"
14 #include "unicode/ustring.h"
15 #include "unicode/schriter.h"
19 // private definitions -----------------------------------------------------
21 #define CASE(id,test) \
26 logln((UnicodeString)""); \
28 errln(__FILE__ " cannot test - failed to create collator."); \
35 // public contructors and destructors --------------------------------------
37 StringSearchTest::StringSearchTest()
38 #if !UCONFIG_NO_BREAK_ITERATION
40 m_en_wordbreaker_(NULL
), m_en_characterbreaker_(NULL
)
43 #if !UCONFIG_NO_BREAK_ITERATION
44 UErrorCode status
= U_ZERO_ERROR
;
46 m_en_us_
= (RuleBasedCollator
*)Collator::createInstance("en_US", status
);
47 m_fr_fr_
= (RuleBasedCollator
*)Collator::createInstance("fr_FR", status
);
48 m_de_
= (RuleBasedCollator
*)Collator::createInstance("de_DE", status
);
49 m_es_
= (RuleBasedCollator
*)Collator::createInstance("es_ES", status
);
50 if(U_FAILURE(status
)) {
59 errln("Collator creation failed with %s", u_errorName(status
));
65 rules
.setTo(((RuleBasedCollator
*)m_de_
)->getRules());
66 UChar extrarules
[128];
67 u_unescape(EXTRACOLLATIONRULE
, extrarules
, 128);
68 rules
.append(extrarules
, u_strlen(extrarules
));
71 m_de_
= new RuleBasedCollator(rules
, status
);
73 rules
.setTo(((RuleBasedCollator
*)m_es_
)->getRules());
74 rules
.append(extrarules
, u_strlen(extrarules
));
78 m_es_
= new RuleBasedCollator(rules
, status
);
80 #if !UCONFIG_NO_BREAK_ITERATION
81 m_en_wordbreaker_
= BreakIterator::createWordInstance(
82 Locale::getEnglish(), status
);
83 m_en_characterbreaker_
= BreakIterator::createCharacterInstance(
84 Locale::getEnglish(), status
);
89 StringSearchTest::~StringSearchTest()
91 #if !UCONFIG_NO_BREAK_ITERATION
96 #if !UCONFIG_NO_BREAK_ITERATION
97 delete m_en_wordbreaker_
;
98 delete m_en_characterbreaker_
;
103 // public methods ----------------------------------------------------------
105 void StringSearchTest::runIndexedTest(int32_t index
, UBool exec
,
106 const char* &name
, char* )
108 #if !UCONFIG_NO_BREAK_ITERATION
109 UBool areBroken
= FALSE
;
110 if (m_en_us_
== NULL
&& m_fr_fr_
== NULL
&& m_de_
== NULL
&&
111 m_es_
== NULL
&& m_en_wordbreaker_
== NULL
&&
112 m_en_characterbreaker_
== NULL
&& exec
) {
117 CASE(0, TestOpenClose
)
118 CASE(1, TestInitialization
)
120 CASE(3, TestNormExact
)
121 CASE(4, TestStrength
)
122 #if UCONFIG_NO_BREAK_ITERATION
124 name
= "TestBreakIterator";
127 CASE(5, TestBreakIterator
)
129 CASE(6, TestVariable
)
131 CASE(8, TestCollator
)
134 CASE(11, TestCompositeBoundaries
)
135 CASE(12, TestGetSetOffset
)
136 CASE(13, TestGetSetAttribute
)
137 CASE(14, TestGetMatch
)
138 CASE(15, TestSetMatch
)
140 CASE(17, TestSupplementary
)
141 CASE(18, TestContraction
)
142 CASE(19, TestIgnorable
)
143 CASE(20, TestCanonical
)
144 CASE(21, TestNormCanonical
)
145 CASE(22, TestStrengthCanonical
)
146 #if UCONFIG_NO_BREAK_ITERATION
148 name
= "TestBreakIteratorCanonical";
151 CASE(23, TestBreakIteratorCanonical
)
153 CASE(24, TestVariableCanonical
)
154 CASE(25, TestOverlapCanonical
)
155 CASE(26, TestCollatorCanonical
)
156 CASE(27, TestPatternCanonical
)
157 CASE(28, TestTextCanonical
)
158 CASE(29, TestCompositeBoundariesCanonical
)
159 CASE(30, TestGetSetOffsetCanonical
)
160 CASE(31, TestSupplementaryCanonical
)
161 CASE(32, TestContractionCanonical
)
162 CASE(33, TestUClassID
)
163 CASE(34, TestSubclass
)
164 CASE(35, TestCoverage
)
165 CASE(36, TestDiacriticMatch
)
166 default: name
= ""; break;
173 #if !UCONFIG_NO_BREAK_ITERATION
174 // private methods ------------------------------------------------------
176 RuleBasedCollator
* StringSearchTest::getCollator(const char *collator
)
178 if (collator
== NULL
) {
181 if (strcmp(collator
, "fr") == 0) {
184 else if (strcmp(collator
, "de") == 0) {
187 else if (strcmp(collator
, "es") == 0) {
195 BreakIterator
* StringSearchTest::getBreakIterator(const char *breaker
)
197 #if UCONFIG_NO_BREAK_ITERATION
200 if (breaker
== NULL
) {
203 if (strcmp(breaker
, "wordbreaker") == 0) {
204 return m_en_wordbreaker_
;
207 return m_en_characterbreaker_
;
212 char * StringSearchTest::toCharString(const UnicodeString
&text
)
214 static char result
[1024];
217 int length
= text
.length();
219 for (; count
< length
; count
++) {
220 UChar ch
= text
[count
];
221 if (ch
>= 0x20 && ch
<= 0x7e) {
222 result
[index
++] = (char)ch
;
225 sprintf(result
+index
, "\\u%04x", ch
);
226 index
+= 6; /* \uxxxx */
234 Collator::ECollationStrength
StringSearchTest::getECollationStrength(
235 const UCollationStrength
&strength
) const
240 return Collator::PRIMARY
;
241 case UCOL_SECONDARY
:
242 return Collator::SECONDARY
;
244 return Collator::TERTIARY
;
246 return Collator::IDENTICAL
;
250 UBool
StringSearchTest::assertEqualWithStringSearch(StringSearch
*strsrch
,
251 const SearchData
*search
)
254 UErrorCode status
= U_ZERO_ERROR
;
255 int32_t matchindex
= search
->offset
[count
];
256 UnicodeString matchtext
;
258 if (strsrch
->getMatchedStart() != USEARCH_DONE
||
259 strsrch
->getMatchedLength() != 0) {
260 errln("Error with the initialization of match start and length");
262 // start of following matches
263 while (U_SUCCESS(status
) && matchindex
>= 0) {
264 int32_t matchlength
= search
->size
[count
];
265 strsrch
->next(status
);
266 if (matchindex
!= strsrch
->getMatchedStart() ||
267 matchlength
!= strsrch
->getMatchedLength()) {
268 char *str
= toCharString(strsrch
->getText());
269 errln("Text: %s", str
);
270 str
= toCharString(strsrch
->getPattern());
271 infoln("Pattern: %s", str
);
272 infoln("Error following match found at %d %d",
273 strsrch
->getMatchedStart(), strsrch
->getMatchedLength());
278 strsrch
->getMatchedText(matchtext
);
280 if (U_FAILURE(status
) ||
281 strsrch
->getText().compareBetween(matchindex
,
282 matchindex
+ matchlength
,
284 matchtext
.length())) {
285 errln("Error getting following matched text");
288 matchindex
= search
->offset
[count
];
290 strsrch
->next(status
);
291 if (strsrch
->getMatchedStart() != USEARCH_DONE
||
292 strsrch
->getMatchedLength() != 0) {
293 char *str
= toCharString(strsrch
->getText());
294 errln("Text: %s", str
);
295 str
= toCharString(strsrch
->getPattern());
296 errln("Pattern: %s", str
);
297 errln("Error following match found at %d %d",
298 strsrch
->getMatchedStart(), strsrch
->getMatchedLength());
301 // start of preceding matches
302 count
= count
== 0 ? 0 : count
- 1;
303 matchindex
= search
->offset
[count
];
304 while (U_SUCCESS(status
) && matchindex
>= 0) {
305 int32_t matchlength
= search
->size
[count
];
306 strsrch
->previous(status
);
307 if (matchindex
!= strsrch
->getMatchedStart() ||
308 matchlength
!= strsrch
->getMatchedLength()) {
309 char *str
= toCharString(strsrch
->getText());
310 errln("Text: %s", str
);
311 str
= toCharString(strsrch
->getPattern());
312 errln("Pattern: %s", str
);
313 errln("Error following match found at %d %d",
314 strsrch
->getMatchedStart(), strsrch
->getMatchedLength());
318 strsrch
->getMatchedText(matchtext
);
320 if (U_FAILURE(status
) ||
321 strsrch
->getText().compareBetween(matchindex
,
322 matchindex
+ matchlength
,
324 matchtext
.length())) {
325 errln("Error getting following matched text");
328 matchindex
= count
> 0 ? search
->offset
[count
- 1] : -1;
331 strsrch
->previous(status
);
332 if (strsrch
->getMatchedStart() != USEARCH_DONE
||
333 strsrch
->getMatchedLength() != 0) {
334 char *str
= toCharString(strsrch
->getText());
335 errln("Text: %s", str
);
336 str
= toCharString(strsrch
->getPattern());
337 errln("Pattern: %s", str
);
338 errln("Error following match found at %d %d",
339 strsrch
->getMatchedStart(), strsrch
->getMatchedLength());
345 UBool
StringSearchTest::assertEqual(const SearchData
*search
)
347 UErrorCode status
= U_ZERO_ERROR
;
349 Collator
*collator
= getCollator(search
->collator
);
350 BreakIterator
*breaker
= getBreakIterator(search
->breaker
);
351 StringSearch
*strsrch
, *strsrch2
;
354 #if UCONFIG_NO_BREAK_ITERATION
355 if(search
->breaker
) {
356 return TRUE
; /* skip test */
359 u_unescape(search
->text
, temp
, 128);
362 u_unescape(search
->pattern
, temp
, 128);
363 UnicodeString pattern
;
366 #if !UCONFIG_NO_BREAK_ITERATION
367 if (breaker
!= NULL
) {
368 breaker
->setText(text
);
371 collator
->setStrength(getECollationStrength(search
->strength
));
372 strsrch
= new StringSearch(pattern
, text
, (RuleBasedCollator
*)collator
,
374 if (U_FAILURE(status
)) {
375 errln("Error opening string search %s", u_errorName(status
));
379 if (!assertEqualWithStringSearch(strsrch
, search
)) {
380 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
386 strsrch2
= strsrch
->clone();
387 if( strsrch2
== strsrch
|| *strsrch2
!= *strsrch
||
388 !assertEqualWithStringSearch(strsrch2
, search
)
390 infoln("failure with StringSearch.clone()");
391 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
398 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
403 UBool
StringSearchTest::assertCanonicalEqual(const SearchData
*search
)
405 UErrorCode status
= U_ZERO_ERROR
;
406 Collator
*collator
= getCollator(search
->collator
);
407 BreakIterator
*breaker
= getBreakIterator(search
->breaker
);
408 StringSearch
*strsrch
;
412 #if UCONFIG_NO_BREAK_ITERATION
413 if(search
->breaker
) {
414 return TRUE
; /* skip test */
418 u_unescape(search
->text
, temp
, 128);
421 u_unescape(search
->pattern
, temp
, 128);
422 UnicodeString pattern
;
425 #if !UCONFIG_NO_BREAK_ITERATION
426 if (breaker
!= NULL
) {
427 breaker
->setText(text
);
430 collator
->setStrength(getECollationStrength(search
->strength
));
431 collator
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
432 strsrch
= new StringSearch(pattern
, text
, (RuleBasedCollator
*)collator
,
434 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
435 if (U_FAILURE(status
)) {
436 errln("Error opening string search %s", u_errorName(status
));
441 if (!assertEqualWithStringSearch(strsrch
, search
)) {
447 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
448 collator
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, status
);
454 UBool
StringSearchTest::assertEqualWithAttribute(const SearchData
*search
,
455 USearchAttributeValue canonical
,
456 USearchAttributeValue overlap
)
458 UErrorCode status
= U_ZERO_ERROR
;
459 Collator
*collator
= getCollator(search
->collator
);
460 BreakIterator
*breaker
= getBreakIterator(search
->breaker
);
461 StringSearch
*strsrch
;
465 #if UCONFIG_NO_BREAK_ITERATION
466 if(search
->breaker
) {
467 return TRUE
; /* skip test */
471 u_unescape(search
->text
, temp
, 128);
474 u_unescape(search
->pattern
, temp
, 128);
475 UnicodeString pattern
;
478 #if !UCONFIG_NO_BREAK_ITERATION
479 if (breaker
!= NULL
) {
480 breaker
->setText(text
);
483 collator
->setStrength(getECollationStrength(search
->strength
));
484 strsrch
= new StringSearch(pattern
, text
, (RuleBasedCollator
*)collator
,
486 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, canonical
, status
);
487 strsrch
->setAttribute(USEARCH_OVERLAP
, overlap
, status
);
489 if (U_FAILURE(status
)) {
490 errln("Error opening string search %s", u_errorName(status
));
494 if (!assertEqualWithStringSearch(strsrch
, search
)) {
495 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
499 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
504 void StringSearchTest::TestOpenClose()
506 UErrorCode status
= U_ZERO_ERROR
;
507 StringSearch
*result
;
508 BreakIterator
*breakiter
= m_en_wordbreaker_
;
509 UnicodeString pattern
;
511 UnicodeString
temp("a");
512 StringCharacterIterator
chariter(text
);
514 /* testing null arguments */
515 result
= new StringSearch(pattern
, text
, NULL
, NULL
, status
);
516 if (U_SUCCESS(status
)) {
517 errln("Error: NULL arguments should produce an error");
521 chariter
.setText(text
);
522 status
= U_ZERO_ERROR
;
523 result
= new StringSearch(pattern
, chariter
, NULL
, NULL
, status
);
524 if (U_SUCCESS(status
)) {
525 errln("Error: NULL arguments should produce an error");
530 status
= U_ZERO_ERROR
;
531 result
= new StringSearch(pattern
, text
, NULL
, NULL
, status
);
532 if (U_SUCCESS(status
)) {
533 errln("Error: Empty pattern should produce an error");
537 chariter
.setText(text
);
538 status
= U_ZERO_ERROR
;
539 result
= new StringSearch(pattern
, chariter
, NULL
, NULL
, status
);
540 if (U_SUCCESS(status
)) {
541 errln("Error: Empty pattern should produce an error");
546 pattern
.append(temp
);
547 status
= U_ZERO_ERROR
;
548 result
= new StringSearch(pattern
, text
, NULL
, NULL
, status
);
549 if (U_SUCCESS(status
)) {
550 errln("Error: Empty text should produce an error");
554 chariter
.setText(text
);
555 status
= U_ZERO_ERROR
;
556 result
= new StringSearch(pattern
, chariter
, NULL
, NULL
, status
);
557 if (U_SUCCESS(status
)) {
558 errln("Error: Empty text should produce an error");
563 status
= U_ZERO_ERROR
;
564 result
= new StringSearch(pattern
, text
, NULL
, NULL
, status
);
565 if (U_SUCCESS(status
)) {
566 errln("Error: NULL arguments should produce an error");
570 chariter
.setText(text
);
571 status
= U_ZERO_ERROR
;
572 result
= new StringSearch(pattern
, chariter
, NULL
, NULL
, status
);
573 if (U_SUCCESS(status
)) {
574 errln("Error: NULL arguments should produce an error");
578 status
= U_ZERO_ERROR
;
579 result
= new StringSearch(pattern
, text
, m_en_us_
, NULL
, status
);
580 if (U_FAILURE(status
)) {
581 errln("Error: NULL break iterator is valid for opening search");
585 status
= U_ZERO_ERROR
;
586 result
= new StringSearch(pattern
, chariter
, m_en_us_
, NULL
, status
);
587 if (U_FAILURE(status
)) {
588 errln("Error: NULL break iterator is valid for opening search");
592 status
= U_ZERO_ERROR
;
593 result
= new StringSearch(pattern
, text
, Locale::getEnglish(), NULL
, status
);
594 if (U_FAILURE(status
) || result
== NULL
) {
595 errln("Error: NULL break iterator is valid for opening search");
599 status
= U_ZERO_ERROR
;
600 result
= new StringSearch(pattern
, chariter
, Locale::getEnglish(), NULL
, status
);
601 if (U_FAILURE(status
)) {
602 errln("Error: NULL break iterator is valid for opening search");
606 status
= U_ZERO_ERROR
;
607 result
= new StringSearch(pattern
, text
, m_en_us_
, breakiter
, status
);
608 if (U_FAILURE(status
)) {
609 errln("Error: Break iterator is valid for opening search");
613 status
= U_ZERO_ERROR
;
614 result
= new StringSearch(pattern
, chariter
, m_en_us_
, NULL
, status
);
615 if (U_FAILURE(status
)) {
616 errln("Error: Break iterator is valid for opening search");
621 void StringSearchTest::TestInitialization()
623 UErrorCode status
= U_ZERO_ERROR
;
624 UnicodeString pattern
;
626 UnicodeString
temp("a");
627 StringSearch
*result
;
630 /* simple test on the pattern ce construction */
631 pattern
.append(temp
);
632 pattern
.append(temp
);
636 result
= new StringSearch(pattern
, text
, m_en_us_
, NULL
, status
);
637 if (U_FAILURE(status
)) {
638 errln("Error opening search %s", u_errorName(status
));
640 StringSearch
*copy
= new StringSearch(*result
);
641 if (*(copy
->getCollator()) != *(result
->getCollator()) ||
642 copy
->getBreakIterator() != result
->getBreakIterator() ||
643 copy
->getMatchedLength() != result
->getMatchedLength() ||
644 copy
->getMatchedStart() != result
->getMatchedStart() ||
645 copy
->getOffset() != result
->getOffset() ||
646 copy
->getPattern() != result
->getPattern() ||
647 copy
->getText() != result
->getText() ||
648 *(copy
) != *(result
))
650 errln("Error copying StringSearch");
654 copy
= (StringSearch
*)result
->safeClone();
655 if (*(copy
->getCollator()) != *(result
->getCollator()) ||
656 copy
->getBreakIterator() != result
->getBreakIterator() ||
657 copy
->getMatchedLength() != result
->getMatchedLength() ||
658 copy
->getMatchedStart() != result
->getMatchedStart() ||
659 copy
->getOffset() != result
->getOffset() ||
660 copy
->getPattern() != result
->getPattern() ||
661 copy
->getText() != result
->getText() ||
662 *(copy
) != *(result
)) {
663 errln("Error copying StringSearch");
667 /* testing if an extremely large pattern will fail the initialization */
668 for (count
= 0; count
< 512; count
++) {
669 pattern
.append(temp
);
671 result
= new StringSearch(pattern
, text
, m_en_us_
, NULL
, status
);
672 if (*result
!= *result
) {
673 errln("Error: string search object expected to match itself");
675 if (*result
== *copy
) {
676 errln("Error: string search objects are not expected to match");
679 if (*(copy
->getCollator()) != *(result
->getCollator()) ||
680 copy
->getBreakIterator() != result
->getBreakIterator() ||
681 copy
->getMatchedLength() != result
->getMatchedLength() ||
682 copy
->getMatchedStart() != result
->getMatchedStart() ||
683 copy
->getOffset() != result
->getOffset() ||
684 copy
->getPattern() != result
->getPattern() ||
685 copy
->getText() != result
->getText() ||
686 *(copy
) != *(result
)) {
687 errln("Error copying StringSearch");
689 if (U_FAILURE(status
)) {
690 errln("Error opening search %s", u_errorName(status
));
696 void StringSearchTest::TestBasic()
699 while (BASIC
[count
].text
!= NULL
) {
700 //printf("count %d", count);
701 if (!assertEqual(&BASIC
[count
])) {
702 infoln("Error at test number %d", count
);
708 void StringSearchTest::TestNormExact()
711 UErrorCode status
= U_ZERO_ERROR
;
712 m_en_us_
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
713 if (U_FAILURE(status
)) {
714 errln("Error setting collation normalization %s",
715 u_errorName(status
));
717 while (BASIC
[count
].text
!= NULL
) {
718 if (!assertEqual(&BASIC
[count
])) {
719 infoln("Error at test number %d", count
);
724 while (NORMEXACT
[count
].text
!= NULL
) {
725 if (!assertEqual(&NORMEXACT
[count
])) {
726 infoln("Error at test number %d", count
);
730 m_en_us_
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, status
);
732 while (NONNORMEXACT
[count
].text
!= NULL
) {
733 if (!assertEqual(&NONNORMEXACT
[count
])) {
734 infoln("Error at test number %d", count
);
740 void StringSearchTest::TestStrength()
743 while (STRENGTH
[count
].text
!= NULL
) {
744 if (!assertEqual(&STRENGTH
[count
])) {
745 infoln("Error at test number %d", count
);
751 #if !UCONFIG_NO_BREAK_ITERATION
753 void StringSearchTest::TestBreakIterator()
756 u_unescape(BREAKITERATOREXACT
[0].text
, temp
, 128);
758 text
.setTo(temp
, u_strlen(temp
));
759 u_unescape(BREAKITERATOREXACT
[0].pattern
, temp
, 128);
760 UnicodeString pattern
;
761 pattern
.setTo(temp
, u_strlen(temp
));
763 UErrorCode status
= U_ZERO_ERROR
;
764 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
766 if (U_FAILURE(status
)) {
767 errln("Error opening string search %s", u_errorName(status
));
770 strsrch
->setBreakIterator(NULL
, status
);
771 if (U_FAILURE(status
) || strsrch
->getBreakIterator() != NULL
) {
772 errln("Error usearch_getBreakIterator returned wrong object");
775 strsrch
->setBreakIterator(m_en_characterbreaker_
, status
);
776 if (U_FAILURE(status
) ||
777 strsrch
->getBreakIterator() != m_en_characterbreaker_
) {
778 errln("Error usearch_getBreakIterator returned wrong object");
781 strsrch
->setBreakIterator(m_en_wordbreaker_
, status
);
782 if (U_FAILURE(status
) ||
783 strsrch
->getBreakIterator() != m_en_wordbreaker_
) {
784 errln("Error usearch_getBreakIterator returned wrong object");
791 // special purposes for tests numbers 0-3
792 const SearchData
*search
= &(BREAKITERATOREXACT
[count
]);
793 RuleBasedCollator
*collator
= getCollator(search
->collator
);
794 BreakIterator
*breaker
= getBreakIterator(search
->breaker
);
795 StringSearch
*strsrch
;
797 u_unescape(search
->text
, temp
, 128);
798 text
.setTo(temp
, u_strlen(temp
));
799 u_unescape(search
->pattern
, temp
, 128);
800 pattern
.setTo(temp
, u_strlen(temp
));
801 if (breaker
!= NULL
) {
802 breaker
->setText(text
);
804 collator
->setStrength(getECollationStrength(search
->strength
));
806 strsrch
= new StringSearch(pattern
, text
, collator
, breaker
, status
);
807 if (U_FAILURE(status
) ||
808 strsrch
->getBreakIterator() != breaker
) {
809 errln("Error setting break iterator");
810 if (strsrch
!= NULL
) {
814 if (!assertEqualWithStringSearch(strsrch
, search
)) {
815 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
818 search
= &(BREAKITERATOREXACT
[count
+ 1]);
819 breaker
= getBreakIterator(search
->breaker
);
820 if (breaker
!= NULL
) {
821 breaker
->setText(text
);
823 strsrch
->setBreakIterator(breaker
, status
);
824 if (U_FAILURE(status
) ||
825 strsrch
->getBreakIterator() != breaker
) {
826 errln("Error setting break iterator");
830 if (!assertEqualWithStringSearch(strsrch
, search
)) {
831 infoln("Error at test number %d", count
);
837 while (BREAKITERATOREXACT
[count
].text
!= NULL
) {
838 if (!assertEqual(&BREAKITERATOREXACT
[count
])) {
839 infoln("Error at test number %d", count
);
847 void StringSearchTest::TestVariable()
850 UErrorCode status
= U_ZERO_ERROR
;
851 m_en_us_
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, status
);
852 if (U_FAILURE(status
)) {
853 errln("Error setting collation alternate attribute %s",
854 u_errorName(status
));
856 while (VARIABLE
[count
].text
!= NULL
) {
857 logln("variable %d", count
);
858 if (!assertEqual(&VARIABLE
[count
])) {
859 infoln("Error at test number %d", count
);
863 m_en_us_
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
,
867 void StringSearchTest::TestOverlap()
870 while (OVERLAP
[count
].text
!= NULL
) {
871 if (!assertEqualWithAttribute(&OVERLAP
[count
], USEARCH_OFF
,
873 errln("Error at overlap test number %d", count
);
878 while (NONOVERLAP
[count
].text
!= NULL
) {
879 if (!assertEqual(&NONOVERLAP
[count
])) {
880 errln("Error at non overlap test number %d", count
);
887 const SearchData
*search
= &(OVERLAP
[count
]);
889 u_unescape(search
->text
, temp
, 128);
891 text
.setTo(temp
, u_strlen(temp
));
892 u_unescape(search
->pattern
, temp
, 128);
893 UnicodeString pattern
;
894 pattern
.setTo(temp
, u_strlen(temp
));
896 RuleBasedCollator
*collator
= getCollator(search
->collator
);
897 UErrorCode status
= U_ZERO_ERROR
;
898 StringSearch
*strsrch
= new StringSearch(pattern
, text
,
902 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_ON
, status
);
903 if (U_FAILURE(status
) ||
904 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_ON
) {
905 errln("Error setting overlap option");
907 if (!assertEqualWithStringSearch(strsrch
, search
)) {
912 search
= &(NONOVERLAP
[count
]);
913 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_OFF
, status
);
914 if (U_FAILURE(status
) ||
915 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_OFF
) {
916 errln("Error setting overlap option");
919 if (!assertEqualWithStringSearch(strsrch
, search
)) {
921 errln("Error at test number %d", count
);
929 void StringSearchTest::TestCollator()
931 // test collator that thinks "o" and "p" are the same thing
933 u_unescape(COLLATOR
[0].text
, temp
, 128);
935 text
.setTo(temp
, u_strlen(temp
));
936 u_unescape(COLLATOR
[0].pattern
, temp
, 128);
937 UnicodeString pattern
;
938 pattern
.setTo(temp
, u_strlen(temp
));
940 UErrorCode status
= U_ZERO_ERROR
;
941 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
943 if (U_FAILURE(status
)) {
944 errln("Error opening string search %s", u_errorName(status
));
948 if (!assertEqualWithStringSearch(strsrch
, &COLLATOR
[0])) {
953 u_unescape(TESTCOLLATORRULE
, temp
, 128);
955 rules
.setTo(temp
, u_strlen(temp
));
956 RuleBasedCollator
*tailored
= new RuleBasedCollator(rules
, status
);
957 tailored
->setStrength(getECollationStrength(COLLATOR
[1].strength
));
959 if (U_FAILURE(status
)) {
960 errln("Error opening rule based collator %s", u_errorName(status
));
962 if (tailored
!= NULL
) {
968 strsrch
->setCollator(tailored
, status
);
969 if (U_FAILURE(status
) || (*strsrch
->getCollator()) != (*tailored
)) {
970 errln("Error setting rule based collator");
972 if (tailored
!= NULL
) {
977 if (!assertEqualWithStringSearch(strsrch
, &COLLATOR
[1])) {
979 if (tailored
!= NULL
) {
985 strsrch
->setCollator(m_en_us_
, status
);
987 if (U_FAILURE(status
) || (*strsrch
->getCollator()) != (*m_en_us_
)) {
988 errln("Error setting rule based collator");
990 if (tailored
!= NULL
) {
994 if (!assertEqualWithStringSearch(strsrch
, &COLLATOR
[0])) {
995 errln("Error searching collator test");
998 if (tailored
!= NULL
) {
1003 void StringSearchTest::TestPattern()
1008 u_unescape(PATTERN
[0].text
, temp
, 512);
1010 text
.setTo(temp
, u_strlen(temp
));
1011 u_unescape(PATTERN
[0].pattern
, temp
, 512);
1012 UnicodeString pattern
;
1013 pattern
.setTo(temp
, u_strlen(temp
));
1015 m_en_us_
->setStrength(getECollationStrength(PATTERN
[0].strength
));
1016 UErrorCode status
= U_ZERO_ERROR
;
1017 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1020 if (U_FAILURE(status
)) {
1021 errln("Error opening string search %s", u_errorName(status
));
1022 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1023 if (strsrch
!= NULL
) {
1028 if (strsrch
->getPattern() != pattern
) {
1029 errln("Error setting pattern");
1031 if (!assertEqualWithStringSearch(strsrch
, &PATTERN
[0])) {
1032 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1033 if (strsrch
!= NULL
) {
1039 u_unescape(PATTERN
[1].pattern
, temp
, 512);
1040 pattern
.setTo(temp
, u_strlen(temp
));
1041 strsrch
->setPattern(pattern
, status
);
1042 if (pattern
!= strsrch
->getPattern()) {
1043 errln("Error setting pattern");
1044 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1045 if (strsrch
!= NULL
) {
1051 if (U_FAILURE(status
)) {
1052 errln("Error setting pattern %s", u_errorName(status
));
1054 if (!assertEqualWithStringSearch(strsrch
, &PATTERN
[1])) {
1055 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1056 if (strsrch
!= NULL
) {
1062 u_unescape(PATTERN
[0].pattern
, temp
, 512);
1063 pattern
.setTo(temp
, u_strlen(temp
));
1064 strsrch
->setPattern(pattern
, status
);
1065 if (pattern
!= strsrch
->getPattern()) {
1066 errln("Error setting pattern");
1067 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1068 if (strsrch
!= NULL
) {
1074 if (U_FAILURE(status
)) {
1075 errln("Error setting pattern %s", u_errorName(status
));
1077 if (!assertEqualWithStringSearch(strsrch
, &PATTERN
[0])) {
1078 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1079 if (strsrch
!= NULL
) {
1084 /* enormous pattern size to see if this crashes */
1085 for (templength
= 0; templength
!= 512; templength
++) {
1086 temp
[templength
] = 0x61;
1089 pattern
.setTo(temp
, 511);
1090 strsrch
->setPattern(pattern
, status
);
1091 if (U_FAILURE(status
)) {
1092 errln("Error setting pattern with size 512, %s", u_errorName(status
));
1094 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1095 if (strsrch
!= NULL
) {
1100 void StringSearchTest::TestText()
1103 u_unescape(TEXT
[0].text
, temp
, 128);
1105 text
.setTo(temp
, u_strlen(temp
));
1106 u_unescape(TEXT
[0].pattern
, temp
, 128);
1107 UnicodeString pattern
;
1108 pattern
.setTo(temp
, u_strlen(temp
));
1110 UErrorCode status
= U_ZERO_ERROR
;
1111 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1113 if (U_FAILURE(status
)) {
1114 errln("Error opening string search %s", u_errorName(status
));
1117 if (text
!= strsrch
->getText()) {
1118 errln("Error setting text");
1120 if (!assertEqualWithStringSearch(strsrch
, &TEXT
[0])) {
1125 u_unescape(TEXT
[1].text
, temp
, 128);
1126 text
.setTo(temp
, u_strlen(temp
));
1127 strsrch
->setText(text
, status
);
1128 if (text
!= strsrch
->getText()) {
1129 errln("Error setting text");
1133 if (U_FAILURE(status
)) {
1134 errln("Error setting text %s", u_errorName(status
));
1136 if (!assertEqualWithStringSearch(strsrch
, &TEXT
[1])) {
1141 u_unescape(TEXT
[0].text
, temp
, 128);
1142 text
.setTo(temp
, u_strlen(temp
));
1143 StringCharacterIterator
chariter(text
);
1144 strsrch
->setText(chariter
, status
);
1145 if (text
!= strsrch
->getText()) {
1146 errln("Error setting text");
1150 if (U_FAILURE(status
)) {
1151 errln("Error setting pattern %s", u_errorName(status
));
1153 if (!assertEqualWithStringSearch(strsrch
, &TEXT
[0])) {
1154 errln("Error searching within set text");
1159 void StringSearchTest::TestCompositeBoundaries()
1162 while (COMPOSITEBOUNDARIES
[count
].text
!= NULL
) {
1163 logln("composite %d", count
);
1164 if (!assertEqual(&COMPOSITEBOUNDARIES
[count
])) {
1165 errln("Error at test number %d", count
);
1171 void StringSearchTest::TestGetSetOffset()
1173 UErrorCode status
= U_ZERO_ERROR
;
1174 UnicodeString
pattern("1234567890123456");
1175 UnicodeString
text("12345678901234567890123456789012");
1176 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
,
1178 /* testing out of bounds error */
1179 strsrch
->setOffset(-1, status
);
1180 if (U_SUCCESS(status
)) {
1181 errln("Error expecting set offset error");
1183 strsrch
->setOffset(128, status
);
1184 if (U_SUCCESS(status
)) {
1185 errln("Error expecting set offset error");
1188 while (BASIC
[index
].text
!= NULL
) {
1189 UErrorCode status
= U_ZERO_ERROR
;
1190 SearchData search
= BASIC
[index
++];
1193 u_unescape(search
.text
, temp
, 128);
1194 text
.setTo(temp
, u_strlen(temp
));
1195 u_unescape(search
.pattern
, temp
, 128);
1196 pattern
.setTo(temp
, u_strlen(temp
));
1197 strsrch
->setText(text
, status
);
1198 strsrch
->setPattern(pattern
, status
);
1199 strsrch
->getCollator()->setStrength(getECollationStrength(
1204 int32_t matchindex
= search
.offset
[count
];
1205 while (U_SUCCESS(status
) && matchindex
>= 0) {
1206 int32_t matchlength
= search
.size
[count
];
1207 strsrch
->next(status
);
1208 if (matchindex
!= strsrch
->getMatchedStart() ||
1209 matchlength
!= strsrch
->getMatchedLength()) {
1210 char *str
= toCharString(strsrch
->getText());
1211 errln("Text: %s", str
);
1212 str
= toCharString(strsrch
->getPattern());
1213 errln("Pattern: %s", str
);
1214 errln("Error match found at %d %d",
1215 strsrch
->getMatchedStart(),
1216 strsrch
->getMatchedLength());
1219 matchindex
= search
.offset
[count
+ 1] == -1 ? -1 :
1220 search
.offset
[count
+ 2];
1221 if (search
.offset
[count
+ 1] != -1) {
1222 strsrch
->setOffset(search
.offset
[count
+ 1] + 1, status
);
1223 if (strsrch
->getOffset() != search
.offset
[count
+ 1] + 1) {
1224 errln("Error setting offset\n");
1231 strsrch
->next(status
);
1232 if (strsrch
->getMatchedStart() != USEARCH_DONE
) {
1233 char *str
= toCharString(strsrch
->getText());
1234 errln("Text: %s", str
);
1235 str
= toCharString(strsrch
->getPattern());
1236 errln("Pattern: %s", str
);
1237 errln("Error match found at %d %d",
1238 strsrch
->getMatchedStart(),
1239 strsrch
->getMatchedLength());
1243 strsrch
->getCollator()->setStrength(getECollationStrength(
1248 void StringSearchTest::TestGetSetAttribute()
1250 UErrorCode status
= U_ZERO_ERROR
;
1251 UnicodeString
pattern("pattern");
1252 UnicodeString
text("text");
1253 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1255 if (U_FAILURE(status
)) {
1256 errln("Error opening search %s", u_errorName(status
));
1260 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_DEFAULT
, status
);
1261 if (U_FAILURE(status
) ||
1262 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_OFF
) {
1263 errln("Error setting overlap to the default");
1265 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_ON
, status
);
1266 if (U_FAILURE(status
) ||
1267 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_ON
) {
1268 errln("Error setting overlap true");
1270 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_OFF
, status
);
1271 if (U_FAILURE(status
) ||
1272 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_OFF
) {
1273 errln("Error setting overlap false");
1275 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_ATTRIBUTE_VALUE_COUNT
,
1277 if (U_SUCCESS(status
)) {
1278 errln("Error setting overlap to illegal value");
1280 status
= U_ZERO_ERROR
;
1281 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_DEFAULT
, status
);
1282 if (U_FAILURE(status
) ||
1283 strsrch
->getAttribute(USEARCH_CANONICAL_MATCH
) != USEARCH_OFF
) {
1284 errln("Error setting canonical match to the default");
1286 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1287 if (U_FAILURE(status
) ||
1288 strsrch
->getAttribute(USEARCH_CANONICAL_MATCH
) != USEARCH_ON
) {
1289 errln("Error setting canonical match true");
1291 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_OFF
, status
);
1292 if (U_FAILURE(status
) ||
1293 strsrch
->getAttribute(USEARCH_CANONICAL_MATCH
) != USEARCH_OFF
) {
1294 errln("Error setting canonical match false");
1296 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
,
1297 USEARCH_ATTRIBUTE_VALUE_COUNT
, status
);
1298 if (U_SUCCESS(status
)) {
1299 errln("Error setting canonical match to illegal value");
1301 status
= U_ZERO_ERROR
;
1302 strsrch
->setAttribute(USEARCH_ATTRIBUTE_COUNT
, USEARCH_DEFAULT
, status
);
1303 if (U_SUCCESS(status
)) {
1304 errln("Error setting illegal attribute success");
1310 void StringSearchTest::TestGetMatch()
1313 SearchData search
= MATCH
[0];
1314 u_unescape(search
.text
, temp
, 128);
1316 text
.setTo(temp
, u_strlen(temp
));
1317 u_unescape(search
.pattern
, temp
, 128);
1318 UnicodeString pattern
;
1319 pattern
.setTo(temp
, u_strlen(temp
));
1321 UErrorCode status
= U_ZERO_ERROR
;
1322 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1324 if (U_FAILURE(status
)) {
1325 errln("Error opening string search %s", u_errorName(status
));
1326 if (strsrch
!= NULL
) {
1333 int32_t matchindex
= search
.offset
[count
];
1334 UnicodeString matchtext
;
1335 while (U_SUCCESS(status
) && matchindex
>= 0) {
1336 int32_t matchlength
= search
.size
[count
];
1337 strsrch
->next(status
);
1338 if (matchindex
!= strsrch
->getMatchedStart() ||
1339 matchlength
!= strsrch
->getMatchedLength()) {
1340 char *str
= toCharString(strsrch
->getText());
1341 errln("Text: %s", str
);
1342 str
= toCharString(strsrch
->getPattern());
1343 errln("Pattern: %s", str
);
1344 errln("Error match found at %d %d", strsrch
->getMatchedStart(),
1345 strsrch
->getMatchedLength());
1350 status
= U_ZERO_ERROR
;
1351 strsrch
->getMatchedText(matchtext
);
1352 if (matchtext
.length() != matchlength
|| U_FAILURE(status
)){
1353 errln("Error getting match text");
1355 matchindex
= search
.offset
[count
];
1357 status
= U_ZERO_ERROR
;
1358 strsrch
->next(status
);
1359 if (strsrch
->getMatchedStart() != USEARCH_DONE
||
1360 strsrch
->getMatchedLength() != 0) {
1361 errln("Error end of match not found");
1363 status
= U_ZERO_ERROR
;
1364 strsrch
->getMatchedText(matchtext
);
1365 if (matchtext
.length() != 0) {
1366 errln("Error getting null matches");
1371 void StringSearchTest::TestSetMatch()
1374 while (MATCH
[count
].text
!= NULL
) {
1375 SearchData search
= MATCH
[count
];
1377 UErrorCode status
= U_ZERO_ERROR
;
1378 u_unescape(search
.text
, temp
, 128);
1380 text
.setTo(temp
, u_strlen(temp
));
1381 u_unescape(search
.pattern
, temp
, 128);
1382 UnicodeString pattern
;
1383 pattern
.setTo(temp
, u_strlen(temp
));
1385 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
,
1387 if (U_FAILURE(status
)) {
1388 errln("Error opening string search %s", u_errorName(status
));
1389 if (strsrch
!= NULL
) {
1396 while (search
.offset
[size
] != -1) {
1400 if (strsrch
->first(status
) != search
.offset
[0] || U_FAILURE(status
)) {
1401 errln("Error getting first match");
1403 if (strsrch
->last(status
) != search
.offset
[size
-1] ||
1404 U_FAILURE(status
)) {
1405 errln("Error getting last match");
1409 while (index
< size
) {
1410 if (index
+ 2 < size
) {
1411 if (strsrch
->following(search
.offset
[index
+ 2] - 1, status
)
1412 != search
.offset
[index
+ 2] || U_FAILURE(status
)) {
1413 errln("Error getting following match at index %d",
1414 search
.offset
[index
+ 2] - 1);
1417 if (index
+ 1 < size
) {
1418 if (strsrch
->preceding(search
.offset
[index
+ 1] +
1419 search
.size
[index
+ 1] + 1,
1420 status
) != search
.offset
[index
+ 1] ||
1421 U_FAILURE(status
)) {
1422 errln("Error getting preceeding match at index %d",
1423 search
.offset
[index
+ 1] + 1);
1428 status
= U_ZERO_ERROR
;
1429 if (strsrch
->following(text
.length(), status
) != USEARCH_DONE
) {
1430 errln("Error expecting out of bounds match");
1432 if (strsrch
->preceding(0, status
) != USEARCH_DONE
) {
1433 errln("Error expecting out of bounds match");
1440 void StringSearchTest::TestReset()
1442 UErrorCode status
= U_ZERO_ERROR
;
1443 UnicodeString
text("fish fish");
1444 UnicodeString
pattern("s");
1445 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1447 if (U_FAILURE(status
)) {
1448 errln("Error opening string search %s", u_errorName(status
));
1449 if (strsrch
!= NULL
) {
1454 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_ON
, status
);
1455 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1456 strsrch
->setOffset(9, status
);
1457 if (U_FAILURE(status
)) {
1458 errln("Error setting attributes and offsets");
1462 if (strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_OFF
||
1463 strsrch
->getAttribute(USEARCH_CANONICAL_MATCH
) != USEARCH_OFF
||
1464 strsrch
->getOffset() != 0 || strsrch
->getMatchedLength() != 0 ||
1465 strsrch
->getMatchedStart() != USEARCH_DONE
) {
1466 errln("Error resetting string search");
1468 strsrch
->previous(status
);
1469 if (strsrch
->getMatchedStart() != 7 ||
1470 strsrch
->getMatchedLength() != 1) {
1471 errln("Error resetting string search\n");
1477 void StringSearchTest::TestSupplementary()
1480 while (SUPPLEMENTARY
[count
].text
!= NULL
) {
1481 if (!assertEqual(&SUPPLEMENTARY
[count
])) {
1482 errln("Error at test number %d", count
);
1488 void StringSearchTest::TestContraction()
1491 UErrorCode status
= U_ZERO_ERROR
;
1493 u_unescape(CONTRACTIONRULE
, temp
, 128);
1494 UnicodeString rules
;
1495 rules
.setTo(temp
, u_strlen(temp
));
1496 RuleBasedCollator
*collator
= new RuleBasedCollator(rules
,
1497 getECollationStrength(UCOL_TERTIARY
), UCOL_ON
, status
);
1498 if (U_FAILURE(status
)) {
1499 errln("Error opening collator %s", u_errorName(status
));
1501 UnicodeString
text("text");
1502 UnicodeString
pattern("pattern");
1503 StringSearch
*strsrch
= new StringSearch(pattern
, text
, collator
, NULL
,
1505 if (U_FAILURE(status
)) {
1506 errln("Error opening string search %s", u_errorName(status
));
1510 while (CONTRACTION
[count
].text
!= NULL
) {
1511 u_unescape(CONTRACTION
[count
].text
, temp
, 128);
1512 text
.setTo(temp
, u_strlen(temp
));
1513 u_unescape(CONTRACTION
[count
].pattern
, temp
, 128);
1514 pattern
.setTo(temp
, u_strlen(temp
));
1515 strsrch
->setText(text
, status
);
1516 strsrch
->setPattern(pattern
, status
);
1517 if (!assertEqualWithStringSearch(strsrch
, &CONTRACTION
[count
])) {
1518 errln("Error at test number %d", count
);
1526 void StringSearchTest::TestIgnorable()
1529 u_unescape(IGNORABLERULE
, temp
, 128);
1530 UnicodeString rules
;
1531 rules
.setTo(temp
, u_strlen(temp
));
1532 UErrorCode status
= U_ZERO_ERROR
;
1534 RuleBasedCollator
*collator
= new RuleBasedCollator(rules
,
1535 getECollationStrength(IGNORABLE
[count
].strength
),
1537 if (U_FAILURE(status
)) {
1538 errln("Error opening collator %s", u_errorName(status
));
1541 UnicodeString
pattern("pattern");
1542 UnicodeString
text("text");
1543 StringSearch
*strsrch
= new StringSearch(pattern
, text
, collator
, NULL
,
1545 if (U_FAILURE(status
)) {
1546 errln("Error opening string search %s", u_errorName(status
));
1551 while (IGNORABLE
[count
].text
!= NULL
) {
1552 u_unescape(IGNORABLE
[count
].text
, temp
, 128);
1553 text
.setTo(temp
, u_strlen(temp
));
1554 u_unescape(IGNORABLE
[count
].pattern
, temp
, 128);
1555 pattern
.setTo(temp
, u_strlen(temp
));
1556 strsrch
->setText(text
, status
);
1557 strsrch
->setPattern(pattern
, status
);
1558 if (!assertEqualWithStringSearch(strsrch
, &IGNORABLE
[count
])) {
1559 errln("Error at test number %d", count
);
1567 void StringSearchTest::TestDiacriticMatch()
1570 UErrorCode status
= U_ZERO_ERROR
;
1572 RuleBasedCollator
* coll
= NULL
;
1573 StringSearch
*strsrch
= NULL
;
1575 UnicodeString
pattern("pattern");
1576 UnicodeString
text("text");
1578 const SearchData
*search
;
1580 search
= &(DIACRITICMATCH
[count
]);
1581 while (search
->text
!= NULL
) {
1582 coll
= getCollator(search
->collator
);
1583 coll
->setStrength(getECollationStrength(search
->strength
));
1584 strsrch
= new StringSearch(pattern
, text
, coll
, getBreakIterator(search
->breaker
), status
);
1585 if (U_FAILURE(status
)) {
1586 errln("Error opening string search %s", u_errorName(status
));
1589 u_unescape(search
->text
, temp
, 128);
1590 text
.setTo(temp
, u_strlen(temp
));
1591 u_unescape(search
->pattern
, temp
, 128);
1592 pattern
.setTo(temp
, u_strlen(temp
));
1593 strsrch
->setText(text
, status
);
1594 strsrch
->setPattern(pattern
, status
);
1595 if (!assertEqualWithStringSearch(strsrch
, search
)) {
1596 errln("Error at test number %d", count
);
1598 search
= &(DIACRITICMATCH
[++count
]);
1604 void StringSearchTest::TestCanonical()
1607 while (BASICCANONICAL
[count
].text
!= NULL
) {
1608 if (!assertCanonicalEqual(&BASICCANONICAL
[count
])) {
1609 errln("Error at test number %d", count
);
1615 void StringSearchTest::TestNormCanonical()
1617 UErrorCode status
= U_ZERO_ERROR
;
1618 m_en_us_
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
1620 while (NORMCANONICAL
[count
].text
!= NULL
) {
1621 if (!assertCanonicalEqual(&NORMCANONICAL
[count
])) {
1622 errln("Error at test number %d", count
);
1626 m_en_us_
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, status
);
1629 void StringSearchTest::TestStrengthCanonical()
1632 while (STRENGTHCANONICAL
[count
].text
!= NULL
) {
1633 if (!assertCanonicalEqual(&STRENGTHCANONICAL
[count
])) {
1634 errln("Error at test number %d", count
);
1640 #if !UCONFIG_NO_BREAK_ITERATION
1642 void StringSearchTest::TestBreakIteratorCanonical()
1644 UErrorCode status
= U_ZERO_ERROR
;
1648 // special purposes for tests numbers 0-3
1650 const SearchData
*search
= &(BREAKITERATORCANONICAL
[count
]);
1652 u_unescape(search
->text
, temp
, 128);
1654 text
.setTo(temp
, u_strlen(temp
));
1655 u_unescape(search
->pattern
, temp
, 128);
1656 UnicodeString pattern
;
1657 pattern
.setTo(temp
, u_strlen(temp
));
1658 RuleBasedCollator
*collator
= getCollator(search
->collator
);
1659 collator
->setStrength(getECollationStrength(search
->strength
));
1661 BreakIterator
*breaker
= getBreakIterator(search
->breaker
);
1662 StringSearch
*strsrch
= new StringSearch(pattern
, text
, collator
,
1664 if (U_FAILURE(status
)) {
1665 errln("Error creating string search data");
1668 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1669 if (U_FAILURE(status
) ||
1670 strsrch
->getBreakIterator() != breaker
) {
1671 errln("Error setting break iterator");
1675 if (!assertEqualWithStringSearch(strsrch
, search
)) {
1676 collator
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1680 search
= &(BREAKITERATOREXACT
[count
+ 1]);
1681 breaker
= getBreakIterator(search
->breaker
);
1682 if (breaker
== NULL
) {
1683 errln("Error creating BreakIterator");
1686 breaker
->setText(strsrch
->getText());
1687 strsrch
->setBreakIterator(breaker
, status
);
1688 if (U_FAILURE(status
) || strsrch
->getBreakIterator() != breaker
) {
1689 errln("Error setting break iterator");
1694 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1695 if (!assertEqualWithStringSearch(strsrch
, search
)) {
1696 errln("Error at test number %d", count
);
1703 while (BREAKITERATORCANONICAL
[count
].text
!= NULL
) {
1704 if (!assertEqual(&BREAKITERATORCANONICAL
[count
])) {
1705 errln("Error at test number %d", count
);
1714 void StringSearchTest::TestVariableCanonical()
1717 UErrorCode status
= U_ZERO_ERROR
;
1718 m_en_us_
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, status
);
1719 if (U_FAILURE(status
)) {
1720 errln("Error setting collation alternate attribute %s",
1721 u_errorName(status
));
1723 while (VARIABLE
[count
].text
!= NULL
) {
1724 logln("variable %d", count
);
1725 if (!assertCanonicalEqual(&VARIABLE
[count
])) {
1726 errln("Error at test number %d", count
);
1730 m_en_us_
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
,
1734 void StringSearchTest::TestOverlapCanonical()
1737 while (OVERLAPCANONICAL
[count
].text
!= NULL
) {
1738 if (!assertEqualWithAttribute(&OVERLAPCANONICAL
[count
], USEARCH_ON
,
1740 errln("Error at overlap test number %d", count
);
1745 while (NONOVERLAP
[count
].text
!= NULL
) {
1746 if (!assertCanonicalEqual(&NONOVERLAPCANONICAL
[count
])) {
1747 errln("Error at non overlap test number %d", count
);
1755 const SearchData
*search
= &(OVERLAPCANONICAL
[count
]);
1756 UErrorCode status
= U_ZERO_ERROR
;
1758 u_unescape(search
->text
, temp
, 128);
1760 text
.setTo(temp
, u_strlen(temp
));
1761 u_unescape(search
->pattern
, temp
, 128);
1762 UnicodeString pattern
;
1763 pattern
.setTo(temp
, u_strlen(temp
));
1764 RuleBasedCollator
*collator
= getCollator(search
->collator
);
1765 StringSearch
*strsrch
= new StringSearch(pattern
, text
, collator
,
1767 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1768 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_ON
, status
);
1769 if (U_FAILURE(status
) ||
1770 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_ON
) {
1771 errln("Error setting overlap option");
1773 if (!assertEqualWithStringSearch(strsrch
, search
)) {
1777 search
= &(NONOVERLAPCANONICAL
[count
]);
1778 strsrch
->setAttribute(USEARCH_OVERLAP
, USEARCH_OFF
, status
);
1779 if (U_FAILURE(status
) ||
1780 strsrch
->getAttribute(USEARCH_OVERLAP
) != USEARCH_OFF
) {
1781 errln("Error setting overlap option");
1784 if (!assertEqualWithStringSearch(strsrch
, search
)) {
1786 errln("Error at test number %d", count
);
1794 void StringSearchTest::TestCollatorCanonical()
1796 /* test collator that thinks "o" and "p" are the same thing */
1798 u_unescape(COLLATORCANONICAL
[0].text
, temp
, 128);
1800 text
.setTo(temp
, u_strlen(temp
));
1801 u_unescape(COLLATORCANONICAL
[0].pattern
, temp
, 128);
1802 UnicodeString pattern
;
1803 pattern
.setTo(temp
, u_strlen(temp
));
1805 UErrorCode status
= U_ZERO_ERROR
;
1806 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
,
1808 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1809 if (U_FAILURE(status
)) {
1810 errln("Error opening string search %s", u_errorName(status
));
1812 if (!assertEqualWithStringSearch(strsrch
, &COLLATORCANONICAL
[0])) {
1817 u_unescape(TESTCOLLATORRULE
, temp
, 128);
1818 UnicodeString rules
;
1819 rules
.setTo(temp
, u_strlen(temp
));
1820 RuleBasedCollator
*tailored
= new RuleBasedCollator(rules
,
1821 getECollationStrength(COLLATORCANONICAL
[1].strength
),
1824 if (U_FAILURE(status
)) {
1825 errln("Error opening rule based collator %s", u_errorName(status
));
1828 strsrch
->setCollator(tailored
, status
);
1829 if (U_FAILURE(status
) || *(strsrch
->getCollator()) != *tailored
) {
1830 errln("Error setting rule based collator");
1833 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1834 if (!assertEqualWithStringSearch(strsrch
, &COLLATORCANONICAL
[1])) {
1836 if (tailored
!= NULL
) {
1843 strsrch
->setCollator(m_en_us_
, status
);
1845 if (U_FAILURE(status
) || *(strsrch
->getCollator()) != *m_en_us_
) {
1846 errln("Error setting rule based collator");
1848 if (!assertEqualWithStringSearch(strsrch
, &COLLATORCANONICAL
[0])) {
1851 if (tailored
!= NULL
) {
1856 void StringSearchTest::TestPatternCanonical()
1861 u_unescape(PATTERNCANONICAL
[0].text
, temp
, 128);
1863 text
.setTo(temp
, u_strlen(temp
));
1864 u_unescape(PATTERNCANONICAL
[0].pattern
, temp
, 128);
1865 UnicodeString pattern
;
1866 pattern
.setTo(temp
, u_strlen(temp
));
1868 m_en_us_
->setStrength(
1869 getECollationStrength(PATTERNCANONICAL
[0].strength
));
1871 UErrorCode status
= U_ZERO_ERROR
;
1872 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1874 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1875 if (U_FAILURE(status
)) {
1876 errln("Error opening string search %s", u_errorName(status
));
1877 goto ENDTESTPATTERN
;
1879 if (pattern
!= strsrch
->getPattern()) {
1880 errln("Error setting pattern");
1882 if (!assertEqualWithStringSearch(strsrch
, &PATTERNCANONICAL
[0])) {
1883 goto ENDTESTPATTERN
;
1886 u_unescape(PATTERNCANONICAL
[1].pattern
, temp
, 128);
1887 pattern
.setTo(temp
, u_strlen(temp
));
1888 strsrch
->setPattern(pattern
, status
);
1889 if (pattern
!= strsrch
->getPattern()) {
1890 errln("Error setting pattern");
1891 goto ENDTESTPATTERN
;
1894 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1895 if (U_FAILURE(status
)) {
1896 errln("Error setting pattern %s", u_errorName(status
));
1898 if (!assertEqualWithStringSearch(strsrch
, &PATTERNCANONICAL
[1])) {
1899 goto ENDTESTPATTERN
;
1902 u_unescape(PATTERNCANONICAL
[0].pattern
, temp
, 128);
1903 pattern
.setTo(temp
, u_strlen(temp
));
1904 strsrch
->setPattern(pattern
, status
);
1905 if (pattern
!= strsrch
->getPattern()) {
1906 errln("Error setting pattern");
1907 goto ENDTESTPATTERN
;
1910 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1911 if (U_FAILURE(status
)) {
1912 errln("Error setting pattern %s", u_errorName(status
));
1914 if (!assertEqualWithStringSearch(strsrch
, &PATTERNCANONICAL
[0])) {
1915 goto ENDTESTPATTERN
;
1918 m_en_us_
->setStrength(getECollationStrength(UCOL_TERTIARY
));
1919 if (strsrch
!= NULL
) {
1924 void StringSearchTest::TestTextCanonical()
1927 u_unescape(TEXTCANONICAL
[0].text
, temp
, 128);
1929 text
.setTo(temp
, u_strlen(temp
));
1930 u_unescape(TEXTCANONICAL
[0].pattern
, temp
, 128);
1931 UnicodeString pattern
;
1932 pattern
.setTo(temp
, u_strlen(temp
));
1934 UErrorCode status
= U_ZERO_ERROR
;
1935 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
1937 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
1939 if (U_FAILURE(status
)) {
1940 errln("Error opening string search %s", u_errorName(status
));
1941 goto ENDTESTPATTERN
;
1943 if (text
!= strsrch
->getText()) {
1944 errln("Error setting text");
1946 if (!assertEqualWithStringSearch(strsrch
, &TEXTCANONICAL
[0])) {
1947 goto ENDTESTPATTERN
;
1950 u_unescape(TEXTCANONICAL
[1].text
, temp
, 128);
1951 text
.setTo(temp
, u_strlen(temp
));
1952 strsrch
->setText(text
, status
);
1953 if (text
!= strsrch
->getText()) {
1954 errln("Error setting text");
1955 goto ENDTESTPATTERN
;
1957 if (U_FAILURE(status
)) {
1958 errln("Error setting text %s", u_errorName(status
));
1960 if (!assertEqualWithStringSearch(strsrch
, &TEXTCANONICAL
[1])) {
1961 goto ENDTESTPATTERN
;
1964 u_unescape(TEXTCANONICAL
[0].text
, temp
, 128);
1965 text
.setTo(temp
, u_strlen(temp
));
1966 strsrch
->setText(text
, status
);
1967 if (text
!= strsrch
->getText()) {
1968 errln("Error setting text");
1969 goto ENDTESTPATTERN
;
1971 if (U_FAILURE(status
)) {
1972 errln("Error setting pattern %s", u_errorName(status
));
1974 if (!assertEqualWithStringSearch(strsrch
, &TEXTCANONICAL
[0])) {
1975 goto ENDTESTPATTERN
;
1978 if (strsrch
!= NULL
) {
1983 void StringSearchTest::TestCompositeBoundariesCanonical()
1986 while (COMPOSITEBOUNDARIESCANONICAL
[count
].text
!= NULL
) {
1987 logln("composite %d", count
);
1988 if (!assertCanonicalEqual(&COMPOSITEBOUNDARIESCANONICAL
[count
])) {
1989 errln("Error at test number %d", count
);
1995 void StringSearchTest::TestGetSetOffsetCanonical()
1998 UErrorCode status
= U_ZERO_ERROR
;
1999 UnicodeString
text("text");
2000 UnicodeString
pattern("pattern");
2001 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
2003 Collator
*collator
= strsrch
->getCollator();
2005 collator
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
2007 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
2008 /* testing out of bounds error */
2009 strsrch
->setOffset(-1, status
);
2010 if (U_SUCCESS(status
)) {
2011 errln("Error expecting set offset error");
2013 strsrch
->setOffset(128, status
);
2014 if (U_SUCCESS(status
)) {
2015 errln("Error expecting set offset error");
2019 while (BASICCANONICAL
[index
].text
!= NULL
) {
2020 SearchData search
= BASICCANONICAL
[index
++];
2021 if (BASICCANONICAL
[index
].text
== NULL
) {
2022 /* skip the last one */
2026 u_unescape(search
.text
, temp
, 128);
2027 text
.setTo(temp
, u_strlen(temp
));
2028 u_unescape(search
.pattern
, temp
, 128);
2029 pattern
.setTo(temp
, u_strlen(temp
));
2031 UErrorCode status
= U_ZERO_ERROR
;
2032 strsrch
->setText(text
, status
);
2034 strsrch
->setPattern(pattern
, status
);
2037 int32_t matchindex
= search
.offset
[count
];
2038 while (U_SUCCESS(status
) && matchindex
>= 0) {
2039 int32_t matchlength
= search
.size
[count
];
2040 strsrch
->next(status
);
2041 if (matchindex
!= strsrch
->getMatchedStart() ||
2042 matchlength
!= strsrch
->getMatchedLength()) {
2043 char *str
= toCharString(strsrch
->getText());
2044 errln("Text: %s", str
);
2045 str
= toCharString(strsrch
->getPattern());
2046 errln("Pattern: %s", str
);
2047 errln("Error match found at %d %d",
2048 strsrch
->getMatchedStart(),
2049 strsrch
->getMatchedLength());
2052 matchindex
= search
.offset
[count
+ 1] == -1 ? -1 :
2053 search
.offset
[count
+ 2];
2054 if (search
.offset
[count
+ 1] != -1) {
2055 strsrch
->setOffset(search
.offset
[count
+ 1] + 1, status
);
2056 if (strsrch
->getOffset() != search
.offset
[count
+ 1] + 1) {
2057 errln("Error setting offset");
2064 strsrch
->next(status
);
2065 if (strsrch
->getMatchedStart() != USEARCH_DONE
) {
2066 char *str
= toCharString(strsrch
->getText());
2067 errln("Text: %s", str
);
2068 str
= toCharString(strsrch
->getPattern());
2069 errln("Pattern: %s", str
);
2070 errln("Error match found at %d %d", strsrch
->getMatchedStart(),
2071 strsrch
->getMatchedLength());
2077 collator
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_OFF
, status
);
2081 void StringSearchTest::TestSupplementaryCanonical()
2084 while (SUPPLEMENTARYCANONICAL
[count
].text
!= NULL
) {
2085 if (!assertCanonicalEqual(&SUPPLEMENTARYCANONICAL
[count
])) {
2086 errln("Error at test number %d", count
);
2092 void StringSearchTest::TestContractionCanonical()
2096 u_unescape(CONTRACTIONRULE
, temp
, 128);
2097 UnicodeString rules
;
2098 rules
.setTo(temp
, u_strlen(temp
));
2100 UErrorCode status
= U_ZERO_ERROR
;
2101 RuleBasedCollator
*collator
= new RuleBasedCollator(rules
,
2102 getECollationStrength(UCOL_TERTIARY
), UCOL_ON
, status
);
2103 if (U_FAILURE(status
)) {
2104 errln("Error opening collator %s", u_errorName(status
));
2106 UnicodeString
text("text");
2107 UnicodeString
pattern("pattern");
2108 StringSearch
*strsrch
= new StringSearch(pattern
, text
, collator
, NULL
,
2110 strsrch
->setAttribute(USEARCH_CANONICAL_MATCH
, USEARCH_ON
, status
);
2111 if (U_FAILURE(status
)) {
2112 errln("Error opening string search %s", u_errorName(status
));
2116 while (CONTRACTIONCANONICAL
[count
].text
!= NULL
) {
2117 u_unescape(CONTRACTIONCANONICAL
[count
].text
, temp
, 128);
2118 text
.setTo(temp
, u_strlen(temp
));
2119 u_unescape(CONTRACTIONCANONICAL
[count
].pattern
, temp
, 128);
2120 pattern
.setTo(temp
, u_strlen(temp
));
2121 strsrch
->setText(text
, status
);
2122 strsrch
->setPattern(pattern
, status
);
2123 if (!assertEqualWithStringSearch(strsrch
,
2124 &CONTRACTIONCANONICAL
[count
])) {
2125 errln("Error at test number %d", count
);
2133 void StringSearchTest::TestUClassID()
2135 char id
= *((char *)StringSearch::getStaticClassID());
2137 errln("Static class id for StringSearch should be 0");
2139 UErrorCode status
= U_ZERO_ERROR
;
2140 UnicodeString
text("text");
2141 UnicodeString
pattern("pattern");
2142 StringSearch
*strsrch
= new StringSearch(pattern
, text
, m_en_us_
, NULL
,
2144 id
= *((char *)strsrch
->getDynamicClassID());
2146 errln("Dynamic class id for StringSearch should be 0");
2151 class TestSearch
: public SearchIterator
2154 TestSearch(const TestSearch
&obj
);
2155 TestSearch(const UnicodeString
&text
,
2156 BreakIterator
*breakiter
,
2157 const UnicodeString
&pattern
);
2160 void setOffset(int32_t position
, UErrorCode
&status
);
2161 int32_t getOffset() const;
2162 SearchIterator
* safeClone() const;
2166 * ICU "poor man's RTTI", returns a UClassID for the actual class.
2170 virtual inline UClassID
getDynamicClassID() const { return getStaticClassID(); }
2173 * ICU "poor man's RTTI", returns a UClassID for this class.
2177 static inline UClassID
getStaticClassID() { return (UClassID
)&fgClassID
; }
2179 UBool
operator!=(const TestSearch
&that
) const;
2181 UnicodeString m_pattern_
;
2184 int32_t handleNext(int32_t position
, UErrorCode
&status
);
2185 int32_t handlePrev(int32_t position
, UErrorCode
&status
);
2186 TestSearch
& operator=(const TestSearch
&that
);
2191 * The address of this static class variable serves as this class's ID
2192 * for ICU "poor man's RTTI".
2194 static const char fgClassID
;
2198 const char TestSearch::fgClassID
=0;
2200 TestSearch::TestSearch(const TestSearch
&obj
) : SearchIterator(obj
)
2202 m_offset_
= obj
.m_offset_
;
2203 m_pattern_
= obj
.m_pattern_
;
2206 TestSearch::TestSearch(const UnicodeString
&text
,
2207 BreakIterator
*breakiter
,
2208 const UnicodeString
&pattern
) : SearchIterator()
2210 m_breakiterator_
= breakiter
;
2211 m_pattern_
= pattern
;
2214 m_pattern_
= pattern
;
2217 TestSearch::~TestSearch()
2222 void TestSearch::setOffset(int32_t position
, UErrorCode
&status
)
2224 if (position
>= 0 && position
<= m_text_
.length()) {
2225 m_offset_
= position
;
2228 status
= U_INDEX_OUTOFBOUNDS_ERROR
;
2232 int32_t TestSearch::getOffset() const
2237 SearchIterator
* TestSearch::safeClone() const
2239 return new TestSearch(m_text_
, m_breakiterator_
, m_pattern_
);
2242 UBool
TestSearch::operator!=(const TestSearch
&that
) const
2244 if (SearchIterator::operator !=(that
)) {
2247 return m_offset_
!= that
.m_offset_
|| m_pattern_
!= that
.m_pattern_
;
2250 int32_t TestSearch::handleNext(int32_t start
, UErrorCode
&status
)
2252 if(U_SUCCESS(status
)) {
2253 int match
= m_text_
.indexOf(m_pattern_
, start
);
2255 m_offset_
= m_text_
.length();
2256 setMatchStart(m_offset_
);
2258 return USEARCH_DONE
;
2260 setMatchStart(match
);
2262 setMatchLength(m_pattern_
.length());
2265 return USEARCH_DONE
;
2269 int32_t TestSearch::handlePrev(int32_t start
, UErrorCode
&status
)
2271 if(U_SUCCESS(status
)) {
2272 int match
= m_text_
.lastIndexOf(m_pattern_
, 0, start
);
2275 setMatchStart(m_offset_
);
2277 return USEARCH_DONE
;
2279 setMatchStart(match
);
2281 setMatchLength(m_pattern_
.length());
2284 return USEARCH_DONE
;
2288 TestSearch
& TestSearch::operator=(const TestSearch
&that
)
2290 SearchIterator::operator=(that
);
2291 m_offset_
= that
.m_offset_
;
2292 m_pattern_
= that
.m_pattern_
;
2296 void StringSearchTest::TestSubclass()
2298 UnicodeString
text("abc abcd abc");
2299 UnicodeString
pattern("abc");
2300 TestSearch
search(text
, NULL
, pattern
);
2301 TestSearch
search2(search
);
2302 int expected
[] = {0, 4, 9};
2303 UErrorCode status
= U_ZERO_ERROR
;
2305 StringCharacterIterator
chariter(text
);
2307 search
.setText(text
, status
);
2308 if (search
.getText() != search2
.getText()) {
2309 errln("Error setting text");
2312 search
.setText(chariter
, status
);
2313 if (search
.getText() != search2
.getText()) {
2314 errln("Error setting text");
2318 // comparing constructors
2320 for (i
= 0; i
< (int)(sizeof(expected
) / sizeof(expected
[0])); i
++) {
2321 if (search
.next(status
) != expected
[i
]) {
2322 errln("Error getting next match");
2324 if (search
.getMatchedLength() != search
.m_pattern_
.length()) {
2325 errln("Error getting next match length");
2328 if (search
.next(status
) != USEARCH_DONE
) {
2329 errln("Error should have reached the end of the iteration");
2331 for (i
= sizeof(expected
) / sizeof(expected
[0]) - 1; i
>= 0; i
--) {
2332 if (search
.previous(status
) != expected
[i
]) {
2333 errln("Error getting previous match");
2335 if (search
.getMatchedLength() != search
.m_pattern_
.length()) {
2336 errln("Error getting previous match length");
2339 if (search
.previous(status
) != USEARCH_DONE
) {
2340 errln("Error should have reached the start of the iteration");
2344 class StubSearchIterator
:public SearchIterator
{
2346 StubSearchIterator(){}
2347 virtual void setOffset(int32_t , UErrorCode
&) {};
2348 virtual int32_t getOffset(void) const {return 0;};
2349 virtual SearchIterator
* safeClone(void) const {return NULL
;};
2350 virtual int32_t handleNext(int32_t , UErrorCode
&){return 0;};
2351 virtual int32_t handlePrev(int32_t , UErrorCode
&) {return 0;};
2352 virtual UClassID
getDynamicClassID() const {
2353 static char classID
= 0;
2354 return (UClassID
)&classID
;
2358 void StringSearchTest::TestCoverage(){
2359 StubSearchIterator stub1
, stub2
;
2360 UErrorCode status
= U_ZERO_ERROR
;
2362 if (stub1
!= stub2
){
2363 errln("new StubSearchIterator should be equal");
2366 stub2
.setText(UnicodeString("ABC"), status
);
2367 if (U_FAILURE(status
)) {
2368 errln("Error: SearchIterator::SetText");
2372 if (stub1
!= stub2
){
2373 errln("SearchIterator::operator = assigned object should be equal");
2377 #endif /* !UCONFIG_NO_BREAK_ITERATION */
2379 #endif /* #if !UCONFIG_NO_COLLATION */