nserver

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 544398fdbe363ddff1980bbd9270554bdf050fa0
parent 83be615f7b034c7a1e6c8c75bf3450c4eeaa7d22
Author: rsiddharth <s@ricketyspace.net>
Date:   Thu, 16 Apr 2020 22:19:48 -0400

nserver: Update stats_tests.

Add test functins from liblcthw.

Diffstat:
nserver/tests/stats_tests.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 64 insertions(+), 5 deletions(-)

diff --git a/nserver/tests/stats_tests.c b/nserver/tests/stats_tests.c @@ -7,14 +7,70 @@ #include "minunit.h" #include <stats.h> #include <string.h> - -/** - * tests for other stats functions are in - * ../../liblcthw/tests/stats_tests.c - */ +#include <math.h> static char *st_str = NULL; +const int NUM_SAMPLES = 10; +double samples[] = { + 6.1061334, 9.6783204, 1.2747090, 8.2395131, 0.3333483, + 6.9755066, 1.0626275, 7.6587523, 4.9382973, 9.5788115 +}; + +Stats expect = { + .sumsq = 425.1641, + .sum = 55.84602, + .min = 0.333, + .max = 9.678, + .n = 10 +}; + +double expect_mean = 5.584602; +double expect_stddev = 3.547868; + +#define EQ(X,Y,N) (round((X) * pow(10, N)) == round((Y) * pow(10, N))) + +char *test_operations() +{ + int i = 0; + Stats *st = Stats_create(); + mu_assert(st != NULL, "Fail to create stats."); + + for (i = 0; i < NUM_SAMPLES; i++) { + Stats_sample(st, samples[i]); + } + + Stats_dump(st); + + mu_assert(EQ(st->sumsq, expect.sumsq, 3), "sumsq not valid"); + mu_assert(EQ(st->sum, expect.sum, 3), "sum not valid"); + mu_assert(EQ(st->min, expect.min, 3), "min not valid"); + mu_assert(EQ(st->max, expect.max, 3), "max not valid"); + mu_assert(EQ(st->n, expect.n, 3), "n not valid"); + mu_assert(EQ(expect_mean, Stats_mean(st), 3), "mean not valid"); + mu_assert(EQ(expect_stddev, Stats_stddev(st), 3), + "stddev not valid"); + + return NULL; +} + +char *test_recreate() +{ + Stats *st = Stats_recreate( + expect.sum, expect.sumsq, expect.n, expect.min, expect.max); + + mu_assert(st->sum == expect.sum, "sum not equal"); + mu_assert(st->sumsq == expect.sumsq, "sumsq not equal"); + mu_assert(st->n == expect.n, "n not equal"); + mu_assert(st->min == expect.min, "min not equal"); + mu_assert(st->max == expect.max, "max not equal"); + mu_assert(EQ(expect_mean, Stats_mean(st), 3), "mean not valid"); + mu_assert(EQ(expect_stddev, Stats_stddev(st), 3), + "stddev not valid"); + + return NULL; +} + char *test_stats_stringify() { Stats *st = Stats_create(); @@ -64,6 +120,9 @@ char *all_tests() { mu_suite_start(); + mu_run_test(test_operations); + mu_run_test(test_recreate); + mu_run_test(test_stats_stringify); mu_run_test(test_stats_unstringify);