darray: tweak parameters a bit for better memory usage
[profile/ivi/libxkbcommon.git] / test / canonicalise.c
1 /*
2  * Copyright © 2009 Daniel Stone
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  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "xkbcommon/xkbcommon.h"
32 #include "xkb-priv.h"
33
34 struct test_data {
35     struct xkb_component_names new;
36     struct xkb_component_names old;
37     int pass_old;
38     const char *exp_keycodes;
39     const char *exp_compat;
40     const char *exp_symbols;
41     const char *exp_types;
42 };
43
44 static struct test_data *
45 new_data(void)
46 {
47     return calloc(1, sizeof(struct test_data));
48 }
49
50 static void
51 free_data(struct test_data *data)
52 {
53     free(data->new.keycodes);
54     free(data->new.compat);
55     free(data->new.symbols);
56     free(data->new.types);
57     free(data->old.keycodes);
58     free(data->old.compat);
59     free(data->old.symbols);
60     free(data->old.types);
61     free(data);
62 }
63
64 static void
65 set_new(struct test_data *data, const char *keycodes, const char *compat,
66         const char *symbols, const char *types)
67 {
68     data->new.keycodes = strdup(keycodes);
69     data->new.compat = strdup(compat);
70     data->new.symbols = strdup(symbols);
71     data->new.types = strdup(types);
72 }
73
74 static void
75 set_old(struct test_data *data, const char *keycodes, const char *compat,
76         const char *symbols, const char *types)
77 {
78     data->old.keycodes = strdup(keycodes);
79     data->old.compat = strdup(compat);
80     data->old.symbols = strdup(symbols);
81     data->old.types = strdup(types);
82     data->pass_old = 1;
83 }
84
85 static void
86 set_exp(struct test_data *data, const char *keycodes, const char *compat,
87         const char *symbols, const char *types)
88 {
89     data->exp_keycodes = keycodes;
90     data->exp_compat = compat;
91     data->exp_symbols = symbols;
92     data->exp_types = types;
93 }
94
95 static int
96 test_canonicalise(struct test_data *data)
97 {
98     fprintf(stderr, "New: %s %s %s %s\n", data->new.keycodes,
99             data->new.compat, data->new.symbols, data->new.types);
100     if (data->pass_old)
101         fprintf(stderr, "Old: %s %s %s %s\n", data->old.keycodes,
102                 data->old.compat, data->old.symbols, data->old.types);
103     fprintf(stderr, "Expected: %s %s %s %s\n", data->exp_keycodes,
104             data->exp_compat, data->exp_symbols, data->exp_types);
105
106     if (data->pass_old)
107         xkb_canonicalise_components(&data->new, &data->old);
108     else
109         xkb_canonicalise_components(&data->new, NULL);
110
111     fprintf(stderr, "Received: %s %s %s %s\n\n", data->new.keycodes,
112             data->new.compat, data->new.symbols, data->new.types);
113
114     return (strcmp(data->new.keycodes, data->exp_keycodes) == 0) &&
115            (strcmp(data->new.compat, data->exp_compat) == 0) &&
116            (strcmp(data->new.symbols, data->exp_symbols) == 0) &&
117            (strcmp(data->new.types, data->exp_types) == 0);
118 }
119
120 int
121 main(void)
122 {
123     struct test_data *twopart, *onepart;
124
125     twopart = new_data();
126     set_new(twopart, "+inet(pc104)",        "%+complete",     "pc(pc104)+%+ctrl(nocaps)",          "|complete");
127     set_old(twopart, "xfree86",             "basic",          "us(dvorak)",                        "xfree86");
128     set_exp(twopart, "xfree86+inet(pc104)", "basic+complete", "pc(pc104)+us(dvorak)+ctrl(nocaps)", "xfree86|complete");
129     assert(test_canonicalise(twopart));
130     free_data(twopart);
131
132     onepart = new_data();
133     set_new(onepart, "evdev", "complete", "pc(pc104)+us+compose(ralt)", "complete");
134     set_exp(onepart, "evdev", "complete", "pc(pc104)+us+compose(ralt)", "complete");
135     assert(test_canonicalise(onepart));
136     free_data(onepart);
137
138     return 0;
139 }