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"
12 #include "lib/fastbuf.h"
20 /*** Simple 4-byte integer keys ***/
26 #define SORT_KEY_REGULAR struct key1
27 #define SORT_PREFIX(x) s1_##x
29 #define SORT_OUTPUT_FB
31 #define SORT_INT(k) (k).x
33 #include "lib/sorter/sorter.h"
36 test_int(int mode, uns N)
40 log(L_INFO, "Integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
42 struct fastbuf *f = bopen_tmp(65536);
43 for (uns i=0; i<N; i++)
44 bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
47 log(L_INFO, "Sorting");
48 f = s1_sort(f, NULL, N-1);
50 log(L_INFO, "Verifying");
51 for (uns i=0; i<N; i++)
55 die("Discrepancy: %d instead of %d", j, i);
60 /*** Integers with merging, but no data ***/
67 static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
69 for (uns i=1; i<n; i++)
70 k[0]->cnt += k[i]->cnt;
71 bwrite(f, k[0], sizeof(struct key2));
74 static inline void s2_copy_merged(struct key2 **k, struct fastbuf **d UNUSED, uns n, struct fastbuf *dest)
76 for (uns i=1; i<n; i++)
77 k[0]->cnt += k[i]->cnt;
78 bwrite(dest, k[0], sizeof(struct key2));
81 #define SORT_KEY_REGULAR struct key2
82 #define SORT_PREFIX(x) s2_##x
84 #define SORT_OUTPUT_FB
86 #define SORT_INT(k) (k).x
88 #include "lib/sorter/sorter.h"
91 test_counted(int mode, uns N)
95 log(L_INFO, "Counted integers (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
97 struct fastbuf *f = bopen_tmp(65536);
98 for (uns i=0; i<2*N; i++)
99 for (uns j=0; j<2; j++)
101 bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
106 log(L_INFO, "Sorting");
107 f = s2_sort(f, NULL, N-1);
109 log(L_INFO, "Verifying");
110 for (uns i=0; i<N; i++)
114 die("Discrepancy: %d instead of %d", j, i);
117 die("Discrepancy: %d has count %d instead of 4", j, k);
122 /*** Longer records with hashes (similar to Shepherd's index records) ***/
130 static inline int s3_compare(struct key3 *x, struct key3 *y)
132 /* FIXME: Maybe unroll manually? */
133 for (uns i=0; i<4; i++)
134 if (x->hash[i] < y->hash[i])
136 else if (x->hash[i] > y->hash[i])
141 static inline uns s3_hash(struct key3 *x)
146 #define SORT_KEY_REGULAR struct key3
147 #define SORT_PREFIX(x) s3_##x
148 #define SORT_INPUT_FB
149 #define SORT_OUTPUT_FB
150 #define SORT_HASH_BITS 32
152 #include "lib/sorter/sorter.h"
155 gen_hash_key(int mode, struct key3 *k, uns i)
158 k->payload[0] = 7*i + 13;
159 k->payload[1] = 13*i + 19;
160 k->payload[2] = 19*i + 7;
165 k->hash[1] = k->payload[0];
166 k->hash[2] = k->payload[1];
167 k->hash[3] = k->payload[2];
171 k->hash[1] = k->payload[0];
172 k->hash[2] = k->payload[1];
173 k->hash[3] = k->payload[2];
176 struct MD5Context ctx;
178 MD5Update(&ctx, (byte*) &k->i, 4);
179 MD5Final((byte*) &k->hash, &ctx);
185 test_hashes(int mode, uns N)
187 log(L_INFO, "Hashes (%s, N=%d)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
188 struct key3 k, lastk;
190 struct fastbuf *f = bopen_tmp(65536);
192 for (uns i=0; i<N; i++)
194 gen_hash_key(mode, &k, i);
195 hash_sum += k.hash[3];
196 bwrite(f, &k, sizeof(k));
200 log(L_INFO, "Sorting");
201 f = s3_sort(f, NULL);
203 log(L_INFO, "Verifying");
204 for (uns i=0; i<N; i++)
206 int ok = breadb(f, &k, sizeof(k));
208 if (i && s3_compare(&k, &lastk) <= 0)
210 gen_hash_key(mode, &lastk, k.i);
211 if (memcmp(&k, &lastk, sizeof(k)))
213 hash_sum -= k.hash[3];
220 main(int argc, char **argv)
223 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
226 fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);