Skip to content

Commit

Permalink
lib: Add test for rng
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse committed Aug 7, 2018
1 parent 4bd4dac commit 03a166b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/Makefile.am
Expand Up @@ -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 \
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/lib/test-lib.inc
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions 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 <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();
}

0 comments on commit 03a166b

Please sign in to comment.