1 /********************************************************************
3 * Copyright (c) 1997-2010, 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"
13 #include "normalizer2impl.h"
17 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof(array[0]))
19 static const char *ignorePropNames
[]={
32 UnicodeTest::UnicodeTest()
34 UErrorCode errorCode
=U_ZERO_ERROR
;
35 unknownPropertyNames
=new U_NAMESPACE_QUALIFIER
Hashtable(errorCode
);
36 if(U_FAILURE(errorCode
)) {
37 delete unknownPropertyNames
;
38 unknownPropertyNames
=NULL
;
40 // Ignore some property names altogether.
41 for(int32_t i
=0; i
<LENGTHOF(ignorePropNames
); ++i
) {
42 unknownPropertyNames
->puti(UnicodeString(ignorePropNames
[i
], -1, US_INV
), 1, errorCode
);
46 UnicodeTest::~UnicodeTest()
48 delete unknownPropertyNames
;
51 void UnicodeTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
53 if (exec
) logln("TestSuite UnicodeTest: ");
55 case 0: name
= "TestAdditionalProperties"; if(exec
) TestAdditionalProperties(); break;
56 case 1: name
= "TestBinaryValues"; if(exec
) TestBinaryValues(); break;
57 case 2: name
= "TestConsistency"; if(exec
) TestConsistency(); break;
58 default: name
= ""; break; //needed to end loop
62 //====================================================
63 // private data used by the tests
64 //====================================================
66 // test DerivedCoreProperties.txt -------------------------------------------
68 // copied from genprops.c
70 getTokenIndex(const char *const tokens
[], int32_t countTokens
, const char *s
) {
74 s
=u_skipWhitespace(s
);
75 for(i
=0; i
<countTokens
; ++i
) {
84 z
=u_skipWhitespace(s
+j
);
85 if(*z
==';' || *z
==0) {
97 static const char *const
107 "Default_Ignorable_Code_Point",
108 "Full_Composition_Exclusion",
110 "Grapheme_Link", /* Unicode 5 moves this property here from PropList.txt */
114 "Changes_When_Lowercased",
115 "Changes_When_Uppercased",
116 "Changes_When_Titlecased",
117 "Changes_When_Casefolded",
118 "Changes_When_Casemapped",
119 "Changes_When_NFKC_Casefolded"
122 static const UProperty
123 derivedPropsIndex
[]={
132 UCHAR_DEFAULT_IGNORABLE_CODE_POINT
,
133 UCHAR_FULL_COMPOSITION_EXCLUSION
,
134 UCHAR_GRAPHEME_EXTEND
,
138 UCHAR_CASE_IGNORABLE
,
139 UCHAR_CHANGES_WHEN_LOWERCASED
,
140 UCHAR_CHANGES_WHEN_UPPERCASED
,
141 UCHAR_CHANGES_WHEN_TITLECASED
,
142 UCHAR_CHANGES_WHEN_CASEFOLDED
,
143 UCHAR_CHANGES_WHEN_CASEMAPPED
,
144 UCHAR_CHANGES_WHEN_NFKC_CASEFOLDED
147 static int32_t numErrors
[LENGTHOF(derivedPropsIndex
)]={ 0 };
149 enum { MAX_ERRORS
=50 };
151 U_CFUNC
void U_CALLCONV
152 derivedPropsLineFn(void *context
,
153 char *fields
[][2], int32_t /* fieldCount */,
154 UErrorCode
*pErrorCode
)
156 UnicodeTest
*me
=(UnicodeTest
*)context
;
160 u_parseCodePointRange(fields
[0][0], &start
, &end
, pErrorCode
);
161 if(U_FAILURE(*pErrorCode
)) {
162 me
->errln("UnicodeTest: syntax error in DerivedCoreProperties.txt or DerivedNormalizationProps.txt field 0 at %s\n", fields
[0][0]);
166 /* parse derived binary property name, ignore unknown names */
167 i
=getTokenIndex(derivedPropsNames
, LENGTHOF(derivedPropsNames
), fields
[1][0]);
169 UnicodeString
propName(fields
[1][0], (int32_t)(fields
[1][1]-fields
[1][0]));
171 if(me
->unknownPropertyNames
->find(propName
)==NULL
) {
172 UErrorCode errorCode
=U_ZERO_ERROR
;
173 me
->unknownPropertyNames
->puti(propName
, 1, errorCode
);
174 me
->errln("UnicodeTest warning: unknown property name '%s' in DerivedCoreProperties.txt or DerivedNormalizationProps.txt\n", fields
[1][0]);
179 me
->derivedProps
[i
].add(start
, end
);
182 void UnicodeTest::TestAdditionalProperties() {
183 #if !UCONFIG_NO_NORMALIZATION
184 // test DerivedCoreProperties.txt and DerivedNormalizationProps.txt
185 if(LENGTHOF(derivedProps
)<LENGTHOF(derivedPropsNames
)) {
186 errln("error: UnicodeTest::derivedProps[] too short, need at least %d UnicodeSets\n",
187 LENGTHOF(derivedPropsNames
));
190 if(LENGTHOF(derivedPropsIndex
)!=LENGTHOF(derivedPropsNames
)) {
191 errln("error in ucdtest.cpp: LENGTHOF(derivedPropsIndex)!=LENGTHOF(derivedPropsNames)\n");
196 char backupPath
[256];
198 UErrorCode errorCode
=U_ZERO_ERROR
;
200 /* Look inside ICU_DATA first */
201 strcpy(newPath
, pathToDataDirectory());
202 strcat(newPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
204 // As a fallback, try to guess where the source data was located
205 // at the time ICU was built, and look there.
207 strcpy(backupPath
, U_TOPSRCDIR U_FILE_SEP_STRING
"data");
209 strcpy(backupPath
, loadTestData(errorCode
));
210 strcat(backupPath
, U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
"data");
212 strcat(backupPath
, U_FILE_SEP_STRING
);
213 strcat(backupPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
216 u_parseDelimitedFile(newPath
, ';', fields
, 2, derivedPropsLineFn
, this, &errorCode
);
218 if(errorCode
==U_FILE_ACCESS_ERROR
) {
219 errorCode
=U_ZERO_ERROR
;
221 u_parseDelimitedFile(backupPath
, ';', fields
, 2, derivedPropsLineFn
, this, &errorCode
);
223 if(U_FAILURE(errorCode
)) {
224 errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode
));
227 char *basename
=path
+strlen(path
)-strlen("DerivedCoreProperties.txt");
228 strcpy(basename
, "DerivedNormalizationProps.txt");
229 u_parseDelimitedFile(path
, ';', fields
, 2, derivedPropsLineFn
, this, &errorCode
);
230 if(U_FAILURE(errorCode
)) {
231 errln("error parsing DerivedNormalizationProps.txt: %s\n", u_errorName(errorCode
));
235 // now we have all derived core properties in the UnicodeSets
236 // run them all through the API
237 int32_t rangeCount
, range
;
241 // test all TRUE properties
242 for(i
=0; i
<LENGTHOF(derivedPropsNames
); ++i
) {
243 rangeCount
=derivedProps
[i
].getRangeCount();
244 for(range
=0; range
<rangeCount
&& numErrors
[i
]<MAX_ERRORS
; ++range
) {
245 start
=derivedProps
[i
].getRangeStart(range
);
246 end
=derivedProps
[i
].getRangeEnd(range
);
247 for(; start
<=end
; ++start
) {
248 if(!u_hasBinaryProperty(start
, derivedPropsIndex
[i
])) {
249 dataerrln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==FALSE is wrong", start
, derivedPropsNames
[i
]);
250 if(++numErrors
[i
]>=MAX_ERRORS
) {
251 dataerrln("Too many errors, moving to the next test");
259 // invert all properties
260 for(i
=0; i
<LENGTHOF(derivedPropsNames
); ++i
) {
261 derivedProps
[i
].complement();
264 // test all FALSE properties
265 for(i
=0; i
<LENGTHOF(derivedPropsNames
); ++i
) {
266 rangeCount
=derivedProps
[i
].getRangeCount();
267 for(range
=0; range
<rangeCount
&& numErrors
[i
]<MAX_ERRORS
; ++range
) {
268 start
=derivedProps
[i
].getRangeStart(range
);
269 end
=derivedProps
[i
].getRangeEnd(range
);
270 for(; start
<=end
; ++start
) {
271 if(u_hasBinaryProperty(start
, derivedPropsIndex
[i
])) {
272 errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==TRUE is wrong\n", start
, derivedPropsNames
[i
]);
273 if(++numErrors
[i
]>=MAX_ERRORS
) {
274 errln("Too many errors, moving to the next test");
281 #endif /* !UCONFIG_NO_NORMALIZATION */
284 void UnicodeTest::TestBinaryValues() {
286 * Unicode 5.1 explicitly defines binary property value aliases.
287 * Verify that they are all recognized.
289 UErrorCode errorCode
=U_ZERO_ERROR
;
290 UnicodeSet
alpha(UNICODE_STRING_SIMPLE("[:Alphabetic:]"), errorCode
);
291 if(U_FAILURE(errorCode
)) {
292 dataerrln("UnicodeSet([:Alphabetic:]) failed - %s", u_errorName(errorCode
));
296 static const char *const falseValues
[]={ "N", "No", "F", "False" };
297 static const char *const trueValues
[]={ "Y", "Yes", "T", "True" };
299 for(i
=0; i
<LENGTHOF(falseValues
); ++i
) {
300 UnicodeString pattern
=UNICODE_STRING_SIMPLE("[:Alphabetic=:]");
301 pattern
.insert(pattern
.length()-2, UnicodeString(falseValues
[i
], -1, US_INV
));
302 errorCode
=U_ZERO_ERROR
;
303 UnicodeSet
set(pattern
, errorCode
);
304 if(U_FAILURE(errorCode
)) {
305 errln("UnicodeSet([:Alphabetic=%s:]) failed - %s\n", falseValues
[i
], u_errorName(errorCode
));
310 errln("UnicodeSet([:Alphabetic=%s:]).complement()!=UnicodeSet([:Alphabetic:])\n", falseValues
[i
]);
313 for(i
=0; i
<LENGTHOF(trueValues
); ++i
) {
314 UnicodeString pattern
=UNICODE_STRING_SIMPLE("[:Alphabetic=:]");
315 pattern
.insert(pattern
.length()-2, UnicodeString(trueValues
[i
], -1, US_INV
));
316 errorCode
=U_ZERO_ERROR
;
317 UnicodeSet
set(pattern
, errorCode
);
318 if(U_FAILURE(errorCode
)) {
319 errln("UnicodeSet([:Alphabetic=%s:]) failed - %s\n", trueValues
[i
], u_errorName(errorCode
));
323 errln("UnicodeSet([:Alphabetic=%s:])!=UnicodeSet([:Alphabetic:])\n", trueValues
[i
]);
328 void UnicodeTest::TestConsistency() {
329 #if !UCONFIG_NO_NORMALIZATION
331 * Test for an example that getCanonStartSet() delivers
332 * all characters that compose from the input one,
333 * even in multiple steps.
334 * For example, the set for "I" (0049) should contain both
335 * I-diaeresis (00CF) and I-diaeresis-acute (1E2E).
336 * In general, the set for the middle such character should be a subset
337 * of the set for the first.
339 IcuTestErrorCode
errorCode(*this, "TestConsistency");
340 const Normalizer2
*nfd
=Normalizer2::getInstance(NULL
, "nfc", UNORM2_DECOMPOSE
, errorCode
);
341 const Normalizer2Impl
*nfcImpl
=Normalizer2Factory::getNFCImpl(errorCode
);
342 if(errorCode
.isFailure()) {
343 dataerrln("Normalizer2::getInstance(NFD) or Normalizer2Factory::getNFCImpl() failed - %s\n",
344 errorCode
.errorName());
349 UnicodeSet set1
, set2
;
350 if (nfcImpl
->getCanonStartSet(0x49, set1
)) {
351 /* enumerate all characters that are plausible to be latin letters */
352 for(UChar start
=0xa0; start
<0x2000; ++start
) {
353 UnicodeString decomp
=nfd
->normalize(UnicodeString(start
), errorCode
);
354 if(decomp
.length()>1 && decomp
[0]==0x49) {
360 errln("[canon start set of 0049] != [all c with canon decomp with 0049]");
362 // This was available in cucdtst.c but the test had to move to intltest
363 // because the new internal normalization functions are in C++.
364 //compareUSets(set1, set2,
365 // "[canon start set of 0049]", "[all c with canon decomp with 0049]",
368 errln("NFC.getCanonStartSet() returned FALSE");