nserver

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

commit 6df55dc3b463b9e9017abb0461c861a6890ec195
parent 0cb98cf1fd7368c2f03821f62f5ab4e9cf07613e
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon,  6 Jan 2020 19:51:18 -0500

nserver: ncmd.c: Update call_function.

* nserver/src/ncmd.c (call_function): Update sssample handling; now
returns mean on success.
* nserver/tests/ncmd_tests.c (test_call_function)
(test_process): Update  tests.

Diffstat:
nserver/src/ncmd.c | 11++++++++---
nserver/tests/ncmd_tests.c | 8++++----
2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -167,6 +167,7 @@ int call_function(int func, struct bstrList *cmd_parts, char *out) return -1; } + double mean = 0.0; switch (func) { case NS_CREATE: if(check_args(cmd_parts, 2) != 0) { @@ -190,13 +191,17 @@ int call_function(int func, struct bstrList *cmd_parts, char *out) } double sample = strtod(bdata(cmd_parts->entry[2]), NULL); - if (sssample(bdata(cmd_parts->entry[1]), sample) != 0) { + mean = sssample(bdata(cmd_parts->entry[1]), sample); + if (mean < 0) { strncpy(out, "error: sample failed\n", RSP_SIZE); return -1; } - strncpy(out, "OK\n", RSP_SIZE); + if (sprintf(out, "Mean: %.2f\n", mean) < 0) { + strncpy(out, "error: sample failed\n", RSP_SIZE); + return -1; + } break; case NS_MEAN: if(check_args(cmd_parts, 2) != 0) { @@ -205,7 +210,7 @@ int call_function(int func, struct bstrList *cmd_parts, char *out) return -1; } - double mean = ssmean(bdata(cmd_parts->entry[1])); + mean = ssmean(bdata(cmd_parts->entry[1])); if (mean < 0) { strncpy(out, "error: mean failed\n", RSP_SIZE); diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -244,14 +244,14 @@ char *test_call_function() mu_assert(parts != NULL, "cmd_parts failed"); rc = call_function(NS_SAMPLE, parts, msg); mu_assert(rc == 0, "call function failed"); - mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + mu_assert(strcmp(msg, "Mean: 4.20\n") == 0, "call function failed"); bacon_sample = "/Sample bacon 6.9"; parts = cmd_parts(bacon_sample); mu_assert(parts != NULL, "cmd_parts failed"); rc = call_function(NS_SAMPLE, parts, msg); mu_assert(rc == 0, "call function failed"); - mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + mu_assert(strcmp(msg, "Mean: 5.55\n") == 0, "call function failed"); char *bacon_mean = "/mean bacon"; parts = cmd_parts(bacon_mean); @@ -324,12 +324,12 @@ char *test_process() char *bacon_sample = "/sample bacon 4.2"; rc = process(bacon_sample, out); mu_assert(rc == 0, "process failed"); - mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + mu_assert(strcmp(out, "Mean: 4.20\n") == 0, "process failed"); bacon_sample = "/Sample bacon 6.9"; rc = process(bacon_sample, out); mu_assert(rc == 0, "process failed"); - mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + mu_assert(strcmp(out, "Mean: 5.55\n") == 0, "process failed"); char *bacon_mean = "/mean bacon"; rc = process(bacon_mean, out);