build: include config.h manually
[platform/upstream/libxkbcommon.git] / test / keysym.c
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
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
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
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
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
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 int
108 main(void)
109 {
110     assert(test_string("Undo", 0xFF65));
111     assert(test_string("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
112     assert(test_string("XF86_Switch_VT_5", 0x1008FE05));
113     assert(test_string("VoidSymbol", 0xFFFFFF));
114     assert(test_string("U4567", 0x1004567));
115     assert(test_string("0x10203040", 0x10203040));
116     assert(test_string("a", 0x61));
117     assert(test_string("A", 0x41));
118     assert(test_string("ch", 0xfea0));
119     assert(test_string("Ch", 0xfea1));
120     assert(test_string("CH", 0xfea2));
121     assert(test_string("THORN", 0x00de));
122     assert(test_string("Thorn", 0x00de));
123     assert(test_string("thorn", 0x00fe));
124
125     assert(test_keysym(0x1008FF56, "XF86Close"));
126     assert(test_keysym(0x0, "NoSymbol"));
127     assert(test_keysym(0x1008FE20, "XF86Ungrab"));
128     assert(test_keysym(0x01001234, "U1234"));
129     /* 16-bit unicode padded to width 4. */
130     assert(test_keysym(0x010002DE, "U02DE"));
131     /* 32-bit unicode padded to width 8. */
132     assert(test_keysym(0x0101F4A9, "U0001F4A9"));
133
134     assert(test_casestring("Undo", 0xFF65));
135     assert(test_casestring("UNDO", 0xFF65));
136     assert(test_casestring("A", 0x61));
137     assert(test_casestring("a", 0x61));
138     assert(test_casestring("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
139     assert(test_casestring("XF86_Switch_vT_5", 0x1008FE05));
140     assert(test_casestring("xF86_SwitcH_VT_5", 0x1008FE05));
141     assert(test_casestring("xF86SwiTch_VT_5", 0x1008FE05));
142     assert(test_casestring("xF86Switch_vt_5", 0x1008FE05));
143     assert(test_casestring("VoidSymbol", 0xFFFFFF));
144     assert(test_casestring("vOIDsymBol", 0xFFFFFF));
145     assert(test_casestring("U4567", 0x1004567));
146     assert(test_casestring("u4567", 0x1004567));
147     assert(test_casestring("0x10203040", 0x10203040));
148     assert(test_casestring("0X10203040", 0x10203040));
149     assert(test_casestring("THORN", 0x00fe));
150     assert(test_casestring("Thorn", 0x00fe));
151     assert(test_casestring("thorn", 0x00fe));
152
153     assert(test_utf8(XKB_KEY_y, "y"));
154     assert(test_utf8(XKB_KEY_u, "u"));
155     assert(test_utf8(XKB_KEY_m, "m"));
156     assert(test_utf8(XKB_KEY_Cyrillic_em, "м"));
157     assert(test_utf8(XKB_KEY_Cyrillic_u, "у"));
158     assert(test_utf8(XKB_KEY_exclam, "!"));
159     assert(test_utf8(XKB_KEY_oslash, "ø"));
160     assert(test_utf8(XKB_KEY_hebrew_aleph, "א"));
161     assert(test_utf8(XKB_KEY_Arabic_sheen, "ش"));
162
163     assert(test_utf8(XKB_KEY_space, " "));
164     assert(test_utf8(XKB_KEY_KP_Space, " "));
165     assert(test_utf8(XKB_KEY_BackSpace, "\b"));
166     assert(test_utf8(XKB_KEY_Escape, "\033"));
167     assert(test_utf8(XKB_KEY_KP_Separator, ","));
168     assert(test_utf8(XKB_KEY_KP_Decimal, "."));
169     assert(test_utf8(XKB_KEY_Tab, "\t"));
170     assert(test_utf8(XKB_KEY_KP_Tab, "\t"));
171     assert(test_utf8(XKB_KEY_hyphen, "­"));
172     assert(test_utf8(XKB_KEY_Linefeed, "\n"));
173     assert(test_utf8(XKB_KEY_Return, "\r"));
174     assert(test_utf8(XKB_KEY_KP_Enter, "\r"));
175     assert(test_utf8(XKB_KEY_KP_Equal, "="));
176     assert(test_utf8(XKB_KEY_9, "9"));
177     assert(test_utf8(XKB_KEY_KP_9, "9"));
178     assert(test_utf8(XKB_KEY_KP_Multiply, "*"));
179     assert(test_utf8(XKB_KEY_KP_Subtract, "-"));
180
181     assert(test_utf8(0x10005d0, "א"));
182     assert(test_utf8(0x110ffff, "\xf4\x8f\xbf\xbf"));
183     assert(test_utf8(0x1110000, NULL) == 0);
184
185     assert(xkb_keysym_is_lower(XKB_KEY_a));
186     assert(xkb_keysym_is_lower(XKB_KEY_Greek_lambda));
187     assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03b1", 0))); /* GREEK SMALL LETTER ALPHA */
188     assert(xkb_keysym_is_lower(xkb_keysym_from_name("U03af", 0))); /* GREEK SMALL LETTER IOTA WITH TONOS */
189
190     assert(xkb_keysym_is_upper(XKB_KEY_A));
191     assert(xkb_keysym_is_upper(XKB_KEY_Greek_LAMBDA));
192     assert(xkb_keysym_is_upper(xkb_keysym_from_name("U0391", 0))); /* GREEK CAPITAL LETTER ALPHA */
193     assert(xkb_keysym_is_upper(xkb_keysym_from_name("U0388", 0))); /* GREEK CAPITAL LETTER EPSILON WITH TONOS */
194
195     assert(!xkb_keysym_is_upper(XKB_KEY_a));
196     assert(!xkb_keysym_is_lower(XKB_KEY_A));
197     assert(!xkb_keysym_is_lower(XKB_KEY_Return));
198     assert(!xkb_keysym_is_upper(XKB_KEY_Return));
199     assert(!xkb_keysym_is_lower(XKB_KEY_hebrew_aleph));
200     assert(!xkb_keysym_is_upper(XKB_KEY_hebrew_aleph));
201     assert(!xkb_keysym_is_upper(xkb_keysym_from_name("U05D0", 0))); /* HEBREW LETTER ALEF */
202     assert(!xkb_keysym_is_lower(xkb_keysym_from_name("U05D0", 0))); /* HEBREW LETTER ALEF */
203     assert(!xkb_keysym_is_lower(XKB_KEY_8));
204     assert(!xkb_keysym_is_upper(XKB_KEY_8));
205
206     assert(xkb_keysym_is_keypad(XKB_KEY_KP_Enter));
207     assert(xkb_keysym_is_keypad(XKB_KEY_KP_6));
208     assert(xkb_keysym_is_keypad(XKB_KEY_KP_Add));
209     assert(!xkb_keysym_is_keypad(XKB_KEY_Num_Lock));
210     assert(!xkb_keysym_is_keypad(XKB_KEY_1));
211     assert(!xkb_keysym_is_keypad(XKB_KEY_Return));
212
213     assert(xkb_keysym_to_upper(XKB_KEY_a) == XKB_KEY_A);
214     assert(xkb_keysym_to_upper(XKB_KEY_A) == XKB_KEY_A);
215     assert(xkb_keysym_to_lower(XKB_KEY_a) == XKB_KEY_a);
216     assert(xkb_keysym_to_lower(XKB_KEY_A) == XKB_KEY_a);
217     assert(xkb_keysym_to_upper(XKB_KEY_Return) == XKB_KEY_Return);
218     assert(xkb_keysym_to_lower(XKB_KEY_Return) == XKB_KEY_Return);
219     assert(xkb_keysym_to_upper(XKB_KEY_Greek_lambda) == XKB_KEY_Greek_LAMBDA);
220     assert(xkb_keysym_to_upper(XKB_KEY_Greek_LAMBDA) == XKB_KEY_Greek_LAMBDA);
221     assert(xkb_keysym_to_lower(XKB_KEY_Greek_lambda) == XKB_KEY_Greek_lambda);
222     assert(xkb_keysym_to_lower(XKB_KEY_Greek_LAMBDA) == XKB_KEY_Greek_lambda);
223     assert(xkb_keysym_to_upper(XKB_KEY_eacute) == XKB_KEY_Eacute);
224     assert(xkb_keysym_to_lower(XKB_KEY_Eacute) == XKB_KEY_eacute);
225
226     test_github_issue_42();
227
228     return 0;
229 }