From 03a166b2cbf2c7904fd275e481783c879cd8f820 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Sun, 25 Mar 2018 19:54:48 +0300 Subject: [PATCH] lib: Add test for rng --- src/lib/Makefile.am | 3 ++- src/lib/test-lib.inc | 1 + src/lib/test-random.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/lib/test-random.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 8781171abc..10988a78f7 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -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: diff --git a/src/lib/test-lib.inc b/src/lib/test-lib.inc index 39ad27a191..7ae22bad2f 100644 --- a/src/lib/test-lib.inc +++ b/src/lib/test-lib.inc @@ -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 index 0000000000..6e93b53354 --- /dev/null +++ b/src/lib/test-random.c @@ -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 + +#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(); +}