]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/sort-test.c
Several bits of the new sorter.
[libucw.git] / lib / sorter / sort-test.c
1 /* A test of sorting routines */
2
3 #include "lib/lib.h"
4 #include "lib/getopt.h"
5 #include "lib/fastbuf.h"
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <fcntl.h>
11
12 struct key {
13   uns x;
14 };
15
16 #define SORT_KEY_REGULAR struct key
17 #define SORT_PREFIX(x) s_##x
18 #define SORT_INPUT_FB
19 #define SORT_OUTPUT_FB
20 #define SORT_INT(k) (k).x
21
22 #include "lib/sorter/sorter.h"
23
24 int
25 main(int argc, char **argv)
26 {
27   log_init(NULL);
28   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
29       optind != argc - 2)
30   {
31     fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
32     exit(1);
33   }
34
35   log(L_INFO, "Generating");
36   struct fastbuf *f = bopen(argv[optind], O_RDWR | O_CREAT | O_TRUNC, 65536);
37 #define N 259309
38 #define K 199483
39   for (uns i=0; i<N; i++)
40     bputl(f, ((u64)i * K + 17) % N);
41   brewind(f);
42
43   log(L_INFO, "Sorting");
44   f = s_sort(f, NULL, N-1);
45
46   log(L_INFO, "Verifying");
47   for (uns i=0; i<N; i++)
48     {
49       uns j = bgetl(f);
50       if (i != j)
51         die("Discrepancy: %d instead of %d", j, i);
52     }
53   bclose(f);
54
55   return 0;
56 }