]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /******************************************************************** |
2 | * COPYRIGHT: | |
729e4ab9 | 3 | * Copyright (c) 1997-2010, International Business Machines Corporation and |
b75a7d8f A |
4 | * others. All Rights Reserved. |
5 | ********************************************************************/ | |
6 | ||
7 | #include "unicode/ucnv.h" | |
8 | #include "unicode/ucnv_err.h" | |
9 | ||
10 | #include "cintltst.h" | |
374ca955 | 11 | #include "ustr_cnv.h" |
729e4ab9 | 12 | #include <string.h> |
374ca955 | 13 | void TestDefaultConverterError(void); /* keep gcc happy */ |
729e4ab9 | 14 | void TestDefaultConverterSet(void); /* keep gcc happy */ |
b75a7d8f A |
15 | |
16 | ||
374ca955 A |
17 | /* This makes sure that a converter isn't leaked when an error is passed to |
18 | u_getDefaultConverter */ | |
19 | void TestDefaultConverterError(void) { | |
b75a7d8f | 20 | UErrorCode err = U_ZERO_ERROR; |
b75a7d8f | 21 | |
374ca955 A |
22 | /* Remove the default converter */ |
23 | ucnv_close(u_getDefaultConverter(&err)); | |
b75a7d8f | 24 | |
b75a7d8f | 25 | if (U_FAILURE(err)) { |
374ca955 A |
26 | log_err("Didn't expect a failure yet %s\n", myErrorName(err)); |
27 | return; | |
b75a7d8f A |
28 | } |
29 | ||
374ca955 A |
30 | /* Set to any radom error state */ |
31 | err = U_FILE_ACCESS_ERROR; | |
32 | if (u_getDefaultConverter(&err) != NULL) { | |
33 | log_err("Didn't expect to get a converter on a failure\n"); | |
b75a7d8f | 34 | } |
b75a7d8f A |
35 | } |
36 | ||
729e4ab9 A |
37 | /* Get the default converter. Copy its name. Put it back. */ |
38 | static void copyDefaultConverterName(char *out, UErrorCode *status) { | |
39 | UConverter *defConv; | |
40 | const char *itsName; | |
41 | out[0]=0; | |
42 | if(U_FAILURE(*status)) return; | |
43 | defConv = u_getDefaultConverter(status); | |
44 | /* get its name */ | |
45 | itsName = ucnv_getName(defConv, status); | |
46 | if(U_FAILURE(*status)) return; | |
47 | strcpy(out, itsName); | |
48 | /* put it back. */ | |
49 | u_releaseDefaultConverter(defConv); | |
50 | } | |
51 | ||
52 | /* | |
53 | Changing the default name may not affect the actual name from u_getDefaultConverter | |
54 | ( for example, if UTF-8 is the fixed converter ). | |
55 | But, if it does cause a change, that change should be reflected when the converter is | |
56 | set back. | |
57 | */ | |
58 | void TestDefaultConverterSet(void) { | |
59 | UErrorCode status = U_ZERO_ERROR; | |
60 | static char defaultName[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; | |
61 | static char nameBeforeSet[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; | |
62 | static char nameAfterSet[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; | |
63 | static char nameAfterRestore[UCNV_MAX_CONVERTER_NAME_LENGTH + 1]; | |
64 | static const char SET_TO[]="iso-8859-3"; | |
65 | strcpy(defaultName, ucnv_getDefaultName()); | |
66 | ||
67 | log_verbose("getDefaultName returned %s\n", defaultName); | |
68 | ||
69 | /* first, flush any extant converter */ | |
70 | u_flushDefaultConverter(); | |
71 | copyDefaultConverterName(nameBeforeSet, &status); | |
72 | log_verbose("name from u_getDefaultConverter() = %s\n", nameBeforeSet); | |
73 | u_flushDefaultConverter(); | |
74 | ucnv_setDefaultName(SET_TO); | |
75 | copyDefaultConverterName(nameAfterSet, &status); | |
76 | log_verbose("name from u_getDefaultConverter() after set to %s (%s) = %s\n", SET_TO, ucnv_getDefaultName(), nameAfterSet); | |
77 | ucnv_setDefaultName(defaultName); | |
78 | copyDefaultConverterName(nameAfterRestore, &status); | |
79 | log_verbose("name from u_getDefaultConverter() after restore = %s\n", nameAfterRestore); | |
80 | u_flushDefaultConverter(); | |
81 | ||
82 | if(U_FAILURE(status)) { | |
83 | log_err("Error in test: %s\n", u_errorName(status)); | |
84 | } else { | |
85 | if(!strcmp(nameBeforeSet, nameAfterSet)) { /* changing the default didn't affect. */ | |
86 | log_info("Skipping test: ucnv_setDefaultName() did not affect actual name of %s\n", nameBeforeSet); | |
87 | } else { | |
88 | if(strcmp(nameBeforeSet, nameAfterRestore)) { | |
89 | log_err("Error: u_getDefaultConverter() is still returning %s (expected %s) even though default converter was set back to %s (was %s)\n", nameAfterRestore, nameBeforeSet, defaultName , SET_TO); | |
90 | } else { | |
91 | log_verbose("Test passed. \n"); | |
92 | } | |
93 | } | |
94 | } | |
95 | } | |
b75a7d8f A |
96 | |
97 |