1 /* Copyright (c) 2018 Dovecot authors, see the included COPYING file */ 2 3 #include "test-lib.h" 4 #include "stats-dist.h" 5 #include "randgen.h" 6 #include <math.h> 7 8 #define TEST_RAND_SIZE_MEDIAN 100000.0 9 test_random_median(void)10static void test_random_median(void) 11 { 12 uint64_t tmp; 13 double median, average; 14 15 struct stats_dist *s = stats_dist_init_with_size(TEST_RAND_SIZE_MEDIAN); 16 test_begin("test_random (median & average)"); 17 for(unsigned int i = 0; i < TEST_RAND_SIZE_MEDIAN; i++) { 18 uint64_t value; 19 value = i_rand_limit(TEST_RAND_SIZE_MEDIAN); 20 stats_dist_add(s, value); 21 } 22 tmp = stats_dist_get_median(s); 23 24 /* median should be 0.5 +-2% */ 25 median = (double)tmp / TEST_RAND_SIZE_MEDIAN; 26 test_assert(fabs(median - 0.5) < 0.01); 27 28 /* average should be 0.5 +- %2 */ 29 average = stats_dist_get_avg(s) / TEST_RAND_SIZE_MEDIAN; 30 31 test_assert(fabs(average - 0.5) < 0.01); 32 33 stats_dist_deinit(&s); 34 test_end(); 35 } 36 test_random_limits(void)37static void test_random_limits(void) 38 { 39 test_begin("random limits"); 40 test_assert(i_rand_limit(1) == 0); 41 test_assert(i_rand_minmax(0, 0) == 0); 42 test_assert(i_rand_minmax(UINT32_MAX, UINT32_MAX) == UINT32_MAX); 43 test_end(); 44 } 45 test_random(void)46void test_random(void) 47 { 48 test_random_median(); 49 test_random_limits(); 50 } 51 fatal_random(unsigned int stage)52enum fatal_test_state fatal_random(unsigned int stage) 53 { 54 switch (stage) { 55 case 0: 56 test_begin("random fatals"); 57 test_expect_fatal_string("min_val <= max_val"); 58 (void)i_rand_minmax(1, 0); 59 return FATAL_TEST_FAILURE; 60 case 1: 61 test_expect_fatal_string("upper_bound > 0"); 62 (void)i_rand_limit(0); 63 return FATAL_TEST_FAILURE; 64 } 65 test_end(); 66 return FATAL_TEST_FINISHED; 67 } 68