]> mj.ucw.cz Git - libucw.git/blob - lib/sorter/sort-test.c
Implemented SORT_UNIFY, SORT_UNIQUE and debugged SORT_VAR_DATA.
[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 static inline void s_write_merged(struct fastbuf *f, struct key **k, void **d, uns n, void *buf)
17 {
18   bwrite(f, k[0], sizeof(struct key));
19   bwrite(f, d[0], 5);
20 }
21
22 static inline void s_copy_merged(struct key **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
23 {
24   bwrite(dest, keys[0], sizeof(struct key));
25   bbcopy(data[0], dest, 5);
26   for (uns i=1; i<n; i++)
27     bskip(data[i], 5);
28 }
29
30 #define SORT_KEY_REGULAR struct key
31 #define SORT_PREFIX(x) s_##x
32 #define SORT_INPUT_FB
33 #define SORT_OUTPUT_FB
34 //#define SORT_KEY_SIZE(k) 4
35 #define SORT_DATA_SIZE(k) 5
36 //#define SORT_UNIQUE
37 #define SORT_UNIFY
38 #define SORT_INT(k) (k).x
39
40 #include "lib/sorter/sorter.h"
41
42 int
43 main(int argc, char **argv)
44 {
45   log_init(NULL);
46   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
47       optind != argc - 2)
48   {
49     fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
50     exit(1);
51   }
52
53   log(L_INFO, "Generating");
54   struct fastbuf *f = bopen(argv[optind], O_RDWR | O_CREAT | O_TRUNC, 65536);
55 #define N 259309
56 #define K 199483
57   for (uns i=0; i<2*N; i++)
58     {
59       bputl(f, ((u64)i * K + 17) % N);
60       bputs(f, "12345");
61       bputl(f, ((u64)i * K + 17) % N);
62       bputs(f, "12345");
63     }
64   brewind(f);
65
66   log(L_INFO, "Sorting");
67   f = s_sort(f, NULL, N-1);
68
69   log(L_INFO, "Verifying");
70   for (uns i=0; i<N; i++)
71     {
72       uns j = bgetl(f);
73       if (i != j)
74         die("Discrepancy: %d instead of %d", j, i);
75       for (uns i='1'; i<='5'; i++)
76         if (bgetc(f) != i)
77           ASSERT(0);
78     }
79   bclose(f);
80
81   return 0;
82 }