1 /********************************************************************
3 * Copyright (c) 1997-2006, 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"
15 #define LENGTHOF(array) (sizeof(array)/sizeof(array[0]))
17 UnicodeTest::UnicodeTest()
21 UnicodeTest::~UnicodeTest()
25 void UnicodeTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
27 if (exec
) logln("TestSuite UnicodeTest: ");
29 case 0: name
= "TestAdditionalProperties"; if(exec
) TestAdditionalProperties(); break;
30 default: name
= ""; break; //needed to end loop
34 //====================================================
35 // private data used by the tests
36 //====================================================
38 // test DerivedCoreProperties.txt -------------------------------------------
40 // copied from genprops.c
42 getTokenIndex(const char *const tokens
[], int32_t countTokens
, const char *s
) {
46 s
=u_skipWhitespace(s
);
47 for(i
=0; i
<countTokens
; ++i
) {
56 z
=u_skipWhitespace(s
+j
);
57 if(*z
==';' || *z
==0) {
69 static const char *const
70 derivedCorePropsNames
[]={
79 "Default_Ignorable_Code_Point",
81 "Grapheme_Link", /* Unicode 5 moves this property here from PropList.txt */
85 static const UProperty
86 derivedCorePropsIndex
[]={
95 UCHAR_DEFAULT_IGNORABLE_CODE_POINT
,
96 UCHAR_GRAPHEME_EXTEND
,
101 U_CFUNC
void U_CALLCONV
102 derivedCorePropsLineFn(void *context
,
103 char *fields
[][2], int32_t /* fieldCount */,
104 UErrorCode
*pErrorCode
)
106 UnicodeTest
*me
=(UnicodeTest
*)context
;
110 u_parseCodePointRange(fields
[0][0], &start
, &end
, pErrorCode
);
111 if(U_FAILURE(*pErrorCode
)) {
112 me
->errln("UnicodeTest: syntax error in DerivedCoreProperties.txt field 0 at %s\n", fields
[0][0]);
116 /* parse derived binary property name, ignore unknown names */
117 i
=getTokenIndex(derivedCorePropsNames
, LENGTHOF(derivedCorePropsNames
), fields
[1][0]);
119 me
->errln("UnicodeTest warning: unknown property name '%s' in \n", fields
[1][0]);
123 me
->derivedCoreProps
[i
].add(start
, end
);
126 void UnicodeTest::TestAdditionalProperties() {
127 // test DerivedCoreProperties.txt
128 if(LENGTHOF(derivedCoreProps
)<LENGTHOF(derivedCorePropsNames
)) {
129 errln("error: UnicodeTest::derivedCoreProps[] too short, need at least %d UnicodeSets\n",
130 LENGTHOF(derivedCorePropsNames
));
133 if(LENGTHOF(derivedCorePropsIndex
)!=LENGTHOF(derivedCorePropsNames
)) {
134 errln("error in ucdtest.cpp: LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)\n");
139 char backupPath
[256];
141 UErrorCode errorCode
=U_ZERO_ERROR
;
143 /* Look inside ICU_DATA first */
144 strcpy(newPath
, pathToDataDirectory());
145 strcat(newPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
147 // As a fallback, try to guess where the source data was located
148 // at the time ICU was built, and look there.
150 strcpy(backupPath
, U_TOPSRCDIR U_FILE_SEP_STRING
"data");
152 strcpy(backupPath
, loadTestData(errorCode
));
153 strcat(backupPath
, U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
"data");
155 strcat(backupPath
, U_FILE_SEP_STRING
);
156 strcat(backupPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
158 u_parseDelimitedFile(newPath
, ';', fields
, 2, derivedCorePropsLineFn
, this, &errorCode
);
160 if(errorCode
==U_FILE_ACCESS_ERROR
) {
161 errorCode
=U_ZERO_ERROR
;
162 u_parseDelimitedFile(backupPath
, ';', fields
, 2, derivedCorePropsLineFn
, this, &errorCode
);
164 if(U_FAILURE(errorCode
)) {
165 errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode
));
169 // now we have all derived core properties in the UnicodeSets
170 // run them all through the API
171 int32_t rangeCount
, range
;
174 int32_t noErrors
= 0;
176 // test all TRUE properties
177 for(i
=0; i
<LENGTHOF(derivedCorePropsNames
); ++i
) {
178 rangeCount
=derivedCoreProps
[i
].getRangeCount();
179 for(range
=0; range
<rangeCount
; ++range
) {
180 start
=derivedCoreProps
[i
].getRangeStart(range
);
181 end
=derivedCoreProps
[i
].getRangeEnd(range
);
182 for(; start
<=end
; ++start
) {
183 if(!u_hasBinaryProperty(start
, derivedCorePropsIndex
[i
])) {
184 errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==FALSE is wrong\n", start
, derivedCorePropsNames
[i
]);
185 if(noErrors
++ > 100) {
186 errln("Too many errors, moving to the next test");
195 // invert all properties
196 for(i
=0; i
<LENGTHOF(derivedCorePropsNames
); ++i
) {
197 derivedCoreProps
[i
].complement();
200 // test all FALSE properties
201 for(i
=0; i
<LENGTHOF(derivedCorePropsNames
); ++i
) {
202 rangeCount
=derivedCoreProps
[i
].getRangeCount();
203 for(range
=0; range
<rangeCount
; ++range
) {
204 start
=derivedCoreProps
[i
].getRangeStart(range
);
205 end
=derivedCoreProps
[i
].getRangeEnd(range
);
206 for(; start
<=end
; ++start
) {
207 if(u_hasBinaryProperty(start
, derivedCorePropsIndex
[i
])) {
208 errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==TRUE is wrong\n", start
, derivedCorePropsNames
[i
]);
209 if(noErrors
++ > 100) {
210 errln("Too many errors, moving to the next test");