From 64ab0153238cd3d06296cd31bd9bc09ba8545f33 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 2 Nov 2019 10:43:43 -0400 Subject: nserver: protocol: Define ssmean. * nserver/src/protocol.c (ssmean): New function. * nserver/tests/protocol_tests.c (test_ssmean): New test. (all_tests): ADd test_ssmean. --- nserver/src/protocol.c | 19 +++++++++++++++++++ nserver/tests/protocol_tests.c | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'nserver') diff --git a/nserver/src/protocol.c b/nserver/src/protocol.c index a174caf..3c09733 100644 --- a/nserver/src/protocol.c +++ b/nserver/src/protocol.c @@ -57,3 +57,22 @@ int sssample(char *key, double s) return -1; } +double ssmean(char *key) +{ + check(hash != NULL, "hash not initialized"); + + // 1. create bstring from 'key'. + bstring k = bfromcstr(key); + + // 2. try to Stats for key. + Stats *st = (Stats *) Hashmap_get(hash, k); + check(st != NULL, "stats not found for key"); + + // 3. get mean. + double m = Stats_mean(st); + + return m; + error: + return -1; +} + diff --git a/nserver/tests/protocol_tests.c b/nserver/tests/protocol_tests.c index 75bf614..6fc2831 100644 --- a/nserver/tests/protocol_tests.c +++ b/nserver/tests/protocol_tests.c @@ -42,12 +42,29 @@ char *test_sssample() return NULL; } +char *test_ssmean() +{ + double m = 0; + + m = ssmean("crimson"); + mu_assert(m == 8, "ssmean failed 0"); + + m = ssmean("/vermilion"); + mu_assert(m == 17, "ssmean failed 1"); + + m = ssmean("/ruby"); + mu_assert(m == -1, "ssmean failed 2"); + + return NULL; +} + char *all_tests() { mu_suite_start(); mu_run_test(test_sscreate); mu_run_test(test_sssample); + mu_run_test(test_ssmean); return NULL; } -- cgit v1.2.3