2 * UCW Library -- Testing the Sorter
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/getopt.h"
12 #include "lib/fastbuf.h"
13 #include "lib/hashfunc.h"
21 /*** Simple 4-byte integer keys ***/
27 #define SORT_KEY_REGULAR struct key1
28 #define SORT_PREFIX(x) s1_##x
30 #define SORT_OUTPUT_FB
32 #define SORT_INT(k) (k).x
34 #include "lib/sorter/sorter.h"
37 test_int(int mode, uns N)
41 log(L_INFO, "Integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
43 struct fastbuf *f = bopen_tmp(65536);
44 for (uns i=0; i<N; i++)
45 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
48 log(L_INFO, "Sorting");
49 f = s1_sort(f, NULL, N-1);
51 log(L_INFO, "Verifying");
52 for (uns i=0; i<N; i++)
56 die("Discrepancy: %d instead of %d", j, i);
61 /*** Integers with merging, but no data ***/
68 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
70 for (uns i=1; i<n; i++)
71 k[0]->cnt += k[i]->cnt;
72 bwrite(f, k[0], sizeof(struct key2));
75 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
77 for (uns i=1; i<n; i++)
78 k[0]->cnt += k[i]->cnt;
79 bwrite(dest, k[0], sizeof(struct key2));
82 #define SORT_KEY_REGULAR struct key2
83 #define SORT_PREFIX(x) s2_##x
85 #define SORT_OUTPUT_FB
87 #define SORT_INT(k) (k).x
89 #include "lib/sorter/sorter.h"
92 test_counted(int mode, uns N)
96 log(L_INFO, "Counted integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
98 struct fastbuf *f = bopen_tmp(65536);
99 for (uns i=0; i<2*N; i++)
100 for (uns j=0; j<2; j++)
102 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
107 log(L_INFO, "Sorting");
108 f = s2_sort(f, NULL, N-1);
110 log(L_INFO, "Verifying");
111 for (uns i=0; i<N; i++)
115 die("Discrepancy: %d instead of %d", j, i);
118 die("Discrepancy: %d has count %d instead of 4", j, k);
123 /*** Longer records with hashes (similar to Shepherd's index records) ***/
131 static inline int s3_compare(struct key3 *x, struct key3 *y)
133 /* FIXME: Maybe unroll manually? */
134 for (uns i=0; i<4; i++)
135 COMPARE(x->hash[i], y->hash[i]);
139 static inline uns s3_hash(struct key3 *x)
144 #define SORT_KEY_REGULAR struct key3
145 #define SORT_PREFIX(x) s3_##x
146 #define SORT_INPUT_FB
147 #define SORT_OUTPUT_FB
148 #define SORT_HASH_BITS 32
150 #include "lib/sorter/sorter.h"
153 gen_hash_key(int mode, struct key3 *k, uns i)
156 k->payload[0] = 7*i + 13;
157 k->payload[1] = 13*i + 19;
158 k->payload[2] = 19*i + 7;
163 k->hash[1] = k->payload[0];
164 k->hash[2] = k->payload[1];
165 k->hash[3] = k->payload[2];
169 k->hash[1] = k->payload[0];
170 k->hash[2] = k->payload[1];
171 k->hash[3] = k->payload[2];
174 struct MD5Context ctx;
176 MD5Update(&ctx, (byte*) &k->i, 4);
177 MD5Final((byte*) &k->hash, &ctx);
183 test_hashes(int mode, uns N)
185 log(L_INFO, "Hashes (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
186 struct key3 k, lastk;
188 struct fastbuf *f = bopen_tmp(65536);
190 for (uns i=0; i<N; i++)
192 gen_hash_key(mode, &k, i);
193 hash_sum += k.hash[3];
194 bwrite(f, &k, sizeof(k));
198 log(L_INFO, "Sorting");
199 f = s3_sort(f, NULL);
201 log(L_INFO, "Verifying");
202 for (uns i=0; i<N; i++)
204 int ok = breadb(f, &k, sizeof(k));
206 if (i && s3_compare(&k, &lastk) <= 0)
208 gen_hash_key(mode, &lastk, k.i);
209 if (memcmp(&k, &lastk, sizeof(k)))
211 hash_sum -= k.hash[3];
217 /*** Variable-length records (strings) with and without var-length data ***/
226 static inline int s4_compare(struct key4 *x, struct key4 *y)
228 uns l = MIN(x->len, y->len);
229 int c = memcmp(x->s, y->s, l);
232 COMPARE(x->len, y->len);
236 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
239 if (x->len == 0xffffffff)
241 ASSERT(x->len < KEY4_MAX);
242 breadb(f, x->s, x->len);
246 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
248 ASSERT(x->len < KEY4_MAX);
250 bwrite(f, x->s, x->len);
253 #define SORT_KEY struct key4
254 #define SORT_PREFIX(x) s4_##x
255 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
256 #define SORT_INPUT_FB
257 #define SORT_OUTPUT_FB
259 #include "lib/sorter/sorter.h"
261 #define s4b_compare s4_compare
262 #define s4b_read_key s4_read_key
263 #define s4b_write_key s4_write_key
265 static inline uns s4_data_size(struct key4 *x)
267 return x->len ? (x->s[0] ^ 0xad) : 0;
270 #define SORT_KEY struct key4
271 #define SORT_PREFIX(x) s4b_##x
272 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
273 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
274 #define SORT_INPUT_FB
275 #define SORT_OUTPUT_FB
277 #include "lib/sorter/sorter.h"
280 gen_key4(struct key4 *k)
282 k->len = random_max(KEY4_MAX);
283 for (uns i=0; i<k->len; i++)
288 gen_data4(byte *buf, uns len, uns h)
298 test_strings(uns mode, uns N)
300 log(L_INFO, "Strings %s(N=%d)", (mode ? "with data " : ""), N);
303 struct key4 k, lastk;
304 byte buf[256], buf2[256];
307 struct fastbuf *f = bopen_tmp(65536);
308 for (uns i=0; i<N; i++)
312 uns h = hash_block(k.s, k.len);
316 gen_data4(buf, s4_data_size(&k), h);
317 bwrite(f, buf, s4_data_size(&k));
322 log(L_INFO, "Sorting");
323 f = (mode ? s4b_sort : s4_sort)(f, NULL);
325 log(L_INFO, "Verifying");
326 for (uns i=0; i<N; i++)
328 int ok = s4_read_key(f, &k);
330 uns h = hash_block(k.s, k.len);
331 if (mode && s4_data_size(&k))
333 ok = breadb(f, buf, s4_data_size(&k));
335 gen_data4(buf2, s4_data_size(&k), h);
336 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
338 if (i && s4_compare(&k, &lastk) < 0)
348 main(int argc, char **argv)
351 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
354 fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);