]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add test for rng
authorAki Tuomi <aki.tuomi@dovecot.fi>
Sun, 25 Mar 2018 16:54:48 +0000 (19:54 +0300)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Tue, 7 Aug 2018 10:19:23 +0000 (13:19 +0300)
src/lib/Makefile.am
src/lib/test-lib.inc
src/lib/test-random.c [new file with mode: 0644]

index 8781171abc25aebc079c5c3c0d73a6827695bd36..10988a78f75d9cbfa77d448c96382af244ea647b 100644 (file)
@@ -394,6 +394,7 @@ test_lib_SOURCES = \
        test-primes.c \
        test-printf-format-fix.c \
        test-priorityq.c \
+       test-random.c \
        test-seq-range-array.c \
        test-stats-dist.c \
        test-str.c \
@@ -414,7 +415,7 @@ test_headers = \
        test-lib.h \
        test-lib.inc
 
-test_lib_LDADD = $(test_libs)
+test_lib_LDADD = $(test_libs) -lm
 test_lib_DEPENDENCIES = $(test_libs)
 
 check-local:
index 39ad27a19123d126b9682547e09622c52d24cdb5..7ae22bad2fc3e03491530b6ae9e11ff1e9d6bd57 100644 (file)
@@ -71,6 +71,7 @@ TEST(test_primes)
 TEST(test_printf_format_fix)
 FATAL(fatal_printf_format_fix)
 TEST(test_priorityq)
+TEST(test_random)
 TEST(test_seq_range_array)
 TEST(test_stats_dist)
 TEST(test_str)
diff --git a/src/lib/test-random.c b/src/lib/test-random.c
new file mode 100644 (file)
index 0000000..6e93b53
--- /dev/null
@@ -0,0 +1,40 @@
+/* Copyright (c) 2018 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "stats-dist.h"
+#include "randgen.h"
+#include <math.h>
+
+#define TEST_RAND_SIZE_MEDIAN 100000.0
+
+static void test_random_median(void)
+{
+       uint64_t tmp;
+       double median, average;
+
+       struct stats_dist *s = stats_dist_init_with_size(TEST_RAND_SIZE_MEDIAN);
+       test_begin("test_random (median & average)");
+       for(unsigned int i = 0; i < TEST_RAND_SIZE_MEDIAN; i++) {
+               uint64_t value;
+               value = i_rand_limit(TEST_RAND_SIZE_MEDIAN);
+               stats_dist_add(s, value);
+       }
+       tmp = stats_dist_get_median(s);
+
+       /* median should be 0.5 +-2% */
+       median = (double)tmp / TEST_RAND_SIZE_MEDIAN;
+       test_assert(fabs(median - 0.5) < 0.01);
+
+       /* average should be 0.5 +- %2 */
+       average = stats_dist_get_avg(s) / TEST_RAND_SIZE_MEDIAN;
+
+       test_assert(fabs(average - 0.5) < 0.01);
+
+       stats_dist_deinit(&s);
+       test_end();
+}
+
+void test_random(void)
+{
+       test_random_median();
+}