2 * Copyright © 2009 Dan Nicholson
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
28 #include "keysym.h" /* For unexported is_lower/upper/keypad() */
31 test_string(const char *string, xkb_keysym_t expected)
35 keysym = xkb_keysym_from_name(string, 0);
37 fprintf(stderr, "Expected string %s -> %x\n", string, expected);
38 fprintf(stderr, "Received string %s -> %x\n\n", string, keysym);
40 return keysym == expected;
44 test_casestring(const char *string, xkb_keysym_t expected)
48 keysym = xkb_keysym_from_name(string, XKB_KEYSYM_CASE_INSENSITIVE);
50 fprintf(stderr, "Expected casestring %s -> %x\n", string, expected);
51 fprintf(stderr, "Received casestring %s -> %x\n\n", string, keysym);
53 return keysym == expected;
57 test_keysym(xkb_keysym_t keysym, const char *expected)
61 xkb_keysym_get_name(keysym, s, sizeof(s));
63 fprintf(stderr, "Expected keysym %#x -> %s\n", keysym, expected);
64 fprintf(stderr, "Received keysym %#x -> %s\n\n", keysym, s);
66 return streq(s, expected);
70 test_utf8(xkb_keysym_t keysym, const char *expected)
75 ret = xkb_keysym_to_utf8(keysym, s, sizeof(s));
79 assert(expected != NULL);
81 fprintf(stderr, "Expected keysym %#x -> %s (%u bytes)\n", keysym, expected,
82 (unsigned) strlen(expected));
83 fprintf(stderr, "Received keysym %#x -> %s (%u bytes)\n\n", keysym, s,
84 (unsigned) strlen(s));
86 return streq(s, expected);
90 test_github_issue_42(void)
92 // Verify we are not dependent on locale, Turkish-i problem in particular.
93 if (setlocale(LC_CTYPE, "tr_TR.UTF-8") == NULL) {
94 // The locale is not available, probably; skip.
98 assert(test_string("i", XKB_KEY_i));
99 assert(test_string("I", XKB_KEY_I));
100 assert(test_casestring("i", XKB_KEY_i));
101 assert(test_casestring("I", XKB_KEY_i));
102 assert(xkb_keysym_to_upper(XKB_KEY_i) == XKB_KEY_I);
103 assert(xkb_keysym_to_lower(XKB_KEY_I) == XKB_KEY_i);
105 setlocale(LC_CTYPE, "C");
109 get_keysym_name(xkb_keysym_t keysym, char *buffer, size_t size)
111 int name_length = xkb_keysym_get_name(keysym, buffer, size);
112 if (name_length < 0) {
113 snprintf(buffer, size, "(unknown: 0x%lx)", (unsigned long)keysym);
118 test_utf32_to_keysym(uint32_t ucs, xkb_keysym_t expected)
120 char expected_name[64];
121 char actual_name[64];
122 xkb_keysym_t actual = xkb_utf32_to_keysym(ucs);
123 get_keysym_name(expected, expected_name, 64);
124 get_keysym_name(actual, actual_name, 64);
126 fprintf(stderr, "Code point 0x%lx: expected keysym: %s, actual: %s\n\n",
127 (unsigned long)ucs, expected_name, actual_name);
128 return expected == actual;
135 assert(test_string("NoSymbol", XKB_KEY_NoSymbol));
136 assert(test_string("Undo", 0xFF65));
137 assert(test_string("UNDO", XKB_KEY_NoSymbol)); /* Require XKB_KEYSYM_CASE_INSENSITIVE */
138 assert(test_string("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
139 assert(test_string("XF86_Switch_VT_5", 0x1008FE05));
140 assert(test_string("VoidSymbol", 0xFFFFFF));
141 assert(test_string("0", 0x30));
142 assert(test_string("9", 0x39));
143 assert(test_string("a", 0x61));
144 assert(test_string("A", 0x41));
145 assert(test_string("ch", 0xfea0));
146 assert(test_string("Ch", 0xfea1));
147 assert(test_string("CH", 0xfea2));
148 assert(test_string("THORN", 0x00de));
149 assert(test_string("Thorn", 0x00de));
150 assert(test_string("thorn", 0x00fe));
151 assert(test_string(" thorn", XKB_KEY_NoSymbol));
152 assert(test_string("thorn ", XKB_KEY_NoSymbol));
154 /* Decimal keysyms are not supported (digits are special cases) */
155 assert(test_string("-1", XKB_KEY_NoSymbol));
156 assert(test_string("10", XKB_KEY_NoSymbol));
157 assert(test_string("010", XKB_KEY_NoSymbol));
158 assert(test_string("4567", XKB_KEY_NoSymbol));
160 /* Unicode: test various ranges */
161 assert(test_string("U0000", XKB_KEY_NoSymbol)); /* Min Unicode */
162 assert(test_string("U001f", XKB_KEY_NoSymbol));
163 assert(test_string("U0020", 0x0000020));
164 assert(test_string("U007E", 0x000007e));
165 assert(test_string("U007f", XKB_KEY_NoSymbol));
166 assert(test_string("U009f", XKB_KEY_NoSymbol));
167 assert(test_string("U00a0", 0x00000a0));
168 assert(test_string("U00ff", 0x00000ff));
169 assert(test_string("U4567", 0x1004567));
170 assert(test_string("U1F4A9", 0x0101F4A9));
171 assert(test_string("U10FFFF", 0x110ffff)); /* Max Unicode */
172 assert(test_string("U110000", XKB_KEY_NoSymbol));
173 /* Unicode: test syntax */
174 assert(test_string("U00004567", 0x1004567)); /* OK: 8 digits */
175 assert(test_string("U000004567", XKB_KEY_NoSymbol)); /* ERR: 9 digits */
176 assert(test_string("U+4567", XKB_KEY_NoSymbol)); /* ERR: Standard Unicode notation */
177 assert(test_string("U+4567ffff", XKB_KEY_NoSymbol));
178 assert(test_string("U+4567ffffff", XKB_KEY_NoSymbol));
179 assert(test_string("U-456", XKB_KEY_NoSymbol)); /* No negative number */
180 assert(test_string("U456w", XKB_KEY_NoSymbol)); /* Not hexadecimal digit */
181 assert(test_string("U4567 ", XKB_KEY_NoSymbol));
182 assert(test_string(" U4567", XKB_KEY_NoSymbol));
183 assert(test_string("U 4567", XKB_KEY_NoSymbol));
184 assert(test_string("U +4567", XKB_KEY_NoSymbol));
185 assert(test_string("u4567", XKB_KEY_NoSymbol)); /* Require XKB_KEYSYM_CASE_INSENSITIVE */
187 /* Hexadecimal: test ranges */
188 assert(test_string(STRINGIFY2(XKB_KEYSYM_MIN), XKB_KEY_NoSymbol)); /* Min keysym. */
189 assert(test_string("0x1", 0x00000001));
190 assert(test_string("0x01234567", 0x01234567));
191 assert(test_string("0x09abcdef", 0x09abcdef));
192 assert(test_string("0x01000100", 0x01000100)); /* Min Unicode. */
193 assert(test_string("0x0110ffff", 0x0110ffff)); /* Max Unicode. */
194 assert(test_string(STRINGIFY2(XKB_KEYSYM_MAX), XKB_KEYSYM_MAX)); /* Max keysym. */
195 assert(test_string("0x20000000", XKB_KEY_NoSymbol));
196 assert(test_string("0xffffffff", XKB_KEY_NoSymbol));
197 assert(test_string("0x100000000", XKB_KEY_NoSymbol));
198 /* Hexadecimal: test syntax */
199 assert(test_string("0x10203040", 0x10203040)); /* OK: 8 digits */
200 assert(test_string("0x102030400", XKB_KEY_NoSymbol)); /* ERR: 9 digits */
201 assert(test_string("0x01020304", 0x1020304)); /* OK: 8 digits, starts with 0 */
202 assert(test_string("0x010203040", XKB_KEY_NoSymbol)); /* ERR: 9 digits, starts with 0 */
203 assert(test_string("0x+10203040", XKB_KEY_NoSymbol));
204 assert(test_string("0x01020304w", XKB_KEY_NoSymbol)); /* Not hexadecimal digit */
205 assert(test_string("0x102030 ", XKB_KEY_NoSymbol));
206 assert(test_string("0x 102030", XKB_KEY_NoSymbol));
207 assert(test_string(" 0x102030", XKB_KEY_NoSymbol));
208 assert(test_string("0x +10203040", XKB_KEY_NoSymbol));
209 assert(test_string("0x-10203040", XKB_KEY_NoSymbol));
210 assert(test_string("0X10203040", XKB_KEY_NoSymbol)); /* Require XKB_KEYSYM_CASE_INSENSITIVE */
211 assert(test_string("10203040", XKB_KEY_NoSymbol)); /* Missing prefix/decimal not implemented */
212 assert(test_string("0b0101", XKB_KEY_NoSymbol)); /* Wrong prefix: binary not implemented */
213 assert(test_string("0o0701", XKB_KEY_NoSymbol)); /* Wrong prefix: octal not implemented */
215 assert(test_keysym(0x1008FF56, "XF86Close"));
216 assert(test_keysym(0x0, "NoSymbol"));
217 assert(test_keysym(0x1008FE20, "XF86Ungrab"));
218 assert(test_keysym(0x01000000, "0x01000000"));
220 assert(test_keysym(0x01000100, "U0100"));
221 assert(test_keysym(0x01001234, "U1234"));
222 /* 16-bit unicode padded to width 4. */
223 assert(test_keysym(0x010002DE, "U02DE"));
224 /* 32-bit unicode padded to width 8. */
225 assert(test_keysym(0x0101F4A9, "U0001F4A9"));
227 assert(test_keysym(0x0110ffff, "U0010FFFF"));
228 /* Max Unicode + 1 */
229 assert(test_keysym(0x01110000, "0x01110000"));
231 assert(test_keysym(XKB_KEYSYM_MIN, "NoSymbol"));
233 assert(test_keysym(XKB_KEYSYM_MAX, STRINGIFY2(XKB_KEYSYM_MAX)));
235 assert(test_keysym(XKB_KEYSYM_MAX + 1, "Invalid"));
236 assert(test_keysym(0xffffffff, "Invalid"));
238 assert(test_casestring("Undo", 0xFF65));
239 assert(test_casestring("UNDO", 0xFF65));
240 assert(test_casestring("A", 0x61));
241 assert(test_casestring("a", 0x61));
242 assert(test_casestring("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
243 assert(test_casestring("XF86_Switch_vT_5", 0x1008FE05));
244 assert(test_casestring("xF86_SwitcH_VT_5", 0x1008FE05));
245 assert(test_casestring("xF86SwiTch_VT_5", 0x1008FE05));
246 assert(test_casestring("xF86Switch_vt_5", 0x1008FE05));
247 assert(test_casestring("VoidSymbol", 0xFFFFFF));
248 assert(test_casestring("vOIDsymBol", 0xFFFFFF));
249 assert(test_casestring("U4567", 0x1004567));
250 assert(test_casestring("u4567", 0x1004567));
251 assert(test_casestring("0x10203040", 0x10203040));
252 assert(test_casestring("0X10203040", 0x10203040));
253 assert(test_casestring("THORN", 0x00fe));
254 assert(test_casestring("Thorn", 0x00fe));
255 assert(test_casestring("thorn", 0x00fe));
257 assert(test_string("", XKB_KEY_NoSymbol));
258 assert(test_casestring("", XKB_KEY_NoSymbol));
260 /* Latin-1 keysyms (1:1 mapping in UTF-32) */
261 assert(test_utf8(0x0020, "\x20"));
262 assert(test_utf8(0x007e, "\x7e"));
263 assert(test_utf8(0x00a0, "\xc2\xa0"));
264 assert(test_utf8(0x00ff, "\xc3\xbf"));
266 assert(test_utf8(XKB_KEY_y, "y"));
267 assert(test_utf8(XKB_KEY_u, "u"));
268 assert(test_utf8(XKB_KEY_m, "m"));
269 assert(test_utf8(XKB_KEY_Cyrillic_em, "м"));
270 assert(test_utf8(XKB_KEY_Cyrillic_u, "у"));
271 assert(test_utf8(XKB_KEY_exclam, "!"));
272 assert(test_utf8(XKB_KEY_oslash, "ø"));
273 assert(test_utf8(XKB_KEY_hebrew_aleph, "א"));
274 assert(test_utf8(XKB_KEY_Arabic_sheen, "ش"));
276 /* Keysyms with special handling */
277 assert(test_utf8(XKB_KEY_space, " "));
278 assert(test_utf8(XKB_KEY_KP_Space, " "));
279 assert(test_utf8(XKB_KEY_BackSpace, "\b"));
280 assert(test_utf8(XKB_KEY_Escape, "\033"));
281 assert(test_utf8(XKB_KEY_KP_Separator, ","));
282 assert(test_utf8(XKB_KEY_KP_Decimal, "."));
283 assert(test_utf8(XKB_KEY_Tab, "\t"));
284 assert(test_utf8(XKB_KEY_KP_Tab, "\t"));
285 assert(test_utf8(XKB_KEY_hyphen, ""));
286 assert(test_utf8(XKB_KEY_Linefeed, "\n"));
287 assert(test_utf8(XKB_KEY_Return, "\r"));
288 assert(test_utf8(XKB_KEY_KP_Enter, "\r"));
289 assert(test_utf8(XKB_KEY_KP_Equal, "="));
290 assert(test_utf8(XKB_KEY_9, "9"));
291 assert(test_utf8(XKB_KEY_KP_9, "9"));
292 assert(test_utf8(XKB_KEY_KP_Multiply, "*"));
293 assert(test_utf8(XKB_KEY_KP_Subtract, "-"));
295 /* Unicode keysyms */
296 assert(test_utf8(0x1000000, NULL) == 0); /* Min Unicode codepoint */
297 assert(test_utf8(0x1000001, "\x01")); /* Currently accepted, but not intended (< 0x100100) */
298 assert(test_utf8(0x1000020, " ")); /* Currently accepted, but not intended (< 0x100100) */
299 assert(test_utf8(0x100007f, "\x7f")); /* Currently accepted, but not intended (< 0x100100) */
300 assert(test_utf8(0x10000a0, "\xc2\xa0")); /* Currently accepted, but not intended (< 0x100100) */
301 assert(test_utf8(0x1000100, "Ā")); /* Min Unicode keysym */
302 assert(test_utf8(0x10005d0, "א"));
303 assert(test_utf8(0x110ffff, "\xf4\x8f\xbf\xbf")); /* Max Unicode */
304 assert(test_utf8(0x0100d800, NULL) == 0); // Unicode surrogates
305 assert(test_utf8(0x0100dfff, NULL) == 0); // Unicode surrogates
306 assert(test_utf8(0x1110000, NULL) == 0);
308 assert(test_utf32_to_keysym('y', XKB_KEY_y));
309 assert(test_utf32_to_keysym('u', XKB_KEY_u));
310 assert(test_utf32_to_keysym('m', XKB_KEY_m));
311 assert(test_utf32_to_keysym(0x43c, XKB_KEY_Cyrillic_em));
312 assert(test_utf32_to_keysym(0x443, XKB_KEY_Cyrillic_u));
313 assert(test_utf32_to_keysym('!', XKB_KEY_exclam));
314 assert(test_utf32_to_keysym(0xF8, XKB_KEY_oslash));
315 assert(test_utf32_to_keysym(0x5D0, XKB_KEY_hebrew_aleph));
316 assert(test_utf32_to_keysym(0x634, XKB_KEY_Arabic_sheen));
317 assert(test_utf32_to_keysym(0x1F609, 0x0101F609)); // ;) emoji
319 assert(test_utf32_to_keysym('\b', XKB_KEY_BackSpace));
320 assert(test_utf32_to_keysym('\t', XKB_KEY_Tab));
321 assert(test_utf32_to_keysym('\n', XKB_KEY_Linefeed));
322 assert(test_utf32_to_keysym(0x0b, XKB_KEY_Clear));
323 assert(test_utf32_to_keysym('\r', XKB_KEY_Return));
324 assert(test_utf32_to_keysym(0x1b, XKB_KEY_Escape));
325 assert(test_utf32_to_keysym(0x7f, XKB_KEY_Delete));
327 assert(test_utf32_to_keysym(' ', XKB_KEY_space));
328 assert(test_utf32_to_keysym(',', XKB_KEY_comma));
329 assert(test_utf32_to_keysym('.', XKB_KEY_period));
330 assert(test_utf32_to_keysym('=', XKB_KEY_equal));
331 assert(test_utf32_to_keysym('9', XKB_KEY_9));
332 assert(test_utf32_to_keysym('*', XKB_KEY_asterisk));
333 assert(test_utf32_to_keysym(0xd7, XKB_KEY_multiply));
334 assert(test_utf32_to_keysym('-', XKB_KEY_minus));
335 assert(test_utf32_to_keysym(0x10fffd, 0x110fffd));
336 assert(test_utf32_to_keysym(0x20ac, XKB_KEY_EuroSign));
338 // Unicode non-characters
339 assert(test_utf32_to_keysym(0xd800, XKB_KEY_NoSymbol)); // Unicode surrogates
340 assert(test_utf32_to_keysym(0xdfff, XKB_KEY_NoSymbol)); // Unicode surrogates
341 assert(test_utf32_to_keysym(0xfdd0, XKB_KEY_NoSymbol));
342 assert(test_utf32_to_keysym(0xfdef, XKB_KEY_NoSymbol));
343 assert(test_utf32_to_keysym(0xfffe, XKB_KEY_NoSymbol));
344 assert(test_utf32_to_keysym(0xffff, XKB_KEY_NoSymbol));
345 assert(test_utf32_to_keysym(0x7fffe, XKB_KEY_NoSymbol));
346 assert(test_utf32_to_keysym(0x7ffff, XKB_KEY_NoSymbol));
347 assert(test_utf32_to_keysym(0xafffe, XKB_KEY_NoSymbol));
348 assert(test_utf32_to_keysym(0xaffff, XKB_KEY_NoSymbol));
350 // Codepoints outside the Unicode planes
351 assert(test_utf32_to_keysym(0x110000, XKB_KEY_NoSymbol));
352 assert(test_utf32_to_keysym(0xdeadbeef, XKB_KEY_NoSymbol));
354 assert(xkb_keysym_is_lower(XKB_KEY_a));
355 assert(xkb_keysym_is_lower(XKB_KEY_Greek_lambda));
356 assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03b1", 0))); /* GREEK SMALL LETTER ALPHA */
357 assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03af", 0))); /* GREEK SMALL LETTER IOTA WITH TONOS */
359 assert(xkb_keysym_is_upper(XKB_KEY_A));
360 assert(xkb_keysym_is_upper(XKB_KEY_Greek_LAMBDA));
361 assert(xkb_keysym_is_upper(xkb_keysym_from_name("U0391", 0))); /* GREEK CAPITAL LETTER ALPHA */
362 assert(xkb_keysym_is_upper(xkb_keysym_from_name("U0388", 0))); /* GREEK CAPITAL LETTER EPSILON WITH TONOS */
364 assert(!xkb_keysym_is_upper(XKB_KEY_a));
365 assert(!xkb_keysym_is_lower(XKB_KEY_A));
366 assert(!xkb_keysym_is_lower(XKB_KEY_Return));
367 assert(!xkb_keysym_is_upper(XKB_KEY_Return));
368 assert(!xkb_keysym_is_lower(XKB_KEY_hebrew_aleph));
369 assert(!xkb_keysym_is_upper(XKB_KEY_hebrew_aleph));
370 assert(!xkb_keysym_is_upper(xkb_keysym_from_name("U05D0", 0))); /* HEBREW LETTER ALEF */
371 assert(!xkb_keysym_is_lower(xkb_keysym_from_name("U05D0", 0))); /* HEBREW LETTER ALEF */
372 assert(!xkb_keysym_is_lower(XKB_KEY_8));
373 assert(!xkb_keysym_is_upper(XKB_KEY_8));
375 assert(xkb_keysym_is_keypad(XKB_KEY_KP_Enter));
376 assert(xkb_keysym_is_keypad(XKB_KEY_KP_6));
377 assert(xkb_keysym_is_keypad(XKB_KEY_KP_Add));
378 assert(!xkb_keysym_is_keypad(XKB_KEY_Num_Lock));
379 assert(!xkb_keysym_is_keypad(XKB_KEY_1));
380 assert(!xkb_keysym_is_keypad(XKB_KEY_Return));
382 assert(xkb_keysym_to_upper(XKB_KEY_a) == XKB_KEY_A);
383 assert(xkb_keysym_to_upper(XKB_KEY_A) == XKB_KEY_A);
384 assert(xkb_keysym_to_lower(XKB_KEY_a) == XKB_KEY_a);
385 assert(xkb_keysym_to_lower(XKB_KEY_A) == XKB_KEY_a);
386 assert(xkb_keysym_to_upper(XKB_KEY_Return) == XKB_KEY_Return);
387 assert(xkb_keysym_to_lower(XKB_KEY_Return) == XKB_KEY_Return);
388 assert(xkb_keysym_to_upper(XKB_KEY_Greek_lambda) == XKB_KEY_Greek_LAMBDA);
389 assert(xkb_keysym_to_upper(XKB_KEY_Greek_LAMBDA) == XKB_KEY_Greek_LAMBDA);
390 assert(xkb_keysym_to_lower(XKB_KEY_Greek_lambda) == XKB_KEY_Greek_lambda);
391 assert(xkb_keysym_to_lower(XKB_KEY_Greek_LAMBDA) == XKB_KEY_Greek_lambda);
392 assert(xkb_keysym_to_upper(XKB_KEY_eacute) == XKB_KEY_Eacute);
393 assert(xkb_keysym_to_lower(XKB_KEY_Eacute) == XKB_KEY_eacute);
395 test_github_issue_42();