1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2010, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
9 #include "unicode/ucnv.h"
10 #include "unicode/ucnv_err.h"
15 void TestDefaultConverterError(void); /* keep gcc happy */
16 void TestDefaultConverterSet(void); /* keep gcc happy */
19 /* This makes sure that a converter isn't leaked when an error is passed to
20 u_getDefaultConverter */
21 void TestDefaultConverterError(void) {
22 UErrorCode err
= U_ZERO_ERROR
;
24 /* Remove the default converter */
25 ucnv_close(u_getDefaultConverter(&err
));
28 log_err("Didn't expect a failure yet %s\n", myErrorName(err
));
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");
39 /* Get the default converter. Copy its name. Put it back. */
40 static void copyDefaultConverterName(char *out
, UErrorCode
*status
) {
44 if(U_FAILURE(*status
)) return;
45 defConv
= u_getDefaultConverter(status
);
47 itsName
= ucnv_getName(defConv
, status
);
48 if(U_FAILURE(*status
)) return;
51 u_releaseDefaultConverter(defConv
);
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
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());
69 log_verbose("getDefaultName returned %s\n", defaultName
);
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();
84 if(U_FAILURE(status
)) {
85 log_err("Error in test: %s\n", u_errorName(status
));
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
);
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
);
93 log_verbose("Test passed. \n");