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 "ucw/getopt.h"
13 #include "ucw/fastbuf.h"
14 #include "ucw/ff-binary.h"
15 #include "ucw/hashfunc.h"
17 #include "ucw/string.h"
18 #include "ucw/prime.h"
26 /*** A hack for overriding radix-sorter configuration ***/
28 #ifdef FORCE_RADIX_BITS
29 #undef CONFIG_UCW_RADIX_SORTER_BITS
30 #define CONFIG_UCW_RADIX_SORTER_BITS FORCE_RADIX_BITS
33 /*** Time measurement ***/
35 static timestamp_t timer;
49 msg(L_INFO, "Test %d took %.3fs", test_id, get_timer(&timer) / 1000.);
52 /*** Simple 4-byte integer keys ***/
58 #define SORT_KEY_REGULAR struct key1
59 #define SORT_PREFIX(x) s1_##x
61 #define SORT_OUTPUT_FB
63 #define SORT_INT(k) (k).x
64 #define SORT_DELETE_INPUT 0
66 #include "ucw/sorter/sorter.h"
69 test_int(int mode, u64 size)
71 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
73 msg(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
75 struct fastbuf *f = bopen_tmp(65536);
76 for (uns i=0; i<N; i++)
77 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
81 f = s1_sort(f, NULL, N-1);
84 SORT_XTRACE(2, "Verifying");
85 for (uns i=0; i<N; i++)
89 die("Discrepancy: %u instead of %u", j, i);
94 /*** Integers with merging, but no data ***/
101 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
103 for (uns i=1; i<n; i++)
104 k[0]->cnt += k[i]->cnt;
105 bwrite(f, k[0], sizeof(struct key2));
108 #define SORT_KEY_REGULAR struct key2
109 #define SORT_PREFIX(x) s2_##x
110 #define SORT_INPUT_FB
111 #define SORT_OUTPUT_FB
113 #define SORT_INT(k) (k).x
115 #include "ucw/sorter/sorter.h"
118 test_counted(int mode, u64 size)
120 u64 items = size / sizeof(struct key2);
122 while (items/(2*mult) > 0xffff0000)
124 uns N = items ? nextprime(items/(2*mult)) : 0;
126 msg(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
128 struct fastbuf *f = bopen_tmp(65536);
129 for (uns m=0; m<mult; m++)
130 for (uns i=0; i<N; i++)
131 for (uns j=0; j<2; j++)
133 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
139 f = s2_sort(f, NULL, N-1);
142 SORT_XTRACE(2, "Verifying");
143 for (uns i=0; i<N; i++)
147 die("Discrepancy: %u instead of %u", j, i);
150 die("Discrepancy: %u has count %u instead of %u", j, k, 2*mult);
155 /*** Longer records with hashes (similar to Shepherd's index records) ***/
163 static inline int s3_compare(struct key3 *x, struct key3 *y)
165 COMPARE(x->hash[0], y->hash[0]);
166 COMPARE(x->hash[1], y->hash[1]);
167 COMPARE(x->hash[2], y->hash[2]);
168 COMPARE(x->hash[3], y->hash[3]);
172 static inline uns s3_hash(struct key3 *x)
177 #define SORT_KEY_REGULAR struct key3
178 #define SORT_PREFIX(x) s3_##x
179 #define SORT_INPUT_FB
180 #define SORT_OUTPUT_FB
181 #define SORT_HASH_BITS 32
183 #include "ucw/sorter/sorter.h"
186 gen_hash_key(int mode, struct key3 *k, uns i)
189 k->payload[0] = 7*i + 13;
190 k->payload[1] = 13*i + 19;
191 k->payload[2] = 19*i + 7;
196 k->hash[1] = k->payload[0];
197 k->hash[2] = k->payload[1];
198 k->hash[3] = k->payload[2];
202 k->hash[1] = k->payload[0];
203 k->hash[2] = k->payload[1];
204 k->hash[3] = k->payload[2];
207 md5_hash_buffer((byte *) &k->hash, (byte *) &k->i, 4);
213 test_hashes(int mode, u64 size)
215 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
216 msg(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
217 struct key3 k, lastk;
219 struct fastbuf *f = bopen_tmp(65536);
221 for (uns i=0; i<N; i++)
223 gen_hash_key(mode, &k, i);
224 hash_sum += k.hash[3];
225 bwrite(f, &k, sizeof(k));
230 f = s3_sort(f, NULL);
233 SORT_XTRACE(2, "Verifying");
234 for (uns i=0; i<N; i++)
236 int ok = breadb(f, &k, sizeof(k));
238 if (i && s3_compare(&k, &lastk) <= 0)
240 gen_hash_key(mode, &lastk, k.i);
241 if (memcmp(&k, &lastk, sizeof(k)))
243 hash_sum -= k.hash[3];
249 /*** Variable-length records (strings) with and without var-length data ***/
258 static inline int s4_compare(struct key4 *x, struct key4 *y)
260 uns l = MIN(x->len, y->len);
261 int c = memcmp(x->s, y->s, l);
264 COMPARE(x->len, y->len);
268 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
271 if (x->len == 0xffffffff)
273 ASSERT(x->len < KEY4_MAX);
274 breadb(f, x->s, x->len);
278 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
280 ASSERT(x->len < KEY4_MAX);
282 bwrite(f, x->s, x->len);
285 #define SORT_KEY struct key4
286 #define SORT_PREFIX(x) s4_##x
287 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
288 #define SORT_INPUT_FB
289 #define SORT_OUTPUT_FB
291 #include "ucw/sorter/sorter.h"
293 #define s4b_compare s4_compare
294 #define s4b_read_key s4_read_key
295 #define s4b_write_key s4_write_key
297 static inline uns s4_data_size(struct key4 *x)
299 return x->len ? (x->s[0] ^ 0xad) : 0;
302 #define SORT_KEY struct key4
303 #define SORT_PREFIX(x) s4b_##x
304 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
305 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
306 #define SORT_INPUT_FB
307 #define SORT_OUTPUT_FB
309 #include "ucw/sorter/sorter.h"
312 gen_key4(struct key4 *k)
314 k->len = random_max(KEY4_MAX);
315 for (uns i=0; i<k->len; i++)
320 gen_data4(byte *buf, uns len, uns h)
330 test_strings(uns mode, u64 size)
332 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
333 uns N = MIN(size / avg_item_size, 0xffffffff);
334 msg(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
337 struct key4 k, lastk;
338 byte buf[256], buf2[256];
341 struct fastbuf *f = bopen_tmp(65536);
342 for (uns i=0; i<N; i++)
346 uns h = hash_block(k.s, k.len);
350 gen_data4(buf, s4_data_size(&k), h);
351 bwrite(f, buf, s4_data_size(&k));
357 f = (mode ? s4b_sort : s4_sort)(f, NULL);
360 SORT_XTRACE(2, "Verifying");
361 for (uns i=0; i<N; i++)
363 int ok = s4_read_key(f, &k);
365 uns h = hash_block(k.s, k.len);
366 if (mode && s4_data_size(&k))
368 ok = breadb(f, buf, s4_data_size(&k));
370 gen_data4(buf2, s4_data_size(&k), h);
371 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
373 if (i && s4_compare(&k, &lastk) < 0)
382 /*** Graph-like structure with custom presorting ***/
389 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
395 static int s5_gen(struct s5_pair *p)
399 if (!s5_N || s5_i >= s5_N-1)
404 p->x = ((u64)s5_j * s5_K) % s5_N;
405 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
410 #define ASORT_PREFIX(x) s5m_##x
411 #define ASORT_KEY_TYPE u32
412 #define ASORT_ELT(i) ary[i]
413 #define ASORT_EXTRA_ARGS , u32 *ary
414 #include "ucw/arraysort.h"
416 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
420 for (uns i=0; i<n; i++)
422 memcpy(&a[m], data[i], 4*keys[i]->cnt);
427 bwrite(f, keys[0], sizeof(struct key5));
431 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
435 for (uns i=0; i<n; i++)
437 k[i] = bgetl(data[i]);
440 struct key5 key = { .x = keys[0]->x, .cnt = m };
441 bwrite(dest, &key, sizeof(key));
445 for (uns i=1; i<n; i++)
450 k[b] = bgetl(data[b]);
456 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
458 COMPARE_LT(x.x, y.x);
459 COMPARE_LT(x.y, y.y);
463 #define ASORT_PREFIX(x) s5p_##x
464 #define ASORT_KEY_TYPE struct s5_pair
465 #define ASORT_LT(x,y) s5p_lt(x,y)
466 #include "ucw/sorter/array.h"
468 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
470 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
471 struct s5_pair *a = buf;
473 while (n<max && s5_gen(&a[n]))
482 while (i < n && a[i].x == a[j].x)
484 struct key5 k = { .x = a[j].x, .cnt = i-j };
485 bwrite(dest, &k, sizeof(k));
487 bputl(dest, a[j++].y);
492 #define SORT_KEY_REGULAR struct key5
493 #define SORT_PREFIX(x) s5_##x
494 #define SORT_DATA_SIZE(k) (4*(k).cnt)
496 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
497 #define SORT_INPUT_PRESORT
498 #define SORT_OUTPUT_THIS_FB
499 #define SORT_INT(k) (k).x
501 #include "ucw/sorter/sorter.h"
503 #define SORT_KEY_REGULAR struct key5
504 #define SORT_PREFIX(x) s5b_##x
505 #define SORT_DATA_SIZE(k) (4*(k).cnt)
507 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
508 #define SORT_INPUT_FB
509 #define SORT_OUTPUT_THIS_FB
510 #define SORT_INT(k) (k).x
511 #define s5b_write_merged s5_write_merged
512 #define s5b_copy_merged s5_copy_merged
514 #include "ucw/sorter/sorter.h"
517 test_graph(uns mode, u64 size)
520 while ((u64)N*(N+2)*4 < size)
524 msg(L_INFO, ">>> Graph%s (N=%u)", (mode ? "" : " with custom presorting"), N);
530 struct fastbuf *in = NULL;
534 in = bopen_tmp(65536);
537 struct key5 k = { .x = p.x, .cnt = 1 };
538 bwrite(in, &k, sizeof(k));
545 struct fastbuf *f = bopen_tmp(65536);
546 bputl(f, 0xfeedcafe);
547 struct fastbuf *g = (mode ? s5b_sort(in, f, s5_N-1) : s5_sort(NULL, f, s5_N-1));
551 SORT_XTRACE(2, "Verifying");
553 ASSERT(c == 0xfeedcafe);
554 for (uns i=0; i<N; i++)
557 int ok = breadb(f, &k, sizeof(k));
561 for (uns j=0; j<N; j++)
570 /*** Simple 8-byte integer keys ***/
576 #define SORT_KEY_REGULAR struct key6
577 #define SORT_PREFIX(x) s6_##x
578 #define SORT_INPUT_FB
579 #define SORT_OUTPUT_FB
581 #define SORT_INT64(k) (k).x
583 #include "ucw/sorter/sorter.h"
586 test_int64(int mode, u64 size)
588 u64 N = size ? nextprime(MIN(size/8, 0xffff0000)) : 0;
590 msg(L_INFO, ">>> 64-bit integers (%s, N=%llu)", ((char *[]) { "increasing", "decreasing", "random" })[mode], (long long)N);
592 struct fastbuf *f = bopen_tmp(65536);
593 for (u64 i=0; i<N; i++)
594 bputq(f, 777777*((mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N));
598 f = s6_sort(f, NULL, 777777*(N-1));
601 SORT_XTRACE(2, "Verifying");
602 for (u64 i=0; i<N; i++)
606 die("Discrepancy: %llu instead of %llu", (long long)j, 777777*(long long)i);
614 run_test(uns i, u64 size)
620 test_int(0, size); break;
622 test_int(1, size); break;
624 test_int(2, size); break;
626 test_counted(0, size); break;
628 test_counted(1, size); break;
630 test_counted(2, size); break;
632 test_hashes(0, size); break;
634 test_hashes(1, size); break;
636 test_hashes(2, size); break;
638 test_strings(0, size); break;
640 test_strings(1, size); break;
642 test_graph(0, size); break;
644 test_graph(1, size); break;
646 test_int64(0, size); break;
648 test_int64(1, size); break;
650 test_int64(2, size); break;
656 main(int argc, char **argv)
663 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
667 sorter_debug = atol(optarg);
670 if (cf_parse_u64(optarg, &size))
676 int f = str_sepsplit(optarg, ',', w, ARRAY_SIZE(w));
680 for (int i=0; i<f; i++)
694 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
700 for (uns i=0; i<TMAX; i++)