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