Debian Codegraph @

test/atom.c

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