1 /********************************************************************
3 * Copyright (c) 1997-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/ustring.h"
8 #include "unicode/uchar.h"
9 #include "unicode/uniset.h"
10 #include "unicode/putil.h"
11 #include "unicode/uscript.h"
14 #include "patternprops.h"
15 #include "normalizer2impl.h"
19 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof(array[0]))
21 static const char *ignorePropNames
[]={
34 UnicodeTest::UnicodeTest()
36 UErrorCode errorCode
=U_ZERO_ERROR
;
37 unknownPropertyNames
=new U_NAMESPACE_QUALIFIER
Hashtable(errorCode
);
38 if(U_FAILURE(errorCode
)) {
39 delete unknownPropertyNames
;
40 unknownPropertyNames
=NULL
;
42 // Ignore some property names altogether.
43 for(int32_t i
=0; i
<LENGTHOF(ignorePropNames
); ++i
) {
44 unknownPropertyNames
->puti(UnicodeString(ignorePropNames
[i
], -1, US_INV
), 1, errorCode
);
48 UnicodeTest::~UnicodeTest()
50 delete unknownPropertyNames
;
53 void UnicodeTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
56 logln("TestSuite UnicodeTest: ");
59 TESTCASE_AUTO(TestAdditionalProperties
);
60 TESTCASE_AUTO(TestBinaryValues
);
61 TESTCASE_AUTO(TestConsistency
);
62 TESTCASE_AUTO(TestPatternProperties
);
63 TESTCASE_AUTO(TestScriptMetadata
);
67 //====================================================
68 // private data used by the tests
69 //====================================================
71 // test DerivedCoreProperties.txt -------------------------------------------
73 // copied from genprops.c
75 getTokenIndex(const char *const tokens
[], int32_t countTokens
, const char *s
) {
79 s
=u_skipWhitespace(s
);
80 for(i
=0; i
<countTokens
; ++i
) {
89 z
=u_skipWhitespace(s
+j
);
90 if(*z
==';' || *z
==0) {
102 static const char *const
103 derivedPropsNames
[]={
112 "Default_Ignorable_Code_Point",
113 "Full_Composition_Exclusion",
115 "Grapheme_Link", /* Unicode 5 moves this property here from PropList.txt */
119 "Changes_When_Lowercased",
120 "Changes_When_Uppercased",
121 "Changes_When_Titlecased",
122 "Changes_When_Casefolded",
123 "Changes_When_Casemapped",
124 "Changes_When_NFKC_Casefolded"
127 static const UProperty
128 derivedPropsIndex
[]={
137 UCHAR_DEFAULT_IGNORABLE_CODE_POINT
,
138 UCHAR_FULL_COMPOSITION_EXCLUSION
,
139 UCHAR_GRAPHEME_EXTEND
,
143 UCHAR_CASE_IGNORABLE
,
144 UCHAR_CHANGES_WHEN_LOWERCASED
,
145 UCHAR_CHANGES_WHEN_UPPERCASED
,
146 UCHAR_CHANGES_WHEN_TITLECASED
,
147 UCHAR_CHANGES_WHEN_CASEFOLDED
,
148 UCHAR_CHANGES_WHEN_CASEMAPPED
,
149 UCHAR_CHANGES_WHEN_NFKC_CASEFOLDED
152 static int32_t numErrors
[LENGTHOF(derivedPropsIndex
)]={ 0 };
154 enum { MAX_ERRORS
=50 };
156 U_CFUNC
void U_CALLCONV
157 derivedPropsLineFn(void *context
,
158 char *fields
[][2], int32_t /* fieldCount */,
159 UErrorCode
*pErrorCode
)
161 UnicodeTest
*me
=(UnicodeTest
*)context
;
165 u_parseCodePointRange(fields
[0][0], &start
, &end
, pErrorCode
);
166 if(U_FAILURE(*pErrorCode
)) {
167 me
->errln("UnicodeTest: syntax error in DerivedCoreProperties.txt or DerivedNormalizationProps.txt field 0 at %s\n", fields
[0][0]);
171 /* parse derived binary property name, ignore unknown names */
172 i
=getTokenIndex(derivedPropsNames
, LENGTHOF(derivedPropsNames
), fields
[1][0]);
174 UnicodeString
propName(fields
[1][0], (int32_t)(fields
[1][1]-fields
[1][0]));
176 if(me
->unknownPropertyNames
->find(propName
)==NULL
) {
177 UErrorCode errorCode
=U_ZERO_ERROR
;
178 me
->unknownPropertyNames
->puti(propName
, 1, errorCode
);
179 me
->errln("UnicodeTest warning: unknown property name '%s' in DerivedCoreProperties.txt or DerivedNormalizationProps.txt\n", fields
[1][0]);
184 me
->derivedProps
[i
].add(start
, end
);
187 void UnicodeTest::TestAdditionalProperties() {
188 #if !UCONFIG_NO_NORMALIZATION
189 // test DerivedCoreProperties.txt and DerivedNormalizationProps.txt
190 if(LENGTHOF(derivedProps
)<LENGTHOF(derivedPropsNames
)) {
191 errln("error: UnicodeTest::derivedProps[] too short, need at least %d UnicodeSets\n",
192 LENGTHOF(derivedPropsNames
));
195 if(LENGTHOF(derivedPropsIndex
)!=LENGTHOF(derivedPropsNames
)) {
196 errln("error in ucdtest.cpp: LENGTHOF(derivedPropsIndex)!=LENGTHOF(derivedPropsNames)\n");
201 char backupPath
[256];
203 UErrorCode errorCode
=U_ZERO_ERROR
;
205 /* Look inside ICU_DATA first */
206 strcpy(newPath
, pathToDataDirectory());
207 strcat(newPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
209 // As a fallback, try to guess where the source data was located
210 // at the time ICU was built, and look there.
212 strcpy(backupPath
, U_TOPSRCDIR U_FILE_SEP_STRING
"data");
214 strcpy(backupPath
, loadTestData(errorCode
));
215 strcat(backupPath
, U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
"data");
217 strcat(backupPath
, U_FILE_SEP_STRING
);
218 strcat(backupPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
221 u_parseDelimitedFile(newPath
, ';', fields
, 2, derivedPropsLineFn
, this, &errorCode
);
223 if(errorCode
==U_FILE_ACCESS_ERROR
) {
224 errorCode
=U_ZERO_ERROR
;
226 u_parseDelimitedFile(backupPath
, ';', fields
, 2, derivedPropsLineFn
, this, &errorCode
);
228 if(U_FAILURE(errorCode
)) {
229 errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode
));
232 char *basename
=path
+strlen(path
)-strlen("DerivedCoreProperties.txt");
233 strcpy(basename
, "DerivedNormalizationProps.txt");
234 u_parseDelimitedFile(path
, ';', fields
, 2, derivedPropsLineFn
, this, &errorCode
);
235 if(U_FAILURE(errorCode
)) {
236 errln("error parsing DerivedNormalizationProps.txt: %s\n", u_errorName(errorCode
));
240 // now we have all derived core properties in the UnicodeSets
241 // run them all through the API
242 int32_t rangeCount
, range
;
246 // test all TRUE properties
247 for(i
=0; i
<LENGTHOF(derivedPropsNames
); ++i
) {
248 rangeCount
=derivedProps
[i
].getRangeCount();
249 for(range
=0; range
<rangeCount
&& numErrors
[i
]<MAX_ERRORS
; ++range
) {
250 start
=derivedProps
[i
].getRangeStart(range
);
251 end
=derivedProps
[i
].getRangeEnd(range
);
252 for(; start
<=end
; ++start
) {
253 if(!u_hasBinaryProperty(start
, derivedPropsIndex
[i
])) {
254 dataerrln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==FALSE is wrong", start
, derivedPropsNames
[i
]);
255 if(++numErrors
[i
]>=MAX_ERRORS
) {
256 dataerrln("Too many errors, moving to the next test");
264 // invert all properties
265 for(i
=0; i
<LENGTHOF(derivedPropsNames
); ++i
) {
266 derivedProps
[i
].complement();
269 // test all FALSE properties
270 for(i
=0; i
<LENGTHOF(derivedPropsNames
); ++i
) {
271 rangeCount
=derivedProps
[i
].getRangeCount();
272 for(range
=0; range
<rangeCount
&& numErrors
[i
]<MAX_ERRORS
; ++range
) {
273 start
=derivedProps
[i
].getRangeStart(range
);
274 end
=derivedProps
[i
].getRangeEnd(range
);
275 for(; start
<=end
; ++start
) {
276 if(u_hasBinaryProperty(start
, derivedPropsIndex
[i
])) {
277 errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==TRUE is wrong\n", start
, derivedPropsNames
[i
]);
278 if(++numErrors
[i
]>=MAX_ERRORS
) {
279 errln("Too many errors, moving to the next test");
286 #endif /* !UCONFIG_NO_NORMALIZATION */
289 void UnicodeTest::TestBinaryValues() {
291 * Unicode 5.1 explicitly defines binary property value aliases.
292 * Verify that they are all recognized.
294 UErrorCode errorCode
=U_ZERO_ERROR
;
295 UnicodeSet
alpha(UNICODE_STRING_SIMPLE("[:Alphabetic:]"), errorCode
);
296 if(U_FAILURE(errorCode
)) {
297 dataerrln("UnicodeSet([:Alphabetic:]) failed - %s", u_errorName(errorCode
));
301 static const char *const falseValues
[]={ "N", "No", "F", "False" };
302 static const char *const trueValues
[]={ "Y", "Yes", "T", "True" };
304 for(i
=0; i
<LENGTHOF(falseValues
); ++i
) {
305 UnicodeString pattern
=UNICODE_STRING_SIMPLE("[:Alphabetic=:]");
306 pattern
.insert(pattern
.length()-2, UnicodeString(falseValues
[i
], -1, US_INV
));
307 errorCode
=U_ZERO_ERROR
;
308 UnicodeSet
set(pattern
, errorCode
);
309 if(U_FAILURE(errorCode
)) {
310 errln("UnicodeSet([:Alphabetic=%s:]) failed - %s\n", falseValues
[i
], u_errorName(errorCode
));
315 errln("UnicodeSet([:Alphabetic=%s:]).complement()!=UnicodeSet([:Alphabetic:])\n", falseValues
[i
]);
318 for(i
=0; i
<LENGTHOF(trueValues
); ++i
) {
319 UnicodeString pattern
=UNICODE_STRING_SIMPLE("[:Alphabetic=:]");
320 pattern
.insert(pattern
.length()-2, UnicodeString(trueValues
[i
], -1, US_INV
));
321 errorCode
=U_ZERO_ERROR
;
322 UnicodeSet
set(pattern
, errorCode
);
323 if(U_FAILURE(errorCode
)) {
324 errln("UnicodeSet([:Alphabetic=%s:]) failed - %s\n", trueValues
[i
], u_errorName(errorCode
));
328 errln("UnicodeSet([:Alphabetic=%s:])!=UnicodeSet([:Alphabetic:])\n", trueValues
[i
]);
333 void UnicodeTest::TestConsistency() {
334 #if !UCONFIG_NO_NORMALIZATION
336 * Test for an example that getCanonStartSet() delivers
337 * all characters that compose from the input one,
338 * even in multiple steps.
339 * For example, the set for "I" (0049) should contain both
340 * I-diaeresis (00CF) and I-diaeresis-acute (1E2E).
341 * In general, the set for the middle such character should be a subset
342 * of the set for the first.
344 IcuTestErrorCode
errorCode(*this, "TestConsistency");
345 const Normalizer2
*nfd
=Normalizer2::getNFDInstance(errorCode
);
346 const Normalizer2Impl
*nfcImpl
=Normalizer2Factory::getNFCImpl(errorCode
);
347 if(!nfcImpl
->ensureCanonIterData(errorCode
) || errorCode
.isFailure()) {
348 dataerrln("Normalizer2::getInstance(NFD) or Normalizer2Factory::getNFCImpl() failed - %s\n",
349 errorCode
.errorName());
354 UnicodeSet set1
, set2
;
355 if (nfcImpl
->getCanonStartSet(0x49, set1
)) {
356 /* enumerate all characters that are plausible to be latin letters */
357 for(UChar start
=0xa0; start
<0x2000; ++start
) {
358 UnicodeString decomp
=nfd
->normalize(UnicodeString(start
), errorCode
);
359 if(decomp
.length()>1 && decomp
[0]==0x49) {
365 errln("[canon start set of 0049] != [all c with canon decomp with 0049]");
367 // This was available in cucdtst.c but the test had to move to intltest
368 // because the new internal normalization functions are in C++.
369 //compareUSets(set1, set2,
370 // "[canon start set of 0049]", "[all c with canon decomp with 0049]",
373 errln("NFC.getCanonStartSet() returned FALSE");
379 * Test various implementations of Pattern_Syntax & Pattern_White_Space.
381 void UnicodeTest::TestPatternProperties() {
382 IcuTestErrorCode
errorCode(*this, "TestPatternProperties()");
384 UnicodeSet
syn_prop(UNICODE_STRING_SIMPLE("[:Pattern_Syntax:]"), errorCode
);
386 "[!-/\\:-@\\[-\\^`\\{-~"
387 "\\u00A1-\\u00A7\\u00A9\\u00AB\\u00AC\\u00AE\\u00B0\\u00B1\\u00B6\\u00BB\\u00BF\\u00D7\\u00F7"
388 "\\u2010-\\u2027\\u2030-\\u203E\\u2041-\\u2053\\u2055-\\u205E\\u2190-\\u245F\\u2500-\\u2775"
389 "\\u2794-\\u2BFF\\u2E00-\\u2E7F\\u3001-\\u3003\\u3008-\\u3020\\u3030\\uFD3E\\uFD3F\\uFE45\\uFE46]", errorCode
);
391 UnicodeSet
ws_prop(UNICODE_STRING_SIMPLE("[:Pattern_White_Space:]"), errorCode
);
392 UnicodeSet
ws_list(UNICODE_STRING_SIMPLE("[\\u0009-\\u000D\\ \\u0085\\u200E\\u200F\\u2028\\u2029]"), errorCode
);
393 UnicodeSet syn_ws_pp
;
394 UnicodeSet
syn_ws_prop(syn_prop
);
395 syn_ws_prop
.addAll(ws_prop
);
396 for(UChar32 c
=0; c
<=0xffff; ++c
) {
397 if(PatternProps::isSyntax(c
)) {
400 if(PatternProps::isWhiteSpace(c
)) {
403 if(PatternProps::isSyntaxOrWhiteSpace(c
)) {
407 compareUSets(syn_pp
, syn_prop
,
408 "PatternProps.isSyntax()", "[:Pattern_Syntax:]", TRUE
);
409 compareUSets(syn_pp
, syn_list
,
410 "PatternProps.isSyntax()", "[Pattern_Syntax ranges]", TRUE
);
411 compareUSets(ws_pp
, ws_prop
,
412 "PatternProps.isWhiteSpace()", "[:Pattern_White_Space:]", TRUE
);
413 compareUSets(ws_pp
, ws_list
,
414 "PatternProps.isWhiteSpace()", "[Pattern_White_Space ranges]", TRUE
);
415 compareUSets(syn_ws_pp
, syn_ws_prop
,
416 "PatternProps.isSyntaxOrWhiteSpace()",
417 "[[:Pattern_Syntax:][:Pattern_White_Space:]]", TRUE
);
420 // So far only minimal port of Java & cucdtst.c compareUSets().
422 UnicodeTest::compareUSets(const UnicodeSet
&a
, const UnicodeSet
&b
,
423 const char *a_name
, const char *b_name
,
426 if(!same
&& diffIsError
) {
427 errln("Sets are different: %s vs. %s\n", a_name
, b_name
);
435 * Maps a special script code to the most common script of its encoded characters.
437 UScriptCode
getCharScript(UScriptCode script
) {
439 case USCRIPT_SIMPLIFIED_HAN
:
440 case USCRIPT_TRADITIONAL_HAN
:
442 case USCRIPT_JAPANESE
:
443 return USCRIPT_HIRAGANA
;
445 return USCRIPT_HANGUL
;
453 void UnicodeTest::TestScriptMetadata() {
454 IcuTestErrorCode
errorCode(*this, "TestScriptMetadata()");
455 UnicodeSet
rtl("[[:bc=R:][:bc=AL:]-[:Cn:]-[:sc=Common:]]", errorCode
);
456 // So far, sample characters are uppercase.
457 // Georgian is special.
458 UnicodeSet
cased("[[:Lu:]-[:sc=Common:]-[:sc=Geor:]]", errorCode
);
459 for(int32_t sci
= 0; sci
< USCRIPT_CODE_LIMIT
; ++sci
) {
460 UScriptCode sc
= (UScriptCode
)sci
;
461 // Run the test with -v to see which script has failures:
462 // .../intltest$ make && ./intltest utility/UnicodeTest/TestScriptMetadata -v | grep -C 3 FAIL
463 logln(uscript_getShortName(sc
));
464 UScriptUsage usage
= uscript_getUsage(sc
);
465 UnicodeString sample
= uscript_getSampleUnicodeString(sc
);
466 UnicodeSet scriptSet
;
467 scriptSet
.applyIntPropertyValue(UCHAR_SCRIPT
, sc
, errorCode
);
468 if(usage
== USCRIPT_USAGE_NOT_ENCODED
) {
469 assertTrue("not encoded, no sample", sample
.isEmpty());
470 assertFalse("not encoded, not RTL", uscript_isRightToLeft(sc
));
471 assertFalse("not encoded, not LB letters", uscript_breaksBetweenLetters(sc
));
472 assertFalse("not encoded, not cased", uscript_isCased(sc
));
473 assertTrue("not encoded, no characters", scriptSet
.isEmpty());
475 assertFalse("encoded, has a sample character", sample
.isEmpty());
476 UChar32 firstChar
= sample
.char32At(0);
477 UScriptCode charScript
= getCharScript(sc
);
478 assertEquals("script(sample(script))",
479 charScript
, uscript_getScript(firstChar
, errorCode
));
480 assertEquals("RTL vs. set", rtl
.contains(firstChar
), uscript_isRightToLeft(sc
));
481 assertEquals("cased vs. set", cased
.contains(firstChar
), uscript_isCased(sc
));
482 assertEquals("encoded, has characters", sc
== charScript
, !scriptSet
.isEmpty());
483 if(uscript_isRightToLeft(sc
)) {
484 rtl
.removeAll(scriptSet
);
486 if(uscript_isCased(sc
)) {
487 cased
.removeAll(scriptSet
);
491 UnicodeString pattern
;
492 assertEquals("no remaining RTL characters",
493 UnicodeString("[]"), rtl
.toPattern(pattern
));
494 assertEquals("no remaining cased characters",
495 UnicodeString("[]"), cased
.toPattern(pattern
));
497 assertTrue("Hani breaks between letters", uscript_breaksBetweenLetters(USCRIPT_HAN
));
498 assertTrue("Thai breaks between letters", uscript_breaksBetweenLetters(USCRIPT_THAI
));
499 assertFalse("Latn does not break between letters", uscript_breaksBetweenLetters(USCRIPT_LATIN
));