nserver

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

commit 89919c902061006471760cbddd2fe275ff57c8d8
parent 10cc7513914fddbf654e27c795c775a939bc636a
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 11 Apr 2020 02:22:12 -0400

nserver: Add Stats_unstringify.

* nserver/src/stats.c (Stats_unstringify): New function definition.
* nserver/src/stats.h (Stats_unstringify): New function declaration.
* nserver/tests/stats_tests.c (tests_stats_unstringify): Test for
Stats_unstringify.
(tests_stats_unstringify): st_str is now a static variable. Don't free
it; it's done in tests_stats_unstringify.
(st_str): New static variable.

Diffstat:
nserver/src/stats.c | 37+++++++++++++++++++++++++++++++++++--
nserver/src/stats.h | 6++++++
nserver/tests/stats_tests.c | 26++++++++++++++++++++++++--
3 files changed, 65 insertions(+), 4 deletions(-)

diff --git a/nserver/src/stats.c b/nserver/src/stats.c @@ -1,7 +1,5 @@ #include <math.h> #include <stats.h> -#include <stdlib.h> -#include <dbg.h> Stats *Stats_recreate(double sum, double sumsq, unsigned long n, double min, double max) @@ -87,3 +85,38 @@ char *Stats_stringify(Stats *st) } return NULL; } + + +Stats *Stats_unstringify(char *st_str) +{ + Stats *st = NULL; + + check(st_str != NULL, "st_str invalid"); + + bstring st_bstr = bfromcstr(st_str); + check(st_bstr != NULL, "st_str bstring convert failed"); + + struct bstrList *st_list = bsplit(st_bstr, ':'); + check(st_list != NULL, "st_bstr split failed"); + check(st_list->qty == 5, "wrong number of st parts"); + + st = Stats_create(); + check(st != NULL, "stats creation failed"); + + st->sum = atof(bdata(st_list->entry[0])); + st->sumsq = atof(bdata(st_list->entry[1])); + st->n = atoi(bdata(st_list->entry[2])); + st->min = atof(bdata(st_list->entry[3])); + st->max = atof(bdata(st_list->entry[4])); + + // clean up. + int rc = bstrListDestroy(st_list); + check(rc == BSTR_OK, "st_list destroy failed"); + + return st; + error: + if (st) { + free(st); + } + return NULL; +} diff --git a/nserver/src/stats.h b/nserver/src/stats.h @@ -1,6 +1,10 @@ #ifndef stats_h #define stats_h + #include <math.h> +#include <stdlib.h> +#include <bstrlib.h> +#include <dbg.h> typedef struct Stats { double sum; @@ -21,6 +25,8 @@ char *Stats_dump(Stats *st); char *Stats_stringify(Stats *st); +Stats *Stats_unstringify(char *st_str); + static inline double Stats_mean(Stats *st) { return st->sum / st->n; diff --git a/nserver/tests/stats_tests.c b/nserver/tests/stats_tests.c @@ -7,6 +7,8 @@ * ../../liblcthw/tests/stats_tests.c */ +static char *st_str = NULL; + char *test_stats_stringify() { Stats *st = Stats_create(); @@ -19,7 +21,7 @@ char *test_stats_stringify() st->min = 28.3921; st->max = 238.27; - char *st_str = Stats_stringify(st); + st_str = Stats_stringify(st); mu_assert(st_str != NULL, "stats stringify failed"); char *expected_st_str = "8238.34:4260238.83:28:28.39:238.27"; @@ -27,17 +29,37 @@ char *test_stats_stringify() "stringified str invalid"); // cleanup - free(st_str); free(st); return NULL; } +char *test_stats_unstringify() +{ + mu_assert(st_str != NULL, "st_str not initialized"); + + Stats *st = Stats_unstringify(st_str); + mu_assert(st != NULL, "stats unstringify failed"); + + mu_assert(st->sum == 8238.34, "stats sum incorrect"); + mu_assert(st->sumsq == 4260238.83, "stats sumsq incorrect"); + mu_assert(st->n == 28, "stats n incorrect"); + mu_assert(st->min == 28.39, "stats min incorrect"); + mu_assert(st->max == 238.27, "stats max incorrect"); + + // clean up + free(st); + free(st_str); + + return NULL; +} + char *all_tests() { mu_suite_start(); mu_run_test(test_stats_stringify); + mu_run_test(test_stats_unstringify); return NULL; }