sys_ioctl: Simplify.
[gnulib.git] / tests / test-unicodeio.c
blobfa28e98e66dc79f3d6015c1d7654229f0b553185
1 /* Tests for Unicode character output.
3 Copyright (C) 2020 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 "macros.h"
30 #define TEST_CODE 0x2022
31 #define TEST_CODE_AS_UTF8 "\xe2\x80\xa2"
32 #define TEST_CODE_AS_GB18030 "\x81\x36\xa6\x31"
34 static char result[64];
36 static long
37 success_callback (const char *buf, size_t buflen, void *callback_arg)
39 memcpy (result, buf, buflen);
40 result[buflen] = '\0';
41 return 42;
44 static long
45 failure_callback (unsigned int code, const char *msg, void *callback_arg)
47 ASSERT (code == TEST_CODE);
48 strcpy (result, ".");
49 return 55;
52 int
53 main (int argc, char *argv[])
55 /* configure should already have checked that the locale is supported. */
56 if (setlocale (LC_ALL, "") == NULL)
57 return 1;
59 switch (unicode_to_mb (TEST_CODE, success_callback, failure_callback, NULL))
61 case 42:
62 if (argc > 1)
63 switch (argv[1][0])
65 case '1': /* On some platforms, the "C" locale has UTF-8 encoding. */
66 case '2':
67 ASSERT (strcmp (result, TEST_CODE_AS_UTF8) == 0);
68 break;
69 case '3':
70 ASSERT (strcmp (result, TEST_CODE_AS_GB18030) == 0);
71 break;
73 break;
74 case 55:
75 ASSERT (strcmp (result, ".") == 0);
76 break;
77 default:
78 ASSERT (0);
81 return 0;