xref: /aosp_15_r20/external/libxkbcommon/test/atom.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Ran Benita <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
5*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
6*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
7*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
9*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
12*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
13*2b949d04SAndroid Build Coastguard Worker  * Software.
14*2b949d04SAndroid Build Coastguard Worker  *
15*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
22*2b949d04SAndroid Build Coastguard Worker  */
23*2b949d04SAndroid Build Coastguard Worker 
24*2b949d04SAndroid Build Coastguard Worker #include "config.h"
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker #include <time.h>
27*2b949d04SAndroid Build Coastguard Worker 
28*2b949d04SAndroid Build Coastguard Worker #include "test.h"
29*2b949d04SAndroid Build Coastguard Worker #include "atom.h"
30*2b949d04SAndroid Build Coastguard Worker 
31*2b949d04SAndroid Build Coastguard Worker #define INTERN_LITERAL(table, literal) \
32*2b949d04SAndroid Build Coastguard Worker     atom_intern(table, literal, sizeof(literal) - 1, true)
33*2b949d04SAndroid Build Coastguard Worker 
34*2b949d04SAndroid Build Coastguard Worker #define LOOKUP_LITERAL(table, literal) \
35*2b949d04SAndroid Build Coastguard Worker     atom_intern(table, literal, sizeof(literal) - 1, false)
36*2b949d04SAndroid Build Coastguard Worker 
37*2b949d04SAndroid Build Coastguard Worker static void
random_string(char ** str_out,size_t * len_out)38*2b949d04SAndroid Build Coastguard Worker random_string(char **str_out, size_t *len_out)
39*2b949d04SAndroid Build Coastguard Worker {
40*2b949d04SAndroid Build Coastguard Worker     /* Keep this small, so collisions might happen. */
41*2b949d04SAndroid Build Coastguard Worker     static const char random_chars[] = {
42*2b949d04SAndroid Build Coastguard Worker         'a', 'b', 'c', 'd', 'e', 'f', 'g'
43*2b949d04SAndroid Build Coastguard Worker     };
44*2b949d04SAndroid Build Coastguard Worker 
45*2b949d04SAndroid Build Coastguard Worker     size_t len;
46*2b949d04SAndroid Build Coastguard Worker     char *str;
47*2b949d04SAndroid Build Coastguard Worker 
48*2b949d04SAndroid Build Coastguard Worker     len = rand() % 15;
49*2b949d04SAndroid Build Coastguard Worker     str = malloc(len + 1);
50*2b949d04SAndroid Build Coastguard Worker     assert(str);
51*2b949d04SAndroid Build Coastguard Worker 
52*2b949d04SAndroid Build Coastguard Worker     for (size_t i = 0; i < len; i++)
53*2b949d04SAndroid Build Coastguard Worker         str[i] = random_chars[rand() % ARRAY_SIZE(random_chars)];
54*2b949d04SAndroid Build Coastguard Worker     /* Don't always terminate it; should work without. */
55*2b949d04SAndroid Build Coastguard Worker     if (rand() % 2 == 0)
56*2b949d04SAndroid Build Coastguard Worker         str[len] = '\0';
57*2b949d04SAndroid Build Coastguard Worker 
58*2b949d04SAndroid Build Coastguard Worker     *str_out = str;
59*2b949d04SAndroid Build Coastguard Worker     *len_out = len;
60*2b949d04SAndroid Build Coastguard Worker }
61*2b949d04SAndroid Build Coastguard Worker 
62*2b949d04SAndroid Build Coastguard Worker static void
test_random_strings(void)63*2b949d04SAndroid Build Coastguard Worker test_random_strings(void)
64*2b949d04SAndroid Build Coastguard Worker {
65*2b949d04SAndroid Build Coastguard Worker     struct atom_string {
66*2b949d04SAndroid Build Coastguard Worker         xkb_atom_t atom;
67*2b949d04SAndroid Build Coastguard Worker         char *string;
68*2b949d04SAndroid Build Coastguard Worker         size_t len;
69*2b949d04SAndroid Build Coastguard Worker     };
70*2b949d04SAndroid Build Coastguard Worker 
71*2b949d04SAndroid Build Coastguard Worker     struct atom_table *table;
72*2b949d04SAndroid Build Coastguard Worker     struct atom_string *arr;
73*2b949d04SAndroid Build Coastguard Worker     int N;
74*2b949d04SAndroid Build Coastguard Worker     xkb_atom_t atom;
75*2b949d04SAndroid Build Coastguard Worker     const char *string;
76*2b949d04SAndroid Build Coastguard Worker 
77*2b949d04SAndroid Build Coastguard Worker     table = atom_table_new();
78*2b949d04SAndroid Build Coastguard Worker     assert(table);
79*2b949d04SAndroid Build Coastguard Worker 
80*2b949d04SAndroid Build Coastguard Worker     unsigned seed = (unsigned) clock();
81*2b949d04SAndroid Build Coastguard Worker     srand(seed);
82*2b949d04SAndroid Build Coastguard Worker 
83*2b949d04SAndroid Build Coastguard Worker     N = 1 + rand() % 100000;
84*2b949d04SAndroid Build Coastguard Worker     arr = calloc(N, sizeof(*arr));
85*2b949d04SAndroid Build Coastguard Worker     assert(arr);
86*2b949d04SAndroid Build Coastguard Worker 
87*2b949d04SAndroid Build Coastguard Worker     for (int i = 0; i < N; i++) {
88*2b949d04SAndroid Build Coastguard Worker         random_string(&arr[i].string, &arr[i].len);
89*2b949d04SAndroid Build Coastguard Worker 
90*2b949d04SAndroid Build Coastguard Worker         atom = atom_intern(table, arr[i].string, arr[i].len, false);
91*2b949d04SAndroid Build Coastguard Worker         if (atom != XKB_ATOM_NONE) {
92*2b949d04SAndroid Build Coastguard Worker             string = atom_text(table, atom);
93*2b949d04SAndroid Build Coastguard Worker             assert(string);
94*2b949d04SAndroid Build Coastguard Worker 
95*2b949d04SAndroid Build Coastguard Worker             if (arr[i].len != strlen(string) ||
96*2b949d04SAndroid Build Coastguard Worker                 strncmp(string, arr[i].string, arr[i].len) != 0) {
97*2b949d04SAndroid Build Coastguard Worker                 fprintf(stderr, "got a collision, but strings don't match!\n");
98*2b949d04SAndroid Build Coastguard Worker                 fprintf(stderr, "existing length %zu, string %s\n",
99*2b949d04SAndroid Build Coastguard Worker                         strlen(string), string);
100*2b949d04SAndroid Build Coastguard Worker                 fprintf(stderr, "new length %zu, string %.*s\n",
101*2b949d04SAndroid Build Coastguard Worker                         arr[i].len, (int) arr[i].len, arr[i].string);
102*2b949d04SAndroid Build Coastguard Worker                 fprintf(stderr, "seed: %u\n", seed);
103*2b949d04SAndroid Build Coastguard Worker                 assert(false);
104*2b949d04SAndroid Build Coastguard Worker             }
105*2b949d04SAndroid Build Coastguard Worker 
106*2b949d04SAndroid Build Coastguard Worker             /* OK, got a real collision. */
107*2b949d04SAndroid Build Coastguard Worker             free(arr[i].string);
108*2b949d04SAndroid Build Coastguard Worker             i--;
109*2b949d04SAndroid Build Coastguard Worker             continue;
110*2b949d04SAndroid Build Coastguard Worker         }
111*2b949d04SAndroid Build Coastguard Worker 
112*2b949d04SAndroid Build Coastguard Worker         arr[i].atom = atom_intern(table, arr[i].string, arr[i].len, true);
113*2b949d04SAndroid Build Coastguard Worker         if (arr[i].atom == XKB_ATOM_NONE) {
114*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "failed to intern! len: %zu, string: %.*s\n",
115*2b949d04SAndroid Build Coastguard Worker                     arr[i].len, (int) arr[i].len, arr[i].string);
116*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "seed: %u\n", seed);
117*2b949d04SAndroid Build Coastguard Worker             assert(false);
118*2b949d04SAndroid Build Coastguard Worker         }
119*2b949d04SAndroid Build Coastguard Worker     }
120*2b949d04SAndroid Build Coastguard Worker 
121*2b949d04SAndroid Build Coastguard Worker     for (int i = 0; i < N; i++) {
122*2b949d04SAndroid Build Coastguard Worker         string = atom_text(table, arr[i].atom);
123*2b949d04SAndroid Build Coastguard Worker         assert(string);
124*2b949d04SAndroid Build Coastguard Worker 
125*2b949d04SAndroid Build Coastguard Worker         if (arr[i].len != strlen(string) ||
126*2b949d04SAndroid Build Coastguard Worker             strncmp(string, arr[i].string, arr[i].len) != 0) {
127*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "looked-up string doesn't match!\n");
128*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "found length %zu, string %s\n",
129*2b949d04SAndroid Build Coastguard Worker                     strlen(string), string);
130*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "expected length %zu, string %.*s\n",
131*2b949d04SAndroid Build Coastguard Worker                     arr[i].len, (int) arr[i].len, arr[i].string);
132*2b949d04SAndroid Build Coastguard Worker 
133*2b949d04SAndroid Build Coastguard Worker             /* Since this is random, we need to dump the failing data,
134*2b949d04SAndroid Build Coastguard Worker              * so we might have some chance to reproduce. */
135*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "START dump of arr, N=%d\n", N);
136*2b949d04SAndroid Build Coastguard Worker             for (int j = 0; j < N; j++) {
137*2b949d04SAndroid Build Coastguard Worker                 fprintf(stderr, "%u\t\t%zu\t\t%.*s\n", arr[i].atom,
138*2b949d04SAndroid Build Coastguard Worker                         arr[i].len, (int) arr[i].len, arr[i].string);
139*2b949d04SAndroid Build Coastguard Worker             }
140*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "END\n");
141*2b949d04SAndroid Build Coastguard Worker 
142*2b949d04SAndroid Build Coastguard Worker             fprintf(stderr, "seed: %u\n", seed);
143*2b949d04SAndroid Build Coastguard Worker             assert(false);
144*2b949d04SAndroid Build Coastguard Worker         }
145*2b949d04SAndroid Build Coastguard Worker     }
146*2b949d04SAndroid Build Coastguard Worker 
147*2b949d04SAndroid Build Coastguard Worker     for (int i = 0; i < N; i++)
148*2b949d04SAndroid Build Coastguard Worker         free(arr[i].string);
149*2b949d04SAndroid Build Coastguard Worker     free(arr);
150*2b949d04SAndroid Build Coastguard Worker     atom_table_free(table);
151*2b949d04SAndroid Build Coastguard Worker }
152*2b949d04SAndroid Build Coastguard Worker 
153*2b949d04SAndroid Build Coastguard Worker int
main(void)154*2b949d04SAndroid Build Coastguard Worker main(void)
155*2b949d04SAndroid Build Coastguard Worker {
156*2b949d04SAndroid Build Coastguard Worker     struct atom_table *table;
157*2b949d04SAndroid Build Coastguard Worker     xkb_atom_t atom1, atom2, atom3;
158*2b949d04SAndroid Build Coastguard Worker 
159*2b949d04SAndroid Build Coastguard Worker     table = atom_table_new();
160*2b949d04SAndroid Build Coastguard Worker     assert(table);
161*2b949d04SAndroid Build Coastguard Worker 
162*2b949d04SAndroid Build Coastguard Worker     assert(atom_text(table, XKB_ATOM_NONE) == NULL);
163*2b949d04SAndroid Build Coastguard Worker     assert(atom_intern(table, NULL, 0, false) == XKB_ATOM_NONE);
164*2b949d04SAndroid Build Coastguard Worker 
165*2b949d04SAndroid Build Coastguard Worker     atom1 = INTERN_LITERAL(table, "hello");
166*2b949d04SAndroid Build Coastguard Worker     assert(atom1 != XKB_ATOM_NONE);
167*2b949d04SAndroid Build Coastguard Worker     assert(atom1 == LOOKUP_LITERAL(table, "hello"));
168*2b949d04SAndroid Build Coastguard Worker     assert(streq(atom_text(table, atom1), "hello"));
169*2b949d04SAndroid Build Coastguard Worker 
170*2b949d04SAndroid Build Coastguard Worker     atom2 = atom_intern(table, "hello", 3, true);
171*2b949d04SAndroid Build Coastguard Worker     assert(atom2 != XKB_ATOM_NONE);
172*2b949d04SAndroid Build Coastguard Worker     assert(atom1 != atom2);
173*2b949d04SAndroid Build Coastguard Worker     assert(streq(atom_text(table, atom2), "hel"));
174*2b949d04SAndroid Build Coastguard Worker     assert(LOOKUP_LITERAL(table, "hel") == atom2);
175*2b949d04SAndroid Build Coastguard Worker     assert(LOOKUP_LITERAL(table, "hell") == XKB_ATOM_NONE);
176*2b949d04SAndroid Build Coastguard Worker     assert(LOOKUP_LITERAL(table, "hello") == atom1);
177*2b949d04SAndroid Build Coastguard Worker 
178*2b949d04SAndroid Build Coastguard Worker     atom3 = atom_intern(table, "", 0, true);
179*2b949d04SAndroid Build Coastguard Worker     assert(atom3 != XKB_ATOM_NONE);
180*2b949d04SAndroid Build Coastguard Worker     assert(LOOKUP_LITERAL(table, "") == atom3);
181*2b949d04SAndroid Build Coastguard Worker 
182*2b949d04SAndroid Build Coastguard Worker     atom_table_free(table);
183*2b949d04SAndroid Build Coastguard Worker 
184*2b949d04SAndroid Build Coastguard Worker     test_random_strings();
185*2b949d04SAndroid Build Coastguard Worker 
186*2b949d04SAndroid Build Coastguard Worker     return 0;
187*2b949d04SAndroid Build Coastguard Worker }
188