]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/ucdtest.cpp
ICU-6.2.22.tar.gz
[apple/icu.git] / icuSources / test / intltest / ucdtest.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2004, 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
17 UnicodeTest::UnicodeTest()
18 {
19 }
20
21 UnicodeTest::~UnicodeTest()
22 {
23 }
24
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
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_Base"
82 };
83
84 static const UProperty
85 derivedCorePropsIndex[]={
86 UCHAR_MATH,
87 UCHAR_ALPHABETIC,
88 UCHAR_LOWERCASE,
89 UCHAR_UPPERCASE,
90 UCHAR_ID_START,
91 UCHAR_ID_CONTINUE,
92 UCHAR_XID_START,
93 UCHAR_XID_CONTINUE,
94 UCHAR_DEFAULT_IGNORABLE_CODE_POINT,
95 UCHAR_GRAPHEME_EXTEND,
96 UCHAR_GRAPHEME_BASE
97 };
98
99 U_CFUNC void U_CALLCONV
100 derivedCorePropsLineFn(void *context,
101 char *fields[][2], int32_t /* fieldCount */,
102 UErrorCode *pErrorCode)
103 {
104 UnicodeTest *me=(UnicodeTest *)context;
105 uint32_t start, end;
106 int32_t i;
107
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]);
111 return;
112 }
113
114 /* parse derived binary property name, ignore unknown names */
115 i=getTokenIndex(derivedCorePropsNames, LENGTHOF(derivedCorePropsNames), fields[1][0]);
116 if(i<0) {
117 me->errln("UnicodeTest warning: unknown property name '%s' in \n", fields[1][0]);
118 return;
119 }
120
121 me->derivedCoreProps[i].add(start, end);
122 }
123
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));
129 return;
130 }
131 if(LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)) {
132 errln("error in ucdtest.cpp: LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)\n");
133 return;
134 }
135
136 char newPath[256];
137 char backupPath[256];
138 char *fields[2][2];
139 UErrorCode errorCode=U_ZERO_ERROR;
140
141 /* Look inside ICU_DATA first */
142 strcpy(newPath, pathToDataDirectory());
143 strcat(newPath, "unidata" U_FILE_SEP_STRING "DerivedCoreProperties.txt");
144
145 // As a fallback, try to guess where the source data was located
146 // at the time ICU was built, and look there.
147 # ifdef U_TOPSRCDIR
148 strcpy(backupPath, U_TOPSRCDIR U_FILE_SEP_STRING "data");
149 # else
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");
152 # endif
153 strcat(backupPath, U_FILE_SEP_STRING);
154 strcat(backupPath, "unidata" U_FILE_SEP_STRING "DerivedCoreProperties.txt");
155
156 u_parseDelimitedFile(newPath, ';', fields, 2, derivedCorePropsLineFn, this, &errorCode);
157
158 if(errorCode==U_FILE_ACCESS_ERROR) {
159 errorCode=U_ZERO_ERROR;
160 u_parseDelimitedFile(backupPath, ';', fields, 2, derivedCorePropsLineFn, this, &errorCode);
161 }
162 if(U_FAILURE(errorCode)) {
163 errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode));
164 return;
165 }
166
167 // now we have all derived core properties in the UnicodeSets
168 // run them all through the API
169 int32_t rangeCount, range;
170 uint32_t i;
171 UChar32 start, end;
172 int32_t noErrors = 0;
173
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");
185 break;
186 }
187 }
188 }
189 }
190 }
191
192 noErrors = 0;
193 // invert all properties
194 for(i=0; i<LENGTHOF(derivedCorePropsNames); ++i) {
195 derivedCoreProps[i].complement();
196 }
197
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");
209 break;
210 }
211 }
212 }
213 }
214 }
215 }