]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
3 | * Copyright (c) 1997-2003, 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 "cstring.h" | |
11 | #include "uparse.h" | |
12 | #include "ucdtest.h" | |
13 | ||
14 | #define LENGTHOF(array) (sizeof(array)/sizeof(array[0])) | |
15 | ||
16 | UnicodeTest::UnicodeTest() | |
17 | { | |
18 | } | |
19 | ||
20 | UnicodeTest::~UnicodeTest() | |
21 | { | |
22 | } | |
23 | ||
24 | void UnicodeTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) | |
25 | { | |
26 | if (exec) logln("TestSuite UnicodeTest: "); | |
27 | switch (index) { | |
28 | case 0: name = "TestAdditionalProperties"; if(exec) TestAdditionalProperties(); break; | |
29 | default: name = ""; break; //needed to end loop | |
30 | } | |
31 | } | |
32 | ||
33 | //==================================================== | |
34 | // private data used by the tests | |
35 | //==================================================== | |
36 | ||
37 | // test DerivedCoreProperties.txt ------------------------------------------- | |
38 | ||
39 | // copied from genprops.c | |
40 | static int32_t | |
41 | getTokenIndex(const char *const tokens[], int32_t countTokens, const char *s) { | |
42 | const char *t, *z; | |
43 | int32_t i, j; | |
44 | ||
45 | s=u_skipWhitespace(s); | |
46 | for(i=0; i<countTokens; ++i) { | |
47 | t=tokens[i]; | |
48 | if(t!=NULL) { | |
49 | for(j=0;; ++j) { | |
50 | if(t[j]!=0) { | |
51 | if(s[j]!=t[j]) { | |
52 | break; | |
53 | } | |
54 | } else { | |
55 | z=u_skipWhitespace(s+j); | |
56 | if(*z==';' || *z==0) { | |
57 | return i; | |
58 | } else { | |
59 | break; | |
60 | } | |
61 | } | |
62 | } | |
63 | } | |
64 | } | |
65 | return -1; | |
66 | } | |
67 | ||
68 | static const char *const | |
69 | derivedCorePropsNames[]={ | |
70 | "Math", | |
71 | "Alphabetic", | |
72 | "Lowercase", | |
73 | "Uppercase", | |
74 | "ID_Start", | |
75 | "ID_Continue", | |
76 | "XID_Start", | |
77 | "XID_Continue", | |
78 | "Default_Ignorable_Code_Point", | |
79 | "Grapheme_Extend", | |
80 | "Grapheme_Base" | |
81 | }; | |
82 | ||
83 | static const UProperty | |
84 | derivedCorePropsIndex[]={ | |
85 | UCHAR_MATH, | |
86 | UCHAR_ALPHABETIC, | |
87 | UCHAR_LOWERCASE, | |
88 | UCHAR_UPPERCASE, | |
89 | UCHAR_ID_START, | |
90 | UCHAR_ID_CONTINUE, | |
91 | UCHAR_XID_START, | |
92 | UCHAR_XID_CONTINUE, | |
93 | UCHAR_DEFAULT_IGNORABLE_CODE_POINT, | |
94 | UCHAR_GRAPHEME_EXTEND, | |
95 | UCHAR_GRAPHEME_BASE | |
96 | }; | |
97 | ||
98 | U_CAPI void U_CALLCONV | |
99 | derivedCorePropsLineFn(void *context, | |
100 | char *fields[][2], int32_t /* fieldCount */, | |
101 | UErrorCode *pErrorCode) | |
102 | { | |
103 | UnicodeTest *me=(UnicodeTest *)context; | |
104 | uint32_t start, end; | |
105 | int32_t i; | |
106 | ||
107 | u_parseCodePointRange(fields[0][0], &start, &end, pErrorCode); | |
108 | if(U_FAILURE(*pErrorCode)) { | |
109 | me->errln("UnicodeTest: syntax error in DerivedCoreProperties.txt field 0 at %s\n", fields[0][0]); | |
110 | return; | |
111 | } | |
112 | ||
113 | /* parse derived binary property name, ignore unknown names */ | |
114 | i=getTokenIndex(derivedCorePropsNames, LENGTHOF(derivedCorePropsNames), fields[1][0]); | |
115 | if(i<0) { | |
116 | me->errln("UnicodeTest warning: unknown property name '%s' in \n", fields[1][0]); | |
117 | return; | |
118 | } | |
119 | ||
120 | me->derivedCoreProps[i].add(start, end); | |
121 | } | |
122 | ||
123 | void UnicodeTest::TestAdditionalProperties() { | |
124 | // test DerivedCoreProperties.txt | |
125 | if(LENGTHOF(derivedCoreProps)<LENGTHOF(derivedCorePropsNames)) { | |
126 | errln("error: UnicodeTest::derivedCoreProps[] too short, need at least %d UnicodeSets\n", | |
127 | LENGTHOF(derivedCorePropsNames)); | |
128 | return; | |
129 | } | |
130 | if(LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)) { | |
131 | errln("error in ucdtest.cpp: LENGTHOF(derivedCorePropsIndex)!=LENGTHOF(derivedCorePropsNames)\n"); | |
132 | return; | |
133 | } | |
134 | ||
135 | char newPath[256]; | |
136 | char backupPath[256]; | |
137 | char *fields[2][2]; | |
138 | int32_t length; | |
139 | UErrorCode errorCode=U_ZERO_ERROR; | |
140 | ||
141 | /* Look inside ICU_DATA first */ | |
142 | strcpy(newPath, u_getDataDirectory()); | |
143 | ||
144 | // remove trailing "out/" | |
145 | length=uprv_strlen(newPath); | |
146 | if(length>=4 && uprv_strcmp(newPath+length-4, "out" U_FILE_SEP_STRING)==0) { | |
147 | newPath[length-4]=0; | |
148 | } | |
149 | ||
150 | strcat(newPath, "unidata" U_FILE_SEP_STRING "DerivedCoreProperties.txt"); | |
151 | ||
152 | // As a fallback, try to guess where the source data was located | |
153 | // at the time ICU was built, and look there. | |
154 | # ifdef U_TOPSRCDIR | |
155 | strcpy(backupPath, U_TOPSRCDIR U_FILE_SEP_STRING "data"); | |
156 | # else | |
157 | strcpy(backupPath, loadTestData(errorCode)); | |
158 | strcat(backupPath, U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING ".." U_FILE_SEP_STRING "data"); | |
159 | # endif | |
160 | strcat(backupPath, U_FILE_SEP_STRING); | |
161 | strcat(backupPath, "unidata" U_FILE_SEP_STRING "DerivedCoreProperties.txt"); | |
162 | ||
163 | u_parseDelimitedFile(newPath, ';', fields, 2, derivedCorePropsLineFn, this, &errorCode); | |
164 | ||
165 | if(errorCode==U_FILE_ACCESS_ERROR) { | |
166 | errorCode=U_ZERO_ERROR; | |
167 | u_parseDelimitedFile(backupPath, ';', fields, 2, derivedCorePropsLineFn, this, &errorCode); | |
168 | } | |
169 | if(U_FAILURE(errorCode)) { | |
170 | errln("error parsing DerivedCoreProperties.txt: %s\n", u_errorName(errorCode)); | |
171 | return; | |
172 | } | |
173 | ||
174 | // now we have all derived core properties in the UnicodeSets | |
175 | // run them all through the API | |
176 | int32_t rangeCount, range; | |
177 | uint32_t i; | |
178 | UChar32 start, end; | |
179 | int32_t noErrors = 0; | |
180 | ||
181 | // test all TRUE properties | |
182 | for(i=0; i<LENGTHOF(derivedCorePropsNames); ++i) { | |
183 | rangeCount=derivedCoreProps[i].getRangeCount(); | |
184 | for(range=0; range<rangeCount; ++range) { | |
185 | start=derivedCoreProps[i].getRangeStart(range); | |
186 | end=derivedCoreProps[i].getRangeEnd(range); | |
187 | for(; start<=end; ++start) { | |
188 | if(!u_hasBinaryProperty(start, derivedCorePropsIndex[i])) { | |
189 | errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==FALSE is wrong\n", start, derivedCorePropsNames[i]); | |
190 | if(noErrors++ > 100) { | |
191 | errln("Too many errors, moving to the next test"); | |
192 | break; | |
193 | } | |
194 | } | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
199 | noErrors = 0; | |
200 | // invert all properties | |
201 | for(i=0; i<LENGTHOF(derivedCorePropsNames); ++i) { | |
202 | derivedCoreProps[i].complement(); | |
203 | } | |
204 | ||
205 | // test all FALSE properties | |
206 | for(i=0; i<LENGTHOF(derivedCorePropsNames); ++i) { | |
207 | rangeCount=derivedCoreProps[i].getRangeCount(); | |
208 | for(range=0; range<rangeCount; ++range) { | |
209 | start=derivedCoreProps[i].getRangeStart(range); | |
210 | end=derivedCoreProps[i].getRangeEnd(range); | |
211 | for(; start<=end; ++start) { | |
212 | if(u_hasBinaryProperty(start, derivedCorePropsIndex[i])) { | |
213 | errln("UnicodeTest error: u_hasBinaryProperty(U+%04lx, %s)==TRUE is wrong\n", start, derivedCorePropsNames[i]); | |
214 | if(noErrors++ > 100) { | |
215 | errln("Too many errors, moving to the next test"); | |
216 | break; | |
217 | } | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | } |