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