]>
Commit | Line | Data |
---|---|---|
73c04bcf A |
1 | /* |
2 | ********************************************************************** | |
2ca993e8 | 3 | * Copyright (C) 2005-2016, International Business Machines |
73c04bcf A |
4 | * Corporation and others. All Rights Reserved. |
5 | ********************************************************************** | |
6 | * file name: strtst.c | |
7 | * created by: George Rhoten | |
8 | */ | |
9 | ||
2ca993e8 | 10 | #include "cmemory.h" |
73c04bcf A |
11 | #include "iotest.h" |
12 | #include "unicode/ustdio.h" | |
13 | #include "unicode/utrans.h" | |
14 | #include "unicode/ustring.h" | |
15 | ||
16 | static void TestTranslitOps(void) | |
17 | { | |
18 | #if !UCONFIG_NO_TRANSLITERATION | |
19 | UFILE *f; | |
20 | UErrorCode err = U_ZERO_ERROR; | |
21 | UTransliterator *a = NULL, *b = NULL, *c = NULL; | |
22 | ||
23 | log_verbose("opening a transliterator and UFILE for testing\n"); | |
24 | ||
25 | f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", NULL); | |
26 | if(f == NULL) | |
27 | { | |
28 | log_err("Couldn't open test file for writing\n"); | |
29 | return; | |
30 | } | |
31 | ||
32 | a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err); | |
33 | if(U_FAILURE(err)) | |
34 | { | |
35 | log_err("Error opening transliterator %s\n", u_errorName(err)); | |
36 | u_fclose(f); | |
37 | return; | |
38 | } | |
39 | ||
40 | ||
41 | log_verbose("setting a transliterator\n"); | |
42 | b = u_fsettransliterator(f, U_WRITE, a, &err); | |
43 | if(U_FAILURE(err)) | |
44 | { | |
45 | log_err("Error setting transliterator %s\n", u_errorName(err)); | |
46 | u_fclose(f); | |
47 | return; | |
48 | } | |
49 | ||
50 | if(b != NULL) | |
51 | { | |
52 | log_err("Error, a transliterator was already set!\n"); | |
53 | } | |
54 | ||
55 | b = u_fsettransliterator(NULL, U_WRITE, a, &err); | |
56 | if(err != U_ILLEGAL_ARGUMENT_ERROR) | |
57 | { | |
58 | log_err("Error setting transliterator on NULL file err=%s\n", u_errorName(err)); | |
59 | } | |
60 | ||
61 | if(b != a) | |
62 | { | |
63 | log_err("Error getting the same transliterator was not returned on NULL file\n"); | |
64 | } | |
65 | ||
66 | err = U_FILE_ACCESS_ERROR; | |
67 | b = u_fsettransliterator(f, U_WRITE, a, &err); | |
68 | if(err != U_FILE_ACCESS_ERROR) | |
69 | { | |
70 | log_err("Error setting transliterator on error status err=%s\n", u_errorName(err)); | |
71 | } | |
72 | ||
73 | if(b != a) | |
74 | { | |
75 | log_err("Error getting the same transliterator on error status\n"); | |
76 | } | |
77 | err = U_ZERO_ERROR; | |
78 | ||
79 | ||
80 | log_verbose("un-setting transliterator (setting to null)\n"); | |
81 | c = u_fsettransliterator(f, U_WRITE, NULL, &err); | |
82 | if(U_FAILURE(err)) | |
83 | { | |
84 | log_err("Err setting transliterator %s\n", u_errorName(err)); | |
85 | u_fclose(f); | |
86 | return; | |
87 | } | |
88 | ||
89 | if(c != a) | |
90 | { | |
91 | log_err("Err, transliterator that came back was not the original one.\n"); | |
92 | } | |
93 | ||
94 | log_verbose("Trying to set read transliterator (should fail)\n"); | |
95 | b = u_fsettransliterator(f, U_READ, NULL, &err); | |
96 | if(err != U_UNSUPPORTED_ERROR) | |
97 | { | |
98 | log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err)); | |
99 | u_fclose(f); | |
100 | return; | |
101 | } | |
102 | else | |
103 | { | |
104 | log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err)); | |
105 | err = U_ZERO_ERROR; | |
106 | } | |
107 | ||
108 | ||
109 | utrans_close(c); | |
110 | u_fclose(f); | |
111 | #endif | |
112 | } | |
113 | ||
114 | static void TestTranslitFileOut(void) | |
115 | { | |
116 | #if !UCONFIG_NO_FORMATTING | |
117 | #if !UCONFIG_NO_TRANSLITERATION | |
118 | UFILE *f; | |
119 | UErrorCode err = U_ZERO_ERROR; | |
120 | UTransliterator *a = NULL, *b = NULL, *c = NULL; | |
121 | FILE *infile; | |
122 | UChar compare[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x043C, 0x0000 }; | |
123 | UChar ubuf[256]; | |
124 | int len; | |
125 | ||
126 | log_verbose("opening a transliterator and UFILE for testing\n"); | |
127 | ||
128 | f = u_fopen(STANDARD_TEST_FILE, "w", "en_US_POSIX", "utf-16"); | |
129 | if(f == NULL) | |
130 | { | |
131 | log_err("Couldn't open test file for writing\n"); | |
132 | return; | |
133 | } | |
134 | ||
135 | a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err); | |
136 | if(U_FAILURE(err)) | |
137 | { | |
138 | log_err("Err opening transliterator %s\n", u_errorName(err)); | |
139 | u_fclose(f); | |
140 | return; | |
141 | } | |
142 | ||
143 | log_verbose("setting a transliterator\n"); | |
144 | b = u_fsettransliterator(f, U_WRITE, a, &err); | |
145 | if(U_FAILURE(err)) | |
146 | { | |
147 | log_err("Err setting transliterator %s\n", u_errorName(err)); | |
148 | u_fclose(f); | |
149 | return; | |
150 | } | |
151 | ||
152 | if(b != NULL) | |
153 | { | |
154 | log_err("Err, a transliterator was already set!\n"); | |
155 | } | |
156 | ||
157 | u_fprintf(f, "Stephanos"); | |
158 | ||
159 | c = utrans_open("Latin-Cyrillic", UTRANS_FORWARD, NULL, -1, NULL, &err); | |
160 | if(U_FAILURE(err)) | |
161 | { | |
162 | log_err("Err opening transliterator %s\n", u_errorName(err)); | |
163 | u_fclose(f); | |
164 | return; | |
165 | } | |
166 | ||
167 | log_verbose("setting a transliterator\n"); | |
168 | b = u_fsettransliterator(f, U_WRITE, c, &err); | |
169 | if(U_FAILURE(err)) | |
170 | { | |
171 | log_err("Err setting transliterator %s\n", u_errorName(err)); | |
172 | u_fclose(f); | |
173 | return; | |
174 | } | |
175 | ||
176 | if(b != a) | |
177 | { | |
178 | log_err("Error: a different transliterator was returned!\n"); | |
179 | } | |
180 | utrans_close(b); | |
181 | ||
182 | u_fprintf(f, "m"); | |
183 | ||
184 | u_fclose(f); | |
185 | ||
186 | log_verbose("Re reading test file to verify transliteration\n"); | |
187 | infile = fopen(STANDARD_TEST_FILE, "rb"); | |
188 | if(infile == NULL) | |
189 | { | |
190 | log_err("Couldn't reopen test file\n"); | |
191 | return; | |
192 | } | |
193 | ||
194 | len=fread(ubuf, sizeof(UChar), u_strlen(compare), infile); | |
195 | log_verbose("Read %d UChars\n", len); | |
196 | if(len != u_strlen(compare)) | |
197 | { | |
198 | log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare), len); | |
199 | } | |
200 | ubuf[len]=0; | |
201 | ||
202 | if(u_strlen(compare) != u_strlen(ubuf)) | |
203 | { | |
204 | log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), len); | |
205 | } | |
206 | ||
207 | if(u_strcmp(compare, ubuf)) | |
208 | { | |
209 | log_err("Read string doesn't match expected.\n"); | |
210 | } | |
211 | else | |
212 | { | |
213 | log_verbose("Read string matches expected.\n"); | |
214 | } | |
215 | ||
216 | fclose(infile); | |
217 | #endif | |
218 | #endif | |
219 | } | |
220 | ||
221 | static void TestTranslitStringOut(void) | |
222 | { | |
223 | #if !UCONFIG_NO_FORMATTING | |
224 | #if !UCONFIG_NO_TRANSLITERATION | |
225 | UFILE *f; | |
226 | UErrorCode err = U_ZERO_ERROR; | |
227 | UTransliterator *a = NULL, *b = NULL, *c = NULL; | |
228 | UChar compare[] = { 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x043C, 0x0000 }; | |
229 | UChar ubuf[256]; | |
230 | ||
231 | log_verbose("opening a transliterator and UFILE for testing\n"); | |
232 | ||
2ca993e8 | 233 | f = u_fstropen(ubuf, UPRV_LENGTHOF(ubuf), "en_US_POSIX"); |
73c04bcf A |
234 | if(f == NULL) |
235 | { | |
236 | log_err("Couldn't open test file for writing\n"); | |
237 | return; | |
238 | } | |
239 | ||
240 | a = utrans_open("Latin-Greek", UTRANS_FORWARD, NULL, -1, NULL, &err); | |
241 | if(U_FAILURE(err)) | |
242 | { | |
243 | log_err("Err opening transliterator %s\n", u_errorName(err)); | |
244 | u_fclose(f); | |
245 | return; | |
246 | } | |
247 | ||
248 | log_verbose("setting a transliterator\n"); | |
249 | b = u_fsettransliterator(f, U_WRITE, a, &err); | |
250 | if(U_FAILURE(err)) | |
251 | { | |
252 | log_err("Err setting transliterator %s\n", u_errorName(err)); | |
253 | u_fclose(f); | |
254 | return; | |
255 | } | |
256 | ||
257 | if(b != NULL) | |
258 | { | |
259 | log_err("Err, a transliterator was already set!\n"); | |
260 | } | |
261 | ||
262 | u_fprintf(f, "Stephanos"); | |
263 | ||
264 | c = utrans_open("Latin-Cyrillic", UTRANS_FORWARD, NULL, -1, NULL, &err); | |
265 | if(U_FAILURE(err)) | |
266 | { | |
267 | log_err("Err opening transliterator %s\n", u_errorName(err)); | |
268 | u_fclose(f); | |
269 | return; | |
270 | } | |
271 | ||
272 | log_verbose("setting a transliterator\n"); | |
273 | b = u_fsettransliterator(f, U_WRITE, c, &err); | |
274 | if(U_FAILURE(err)) | |
275 | { | |
276 | log_err("Err setting transliterator %s\n", u_errorName(err)); | |
277 | u_fclose(f); | |
278 | return; | |
279 | } | |
280 | ||
281 | if(b != a) | |
282 | { | |
283 | log_err("Error: a different transliterator was returned!\n"); | |
284 | } | |
285 | utrans_close(b); | |
286 | ||
287 | u_fprintf(f, "m"); | |
288 | ||
289 | u_fclose(f); | |
290 | ||
291 | if(u_strlen(compare) != u_strlen(ubuf)) | |
292 | { | |
293 | log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare), u_strlen(ubuf)); | |
294 | } | |
295 | ||
296 | if(u_strcmp(compare, ubuf)) | |
297 | { | |
298 | log_err("Read string doesn't match expected.\n"); | |
299 | } | |
300 | else | |
301 | { | |
302 | log_verbose("Read string matches expected.\n"); | |
303 | } | |
304 | #endif | |
305 | #endif | |
306 | } | |
307 | ||
308 | U_CFUNC void | |
309 | addTranslitTest(TestNode** root) { | |
310 | #if !UCONFIG_NO_TRANSLITERATION | |
311 | addTest(root, &TestTranslitOps, "translit/ops"); | |
312 | #if !UCONFIG_NO_FORMATTING | |
313 | addTest(root, &TestTranslitFileOut, "translit/fileOut"); | |
314 | addTest(root, &TestTranslitStringOut, "translit/stringOut"); | |
315 | #endif | |
316 | #endif | |
317 | } |