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