]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/sorter/sort-test.c
Opt: Added OPT_HELP_COLUMNS
[libucw.git] / ucw / sorter / sort-test.c
index 17c6e7f0d09c72b7aeeed71adf021df7b8d9e06c..f8384a7554a144eba31211e414ab1fb2b282f037 100644 (file)
@@ -7,15 +7,16 @@
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/getopt.h"
-#include "ucw/conf.h"
-#include "ucw/fastbuf.h"
-#include "ucw/ff-binary.h"
-#include "ucw/hashfunc.h"
-#include "ucw/md5.h"
-#include "ucw/string.h"
-#include "ucw/prime.h"
+#include <ucw/lib.h>
+#include <ucw/getopt.h>
+#include <ucw/conf.h>
+#include <ucw/fastbuf.h>
+#include <ucw/ff-binary.h>
+#include <ucw/hashfunc.h>
+#include <ucw/md5.h>
+#include <ucw/string.h>
+#include <ucw/prime.h>
+#include <ucw/time.h>
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -33,7 +34,7 @@
 /*** Time measurement ***/
 
 static timestamp_t timer;
-static uns test_id;
+static uint test_id;
 
 static void
 start(void)
@@ -63,17 +64,17 @@ struct key1 {
 #define SORT_INT(k) (k).x
 #define SORT_DELETE_INPUT 0
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 static void
 test_int(int mode, u64 size)
 {
-  uns N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
-  uns K = N/4*3;
+  uint N = size ? nextprime(MIN(size/4, 0xffff0000)) : 0;
+  uint K = N/4*3;
   msg(L_INFO, ">>> Integers (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
 
   struct fastbuf *f = bopen_tmp(65536);
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     bputl(f, (mode==0) ? i : (mode==1) ? N-1-i : ((u64)i * K + 17) % N);
   brewind(f);
 
@@ -82,9 +83,9 @@ test_int(int mode, u64 size)
   stop();
 
   SORT_XTRACE(2, "Verifying");
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     {
-      uns j = bgetl(f);
+      uint j = bgetl(f);
       if (i != j)
        die("Discrepancy: %u instead of %u", j, i);
     }
@@ -98,9 +99,9 @@ struct key2 {
   u32 cnt;
 };
 
-static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uns n, void *buf UNUSED)
+static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d UNUSED, uint n, void *buf UNUSED)
 {
-  for (uns i=1; i<n; i++)
+  for (uint i=1; i<n; i++)
     k[0]->cnt += k[i]->cnt;
   bwrite(f, k[0], sizeof(struct key2));
 }
@@ -112,23 +113,23 @@ static inline void s2_write_merged(struct fastbuf *f, struct key2 **k, void **d
 #define SORT_UNIFY
 #define SORT_INT(k) (k).x
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 static void
 test_counted(int mode, u64 size)
 {
   u64 items = size / sizeof(struct key2);
-  uns mult = 2;
+  uint mult = 2;
   while (items/(2*mult) > 0xffff0000)
     mult++;
-  uns N = items ? nextprime(items/(2*mult)) : 0;
-  uns K = N/4*3;
+  uint N = items ? nextprime(items/(2*mult)) : 0;
+  uint K = N/4*3;
   msg(L_INFO, ">>> Counted integers (%s, N=%u, mult=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N, mult);
 
   struct fastbuf *f = bopen_tmp(65536);
-  for (uns m=0; m<mult; m++)
-    for (uns i=0; i<N; i++)
-      for (uns j=0; j<2; j++)
+  for (uint m=0; m<mult; m++)
+    for (uint i=0; i<N; i++)
+      for (uint j=0; j<2; j++)
        {
          bputl(f, (mode==0) ? (i%N) : (mode==1) ? N-1-(i%N) : ((u64)i * K + 17) % N);
          bputl(f, 1);
@@ -140,12 +141,12 @@ test_counted(int mode, u64 size)
   stop();
 
   SORT_XTRACE(2, "Verifying");
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     {
-      uns j = bgetl(f);
+      uint j = bgetl(f);
       if (i != j)
        die("Discrepancy: %u instead of %u", j, i);
-      uns k = bgetl(f);
+      uint k = bgetl(f);
       if (k != 2*mult)
        die("Discrepancy: %u has count %u instead of %u", j, k, 2*mult);
     }
@@ -169,7 +170,7 @@ static inline int s3_compare(struct key3 *x, struct key3 *y)
   return 0;
 }
 
-static inline uns s3_hash(struct key3 *x)
+static inline uint s3_hash(struct key3 *x)
 {
   return x->hash[0];
 }
@@ -180,10 +181,10 @@ static inline uns s3_hash(struct key3 *x)
 #define SORT_OUTPUT_FB
 #define SORT_HASH_BITS 32
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 static void
-gen_hash_key(int mode, struct key3 *k, uns i)
+gen_hash_key(int mode, struct key3 *k, uint i)
 {
   k->i = i;
   k->payload[0] = 7*i + 13;
@@ -212,13 +213,13 @@ gen_hash_key(int mode, struct key3 *k, uns i)
 static void
 test_hashes(int mode, u64 size)
 {
-  uns N = MIN(size / sizeof(struct key3), 0xffffffff);
+  uint N = MIN(size / sizeof(struct key3), 0xffffffff);
   msg(L_INFO, ">>> Hashes (%s, N=%u)", ((char *[]) { "increasing", "decreasing", "random" })[mode], N);
   struct key3 k, lastk;
 
   struct fastbuf *f = bopen_tmp(65536);
-  uns hash_sum = 0;
-  for (uns i=0; i<N; i++)
+  uint hash_sum = 0;
+  for (uint i=0; i<N; i++)
     {
       gen_hash_key(mode, &k, i);
       hash_sum += k.hash[3];
@@ -231,7 +232,7 @@ test_hashes(int mode, u64 size)
   stop();
 
   SORT_XTRACE(2, "Verifying");
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     {
       int ok = breadb(f, &k, sizeof(k));
       ASSERT(ok);
@@ -251,13 +252,13 @@ test_hashes(int mode, u64 size)
 #define KEY4_MAX 256
 
 struct key4 {
-  uns len;
+  uint len;
   byte s[KEY4_MAX];
 };
 
 static inline int s4_compare(struct key4 *x, struct key4 *y)
 {
-  uns l = MIN(x->len, y->len);
+  uint l = MIN(x->len, y->len);
   int c = memcmp(x->s, y->s, l);
   if (c)
     return c;
@@ -288,13 +289,13 @@ static inline void s4_write_key(struct fastbuf *f, struct key4 *x)
 #define SORT_INPUT_FB
 #define SORT_OUTPUT_FB
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 #define s4b_compare s4_compare
 #define s4b_read_key s4_read_key
 #define s4b_write_key s4_write_key
 
-static inline uns s4_data_size(struct key4 *x)
+static inline uint s4_data_size(struct key4 *x)
 {
   return x->len ? (x->s[0] ^ 0xad) : 0;
 }
@@ -306,18 +307,18 @@ static inline uns s4_data_size(struct key4 *x)
 #define SORT_INPUT_FB
 #define SORT_OUTPUT_FB
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 static void
 gen_key4(struct key4 *k)
 {
   k->len = random_max(KEY4_MAX);
-  for (uns i=0; i<k->len; i++)
+  for (uint i=0; i<k->len; i++)
     k->s[i] = random();
 }
 
 static void
-gen_data4(byte *buf, uns len, uns h)
+gen_data4(byte *buf, uint len, uint h)
 {
   while (len--)
     {
@@ -327,23 +328,23 @@ gen_data4(byte *buf, uns len, uns h)
 }
 
 static void
-test_strings(uns mode, u64 size)
+test_strings(uint mode, u64 size)
 {
-  uns avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
-  uns N = MIN(size / avg_item_size, 0xffffffff);
+  uint avg_item_size = KEY4_MAX/2 + 4 + (mode ? 128 : 0);
+  uint N = MIN(size / avg_item_size, 0xffffffff);
   msg(L_INFO, ">>> Strings %s(N=%u)", (mode ? "with data " : ""), N);
   srand(1);
 
   struct key4 k, lastk;
   byte buf[256], buf2[256];
-  uns sum = 0;
+  uint sum = 0;
 
   struct fastbuf *f = bopen_tmp(65536);
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     {
       gen_key4(&k);
       s4_write_key(f, &k);
-      uns h = hash_block(k.s, k.len);
+      uint h = hash_block(k.s, k.len);
       sum += h;
       if (mode)
        {
@@ -358,11 +359,11 @@ test_strings(uns mode, u64 size)
   stop();
 
   SORT_XTRACE(2, "Verifying");
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     {
       int ok = s4_read_key(f, &k);
       ASSERT(ok);
-      uns h = hash_block(k.s, k.len);
+      uint h = hash_block(k.s, k.len);
       if (mode && s4_data_size(&k))
        {
          ok = breadb(f, buf, s4_data_size(&k));
@@ -386,10 +387,10 @@ struct key5 {
   u32 cnt;
 };
 
-static uns s5_N, s5_K, s5_L, s5_i, s5_j;
+static uint s5_N, s5_K, s5_L, s5_i, s5_j;
 
 struct s5_pair {
-  uns x, y;
+  uint x, y;
 };
 
 static int s5_gen(struct s5_pair *p)
@@ -409,30 +410,28 @@ static int s5_gen(struct s5_pair *p)
 
 #define ASORT_PREFIX(x) s5m_##x
 #define ASORT_KEY_TYPE u32
-#define ASORT_ELT(i) ary[i]
-#define ASORT_EXTRA_ARGS , u32 *ary
-#include "ucw/sorter/array-simple.h"
+#include <ucw/sorter/array-simple.h>
 
-static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uns n, void *buf)
+static void s5_write_merged(struct fastbuf *f, struct key5 **keys, void **data, uint n, void *buf)
 {
   u32 *a = buf;
-  uns m = 0;
-  for (uns i=0; i<n; i++)
+  uint m = 0;
+  for (uint i=0; i<n; i++)
     {
       memcpy(&a[m], data[i], 4*keys[i]->cnt);
       m += keys[i]->cnt;
     }
-  s5m_sort(m, a);
+  s5m_sort(a, m);
   keys[0]->cnt = m;
   bwrite(f, keys[0], sizeof(struct key5));
   bwrite(f, a, 4*m);
 }
 
-static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, struct fastbuf *dest)
+static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uint n, struct fastbuf *dest)
 {
   u32 k[n];
-  uns m = 0;
-  for (uns i=0; i<n; i++)
+  uint m = 0;
+  for (uint i=0; i<n; i++)
     {
       k[i] = bgetl(data[i]);
       m += keys[i]->cnt;
@@ -441,8 +440,8 @@ static void s5_copy_merged(struct key5 **keys, struct fastbuf **data, uns n, str
   bwrite(dest, &key, sizeof(key));
   while (key.cnt--)
     {
-      uns b = 0;
-      for (uns i=1; i<n; i++)
+      uint b = 0;
+      for (uint i=1; i<n; i++)
        if (k[i] < k[b])
          b = i;
       bputl(dest, k[b]);
@@ -463,22 +462,22 @@ static inline int s5p_lt(struct s5_pair x, struct s5_pair y)
 #define ASORT_PREFIX(x) s5p_##x
 #define ASORT_KEY_TYPE struct s5_pair
 #define ASORT_LT(x,y) s5p_lt(x,y)
-#include "ucw/sorter/array.h"
+#include <ucw/sorter/array.h>
 
 static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
 {
-  uns max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
+  uint max = MIN(bufsize/sizeof(struct s5_pair), 0xffffffff);
   struct s5_pair *a = buf;
-  uns n = 0;
+  uint n = 0;
   while (n<max && s5_gen(&a[n]))
     n++;
   if (!n)
     return 0;
   s5p_sort(a, n);
-  uns i = 0;
+  uint i = 0;
   while (i < n)
     {
-      uns j = i;
+      uint j = i;
       while (i < n && a[i].x == a[j].x)
        i++;
       struct key5 k = { .x = a[j].x, .cnt = i-j };
@@ -498,7 +497,7 @@ static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
 #define SORT_OUTPUT_THIS_FB
 #define SORT_INT(k) (k).x
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 #define SORT_KEY_REGULAR struct key5
 #define SORT_PREFIX(x) s5b_##x
@@ -511,12 +510,12 @@ static int s5_presort(struct fastbuf *dest, void *buf, size_t bufsize)
 #define s5b_write_merged s5_write_merged
 #define s5b_copy_merged s5_copy_merged
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 static void
-test_graph(uns mode, u64 size)
+test_graph(uint mode, u64 size)
 {
-  uns N = 3;
+  uint N = 3;
   while ((u64)N*(N+2)*4 < size)
     N = nextprime(N);
   if (!size)
@@ -549,18 +548,18 @@ test_graph(uns mode, u64 size)
   stop();
 
   SORT_XTRACE(2, "Verifying");
-  uns c = bgetl(f);
+  uint c = bgetl(f);
   ASSERT(c == 0xfeedcafe);
-  for (uns i=0; i<N; i++)
+  for (uint i=0; i<N; i++)
     {
       struct key5 k;
       int ok = breadb(f, &k, sizeof(k));
       ASSERT(ok);
       ASSERT(k.x == i);
       ASSERT(k.cnt == N);
-      for (uns j=0; j<N; j++)
+      for (uint j=0; j<N; j++)
        {
-         uns y = bgetl(f);
+         uint y = bgetl(f);
          ASSERT(y == j);
        }
     }
@@ -580,7 +579,7 @@ struct key6 {
 #define SORT_UNIQUE
 #define SORT_INT64(k) (k).x
 
-#include "ucw/sorter/sorter.h"
+#include <ucw/sorter/sorter.h>
 
 static void
 test_int64(int mode, u64 size)
@@ -611,7 +610,7 @@ test_int64(int mode, u64 size)
 /*** Main ***/
 
 static void
-run_test(uns i, u64 size)
+run_test(uint i, u64 size)
 {
   test_id = i;
   switch (i)
@@ -658,7 +657,7 @@ main(int argc, char **argv)
   log_init(NULL);
   int c;
   u64 size = 10000000;
-  uns t = ~0;
+  uint t = ~0;
 
   while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS "d:s:t:v", CF_NO_LONG_OPTS, NULL)) >= 0)
     switch (c)
@@ -697,7 +696,7 @@ main(int argc, char **argv)
   if (optind != argc)
     goto usage;
 
-  for (uns i=0; i<TMAX; i++)
+  for (uint i=0; i<TMAX; i++)
     if (t & (1 << i))
       run_test(i, size);