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