Debian Codegraph @

test/keysym-unicode.c

raw
1 /*
2 * Copyright © 2025 Pierre Le Marre <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5 #include "config.h"
6
7 #include "test.h"
8
9 #ifdef _WIN32
10
11 int
12 main(void)
13 {
14 test_init();
15 return SKIP_TEST;
16 }
17
18 #else
19
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/wait.h>
25
26 #include "xkbcommon/xkbcommon-keysyms.h"
27 #include "xkbcommon/xkbcommon.h"
28
29 #include "test.h"
30 #include "utils.h"
31 #include "src/keysym.h"
32
33 static void
34 test_unicode_keysyms_consistency(uint32_t start, uint32_t end)
35 {
36 static_assert(XKB_KEYSYM_NAME_MAX_SIZE > XKB_KEYSYM_UTF8_MAX_SIZE,
37 "Buffer too small");
38 char buffer[XKB_KEYSYM_NAME_MAX_SIZE] = {0};
39 char utf8[XKB_KEYSYM_UTF8_MAX_SIZE] = {0};
40 for (uint32_t cp = start; cp <= end; cp++) {
41 xkb_keysym_t unicode = XKB_KEYSYM_UNICODE_OFFSET + cp;
42 xkb_keysym_t canonical = xkb_utf32_to_keysym(cp);
43 int count = xkb_keysym_get_name(unicode, buffer, sizeof(buffer));
44 assert(count > 0);
45 if (cp == 0 || is_surrogate(cp)) {
46 /*
47 * Invalid code points
48 */
49
50 /* No conversion from code point */
51 assert(canonical == XKB_KEY_NoSymbol);
52 /* No conversion to code point */
53 assert(xkb_keysym_to_utf32(unicode) == 0);
54 char *end_ptr = NULL;
55 if (cp == 0) {
56 /* Corresponding name hexadecimal format, unchanged */
57 assert(strtoull(buffer, &end_ptr, 16) == unicode);
58 } else {
59 /* Unicode notation */
60 assert(count == 5 && buffer[0] == 'U');
61 assert(strtoull(buffer + 1, &end_ptr, 16) == cp);
62 }
63 assert(*end_ptr == '\0');
64 /* Roundtrip hexadecimal name */
65 xkb_keysym_t ks = xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS);
66 assert(ks == unicode);
67 /* Check Unicode format */
68 snprintf(buffer, sizeof(buffer), "U%"PRIX32, cp);
69 ks = xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS);
70 assert((cp == 0 && ks == XKB_KEY_NoSymbol) ^
71 (is_surrogate(cp) && ks == unicode));
72 /* Cannot convert to UTF-8 */
73 count = xkb_keysym_to_utf8(unicode, buffer, sizeof(buffer));
74 assert(count == 0);
75 } else {
76 /*
77 * Valid code points
78 */
79
80 /* Canonical keysym may be different but is set */
81 assert_printf((canonical == unicode) ^
82 ((0x20 <= cp && cp <= 0x100 && cp != 0x7f && canonical == cp) ||
83 (canonical != unicode &&
84 canonical != XKB_KEY_NoSymbol &&
85 (canonical != cp || canonical == XKB_KEY_EuroSign))),
86 "Code point: U+%04"PRIX32", Unicode: %#"PRIx32", "
87 "canonical: %#"PRIx32"\n",
88 cp, unicode, canonical);
89 /* Conversion to code point has the same expected result */
90 assert(xkb_keysym_to_utf32(unicode) == cp);
91 assert(xkb_keysym_to_utf32(canonical) == cp); /* roundtrip */
92 /* Check name roundtrip */
93 xkb_keysym_t ks = xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS);
94 assert((unicode != canonical && ks == canonical) ^ (ks == unicode));
95 /* Can use Unicode format */
96 if (buffer[0] == 'U' && count > 4 && is_digit(buffer[1])) {
97 /* Name already a Unicode notation: skip.
98 * Note that the heuristic may fail to detect a Unicode notation,
99 * but it is only meant to make the overall test faster. */
100 } else {
101 snprintf(buffer, sizeof(buffer), "U%"PRIX32, cp);
102 ks = xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS);
103 assert((unicode != canonical && ks == canonical) ^ (ks == unicode));
104 }
105 /* Roundtrip: numeric hexadecimal format for Unicode keysym */
106 assert(snprintf(buffer, sizeof(buffer), "%#"PRIx32, unicode) == 9);
107 ks = xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS);
108 assert(ks == unicode);
109 /* Can convert to UTF-8 (Unicode keysym) */
110 count = xkb_keysym_to_utf8(unicode, buffer, sizeof(buffer));
111 assert(count > 0);
112 if (canonical != unicode) {
113 /* Canonical keysym convert to same UTF-8 */
114 const int count2 = xkb_keysym_to_utf8(canonical, utf8, sizeof(utf8));
115 assert(count2 == count);
116 assert(strcmp(buffer, utf8) == 0);
117 /* Roundtrip: numeric hexadecimal format for canonical keysym */
118 count = xkb_keysym_get_name(canonical, buffer, sizeof(buffer));
119 assert(count > 0);
120 assert(xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS) == canonical);
121 assert(snprintf(buffer, sizeof(buffer), "%#"PRIx32, canonical) > 2);
122 assert(xkb_keysym_from_name(buffer, XKB_KEYSYM_NO_FLAGS) == canonical);
123 }
124 }
125 }
126 }
127
128 int
129 main(int argc, char *argv[])
130 {
131 test_init();
132
133 unsigned long int NUM_PROCESSES = 0;
134 if (argc > 1) {
135 NUM_PROCESSES = strtoul(argv[1], NULL, 10);
136 }
137 if (NUM_PROCESSES == 0 || NUM_PROCESSES > 32)
138 NUM_PROCESSES = 4;
139
140 const uint32_t max_codepoint = 0x10ffff;
141 const uint32_t chunk = max_codepoint / NUM_PROCESSES;
142 pid_t pids[NUM_PROCESSES];
143
144 for (unsigned long int i = 0; i < NUM_PROCESSES; i++) {
145 pid_t pid = fork();
146 if (pid == 0) {
147 /* Child */
148 const uint32_t start = i * chunk;
149 const uint32_t end = (i == NUM_PROCESSES - 1)
150 ? max_codepoint
151 : start + chunk;
152 test_unicode_keysyms_consistency(start, end);
153 exit(EXIT_SUCCESS);
154 } else if (pid > 0) {
155 /* Parent */
156 pids[i] = pid;
157 } else {
158 perror("fork");
159 return TEST_SETUP_FAILURE;
160 }
161 }
162
163 /* Wait for children */
164 int exit_code = EXIT_SUCCESS;
165 for (unsigned long int i = 0; i < NUM_PROCESSES; i++) {
166 int status;
167 if (waitpid(pids[i], &status, 0) == -1) {
168 perror("waitpid");
169 exit_code = EXIT_FAILURE;
170 } else if (WIFEXITED(status)) {
171 int code = WEXITSTATUS(status);
172 if (code != EXIT_SUCCESS) {
173 fprintf(stderr,
174 "Child PID %d exited with code %d\n",
175 pids[i], code);
176 exit_code = EXIT_FAILURE;
177 }
178 } else if (WIFSIGNALED(status)) {
179 fprintf(stderr,
180 "Child PID %d terminated by signal %d\n",
181 pids[i], WTERMSIG(status));
182 exit_code = EXIT_FAILURE;
183 }
184 }
185
186 return exit_code;
187 }
188 #endif