]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/iotest/trnstst.c
b82e79df4fc263a832a01352c6b0f6b061880563
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2005-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * created by: George Rhoten
14 #include "unicode/ustdio.h"
15 #include "unicode/utrans.h"
16 #include "unicode/ustring.h"
18 static void TestTranslitOps(void)
20 #if !UCONFIG_NO_TRANSLITERATION
22 UErrorCode err
= U_ZERO_ERROR
;
23 UTransliterator
*a
= NULL
, *b
= NULL
, *c
= NULL
;
25 log_verbose("opening a transliterator and UFILE for testing\n");
27 f
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", NULL
);
30 log_err("Couldn't open test file for writing\n");
34 a
= utrans_open("Latin-Greek", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
37 log_err("Error opening transliterator %s\n", u_errorName(err
));
43 log_verbose("setting a transliterator\n");
44 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
47 log_err("Error setting transliterator %s\n", u_errorName(err
));
54 log_err("Error, a transliterator was already set!\n");
57 b
= u_fsettransliterator(NULL
, U_WRITE
, a
, &err
);
58 if(err
!= U_ILLEGAL_ARGUMENT_ERROR
)
60 log_err("Error setting transliterator on NULL file err=%s\n", u_errorName(err
));
65 log_err("Error getting the same transliterator was not returned on NULL file\n");
68 err
= U_FILE_ACCESS_ERROR
;
69 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
70 if(err
!= U_FILE_ACCESS_ERROR
)
72 log_err("Error setting transliterator on error status err=%s\n", u_errorName(err
));
77 log_err("Error getting the same transliterator on error status\n");
82 log_verbose("un-setting transliterator (setting to null)\n");
83 c
= u_fsettransliterator(f
, U_WRITE
, NULL
, &err
);
86 log_err("Err setting transliterator %s\n", u_errorName(err
));
93 log_err("Err, transliterator that came back was not the original one.\n");
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
)
100 log_err("Should have U_UNSUPPORTED_ERROR setting Read transliterator but got %s - REVISIT AND UPDATE TEST\n", u_errorName(err
));
106 log_verbose("Got %s error (expected) setting READ transliterator.\n", u_errorName(err
));
116 static void TestTranslitFileOut(void)
118 #if !UCONFIG_NO_FORMATTING
119 #if !UCONFIG_NO_TRANSLITERATION
121 UErrorCode err
= U_ZERO_ERROR
;
122 UTransliterator
*a
= NULL
, *b
= NULL
, *c
= NULL
;
124 UChar compare
[] = { 0xfeff, 0x03a3, 0x03c4, 0x03b5, 0x03c6, 0x1f00, 0x03bd, 0x03bf, 0x03c2, 0x043C, 0x0000 };
128 log_verbose("opening a transliterator and UFILE for testing\n");
130 f
= u_fopen(STANDARD_TEST_FILE
, "w", "en_US_POSIX", "utf-16");
133 log_err("Couldn't open test file for writing\n");
137 a
= utrans_open("Latin-Greek", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
140 log_err("Err opening transliterator %s\n", u_errorName(err
));
145 log_verbose("setting a transliterator\n");
146 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
149 log_err("Err setting transliterator %s\n", u_errorName(err
));
156 log_err("Err, a transliterator was already set!\n");
159 u_fprintf(f
, "Stephanos");
161 c
= utrans_open("Latin-Cyrillic", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
164 log_err("Err opening transliterator %s\n", u_errorName(err
));
169 log_verbose("setting a transliterator\n");
170 b
= u_fsettransliterator(f
, U_WRITE
, c
, &err
);
173 log_err("Err setting transliterator %s\n", u_errorName(err
));
180 log_err("Error: a different transliterator was returned!\n");
188 log_verbose("Re reading test file to verify transliteration\n");
189 infile
= fopen(STANDARD_TEST_FILE
, "rb");
192 log_err("Couldn't reopen test file\n");
196 len
= (int)fread(ubuf
, sizeof(UChar
), u_strlen(compare
), infile
);
197 log_verbose("Read %d UChars\n", len
);
198 if(len
!= u_strlen(compare
))
200 log_err("Wanted %d UChars from file, got %d\n", u_strlen(compare
), len
);
204 if(u_strlen(compare
) != u_strlen(ubuf
))
206 log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare
), len
);
209 if(u_strcmp(compare
, ubuf
))
211 log_err("Read string doesn't match expected.\n");
215 log_verbose("Read string matches expected.\n");
223 static void TestTranslitStringOut(void)
225 #if !UCONFIG_NO_FORMATTING
226 #if !UCONFIG_NO_TRANSLITERATION
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 };
233 log_verbose("opening a transliterator and UFILE for testing\n");
235 f
= u_fstropen(ubuf
, UPRV_LENGTHOF(ubuf
), "en_US_POSIX");
238 log_err("Couldn't open test file for writing\n");
242 a
= utrans_open("Latin-Greek", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
245 log_err("Err opening transliterator %s\n", u_errorName(err
));
250 log_verbose("setting a transliterator\n");
251 b
= u_fsettransliterator(f
, U_WRITE
, a
, &err
);
254 log_err("Err setting transliterator %s\n", u_errorName(err
));
261 log_err("Err, a transliterator was already set!\n");
264 u_fprintf(f
, "Stephanos");
266 c
= utrans_open("Latin-Cyrillic", UTRANS_FORWARD
, NULL
, -1, NULL
, &err
);
269 log_err("Err opening transliterator %s\n", u_errorName(err
));
274 log_verbose("setting a transliterator\n");
275 b
= u_fsettransliterator(f
, U_WRITE
, c
, &err
);
278 log_err("Err setting transliterator %s\n", u_errorName(err
));
285 log_err("Error: a different transliterator was returned!\n");
293 if(u_strlen(compare
) != u_strlen(ubuf
))
295 log_err("Wanted %d UChars from file, but u_strlen() returns %d\n", u_strlen(compare
), u_strlen(ubuf
));
298 if(u_strcmp(compare
, ubuf
))
300 log_err("Read string doesn't match expected.\n");
304 log_verbose("Read string matches expected.\n");
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");