1 /*
2 * Copyright © 2009 Dan Nicholson
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23 #include "config.h"
24
25 #include <locale.h>
26
27 #include "test.h"
28 #include "keysym.h" /* For unexported is_lower/upper/keypad() */
29
30 static int
test_string(const char * string,xkb_keysym_t expected)31 test_string(const char *string, xkb_keysym_t expected)
32 {
33 xkb_keysym_t keysym;
34
35 keysym = xkb_keysym_from_name(string, 0);
36
37 fprintf(stderr, "Expected string %s -> %x\n", string, expected);
38 fprintf(stderr, "Received string %s -> %x\n\n", string, keysym);
39
40 return keysym == expected;
41 }
42
43 static int
test_casestring(const char * string,xkb_keysym_t expected)44 test_casestring(const char *string, xkb_keysym_t expected)
45 {
46 xkb_keysym_t keysym;
47
48 keysym = xkb_keysym_from_name(string, XKB_KEYSYM_CASE_INSENSITIVE);
49
50 fprintf(stderr, "Expected casestring %s -> %x\n", string, expected);
51 fprintf(stderr, "Received casestring %s -> %x\n\n", string, keysym);
52
53 return keysym == expected;
54 }
55
56 static int
test_keysym(xkb_keysym_t keysym,const char * expected)57 test_keysym(xkb_keysym_t keysym, const char *expected)
58 {
59 char s[16];
60
61 xkb_keysym_get_name(keysym, s, sizeof(s));
62
63 fprintf(stderr, "Expected keysym %#x -> %s\n", keysym, expected);
64 fprintf(stderr, "Received keysym %#x -> %s\n\n", keysym, s);
65
66 return streq(s, expected);
67 }
68
69 static int
test_utf8(xkb_keysym_t keysym,const char * expected)70 test_utf8(xkb_keysym_t keysym, const char *expected)
71 {
72 char s[8];
73 int ret;
74
75 ret = xkb_keysym_to_utf8(keysym, s, sizeof(s));
76 if (ret <= 0)
77 return ret;
78
79 fprintf(stderr, "Expected keysym %#x -> %s (%u bytes)\n", keysym, expected,
80 (unsigned) strlen(expected));
81 fprintf(stderr, "Received keysym %#x -> %s (%u bytes)\n\n", keysym, s,
82 (unsigned) strlen(s));
83
84 assert(expected != NULL);
85 return streq(s, expected);
86 }
87
88 static void
test_github_issue_42(void)89 test_github_issue_42(void)
90 {
91 // Verify we are not dependent on locale, Turkish-i problem in particular.
92 if (setlocale(LC_CTYPE, "tr_TR.UTF-8") == NULL) {
93 // The locale is not available, probably; skip.
94 return;
95 }
96
97 assert(test_string("i", XKB_KEY_i));
98 assert(test_string("I", XKB_KEY_I));
99 assert(test_casestring("i", XKB_KEY_i));
100 assert(test_casestring("I", XKB_KEY_i));
101 assert(xkb_keysym_to_upper(XKB_KEY_i) == XKB_KEY_I);
102 assert(xkb_keysym_to_lower(XKB_KEY_I) == XKB_KEY_i);
103
104 setlocale(LC_CTYPE, "C");
105 }
106
107 static void
get_keysym_name(xkb_keysym_t keysym,char * buffer,size_t size)108 get_keysym_name(xkb_keysym_t keysym, char *buffer, size_t size)
109 {
110 int name_length = xkb_keysym_get_name(keysym, buffer, size);
111 if (name_length < 0) {
112 snprintf(buffer, size, "(unknown: 0x%lx)", (unsigned long)keysym);
113 }
114 }
115
116 static int
test_utf32_to_keysym(uint32_t ucs,xkb_keysym_t expected)117 test_utf32_to_keysym(uint32_t ucs, xkb_keysym_t expected)
118 {
119 char expected_name[64];
120 char actual_name[64];
121 xkb_keysym_t actual = xkb_utf32_to_keysym(ucs);
122 get_keysym_name(expected, expected_name, 64);
123 get_keysym_name(actual, actual_name, 64);
124
125 fprintf(stderr, "Code point 0x%lx: expected keysym: %s, actual: %s\n\n",
126 (unsigned long)ucs, expected_name, actual_name);
127 return expected == actual;
128 }
129
130 int
main(void)131 main(void)
132 {
133 assert(test_string("Undo", 0xFF65));
134 assert(test_string("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
135 assert(test_string("XF86_Switch_VT_5", 0x1008FE05));
136 assert(test_string("VoidSymbol", 0xFFFFFF));
137 assert(test_string("U4567", 0x1004567));
138 assert(test_string("0x10203040", 0x10203040));
139 assert(test_string("a", 0x61));
140 assert(test_string("A", 0x41));
141 assert(test_string("ch", 0xfea0));
142 assert(test_string("Ch", 0xfea1));
143 assert(test_string("CH", 0xfea2));
144 assert(test_string("THORN", 0x00de));
145 assert(test_string("Thorn", 0x00de));
146 assert(test_string("thorn", 0x00fe));
147
148 assert(test_keysym(0x1008FF56, "XF86Close"));
149 assert(test_keysym(0x0, "NoSymbol"));
150 assert(test_keysym(0x1008FE20, "XF86Ungrab"));
151 assert(test_keysym(0x01001234, "U1234"));
152 /* 16-bit unicode padded to width 4. */
153 assert(test_keysym(0x010002DE, "U02DE"));
154 /* 32-bit unicode padded to width 8. */
155 assert(test_keysym(0x0101F4A9, "U0001F4A9"));
156
157 assert(test_casestring("Undo", 0xFF65));
158 assert(test_casestring("UNDO", 0xFF65));
159 assert(test_casestring("A", 0x61));
160 assert(test_casestring("a", 0x61));
161 assert(test_casestring("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
162 assert(test_casestring("XF86_Switch_vT_5", 0x1008FE05));
163 assert(test_casestring("xF86_SwitcH_VT_5", 0x1008FE05));
164 assert(test_casestring("xF86SwiTch_VT_5", 0x1008FE05));
165 assert(test_casestring("xF86Switch_vt_5", 0x1008FE05));
166 assert(test_casestring("VoidSymbol", 0xFFFFFF));
167 assert(test_casestring("vOIDsymBol", 0xFFFFFF));
168 assert(test_casestring("U4567", 0x1004567));
169 assert(test_casestring("u4567", 0x1004567));
170 assert(test_casestring("0x10203040", 0x10203040));
171 assert(test_casestring("0X10203040", 0x10203040));
172 assert(test_casestring("THORN", 0x00fe));
173 assert(test_casestring("Thorn", 0x00fe));
174 assert(test_casestring("thorn", 0x00fe));
175
176 assert(test_utf8(XKB_KEY_y, "y"));
177 assert(test_utf8(XKB_KEY_u, "u"));
178 assert(test_utf8(XKB_KEY_m, "m"));
179 assert(test_utf8(XKB_KEY_Cyrillic_em, "м"));
180 assert(test_utf8(XKB_KEY_Cyrillic_u, "у"));
181 assert(test_utf8(XKB_KEY_exclam, "!"));
182 assert(test_utf8(XKB_KEY_oslash, "ø"));
183 assert(test_utf8(XKB_KEY_hebrew_aleph, "א"));
184 assert(test_utf8(XKB_KEY_Arabic_sheen, "ش"));
185
186 assert(test_utf8(XKB_KEY_space, " "));
187 assert(test_utf8(XKB_KEY_KP_Space, " "));
188 assert(test_utf8(XKB_KEY_BackSpace, "\b"));
189 assert(test_utf8(XKB_KEY_Escape, "\033"));
190 assert(test_utf8(XKB_KEY_KP_Separator, ","));
191 assert(test_utf8(XKB_KEY_KP_Decimal, "."));
192 assert(test_utf8(XKB_KEY_Tab, "\t"));
193 assert(test_utf8(XKB_KEY_KP_Tab, "\t"));
194 assert(test_utf8(XKB_KEY_hyphen, ""));
195 assert(test_utf8(XKB_KEY_Linefeed, "\n"));
196 assert(test_utf8(XKB_KEY_Return, "\r"));
197 assert(test_utf8(XKB_KEY_KP_Enter, "\r"));
198 assert(test_utf8(XKB_KEY_KP_Equal, "="));
199 assert(test_utf8(XKB_KEY_9, "9"));
200 assert(test_utf8(XKB_KEY_KP_9, "9"));
201 assert(test_utf8(XKB_KEY_KP_Multiply, "*"));
202 assert(test_utf8(XKB_KEY_KP_Subtract, "-"));
203
204 assert(test_utf8(0x10005d0, "א"));
205 assert(test_utf8(0x110ffff, "\xf4\x8f\xbf\xbf"));
206 assert(test_utf8(0x1110000, NULL) == 0);
207
208 assert(test_utf32_to_keysym('y', XKB_KEY_y));
209 assert(test_utf32_to_keysym('u', XKB_KEY_u));
210 assert(test_utf32_to_keysym('m', XKB_KEY_m));
211 assert(test_utf32_to_keysym(0x43c, XKB_KEY_Cyrillic_em));
212 assert(test_utf32_to_keysym(0x443, XKB_KEY_Cyrillic_u));
213 assert(test_utf32_to_keysym('!', XKB_KEY_exclam));
214 assert(test_utf32_to_keysym(0xF8, XKB_KEY_oslash));
215 assert(test_utf32_to_keysym(0x5D0, XKB_KEY_hebrew_aleph));
216 assert(test_utf32_to_keysym(0x634, XKB_KEY_Arabic_sheen));
217 assert(test_utf32_to_keysym(0x1F609, 0x0101F609)); // ;) emoji
218
219 assert(test_utf32_to_keysym('\b', XKB_KEY_BackSpace));
220 assert(test_utf32_to_keysym('\t', XKB_KEY_Tab));
221 assert(test_utf32_to_keysym('\n', XKB_KEY_Linefeed));
222 assert(test_utf32_to_keysym(0x0b, XKB_KEY_Clear));
223 assert(test_utf32_to_keysym('\r', XKB_KEY_Return));
224 assert(test_utf32_to_keysym(0x1b, XKB_KEY_Escape));
225 assert(test_utf32_to_keysym(0x7f, XKB_KEY_Delete));
226
227 assert(test_utf32_to_keysym(' ', XKB_KEY_space));
228 assert(test_utf32_to_keysym(',', XKB_KEY_comma));
229 assert(test_utf32_to_keysym('.', XKB_KEY_period));
230 assert(test_utf32_to_keysym('=', XKB_KEY_equal));
231 assert(test_utf32_to_keysym('9', XKB_KEY_9));
232 assert(test_utf32_to_keysym('*', XKB_KEY_asterisk));
233 assert(test_utf32_to_keysym(0xd7, XKB_KEY_multiply));
234 assert(test_utf32_to_keysym('-', XKB_KEY_minus));
235 assert(test_utf32_to_keysym(0x10fffd, 0x110fffd));
236
237 // Unicode non-characters
238 assert(test_utf32_to_keysym(0xfdd0, XKB_KEY_NoSymbol));
239 assert(test_utf32_to_keysym(0xfdef, XKB_KEY_NoSymbol));
240 assert(test_utf32_to_keysym(0xfffe, XKB_KEY_NoSymbol));
241 assert(test_utf32_to_keysym(0xffff, XKB_KEY_NoSymbol));
242 assert(test_utf32_to_keysym(0x7fffe, XKB_KEY_NoSymbol));
243 assert(test_utf32_to_keysym(0x7ffff, XKB_KEY_NoSymbol));
244 assert(test_utf32_to_keysym(0xafffe, XKB_KEY_NoSymbol));
245 assert(test_utf32_to_keysym(0xaffff, XKB_KEY_NoSymbol));
246
247 // Codepoints outside the Unicode planes
248 assert(test_utf32_to_keysym(0x110000, XKB_KEY_NoSymbol));
249 assert(test_utf32_to_keysym(0xdeadbeef, XKB_KEY_NoSymbol));
250
251 assert(xkb_keysym_is_lower(XKB_KEY_a));
252 assert(xkb_keysym_is_lower(XKB_KEY_Greek_lambda));
253 assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03b1", 0))); /* GREEK SMALL LETTER ALPHA */
254 assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03af", 0))); /* GREEK SMALL LETTER IOTA WITH TONOS */
255
256 assert(xkb_keysym_is_upper(XKB_KEY_A));
257 assert(xkb_keysym_is_upper(XKB_KEY_Greek_LAMBDA));
258 assert(xkb_keysym_is_upper(xkb_keysym_from_name("U0391", 0))); /* GREEK CAPITAL LETTER ALPHA */
259 assert(xkb_keysym_is_upper(xkb_keysym_from_name("U0388", 0))); /* GREEK CAPITAL LETTER EPSILON WITH TONOS */
260
261 assert(!xkb_keysym_is_upper(XKB_KEY_a));
262 assert(!xkb_keysym_is_lower(XKB_KEY_A));
263 assert(!xkb_keysym_is_lower(XKB_KEY_Return));
264 assert(!xkb_keysym_is_upper(XKB_KEY_Return));
265 assert(!xkb_keysym_is_lower(XKB_KEY_hebrew_aleph));
266 assert(!xkb_keysym_is_upper(XKB_KEY_hebrew_aleph));
267 assert(!xkb_keysym_is_upper(xkb_keysym_from_name("U05D0", 0))); /* HEBREW LETTER ALEF */
268 assert(!xkb_keysym_is_lower(xkb_keysym_from_name("U05D0", 0))); /* HEBREW LETTER ALEF */
269 assert(!xkb_keysym_is_lower(XKB_KEY_8));
270 assert(!xkb_keysym_is_upper(XKB_KEY_8));
271
272 assert(xkb_keysym_is_keypad(XKB_KEY_KP_Enter));
273 assert(xkb_keysym_is_keypad(XKB_KEY_KP_6));
274 assert(xkb_keysym_is_keypad(XKB_KEY_KP_Add));
275 assert(!xkb_keysym_is_keypad(XKB_KEY_Num_Lock));
276 assert(!xkb_keysym_is_keypad(XKB_KEY_1));
277 assert(!xkb_keysym_is_keypad(XKB_KEY_Return));
278
279 assert(xkb_keysym_to_upper(XKB_KEY_a) == XKB_KEY_A);
280 assert(xkb_keysym_to_upper(XKB_KEY_A) == XKB_KEY_A);
281 assert(xkb_keysym_to_lower(XKB_KEY_a) == XKB_KEY_a);
282 assert(xkb_keysym_to_lower(XKB_KEY_A) == XKB_KEY_a);
283 assert(xkb_keysym_to_upper(XKB_KEY_Return) == XKB_KEY_Return);
284 assert(xkb_keysym_to_lower(XKB_KEY_Return) == XKB_KEY_Return);
285 assert(xkb_keysym_to_upper(XKB_KEY_Greek_lambda) == XKB_KEY_Greek_LAMBDA);
286 assert(xkb_keysym_to_upper(XKB_KEY_Greek_LAMBDA) == XKB_KEY_Greek_LAMBDA);
287 assert(xkb_keysym_to_lower(XKB_KEY_Greek_lambda) == XKB_KEY_Greek_lambda);
288 assert(xkb_keysym_to_lower(XKB_KEY_Greek_LAMBDA) == XKB_KEY_Greek_lambda);
289 assert(xkb_keysym_to_upper(XKB_KEY_eacute) == XKB_KEY_Eacute);
290 assert(xkb_keysym_to_lower(XKB_KEY_Eacute) == XKB_KEY_eacute);
291
292 test_github_issue_42();
293
294 return 0;
295 }
296