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"
13 #include "lib/fastbuf.h"
14 #include "lib/hashfunc.h"
23 /*** Time measurement ***/
36 log(L_INFO, "Test took %.3fs", get_timer() / 1000.);
39 /*** Simple 4-byte integer keys ***/
45 #define SORT_KEY_REGULAR struct key1
46 #define SORT_PREFIX(x) s1_##x
48 #define SORT_OUTPUT_FB
50 #define SORT_INT(k) (k).x
52 #include "lib/sorter/sorter.h"
55 test_int(int mode, u64 size)
57 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
59 log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
61 struct fastbuf *f = bopen_tmp(65536);
62 for (uns i=0; i<N; i++)
63 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
67 f = s1_sort(f, NULL, N-1);
70 SORT_XTRACE(2, "Verifying");
71 for (uns i=0; i<N; i++)
75 die("Discrepancy: %u instead of %u", j, i);
80 /*** Integers with merging, but no data ***/
87 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
89 for (uns i=1; i<n; i++)
90 k[0]->cnt += k[i]->cnt;
91 bwrite(f, k[0], sizeof(struct key2));
94 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
96 for (uns i=1; i<n; i++)
97 k[0]->cnt += k[i]->cnt;
98 bwrite(dest, k[0], sizeof(struct key2));
101 #define SORT_KEY_REGULAR struct key2
102 #define SORT_PREFIX(x) s2_##x
103 #define SORT_INPUT_FB
104 #define SORT_OUTPUT_FB
106 #define SORT_INT(k) (k).x
108 #include "lib/sorter/sorter.h"
111 test_counted(int mode, u64 size)
113 u64 items = size / sizeof(struct key2);
115 while (items/(2*mult) > 0xffff0000)
117 uns N = items ? nextprime(items/(2*mult)) : 0;
119 log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
121 struct fastbuf *f = bopen_tmp(65536);
122 for (uns m=0; m<mult; m++)
123 for (uns i=0; i<N; i++)
124 for (uns j=0; j<2; j++)
126 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
132 f = s2_sort(f, NULL, N-1);
135 SORT_XTRACE(2, "Verifying");
136 for (uns i=0; i<N; i++)
140 die("Discrepancy: %u instead of %u", j, i);
143 die("Discrepancy: %u has count %u instead of %u", j, k, mult);
148 /*** Longer records with hashes (similar to Shepherd's index records) ***/
156 static inline int s3_compare(struct key3 *x, struct key3 *y)
158 /* FIXME: Maybe unroll manually? */
159 for (uns i=0; i<4; i++)
160 COMPARE(x->hash[i], y->hash[i]);
164 static inline uns s3_hash(struct key3 *x)
169 #define SORT_KEY_REGULAR struct key3
170 #define SORT_PREFIX(x) s3_##x
171 #define SORT_INPUT_FB
172 #define SORT_OUTPUT_FB
173 #define SORT_HASH_BITS 32
175 #include "lib/sorter/sorter.h"
178 gen_hash_key(int mode, struct key3 *k, uns i)
181 k->payload[0] = 7*i + 13;
182 k->payload[1] = 13*i + 19;
183 k->payload[2] = 19*i + 7;
188 k->hash[1] = k->payload[0];
189 k->hash[2] = k->payload[1];
190 k->hash[3] = k->payload[2];
194 k->hash[1] = k->payload[0];
195 k->hash[2] = k->payload[1];
196 k->hash[3] = k->payload[2];
199 struct MD5Context ctx;
201 MD5Update(&ctx, (byte*) &k->i, 4);
202 MD5Final((byte*) &k->hash, &ctx);
208 test_hashes(int mode, u64 size)
210 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
211 log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
212 struct key3 k, lastk;
214 struct fastbuf *f = bopen_tmp(65536);
216 for (uns i=0; i<N; i++)
218 gen_hash_key(mode, &k, i);
219 hash_sum += k.hash[3];
220 bwrite(f, &k, sizeof(k));
225 f = s3_sort(f, NULL);
228 SORT_XTRACE(2, "Verifying");
229 for (uns i=0; i<N; i++)
231 int ok = breadb(f, &k, sizeof(k));
233 if (i && s3_compare(&k, &lastk) <= 0)
235 gen_hash_key(mode, &lastk, k.i);
236 if (memcmp(&k, &lastk, sizeof(k)))
238 hash_sum -= k.hash[3];
244 /*** Variable-length records (strings) with and without var-length data ***/
253 static inline int s4_compare(struct key4 *x, struct key4 *y)
255 uns l = MIN(x->len, y->len);
256 int c = memcmp(x->s, y->s, l);
259 COMPARE(x->len, y->len);
263 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
266 if (x->len == 0xffffffff)
268 ASSERT(x->len < KEY4_MAX);
269 breadb(f, x->s, x->len);
273 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
275 ASSERT(x->len < KEY4_MAX);
277 bwrite(f, x->s, x->len);
280 #define SORT_KEY struct key4
281 #define SORT_PREFIX(x) s4_##x
282 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
283 #define SORT_INPUT_FB
284 #define SORT_OUTPUT_FB
286 #include "lib/sorter/sorter.h"
288 #define s4b_compare s4_compare
289 #define s4b_read_key s4_read_key
290 #define s4b_write_key s4_write_key
292 static inline uns s4_data_size(struct key4 *x)
294 return x->len ? (x->s[0] ^ 0xad) : 0;
297 #define SORT_KEY struct key4
298 #define SORT_PREFIX(x) s4b_##x
299 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
300 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
301 #define SORT_INPUT_FB
302 #define SORT_OUTPUT_FB
304 #include "lib/sorter/sorter.h"
307 gen_key4(struct key4 *k)
309 k->len = random_max(KEY4_MAX);
310 for (uns i=0; i<k->len; i++)
315 gen_data4(byte *buf, uns len, uns h)
325 test_strings(uns mode, u64 size)
327 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
328 uns N = MIN(size / avg_item_size, 0xffffffff);
329 log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
332 struct key4 k, lastk;
333 byte buf[256], buf2[256];
336 struct fastbuf *f = bopen_tmp(65536);
337 for (uns i=0; i<N; i++)
341 uns h = hash_block(k.s, k.len);
345 gen_data4(buf, s4_data_size(&k), h);
346 bwrite(f, buf, s4_data_size(&k));
352 f = (mode ? s4b_sort : s4_sort)(f, NULL);
355 SORT_XTRACE(2, "Verifying");
356 for (uns i=0; i<N; i++)
358 int ok = s4_read_key(f, &k);
360 uns h = hash_block(k.s, k.len);
361 if (mode && s4_data_size(&k))
363 ok = breadb(f, buf, s4_data_size(&k));
365 gen_data4(buf2, s4_data_size(&k), h);
366 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
368 if (i && s4_compare(&k, &lastk) < 0)
377 /*** Graph-like structure with custom presorting ***/
384 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
390 static int s5_gen(struct s5_pair *p)
399 p->x = ((u64)s5_j * s5_K) % s5_N;
400 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
405 #define ASORT_PREFIX(x) s5m_##x
406 #define ASORT_KEY_TYPE u32
407 #define ASORT_ELT(i) ary[i]
408 #define ASORT_EXTRA_ARGS , u32 *ary
409 #include "lib/arraysort.h"
411 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
413 /* FIXME: Allow mode where this function is not defined? */
416 for (uns i=0; i<n; i++)
418 memcpy(&a[m], data[i], 4*keys[i]->cnt);
423 bwrite(f, keys[0], sizeof(struct key5));
424 bwrite(f, a, 4*m); /* FIXME: Might overflow here */
427 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
431 for (uns i=0; i<n; i++)
433 k[i] = bgetl(data[i]);
436 struct key5 key = { .x = keys[0]->x, .cnt = m };
437 bwrite(dest, &key, sizeof(key));
441 for (uns i=1; i<n; i++)
446 k[b] = bgetl(data[b]);
452 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
454 COMPARE_LT(x.x, y.x);
455 COMPARE_LT(x.y, y.y);
459 /* FIXME: Use smarter internal sorter when it's available */
460 #define ASORT_PREFIX(x) s5p_##x
461 #define ASORT_KEY_TYPE struct s5_pair
462 #define ASORT_ELT(i) ary[i]
463 #define ASORT_LT(x,y) s5p_lt(x,y)
464 #define ASORT_EXTRA_ARGS , struct s5_pair *ary
465 #include "lib/arraysort.h"
467 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
469 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
470 struct s5_pair *a = buf;
472 while (n<max && s5_gen(&a[n]))
481 while (i < n && a[i].x == a[j].x)
483 struct key5 k = { .x = a[j].x, .cnt = i-j };
484 bwrite(dest, &k, sizeof(k));
486 bputl(dest, a[j++].y);
491 #define SORT_KEY_REGULAR struct key5
492 #define SORT_PREFIX(x) s5_##x
493 #define SORT_DATA_SIZE(k) (4*(k).cnt)
495 #define SORT_INPUT_PRESORT
496 #define SORT_OUTPUT_THIS_FB
497 #define SORT_INT(k) (k).x
499 #include "lib/sorter/sorter.h"
501 #define SORT_KEY_REGULAR struct key5
502 #define SORT_PREFIX(x) s5b_##x
503 #define SORT_DATA_SIZE(k) (4*(k).cnt)
505 #define SORT_INPUT_FB
506 #define SORT_OUTPUT_THIS_FB
507 #define SORT_INT(k) (k).x
508 #define s5b_write_merged s5_write_merged
509 #define s5b_copy_merged s5_copy_merged
511 #include "lib/sorter/sorter.h"
514 test_graph(uns mode, u64 size)
517 while ((u64)N*(N+2)*4 < size)
519 log(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
525 struct fastbuf *in = NULL;
529 in = bopen_tmp(65536);
532 struct key5 k = { .x = p.x, .cnt = 1 };
533 bwrite(in, &k, sizeof(k));
540 struct fastbuf *f = bopen_tmp(65536);
541 bputl(f, 0xfeedcafe);
542 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
546 SORT_XTRACE(2, "Verifying");
548 ASSERT(c == 0xfeedcafe);
549 for (uns i=0; i<N; i++)
552 int ok = breadb(f, &k, sizeof(k));
556 for (uns j=0; j<N; j++)
568 run_test(uns i, u64 size)
573 test_int(0, size); break;
575 test_int(1, size); break;
577 test_int(2, size); break;
579 test_counted(0, size); break;
581 test_counted(1, size); break;
583 test_counted(2, size); break;
585 test_hashes(0, size); break;
587 test_hashes(1, size); break;
589 test_hashes(2, size); break;
591 test_strings(0, size); break;
593 test_strings(1, size); break;
595 test_graph(0, size); break;
597 test_graph(1, size); break;
603 main(int argc, char **argv)
610 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
614 sorter_debug = atol(optarg);
617 if (cf_parse_u64(optarg, &size))
630 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
639 for (uns i=0; i<TMAX; i++)