Organize src/ and test/ headers
[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
24 #include "test.h"
25
26 static int
27 test_string(const char *string, xkb_keysym_t expected)
28 {
29     xkb_keysym_t keysym;
30
31     keysym = xkb_keysym_from_name(string);
32
33     fprintf(stderr, "Expected string %s -> %x\n", string, expected);
34     fprintf(stderr, "Received string %s -> %x\n\n", string, keysym);
35
36     return keysym == expected;
37 }
38
39 static int
40 test_keysym(xkb_keysym_t keysym, const char *expected)
41 {
42     char s[16];
43
44     xkb_keysym_get_name(keysym, s, sizeof(s));
45
46     fprintf(stderr, "Expected keysym %#x -> %s\n", keysym, expected);
47     fprintf(stderr, "Received keysym %#x -> %s\n\n", keysym, s);
48
49     return streq(s, expected);
50 }
51
52 static int
53 test_utf8(xkb_keysym_t keysym, const char *expected)
54 {
55     char s[8];
56     int ret;
57
58     ret = xkb_keysym_to_utf8(keysym, s, sizeof(s));
59     if (ret <= 0)
60         return ret;
61
62     fprintf(stderr, "Expected keysym %#x -> %s\n", keysym, expected);
63     fprintf(stderr, "Received keysym %#x -> %s\n\n", keysym, s);
64
65     return streq(s, expected);
66 }
67
68 int
69 main(void)
70 {
71     assert(test_string("Undo", 0xFF65));
72     assert(test_string("ThisKeyShouldNotExist", XKB_KEY_NoSymbol));
73     assert(test_string("XF86_Switch_VT_5", 0x1008FE05));
74     assert(test_string("VoidSymbol", 0xFFFFFF));
75     assert(test_string("U4567", 0x1004567));
76     assert(test_string("0x10203040", 0x10203040));
77
78     assert(test_keysym(0x1008FF56, "XF86Close"));
79     assert(test_keysym(0x0, "NoSymbol"));
80     assert(test_keysym(0x1008FE20, "XF86Ungrab"));
81     assert(test_keysym(0x01001234, "U1234"));
82
83     assert(test_utf8(XKB_KEY_y, "y"));
84     assert(test_utf8(XKB_KEY_u, "u"));
85     assert(test_utf8(XKB_KEY_m, "m"));
86     assert(test_utf8(XKB_KEY_Cyrillic_em, "м"));
87     assert(test_utf8(XKB_KEY_Cyrillic_u, "у"));
88     assert(test_utf8(XKB_KEY_exclam, "!"));
89     assert(test_utf8(XKB_KEY_oslash, "ø"));
90     assert(test_utf8(XKB_KEY_hebrew_aleph, "א"));
91     assert(test_utf8(XKB_KEY_Arabic_sheen, "ش"));
92
93     assert(test_utf8(XKB_KEY_space, " "));
94     assert(test_utf8(XKB_KEY_KP_Space, " "));
95     assert(test_utf8(XKB_KEY_9, "9"));
96     assert(test_utf8(XKB_KEY_KP_9, "9"));
97     assert(test_utf8(XKB_KEY_KP_Multiply, "*"));
98     assert(test_utf8(XKB_KEY_KP_Subtract, "-"));
99
100     return 0;
101 }