1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2006, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unicode/ustring.h"
8 #include "unicode/uchar.h"
9 #include "unicode/uniset.h"
10 #include "unicode/putil.h"
11 #include "cstring.h"
12 #include "uparse.h"
13 #include "ucdtest.h"
14
15 #define LENGTHOF(array) (sizeof(array)/sizeof(array[0]))
16
UnicodeTest()17 UnicodeTest::UnicodeTest()
18 {
19 }
20
~UnicodeTest()21 UnicodeTest::~UnicodeTest()
22 {
23 }
24
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)25 void UnicodeTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
26 {
27 if (exec) logln("TestSuite UnicodeTest: ");
28 switch (index) {
29 case 0: name = "TestAdditionalProperties"; if(exec) TestAdditionalProperties(); break;
30 default: name = ""; break; //needed to end loop
31 }
32 }
33
34 //====================================================
35 // private data used by the tests
36 //====================================================
37
38 // test DerivedCoreProperties.txt -------------------------------------------
39
40 // copied from genprops.c
41 static int32_t
getTokenIndex(const char * const tokens[],int32_t countTokens,const char * s)42 getTokenIndex(const char *const tokens[], int32_t countTokens, const char *s) {
43 const char *t, *z;
44 int32_t i, j;
45
46 s=u_skipWhitespace(s);
47 for(i=0; i<countTokens; ++i) {
48 t=tokens[i];
49 if(t!=NULL) {
50 for(j=0;; ++j) {
51 if(t[j]!=0) {
52 if(s[j]!=t[j]) {
53 break;
54 }
55 } else {
56 z=u_skipWhitespace(s+j);
57 if(*z==';' || *z==0) {
58 return i;
59 } else {
60 break;
61 }
62 }
63 }
64 }
65 }
66 return -1;
67 }
68
69 static const char *const
70 derivedCorePropsNames[]={
71 "Math",
72 "Alphabetic",
73 "Lowercase",
74 "Uppercase",
75 "ID_Start",
76 "ID_Continue",
77 "XID_Start",
78 "XID_Continue",
79 "Default_Ignorable_Code_Point",
80 "Grapheme_Extend",
81 "Grapheme_Link", /* Unicode 5 moves this property here from PropList.txt */
82 "Grapheme_Base"
83 };
84
85 static const UProperty
86 derivedCorePropsIndex[]={
87 UCHAR_MATH,
88 UCHAR_ALPHABETIC,
89 UCHAR_LOWERCASE,
90 UCHAR_UPPERCASE,
91 UCHAR_ID_START,
92 UCHAR_ID_CONTINUE,
93 UCHAR_XID_START,
94 UCHAR_XID_CONTINUE,
95 UCHAR_DEFAULT_IGNORABLE_CODE_POINT,
96 UCHAR_GRAPHEME_EXTEND,
97 UCHAR_GRAPHEME_LINK,
98 UCHAR_GRAPHEME_BASE
99 };
100
101 U_CFUNC void U_CALLCONV
derivedCorePropsLineFn(void * context,char * fields[][2],int32_t,UErrorCode * pErrorCode)102 derivedCorePropsLineFn(void *context,
103 char *fields[][2], int32_t /* fieldCount */,
104 UErrorCode *pErrorCode)
105 {
106 UnicodeTest *me=(UnicodeTest *)context;
107 uint32_t start, end;
108 int32_t i;
109
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]);
113 return;
114 }
115
116 /* parse derived binary property name, ignore unknown names */
117 i=getTokenIndex(derivedCorePropsNames, LENGTHOF(derivedCorePropsNames), fields[1][0]);
118 if(i<0) {
119 me->errln("UnicodeTest warning: unknown property name '%s' in \n", fields[1][0]);
120 return;
121 }
122
123 me->derivedCoreProps[i].add(start, end);
124 }
125
TestAdditionalProperties()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));
131 return;
132 }
133 if(LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)) {
134 errln("error in ucdtest.cpp: LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)\n");
135 return;
136 }
137
138 char newPath[256];
139 char backupPath[256];
140 char *fields[2][2];
141 UErrorCode errorCode=U_ZERO_ERROR;
142
143 /* Look inside ICU_DATA first */
144 strcpy(newPath, pathToDataDirectory());
145 strcat(newPath, "unidata" U_FILE_SEP_STRING "DerivedCoreProperties.txt");
146
147 // As a fallback, try to guess where the source data was located
148 // at the time ICU was built, and look there.
149 # ifdef U_TOPSRCDIR
150 strcpy(backupPath, U_TOPSRCDIR U_FILE_SEP_STRING "data");
151 # else
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");
154 # endif
155 strcat(backupPath, U_FILE_SEP_STRING);
156 strcat(backupPath, "unidata" U_FILE_SEP_STRING "DerivedCoreProperties.txt");
157
158 u_parseDelimitedFile(newPath, ';', fields, 2, derivedCorePropsLineFn, this, &errorCode);
159
160 if(errorCode==U_FILE_ACCESS_ERROR) {
161 errorCode=U_ZERO_ERROR;
162 u_parseDelimitedFile(backupPath, ';', fields, 2, derivedCorePropsLineFn, this, &errorCode);
163 }
164 if(U_FAILURE(errorCode)) {
165 errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode));
166 return;
167 }
168
169 // now we have all derived core properties in the UnicodeSets
170 // run them all through the API
171 int32_t rangeCount, range;
172 uint32_t i;
173 UChar32 start, end;
174 int32_t noErrors = 0;
175
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");
187 break;
188 }
189 }
190 }
191 }
192 }
193
194 noErrors = 0;
195 // invert all properties
196 for(i=0; i<LENGTHOF(derivedCorePropsNames); ++i) {
197 derivedCoreProps[i].complement();
198 }
199
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");
211 break;
212 }
213 }
214 }
215 }
216 }
217 }
218