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;
39 log(L_INFO, "Test took %.3fs", get_timer(&timer) / 1000.);
42 /*** Simple 4-byte integer keys ***/
48 #define SORT_KEY_REGULAR struct key1
49 #define SORT_PREFIX(x) s1_##x
51 #define SORT_OUTPUT_FB
53 #define SORT_INT(k) (k).x
54 #define SORT_DELETE_INPUT 0
56 #include "lib/sorter/sorter.h"
59 test_int(int mode, u64 size)
61 uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
63 log(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
65 struct fastbuf *f = bopen_tmp(65536);
66 for (uns i=0; i<N; i++)
67 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
71 f = s1_sort(f, NULL, N-1);
74 SORT_XTRACE(2, "Verifying");
75 for (uns i=0; i<N; i++)
79 die("Discrepancy: %u instead of %u", j, i);
84 /*** Integers with merging, but no data ***/
91 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
93 for (uns i=1; i<n; i++)
94 k[0]->cnt += k[i]->cnt;
95 bwrite(f, k[0], sizeof(struct key2));
98 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
100 for (uns i=1; i<n; i++)
101 k[0]->cnt += k[i]->cnt;
102 bwrite(dest, k[0], sizeof(struct key2));
105 #define SORT_KEY_REGULAR struct key2
106 #define SORT_PREFIX(x) s2_##x
107 #define SORT_INPUT_FB
108 #define SORT_OUTPUT_FB
110 #define SORT_INT(k) (k).x
112 #include "lib/sorter/sorter.h"
115 test_counted(int mode, u64 size)
117 u64 items = size / sizeof(struct key2);
119 while (items/(2*mult) > 0xffff0000)
121 uns N = items ? nextprime(items/(2*mult)) : 0;
123 log(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
125 struct fastbuf *f = bopen_tmp(65536);
126 for (uns m=0; m<mult; m++)
127 for (uns i=0; i<N; i++)
128 for (uns j=0; j<2; j++)
130 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
136 f = s2_sort(f, NULL, N-1);
139 SORT_XTRACE(2, "Verifying");
140 for (uns i=0; i<N; i++)
144 die("Discrepancy: %u instead of %u", j, i);
147 die("Discrepancy: %u has count %u instead of %u", j, k, mult);
152 /*** Longer records with hashes (similar to Shepherd's index records) ***/
160 static inline int s3_compare(struct key3 *x, struct key3 *y)
162 /* FIXME: Maybe unroll manually? */
163 for (uns i=0; i<4; i++)
164 COMPARE(x->hash[i], y->hash[i]);
168 static inline uns s3_hash(struct key3 *x)
173 #define SORT_KEY_REGULAR struct key3
174 #define SORT_PREFIX(x) s3_##x
175 #define SORT_INPUT_FB
176 #define SORT_OUTPUT_FB
177 #define SORT_HASH_BITS 32
179 #include "lib/sorter/sorter.h"
182 gen_hash_key(int mode, struct key3 *k, uns i)
185 k->payload[0] = 7*i + 13;
186 k->payload[1] = 13*i + 19;
187 k->payload[2] = 19*i + 7;
192 k->hash[1] = k->payload[0];
193 k->hash[2] = k->payload[1];
194 k->hash[3] = k->payload[2];
198 k->hash[1] = k->payload[0];
199 k->hash[2] = k->payload[1];
200 k->hash[3] = k->payload[2];
203 struct MD5Context ctx;
205 MD5Update(&ctx, (byte*) &k->i, 4);
206 MD5Final((byte*) &k->hash, &ctx);
212 test_hashes(int mode, u64 size)
214 uns N = MIN(size / sizeof(struct key3), 0xffffffff);
215 log(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
216 struct key3 k, lastk;
218 struct fastbuf *f = bopen_tmp(65536);
220 for (uns i=0; i<N; i++)
222 gen_hash_key(mode, &k, i);
223 hash_sum += k.hash[3];
224 bwrite(f, &k, sizeof(k));
229 f = s3_sort(f, NULL);
232 SORT_XTRACE(2, "Verifying");
233 for (uns i=0; i<N; i++)
235 int ok = breadb(f, &k, sizeof(k));
237 if (i && s3_compare(&k, &lastk) <= 0)
239 gen_hash_key(mode, &lastk, k.i);
240 if (memcmp(&k, &lastk, sizeof(k)))
242 hash_sum -= k.hash[3];
248 /*** Variable-length records (strings) with and without var-length data ***/
257 static inline int s4_compare(struct key4 *x, struct key4 *y)
259 uns l = MIN(x->len, y->len);
260 int c = memcmp(x->s, y->s, l);
263 COMPARE(x->len, y->len);
267 static inline int s4_read_key(struct fastbuf *f, struct key4 *x)
270 if (x->len == 0xffffffff)
272 ASSERT(x->len < KEY4_MAX);
273 breadb(f, x->s, x->len);
277 static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
279 ASSERT(x->len < KEY4_MAX);
281 bwrite(f, x->s, x->len);
284 #define SORT_KEY struct key4
285 #define SORT_PREFIX(x) s4_##x
286 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
287 #define SORT_INPUT_FB
288 #define SORT_OUTPUT_FB
290 #include "lib/sorter/sorter.h"
292 #define s4b_compare s4_compare
293 #define s4b_read_key s4_read_key
294 #define s4b_write_key s4_write_key
296 static inline uns s4_data_size(struct key4 *x)
298 return x->len ? (x->s[0] ^ 0xad) : 0;
301 #define SORT_KEY struct key4
302 #define SORT_PREFIX(x) s4b_##x
303 #define SORT_KEY_SIZE(x) (sizeof(struct key4) - KEY4_MAX + (x).len)
304 #define SORT_DATA_SIZE(x) s4_data_size(&(x))
305 #define SORT_INPUT_FB
306 #define SORT_OUTPUT_FB
308 #include "lib/sorter/sorter.h"
311 gen_key4(struct key4 *k)
313 k->len = random_max(KEY4_MAX);
314 for (uns i=0; i<k->len; i++)
319 gen_data4(byte *buf, uns len, uns h)
329 test_strings(uns mode, u64 size)
331 uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
332 uns N = MIN(size / avg_item_size, 0xffffffff);
333 log(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
336 struct key4 k, lastk;
337 byte buf[256], buf2[256];
340 struct fastbuf *f = bopen_tmp(65536);
341 for (uns i=0; i<N; i++)
345 uns h = hash_block(k.s, k.len);
349 gen_data4(buf, s4_data_size(&k), h);
350 bwrite(f, buf, s4_data_size(&k));
356 f = (mode ? s4b_sort : s4_sort)(f, NULL);
359 SORT_XTRACE(2, "Verifying");
360 for (uns i=0; i<N; i++)
362 int ok = s4_read_key(f, &k);
364 uns h = hash_block(k.s, k.len);
365 if (mode && s4_data_size(&k))
367 ok = breadb(f, buf, s4_data_size(&k));
369 gen_data4(buf2, s4_data_size(&k), h);
370 ASSERT(!memcmp(buf, buf2, s4_data_size(&k)));
372 if (i && s4_compare(&k, &lastk) < 0)
381 /*** Graph-like structure with custom presorting ***/
388 static uns s5_N, s5_K, s5_L, s5_i, s5_j;
394 static int s5_gen(struct s5_pair *p)
403 p->x = ((u64)s5_j * s5_K) % s5_N;
404 p->y = ((u64)(s5_i + s5_j) * s5_L) % s5_N;
409 #define ASORT_PREFIX(x) s5m_##x
410 #define ASORT_KEY_TYPE u32
411 #define ASORT_ELT(i) ary[i]
412 #define ASORT_EXTRA_ARGS , u32 *ary
413 #include "lib/arraysort.h"
415 static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
419 for (uns i=0; i<n; i++)
421 memcpy(&a[m], data[i], 4*keys[i]->cnt);
426 bwrite(f, keys[0], sizeof(struct key5));
427 bwrite(f, a, 4*m); /* FIXME: Might overflow here */
430 static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
434 for (uns i=0; i<n; i++)
436 k[i] = bgetl(data[i]);
439 struct key5 key = { .x = keys[0]->x, .cnt = m };
440 bwrite(dest, &key, sizeof(key));
444 for (uns i=1; i<n; i++)
449 k[b] = bgetl(data[b]);
455 static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
457 COMPARE_LT(x.x, y.x);
458 COMPARE_LT(x.y, y.y);
462 /* FIXME: Use smarter internal sorter when it's available */
463 #define ASORT_PREFIX(x) s5p_##x
464 #define ASORT_KEY_TYPE struct s5_pair
465 #define ASORT_ELT(i) ary[i]
466 #define ASORT_LT(x,y) s5p_lt(x,y)
467 #define ASORT_EXTRA_ARGS , struct s5_pair *ary
468 #include "lib/arraysort.h"
470 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
472 uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
473 struct s5_pair *a = buf;
475 while (n<max && s5_gen(&a[n]))
484 while (i < n && a[i].x == a[j].x)
486 struct key5 k = { .x = a[j].x, .cnt = i-j };
487 bwrite(dest, &k, sizeof(k));
489 bputl(dest, a[j++].y);
494 #define SORT_KEY_REGULAR struct key5
495 #define SORT_PREFIX(x) s5_##x
496 #define SORT_DATA_SIZE(k) (4*(k).cnt)
498 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
499 #define SORT_INPUT_PRESORT
500 #define SORT_OUTPUT_THIS_FB
501 #define SORT_INT(k) (k).x
503 #include "lib/sorter/sorter.h"
505 #define SORT_KEY_REGULAR struct key5
506 #define SORT_PREFIX(x) s5b_##x
507 #define SORT_DATA_SIZE(k) (4*(k).cnt)
509 #define SORT_UNIFY_WORKSPACE(k) SORT_DATA_SIZE(k)
510 #define SORT_INPUT_FB
511 #define SORT_OUTPUT_THIS_FB
512 #define SORT_INT(k) (k).x
513 #define s5b_write_merged s5_write_merged
514 #define s5b_copy_merged s5_copy_merged
516 #include "lib/sorter/sorter.h"
519 test_graph(uns mode, u64 size)
522 while ((u64)N*(N+2)*4 < size)
524 log(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++)
573 run_test(uns i, u64 size)
578 test_int(0, size); break;
580 test_int(1, size); break;
582 test_int(2, size); break;
584 test_counted(0, size); break;
586 test_counted(1, size); break;
588 test_counted(2, size); break;
590 test_hashes(0, size); break;
592 test_hashes(1, size); break;
594 test_hashes(2, size); break;
596 test_strings(0, size); break;
598 test_strings(1, size); break;
600 test_graph(0, size); break;
602 test_graph(1, size); break;
608 main(int argc, char **argv)
615 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
619 sorter_debug = atol(optarg);
622 if (cf_parse_u64(optarg, &size))
635 fputs("Usage: sort-test [-v] [-d <debug>] [-s <size>] [-t <test>]\n", stderr);
644 for (uns i=0; i<TMAX; i++)