]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/random-test.c
Random: Implemented strong random source.
[libucw.git] / ucw / random-test.c
index 1e51aa74fb97ce7599b5e9cc915398ece02446cb..2fd375956b1c2e08b8008aa551d39e0d72a4d867 100644 (file)
@@ -20,6 +20,7 @@
 #include <unistd.h>
 
 static int o_test_fast;
+static int o_test_strong;
 
 static double o_bench_scale = 1.0;
 static int o_bench_only_safe;
@@ -27,6 +28,7 @@ static int o_bench_all;
 static int o_bench_libs;
 static int o_bench_legacy;
 static int o_bench_fast;
+static int o_bench_strong;
 
 static struct opt_section options = {
   OPT_ITEMS {
@@ -35,10 +37,12 @@ static struct opt_section options = {
     OPT_HELP("Options:"),
     OPT_HELP_OPTION,
     OPT_BOOL(0, "test-fast", o_test_fast, 0, "\tTest fast random generator"),
+    OPT_BOOL(0, "test-strong", o_test_strong, 0, "\tTest strong random generator"),
     OPT_BOOL(0, "bench-all", o_bench_all, 0, "\tBench everything"),
     OPT_BOOL(0, "bench-libs", o_bench_libs, 0, "\tBench libc"),
     OPT_BOOL(0, "bench-legacy", o_bench_legacy, 0, "\tBench legacy interface"),
     OPT_BOOL(0, "bench-fast", o_bench_fast, 0, "\tBench fast random generator"),
+    OPT_BOOL(0, "bench-strong", o_bench_strong, 0, "\tBench strong random generator"),
     OPT_DOUBLE(0, "bench-scale", o_bench_scale, OPT_REQUIRED_VALUE, "<scale>\tIncrease/decrease the length of benchmark (default: 1.0)"),
     OPT_BOOL(0, "bench-only-safe", o_bench_only_safe, 0, "\tDon't benchmark too verbose or risky functions"),
     OPT_END,
@@ -79,6 +83,23 @@ static void test_fast(void)
   printf("OK\n");
 }
 
+static void test_strong(void)
+{
+  byte buf[100];
+  for (uint j = 0; j < 2; j++)
+    {
+      struct strongrand *r = strongrand_std_open((j == 0) ? 0 : 128, STRONGRAND_STD_URANDOM);
+      for (uint i = 1; i <= 100; i++)
+       {
+         strongrand_mem(r, buf, i);
+         if (i % 10 == 0)
+           strongrand_reset(r);
+       }
+      strongrand_close(r);
+    }
+  printf("OK\n");
+}
+
 #define BENCH(func) BENCH_SLOW(1, func)
 #define BENCH_SLOW(mult, func) \
   do { \
@@ -189,6 +210,30 @@ static void bench_fast(void)
   fastrand_delete(r);
 }
 
+static void bench_strong(void)
+{
+  byte buf[100];
+  struct strongrand *r;
+
+  msg(L_INFO, "Benchmarking unbuffered strong random generator");
+  r = strongrand_std_open(0, STRONGRAND_STD_URANDOM);
+  BENCH_SLOW(50, strongrand_mem(r, buf, 1));
+  BENCH_SLOW(200, strongrand_mem(r, buf, 100));
+  strongrand_close(r);
+
+  msg(L_INFO, "Benchmarking buffered strong random generator");
+  r = strongrand_std_open(128, STRONGRAND_STD_URANDOM);
+  BENCH_SLOW(50, strongrand_mem(r, buf, 1));
+  BENCH_SLOW(200, strongrand_mem(r, buf, 100));
+  strongrand_close(r);
+
+  msg(L_INFO, "Benchmarking buffered strong random generator with autoreset");
+  r = strongrand_std_open(128, STRONGRAND_STD_URANDOM | STRONGRAND_STD_AUTO_RESET);
+  BENCH_SLOW(50, strongrand_mem(r, buf, 1));
+  BENCH_SLOW(200, strongrand_mem(r, buf, 100));
+  strongrand_close(r);
+}
+
 int main(int argc UNUSED, char **argv)
 {
   cf_def_file = INSTALL_CONFIG_DIR "/common";
@@ -196,6 +241,8 @@ int main(int argc UNUSED, char **argv)
 
   if (o_test_fast)
     test_fast();
+  if (o_test_strong)
+    test_strong();
 
   if (o_bench_all || o_bench_libs)
     bench_libs();
@@ -203,6 +250,8 @@ int main(int argc UNUSED, char **argv)
     bench_legacy();
   if (o_bench_all || o_bench_fast)
     bench_fast();
+  if (o_bench_all || o_bench_strong)
+    bench_strong();
 
   return 0;
 }