1 /********************************************************************
3 * Copyright (c) 1997-2004, 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",
84 static const UProperty
85 derivedCorePropsIndex
[]={
94 UCHAR_DEFAULT_IGNORABLE_CODE_POINT
,
95 UCHAR_GRAPHEME_EXTEND
,
99 U_CFUNC
void U_CALLCONV
100 derivedCorePropsLineFn(void *context
,
101 char *fields
[][2], int32_t /* fieldCount */,
102 UErrorCode
*pErrorCode
)
104 UnicodeTest
*me
=(UnicodeTest
*)context
;
108 u_parseCodePointRange(fields
[0][0], &start
, &end
, pErrorCode
);
109 if(U_FAILURE(*pErrorCode
)) {
110 me
->errln("UnicodeTest: syntax error in DerivedCoreProperties.txt field 0 at %s\n", fields
[0][0]);
114 /* parse derived binary property name, ignore unknown names */
115 i
=getTokenIndex(derivedCorePropsNames
, LENGTHOF(derivedCorePropsNames
), fields
[1][0]);
117 me
->errln("UnicodeTest warning: unknown property name '%s' in \n", fields
[1][0]);
121 me
->derivedCoreProps
[i
].add(start
, end
);
124 void UnicodeTest::TestAdditionalProperties() {
125 // test DerivedCoreProperties.txt
126 if(LENGTHOF(derivedCoreProps
)<LENGTHOF(derivedCorePropsNames
)) {
127 errln("error: UnicodeTest::derivedCoreProps[] too short, need at least %d UnicodeSets\n",
128 LENGTHOF(derivedCorePropsNames
));
131 if(LENGTHOF(derivedCorePropsIndex
)!=LENGTHOF(derivedCorePropsNames
)) {
132 errln("error in ucdtest.cpp: LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)\n");
137 char backupPath
[256];
139 UErrorCode errorCode
=U_ZERO_ERROR
;
141 /* Look inside ICU_DATA first */
142 strcpy(newPath
, pathToDataDirectory());
143 strcat(newPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
145 // As a fallback, try to guess where the source data was located
146 // at the time ICU was built, and look there.
148 strcpy(backupPath
, U_TOPSRCDIR U_FILE_SEP_STRING
"data");
150 strcpy(backupPath
, loadTestData(errorCode
));
151 strcat(backupPath
, U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
".." U_FILE_SEP_STRING
"data");
153 strcat(backupPath
, U_FILE_SEP_STRING
);
154 strcat(backupPath
, "unidata" U_FILE_SEP_STRING
"DerivedCoreProperties.txt");
156 u_parseDelimitedFile(newPath
, ';', fields
, 2, derivedCorePropsLineFn
, this, &errorCode
);
158 if(errorCode
==U_FILE_ACCESS_ERROR
) {
159 errorCode
=U_ZERO_ERROR
;
160 u_parseDelimitedFile(backupPath
, ';', fields
, 2, derivedCorePropsLineFn
, this, &errorCode
);
162 if(U_FAILURE(errorCode
)) {
163 errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode
));
167 // now we have all derived core properties in the UnicodeSets
168 // run them all through the API
169 int32_t rangeCount
, range
;
172 int32_t noErrors
= 0;
174 // test all TRUE properties
175 for(i
=0; i
<LENGTHOF(derivedCorePropsNames
); ++i
) {
176 rangeCount
=derivedCoreProps
[i
].getRangeCount();
177 for(range
=0; range
<rangeCount
; ++range
) {
178 start
=derivedCoreProps
[i
].getRangeStart(range
);
179 end
=derivedCoreProps
[i
].getRangeEnd(range
);
180 for(; start
<=end
; ++start
) {
181 if(!u_hasBinaryProperty(start
, derivedCorePropsIndex
[i
])) {
182 errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==FALSE is wrong\n", start
, derivedCorePropsNames
[i
]);
183 if(noErrors
++ > 100) {
184 errln("Too many errors, moving to the next test");
193 // invert all properties
194 for(i
=0; i
<LENGTHOF(derivedCorePropsNames
); ++i
) {
195 derivedCoreProps
[i
].complement();
198 // test all FALSE properties
199 for(i
=0; i
<LENGTHOF(derivedCorePropsNames
); ++i
) {
200 rangeCount
=derivedCoreProps
[i
].getRangeCount();
201 for(range
=0; range
<rangeCount
; ++range
) {
202 start
=derivedCoreProps
[i
].getRangeStart(range
);
203 end
=derivedCoreProps
[i
].getRangeEnd(range
);
204 for(; start
<=end
; ++start
) {
205 if(u_hasBinaryProperty(start
, derivedCorePropsIndex
[i
])) {
206 errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==TRUE is wrong\n", start
, derivedCorePropsNames
[i
]);
207 if(noErrors
++ > 100) {
208 errln("Too many errors, moving to the next test");