2 *******************************************************************************
4 * Copyright (C) 1999-2007, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/utypes.h"
11 #include "unicode/uclean.h"
12 #include "unicode/uchar.h"
13 #include "unicode/unistr.h"
14 #include "unicode/uscript.h"
15 #include "unicode/putil.h"
16 #include "unicode/ctest.h"
18 #include "layout/LETypes.h"
19 #include "layout/LEScripts.h"
24 #include "xmlreader.h"
26 #include "xmlparser.h"
34 #define CH_COMMA 0x002C
36 static le_uint32
*getHexArray(const UnicodeString
&numbers
, int32_t &arraySize
)
41 while((offset
= numbers
.indexOf(CH_COMMA
, offset
+ 1)) >= 0) {
45 le_uint32
*array
= NEW_ARRAY(le_uint32
, arraySize
);
48 le_int32 start
= 0, end
= 0;
51 // trim leading whitespace
52 while(u_isUWhiteSpace(numbers
[start
])) {
56 while((end
= numbers
.indexOf(CH_COMMA
, start
)) >= 0) {
57 len
= numbers
.extract(start
, end
- start
, number
, ARRAY_SIZE(number
), US_INV
);
61 sscanf(number
, "%x", &array
[count
++]);
63 // trim whitespace following the comma
64 while(u_isUWhiteSpace(numbers
[start
])) {
69 // trim trailing whitespace
70 end
= numbers
.length();
71 while(u_isUWhiteSpace(numbers
[end
- 1])) {
75 len
= numbers
.extract(start
, end
- start
, number
, ARRAY_SIZE(number
), US_INV
);
77 sscanf(number
, "%x", &array
[count
]);
82 static float *getFloatArray(const UnicodeString
&numbers
, int32_t &arraySize
)
87 while((offset
= numbers
.indexOf(CH_COMMA
, offset
+ 1)) >= 0) {
91 float *array
= NEW_ARRAY(float, arraySize
);
94 le_int32 start
= 0, end
= 0;
97 // trim leading whitespace
98 while(u_isUWhiteSpace(numbers
[start
])) {
102 while((end
= numbers
.indexOf(CH_COMMA
, start
)) >= 0) {
103 len
= numbers
.extract(start
, end
- start
, number
, ARRAY_SIZE(number
), US_INV
);
107 sscanf(number
, "%f", &array
[count
++]);
109 // trim whiteapce following the comma
110 while(u_isUWhiteSpace(numbers
[start
])) {
115 while(u_isUWhiteSpace(numbers
[start
])) {
119 // trim trailing whitespace
120 end
= numbers
.length();
121 while(u_isUWhiteSpace(numbers
[end
- 1])) {
125 len
= numbers
.extract(start
, end
- start
, number
, ARRAY_SIZE(number
), US_INV
);
127 sscanf(number
, "%f", &array
[count
]);
133 void readTestFile(const char *testFilePath
, TestCaseCallback callback
)
135 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
136 UErrorCode status
= U_ZERO_ERROR
;
137 UXMLParser
*parser
= UXMLParser::createParser(status
);
138 UXMLElement
*root
= parser
->parseFile(testFilePath
, status
);
141 log_err("Could not open the test data file: %s\n", testFilePath
);
146 UnicodeString test_case
= UNICODE_STRING_SIMPLE("test-case");
147 UnicodeString test_text
= UNICODE_STRING_SIMPLE("test-text");
148 UnicodeString test_font
= UNICODE_STRING_SIMPLE("test-font");
149 UnicodeString result_glyphs
= UNICODE_STRING_SIMPLE("result-glyphs");
150 UnicodeString result_indices
= UNICODE_STRING_SIMPLE("result-indices");
151 UnicodeString result_positions
= UNICODE_STRING_SIMPLE("result-positions");
153 // test-case attributes
154 UnicodeString id_attr
= UNICODE_STRING_SIMPLE("id");
155 UnicodeString script_attr
= UNICODE_STRING_SIMPLE("script");
156 UnicodeString lang_attr
= UNICODE_STRING_SIMPLE("lang");
158 // test-font attributes
159 UnicodeString name_attr
= UNICODE_STRING_SIMPLE("name");
160 UnicodeString ver_attr
= UNICODE_STRING_SIMPLE("version");
161 UnicodeString cksum_attr
= UNICODE_STRING_SIMPLE("checksum");
163 const UXMLElement
*testCase
;
166 while((testCase
= root
->nextChildElement(tc
)) != NULL
) {
167 if (testCase
->getTagName().compare(test_case
) == 0) {
168 char *id
= getCString(testCase
->getAttribute(id_attr
));
169 char *script
= getCString(testCase
->getAttribute(script_attr
));
170 char *lang
= getCString(testCase
->getAttribute(lang_attr
));
171 char *fontName
= NULL
;
172 char *fontVer
= NULL
;
173 char *fontCksum
= NULL
;
174 const UXMLElement
*element
;
176 int32_t charCount
= 0;
177 int32_t typoFlags
= 3; // kerning + ligatures...
178 UScriptCode scriptCode
;
179 le_int32 languageCode
= -1;
180 UnicodeString text
, glyphs
, indices
, positions
;
181 int32_t glyphCount
= 0, indexCount
= 0, positionCount
= 0;
182 TestResult expected
= {0, NULL
, NULL
, NULL
};
184 uscript_getCode(script
, &scriptCode
, 1, &status
);
185 if (LE_FAILURE(status
)) {
186 log_err("invalid script name: %s.\n", script
);
191 languageCode
= getLanguageCode(lang
);
193 if (languageCode
< 0) {
194 log_err("invalid language name: %s.\n", lang
);
199 while((element
= testCase
->nextChildElement(ec
)) != NULL
) {
200 UnicodeString tag
= element
->getTagName();
202 // TODO: make sure that each element is only used once.
203 if (tag
.compare(test_font
) == 0) {
204 fontName
= getCString(element
->getAttribute(name_attr
));
205 fontVer
= getCString(element
->getAttribute(ver_attr
));
206 fontCksum
= getCString(element
->getAttribute(cksum_attr
));
208 } else if (tag
.compare(test_text
) == 0) {
209 text
= element
->getText(TRUE
);
210 charCount
= text
.length();
211 } else if (tag
.compare(result_glyphs
) == 0) {
212 glyphs
= element
->getText(TRUE
);
213 } else if (tag
.compare(result_indices
) == 0) {
214 indices
= element
->getText(TRUE
);
215 } else if (tag
.compare(result_positions
) == 0) {
216 positions
= element
->getText(TRUE
);
219 char *cTag
= getCString(&tag
);
221 log_info("Test %s: unknown element with tag \"%s\"\n", id
, cTag
);
226 expected
.glyphs
= (LEGlyphID
*) getHexArray(glyphs
, glyphCount
);
227 expected
.indices
= (le_int32
*) getHexArray(indices
, indexCount
);
228 expected
.positions
= getFloatArray(positions
, positionCount
);
230 expected
.glyphCount
= glyphCount
;
232 if (glyphCount
< charCount
|| indexCount
!= glyphCount
|| positionCount
< glyphCount
* 2 + 2) {
233 log_err("Test %s: inconsistent input data: charCount = %d, glyphCount = %d, indexCount = %d, positionCount = %d\n",
234 id
, charCount
, glyphCount
, indexCount
, positionCount
);
238 (*callback
)(id
, fontName
, fontVer
, fontCksum
, scriptCode
, languageCode
, text
.getBuffer(), charCount
, &expected
);
241 DELETE_ARRAY(expected
.positions
);
242 DELETE_ARRAY(expected
.indices
);
243 DELETE_ARRAY(expected
.glyphs
);
246 freeCString(fontCksum
);
247 freeCString(fontVer
);
248 freeCString(fontName
);