d42d4514caea5004977cbdc5c453a1861f9ece46
[platform/upstream/libxkbcommon.git] / test / atom.c
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
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 "config.h"
25
26 #include <time.h>
27
28 #include "test.h"
29 #include "atom.h"
30
31 #define INTERN_LITERAL(table, literal) \
32     atom_intern(table, literal, sizeof(literal) - 1, true)
33
34 #define LOOKUP_LITERAL(table, literal) \
35     atom_intern(table, literal, sizeof(literal) - 1, false)
36
37 static void
38 random_string(char **str_out, size_t *len_out)
39 {
40     /* Keep this small, so collisions might happen. */
41     static const char random_chars[] = {
42         'a', 'b', 'c', 'd', 'e', 'f', 'g'
43     };
44
45     size_t len;
46     char *str;
47
48     srand((unsigned) time(NULL));
49
50     len = rand() % 15;
51     str = malloc(len + 1);
52     assert(str);
53
54     for (size_t i = 0; i < len; i++)
55         str[i] = random_chars[rand() % ARRAY_SIZE(random_chars)];
56     /* Don't always terminate it; should work without. */
57     if (rand() % 2 == 0)
58         str[len] = '\0';
59
60     *str_out = str;
61     *len_out = len;
62 }
63
64 static void
65 test_random_strings(void)
66 {
67     struct atom_string {
68         xkb_atom_t atom;
69         char *string;
70         size_t len;
71     };
72
73     struct atom_table *table;
74     struct atom_string *arr;
75     int N;
76     xkb_atom_t atom;
77     const char *string;
78
79     table = atom_table_new();
80     assert(table);
81
82     unsigned seed = (unsigned) clock();
83     srand(seed);
84
85     N = 1 + rand() % 100000;
86     arr = calloc(N, sizeof(*arr));
87     assert(arr);
88
89     for (int i = 0; i < N; i++) {
90         random_string(&arr[i].string, &arr[i].len);
91
92         atom = atom_intern(table, arr[i].string, arr[i].len, false);
93         if (atom != XKB_ATOM_NONE) {
94             string = atom_text(table, atom);
95             assert(string);
96
97             if (arr[i].len != strlen(string) ||
98                 strncmp(string, arr[i].string, arr[i].len) != 0) {
99                 fprintf(stderr, "got a collision, but strings don't match!\n");
100                 fprintf(stderr, "existing length %zu, string %s\n",
101                         strlen(string), string);
102                 fprintf(stderr, "new length %zu, string %.*s\n",
103                         arr[i].len, (int) arr[i].len, arr[i].string);
104                 fprintf(stderr, "seed: %u\n", seed);
105                 assert(false);
106             }
107
108             /* OK, got a real collision. */
109             free(arr[i].string);
110             i--;
111             continue;
112         }
113
114         arr[i].atom = atom_intern(table, arr[i].string, arr[i].len, true);
115         if (arr[i].atom == XKB_ATOM_NONE) {
116             fprintf(stderr, "failed to intern! len: %zu, string: %.*s\n",
117                     arr[i].len, (int) arr[i].len, arr[i].string);
118             fprintf(stderr, "seed: %u\n", seed);
119             assert(false);
120         }
121     }
122
123     for (int i = 0; i < N; i++) {
124         string = atom_text(table, arr[i].atom);
125         assert(string);
126
127         if (arr[i].len != strlen(string) ||
128             strncmp(string, arr[i].string, arr[i].len) != 0) {
129             fprintf(stderr, "looked-up string doesn't match!\n");
130             fprintf(stderr, "found length %zu, string %s\n",
131                     strlen(string), string);
132             fprintf(stderr, "expected length %zu, string %.*s\n",
133                     arr[i].len, (int) arr[i].len, arr[i].string);
134
135             /* Since this is random, we need to dump the failing data,
136              * so we might have some chance to reproduce. */
137             fprintf(stderr, "START dump of arr, N=%d\n", N);
138             for (int j = 0; j < N; j++) {
139                 fprintf(stderr, "%u\t\t%zu\t\t%.*s\n", arr[i].atom,
140                         arr[i].len, (int) arr[i].len, arr[i].string);
141             }
142             fprintf(stderr, "END\n");
143
144             fprintf(stderr, "seed: %u\n", seed);
145             assert(false);
146         }
147     }
148
149     for (int i = 0; i < N; i++)
150         free(arr[i].string);
151     free(arr);
152     atom_table_free(table);
153 }
154
155 int
156 main(void)
157 {
158     struct atom_table *table;
159     xkb_atom_t atom1, atom2, atom3;
160
161     table = atom_table_new();
162     assert(table);
163
164     assert(atom_text(table, XKB_ATOM_NONE) == NULL);
165     assert(atom_intern(table, NULL, 0, false) == XKB_ATOM_NONE);
166
167     atom1 = INTERN_LITERAL(table, "hello");
168     assert(atom1 != XKB_ATOM_NONE);
169     assert(atom1 == LOOKUP_LITERAL(table, "hello"));
170     assert(streq(atom_text(table, atom1), "hello"));
171
172     atom2 = atom_intern(table, "hello", 3, true);
173     assert(atom2 != XKB_ATOM_NONE);
174     assert(atom1 != atom2);
175     assert(streq(atom_text(table, atom2), "hel"));
176     assert(LOOKUP_LITERAL(table, "hel") == atom2);
177     assert(LOOKUP_LITERAL(table, "hell") == XKB_ATOM_NONE);
178     assert(LOOKUP_LITERAL(table, "hello") == atom1);
179
180     atom3 = atom_intern(table, "", 0, true);
181     assert(atom3 != XKB_ATOM_NONE);
182     assert(LOOKUP_LITERAL(table, "") == atom3);
183
184     atom_table_free(table);
185
186     test_random_strings();
187
188     return 0;
189 }