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/ff-binary.h"
15 #include "lib/hashfunc.h"
24 /*** Time measurement ***/
26 static timestamp_t timer;
40 msg(L_INFO, "Test %d took %.3fs", test_id, get_timer(&timer) / 1000.);
43 /*** Simple 4-byte integer keys ***/
49 #define SORT_KEY_REGULAR struct key1
50 #define SORT_PREFIX(x) s1_##x
52 #define SORT_OUTPUT_FB
54 #define SORT_INT(k) (k).x
55 #define SORT_DELETE_INPUT 0
57 #include "lib/sorter/sorter.h"
60 test_int(int mode, u64 size)
62 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
64 msg(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
66 struct fastbuf *f = bopen_tmp(65536);
67 for (uns i=0; i<N; i++)
68 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
72 f = s1_sort(f, NULL, N-1);
75 SORT_XTRACE(2, "Verifying");
76 for (uns i=0; i<N; i++)
80 die("Discrepancy: %u instead of %u", j, i);
85 /*** Integers with merging, but no data ***/
92 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
94 for (uns i=1; i<n; i++)
95 k[0]->cnt += k[i]->cnt;
96 bwrite(f, k[0], sizeof(struct key2));
99 #define SORT_KEY_REGULAR struct key2
100 #define SORT_PREFIX(x) s2_##x
101 #define SORT_INPUT_FB
102 #define SORT_OUTPUT_FB
104 #define SORT_INT(k) (k).x
106 #include "lib/sorter/sorter.h"
109 test_counted(int mode, u64 size)
111 u64 items = size / sizeof(struct key2);
113 while (items/(2*mult) > 0xffff0000)
115 uns N = items ? nextprime(items/(2*mult)) : 0;
117 msg(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
119 struct fastbuf *f = bopen_tmp(65536);
120 for (uns m=0; m<mult; m++)
121 for (uns i=0; i<N; i++)
122 for (uns j=0; j<2; j++)
124 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
130 f = s2_sort(f, NULL, N-1);
133 SORT_XTRACE(2, "Verifying");
134 for (uns i=0; i<N; i++)
138 die("Discrepancy: %u instead of %u", j, i);
141 die("Discrepancy: %u has count %u instead of %u", j, k, 2*mult);
146 /*** Longer records with hashes (similar to Shepherd's index records) ***/
154 static inline int s3_compare(struct key3 *x, struct key3 *y)
156 COMPARE(x->hash[0], y->hash[0]);
157 COMPARE(x->hash[1], y->hash[1]);
158 COMPARE(x->hash[2], y->hash[2]);
159 COMPARE(x->hash[3], y->hash[3]);
163 static inline uns s3_hash(struct key3 *x)
168 #define SORT_KEY_REGULAR struct key3
169 #define SORT_PREFIX(x) s3_##x
170 #define SORT_INPUT_FB
171 #define SORT_OUTPUT_FB
172 #define SORT_HASH_BITS 32
174 #include "lib/sorter/sorter.h"
177 gen_hash_key(int mode, struct key3 *k, uns i)
180 k->payload[0] = 7*i + 13;
181 k->payload[1] = 13*i + 19;
182 k->payload[2] = 19*i + 7;
187 k->hash[1] = k->payload[0];
188 k->hash[2] = k->payload[1];
189 k->hash[3] = k->payload[2];
193 k->hash[1] = k->payload[0];
194 k->hash[2] = k->payload[1];
195 k->hash[3] = k->payload[2];
198 struct MD5Context ctx;
200 MD5Update(&ctx, (byte*) &k->i, 4);
201 MD5Final((byte*) &k->hash, &ctx);
207 test_hashes(int mode, u64 size)
209 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
210 msg(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
211 struct key3 k, lastk;
213 struct fastbuf *f = bopen_tmp(65536);
215 for (uns i=0; i<N; i++)
217 gen_hash_key(mode, &k, i);
218 hash_sum += k.hash[3];
219 bwrite(f, &k, sizeof(k));
224 f = s3_sort(f, NULL);
227 SORT_XTRACE(2, "Verifying");
228 for (uns i=0; i<N; i++)
230 int ok = breadb(f, &k, sizeof(k));
232 if (i && s3_compare(&k, &lastk) <= 0)
234 gen_hash_key(mode, &lastk, k.i);
235 if (memcmp(&k, &lastk, sizeof(k)))
237 hash_sum -= k.hash[3];
243 /*** Variable-length records (strings) with and without var-length data ***/
252 static inline int s4_compare(struct key4 *x, struct key4 *y)
254 uns l = MIN(x->len, y->len);
255 int c = memcmp(x->s, y->s, l);
258 COMPARE(x->len, y->len);
262 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
265 if (x->len == 0xffffffff)
267 ASSERT(x->len < KEY4_MAX);
268 breadb(f, x->s, x->len);
272 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
274 ASSERT(x->len < KEY4_MAX);
276 bwrite(f, x->s, x->len);
279 #define SORT_KEY struct key4
280 #define SORT_PREFIX(x) s4_##x
281 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
282 #define SORT_INPUT_FB
283 #define SORT_OUTPUT_FB
285 #include "lib/sorter/sorter.h"
287 #define s4b_compare s4_compare
288 #define s4b_read_key s4_read_key
289 #define s4b_write_key s4_write_key
291 static inline uns s4_data_size(struct key4 *x)
293 return x->len ? (x->s[0] ^ 0xad) : 0;
296 #define SORT_KEY struct key4
297 #define SORT_PREFIX(x) s4b_##x
298 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
299 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
300 #define SORT_INPUT_FB
301 #define SORT_OUTPUT_FB
303 #include "lib/sorter/sorter.h"
306 gen_key4(struct key4 *k)
308 k->len = random_max(KEY4_MAX);
309 for (uns i=0; i<k->len; i++)
314 gen_data4(byte *buf, uns len, uns h)
324 test_strings(uns mode, u64 size)
326 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
327 uns N = MIN(size / avg_item_size, 0xffffffff);
328 msg(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
331 struct key4 k, lastk;
332 byte buf[256], buf2[256];
335 struct fastbuf *f = bopen_tmp(65536);
336 for (uns i=0; i<N; i++)
340 uns h = hash_block(k.s, k.len);
344 gen_data4(buf, s4_data_size(&k), h);
345 bwrite(f, buf, s4_data_size(&k));
351 f = (mode ? s4b_sort : s4_sort)(f, NULL);
354 SORT_XTRACE(2, "Verifying");
355 for (uns i=0; i<N; i++)
357 int ok = s4_read_key(f, &k);
359 uns h = hash_block(k.s, k.len);
360 if (mode && s4_data_size(&k))
362 ok = breadb(f, buf, s4_data_size(&k));
364 gen_data4(buf2, s4_data_size(&k), h);
365 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
367 if (i && s4_compare(&k, &lastk) < 0)
376 /*** Graph-like structure with custom presorting ***/
383 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
389 static int s5_gen(struct s5_pair *p)
398 p->x = ((u64)s5_j * s5_K) % s5_N;
399 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
404 #define ASORT_PREFIX(x) s5m_##x
405 #define ASORT_KEY_TYPE u32
406 #define ASORT_ELT(i) ary[i]
407 #define ASORT_EXTRA_ARGS , u32 *ary
408 #include "lib/arraysort.h"
410 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
414 for (uns i=0; i<n; i++)
416 memcpy(&a[m], data[i], 4*keys[i]->cnt);
421 bwrite(f, keys[0], sizeof(struct key5));
425 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
429 for (uns i=0; i<n; i++)
431 k[i] = bgetl(data[i]);
434 struct key5 key = { .x = keys[0]->x, .cnt = m };
435 bwrite(dest, &key, sizeof(key));
439 for (uns i=1; i<n; i++)
444 k[b] = bgetl(data[b]);
450 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
452 COMPARE_LT(x.x, y.x);
453 COMPARE_LT(x.y, y.y);
457 #define ASORT_PREFIX(x) s5p_##x
458 #define ASORT_KEY_TYPE struct s5_pair
459 #define ASORT_LT(x,y) s5p_lt(x,y)
460 #include "lib/sorter/array.h"
462 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
464 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
465 struct s5_pair *a = buf;
467 while (n<max && s5_gen(&a[n]))
471 s5p_sort(a, n, NULL, 0);
476 while (i < n && a[i].x == a[j].x)
478 struct key5 k = { .x = a[j].x, .cnt = i-j };
479 bwrite(dest, &k, sizeof(k));
481 bputl(dest, a[j++].y);
486 #define SORT_KEY_REGULAR struct key5
487 #define SORT_PREFIX(x) s5_##x
488 #define SORT_DATA_SIZE(k) (4*(k).cnt)
490 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
491 #define SORT_INPUT_PRESORT
492 #define SORT_OUTPUT_THIS_FB
493 #define SORT_INT(k) (k).x
495 #include "lib/sorter/sorter.h"
497 #define SORT_KEY_REGULAR struct key5
498 #define SORT_PREFIX(x) s5b_##x
499 #define SORT_DATA_SIZE(k) (4*(k).cnt)
501 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
502 #define SORT_INPUT_FB
503 #define SORT_OUTPUT_THIS_FB
504 #define SORT_INT(k) (k).x
505 #define s5b_write_merged s5_write_merged
506 #define s5b_copy_merged s5_copy_merged
508 #include "lib/sorter/sorter.h"
511 test_graph(uns mode, u64 size)
514 while ((u64)N*(N+2)*4 < size)
516 msg(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
522 struct fastbuf *in = NULL;
526 in = bopen_tmp(65536);
529 struct key5 k = { .x = p.x, .cnt = 1 };
530 bwrite(in, &k, sizeof(k));
537 struct fastbuf *f = bopen_tmp(65536);
538 bputl(f, 0xfeedcafe);
539 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
543 SORT_XTRACE(2, "Verifying");
545 ASSERT(c == 0xfeedcafe);
546 for (uns i=0; i<N; i++)
549 int ok = breadb(f, &k, sizeof(k));
553 for (uns j=0; j<N; j++)
562 /*** Simple 8-byte integer keys ***/
568 #define SORT_KEY_REGULAR struct key6
569 #define SORT_PREFIX(x) s6_##x
570 #define SORT_INPUT_FB
571 #define SORT_OUTPUT_FB
573 #define SORT_INT64(k) (k).x
575 #include "lib/sorter/sorter.h"
578 test_int64(int mode, u64 size)
580 u64 N = size ? nextprime(MIN(size/8, 0xffff0000)) : 0;
582 msg(L_INFO, ">>> 64-bit integers (%s, N=%llu)", ((char *[]) { "increasing", "decreasing", "random" })[mode], (long long)N);
584 struct fastbuf *f = bopen_tmp(65536);
585 for (u64 i=0; i<N; i++)
586 bputq(f, 777777*((mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N));
590 f = s6_sort(f, NULL, 777777*(N-1));
593 SORT_XTRACE(2, "Verifying");
594 for (u64 i=0; i<N; i++)
598 die("Discrepancy: %llu instead of %llu", (long long)j, 777777*(long long)i);
606 run_test(uns i, u64 size)
612 test_int(0, size); break;
614 test_int(1, size); break;
616 test_int(2, size); break;
618 test_counted(0, size); break;
620 test_counted(1, size); break;
622 test_counted(2, size); break;
624 test_hashes(0, size); break;
626 test_hashes(1, size); break;
628 test_hashes(2, size); break;
630 test_strings(0, size); break;
632 test_strings(1, size); break;
634 test_graph(0, size); break;
636 test_graph(1, size); break;
638 test_int64(0, size); break;
640 test_int64(1, size); break;
642 test_int64(2, size); break;
648 main(int argc, char **argv)
655 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
659 sorter_debug = atol(optarg);
662 if (cf_parse_u64(optarg, &size))
675 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
684 for (uns i=0; i<TMAX; i++)