sethostname tests: Avoid test failure on Cygwin.
[gnulib.git] / tests / test-unicodeio.c
blobccb44756fc7850a53f34300470f9ba07e18fccef
1 /* Tests for Unicode character output.
3 Copyright (C) 2020-2024 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible, 2020. */
20 #include <config.h>
22 /* Specification. */
23 #include "unicodeio.h"
25 #include <locale.h>
26 #include <string.h>
28 #include "localcharset.h"
29 #include "macros.h"
31 #define TEST_CODE 0x2022
32 #define TEST_CODE_AS_UTF8 "\xe2\x80\xa2"
33 #define TEST_CODE_AS_GB18030 "\x81\x36\xa6\x31"
35 static char result[64];
37 static long
38 success_callback (const char *buf, size_t buflen, void *callback_arg)
40 memcpy (result, buf, buflen);
41 result[buflen] = '\0';
42 return 42;
45 static long
46 failure_callback (unsigned int code, const char *msg, void *callback_arg)
48 ASSERT (code == TEST_CODE);
49 strcpy (result, ".");
50 return 55;
53 int
54 main (int argc, char *argv[])
56 /* configure should already have checked that the locale is supported. */
57 if (setlocale (LC_ALL, "") == NULL)
58 return 1;
60 switch (unicode_to_mb (TEST_CODE, success_callback, failure_callback, NULL))
62 case 42:
63 if (argc > 1)
64 switch (argv[1][0])
66 case '1':
67 /* On some platforms, the "C" locale has UTF-8 encoding.
68 And on native Windows, the "C" locale may have an 8-bit encoding
69 such as CP1252, that contains the U+2022 character. */
71 const char *charset = locale_charset ();
72 if (strcmp (charset, "CP874") == 0
73 || strcmp (charset, "CP1250") == 0
74 || strcmp (charset, "CP1251") == 0
75 || strcmp (charset, "CP1252") == 0
76 || strcmp (charset, "CP1253") == 0
77 || strcmp (charset, "CP1254") == 0
78 || strcmp (charset, "CP1255") == 0
79 || strcmp (charset, "CP1256") == 0
80 || strcmp (charset, "CP1257") == 0
81 || strcmp (charset, "CP1258") == 0)
82 ASSERT (strcmp (result, "\x95") == 0);
83 else
84 ASSERT (strcmp (result, TEST_CODE_AS_UTF8) == 0);
86 break;
87 case '2':
88 ASSERT (strcmp (result, TEST_CODE_AS_UTF8) == 0);
89 break;
90 case '3':
91 ASSERT (strcmp (result, TEST_CODE_AS_GB18030) == 0);
92 break;
94 break;
95 case 55:
96 ASSERT (strcmp (result, ".") == 0);
97 break;
98 default:
99 ASSERT (0);
102 return test_exit_status;