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