2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
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:
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
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.
29 #define INTERN_LITERAL(table, literal) \
30 atom_intern(table, literal, sizeof(literal) - 1, true)
32 #define LOOKUP_LITERAL(table, literal) \
33 atom_intern(table, literal, sizeof(literal) - 1, false)
36 random_string(char **str_out, size_t *len_out)
38 /* Keep this small, so collisions might happen. */
39 static const char random_chars[] = {
40 'a', 'b', 'c', 'd', 'e', 'f', 'g'
47 str = malloc(len + 1);
50 for (size_t i = 0; i < len; i++)
51 str[i] = random_chars[rand() % ARRAY_SIZE(random_chars)];
52 /* Don't always terminate it; should work without. */
61 test_random_strings(void)
69 struct atom_table *table;
70 struct atom_string *arr;
75 table = atom_table_new();
78 unsigned seed = (unsigned) clock();
81 N = 1 + rand() % 100000;
82 arr = calloc(N, sizeof(*arr));
85 for (int i = 0; i < N; i++) {
86 random_string(&arr[i].string, &arr[i].len);
88 atom = atom_intern(table, arr[i].string, arr[i].len, false);
89 if (atom != XKB_ATOM_NONE) {
90 string = atom_text(table, atom);
93 if (arr[i].len != strlen(string) ||
94 strncmp(string, arr[i].string, arr[i].len) != 0) {
95 fprintf(stderr, "got a collision, but strings don't match!\n");
96 fprintf(stderr, "existing length %lu, string %s\n",
97 strlen(string), string);
98 fprintf(stderr, "new length %lu, string %.*s\n",
99 arr[i].len, (int) arr[i].len, arr[i].string);
100 fprintf(stderr, "seed: %u\n", seed);
104 /* OK, got a real collision. */
110 arr[i].atom = atom_intern(table, arr[i].string, arr[i].len, true);
111 if (arr[i].atom == XKB_ATOM_NONE) {
112 fprintf(stderr, "failed to intern! len: %lu, string: %.*s\n",
113 arr[i].len, (int) arr[i].len, arr[i].string);
114 fprintf(stderr, "seed: %u\n", seed);
119 for (int i = 0; i < N; i++) {
120 string = atom_text(table, arr[i].atom);
123 if (arr[i].len != strlen(string) ||
124 strncmp(string, arr[i].string, arr[i].len) != 0) {
125 fprintf(stderr, "looked-up string doesn't match!\n");
126 fprintf(stderr, "found length %lu, string %s\n",
127 strlen(string), string);
128 fprintf(stderr, "expected length %lu, string %.*s\n",
129 arr[i].len, (int) arr[i].len, arr[i].string);
131 /* Since this is random, we need to dump the failing data,
132 * so we might have some chance to reproduce. */
133 fprintf(stderr, "START dump of arr, N=%d\n", N);
134 for (int j = 0; j < N; j++) {
135 fprintf(stderr, "%u\t\t%lu\t\t%.*s\n", arr[i].atom,
136 arr[i].len, (int) arr[i].len, arr[i].string);
138 fprintf(stderr, "END\n");
140 fprintf(stderr, "seed: %u\n", seed);
145 for (int i = 0; i < N; i++)
148 atom_table_free(table);
154 struct atom_table *table;
155 xkb_atom_t atom1, atom2, atom3;
157 table = atom_table_new();
160 assert(atom_text(table, XKB_ATOM_NONE) == NULL);
161 assert(atom_intern(table, NULL, 0, false) == XKB_ATOM_NONE);
163 atom1 = INTERN_LITERAL(table, "hello");
164 assert(atom1 != XKB_ATOM_NONE);
165 assert(atom1 == LOOKUP_LITERAL(table, "hello"));
166 assert(streq(atom_text(table, atom1), "hello"));
168 atom2 = atom_intern(table, "hello", 3, true);
169 assert(atom2 != XKB_ATOM_NONE);
170 assert(atom1 != atom2);
171 assert(streq(atom_text(table, atom2), "hel"));
172 assert(LOOKUP_LITERAL(table, "hel") == atom2);
173 assert(LOOKUP_LITERAL(table, "hell") == XKB_ATOM_NONE);
174 assert(LOOKUP_LITERAL(table, "hello") == atom1);
176 atom3 = atom_intern(table, "", 0, true);
177 assert(atom3 != XKB_ATOM_NONE);
178 assert(LOOKUP_LITERAL(table, "") == atom3);
180 atom_table_free(table);
182 test_random_strings();