nserver

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

commit b20d3221befacb0cfd04829fb9bfd9f1276f7ea4
parent c98f28485e5848701fdee76b6506f14d6e75a225
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon,  2 Dec 2019 20:58:50 -0500

nserver: Add call_function.

* nserver/src/ncmd.c (call_function): New function definition. Initial
version.
* nserver/src/ncmd.h (call_function): New function declaration.
* nserver/tests/ncmd_tests.c (test_call_function): Test for
call_function.
(all_tests): Add test_call_function.

Diffstat:
nserver/src/ncmd.c | 102+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
nserver/src/ncmd.h | 2++
nserver/tests/ncmd_tests.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 169 insertions(+), 0 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -168,3 +168,105 @@ int check_args(struct bstrList *cmd_parts, int argc) return -1; } +/** + * TODO: + * - msg needs to be dynamically allocated. + * - error msgs need to be consistent. + */ +char *call_function(int func, struct bstrList *cmd_parts) +{ + char *msg = NULL; + if (func < 0 || cmd_parts == NULL || cmd_parts->qty < 1) { + msg = "function call failed: internal error"; + + return msg; + } + + int rc = 0; + switch (func) { + case NS_CREATE: + if(check_args(cmd_parts, 2) != 0) { + msg = "create failed: command invalid\n"; + break; + } + if (sscreate(bdata(cmd_parts->entry[1])) != 0) { + msg = "create failed: internal error"; + break; + } + msg = "OK\n"; + + break; + case NS_SAMPLE: + if(check_args(cmd_parts, 3) != 0) { + msg = "sample failed: command invalid\n"; + break; + } + + double sample = strtod(bdata(cmd_parts->entry[2]), NULL); + if (sssample(bdata(cmd_parts->entry[1]), sample) != 0) { + msg = "sample failed: internal error"; + break; + } + msg = "OK\n"; + + break; + case NS_MEAN: + if(check_args(cmd_parts, 2) != 0) { + msg = "mean failed: command invalid\n"; + break; + } + + double mean = ssmean(bdata(cmd_parts->entry[1])); + if (mean < 0) { + msg = "mean failed: internal error"; + break; + } + msg = (char *) calloc(20, sizeof(char)); + if (sprintf(msg, "Mean: %.2f\n", mean) < 0) { + msg = "mean failed: internal error"; + } + break; + case NS_DUMP: + if(check_args(cmd_parts, 2) != 0) { + msg = "dump failed: command invalid\n"; + break; + } + + char *dump = ssdump(bdata(cmd_parts->entry[1])); + if (dump == NULL) { + msg = "dump failed: internal error"; + break; + } + msg = dump; + break; + case NS_DELETE: + if(check_args(cmd_parts, 2) != 0) { + msg = "delete failed: command invalid\n"; + break; + } + + if (ssdelete(bdata(cmd_parts->entry[1])) != 0) { + msg = "delete failed: internal error"; + break; + } + msg = "OK\n"; + + break; + case NS_LIST: + if(check_args(cmd_parts, 1) != 0) { + msg = "list failed: command invalid\n"; + break; + } + + msg = sslist(); + if (msg == NULL) { + msg = "list failed: internal error"; + } + break; + default: + msg = "error: function not found"; + break; + } + + return msg; +} diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -5,6 +5,7 @@ #include <bstrlib.h> #include <dbg.h> +#include <protocol.h> #define CMD_MIN_SIZE 5 #define CMD_MAX_SIZE 120 @@ -24,6 +25,7 @@ char *check_cmd(char *cmd); struct bstrList *cmd_parts(char *cmd); int find_function(struct bstrList *cmd_parts); +char *call_function(int func, struct bstrList *cmd_parts); #endif diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -209,6 +209,70 @@ char *test_find_function() return NULL; } +char *test_call_function() +{ + struct bstrList *parts = NULL; + char *msg = 0; + + char *bacon = "/create bacon"; + parts = cmd_parts(bacon); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_CREATE, parts); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + char *ham = "/create ham"; + parts = cmd_parts(ham); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_CREATE, parts); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + char *beef = "/create beef"; + parts = cmd_parts(beef); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_CREATE, parts); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + char *bacon_sample = "/sample bacon 4.2"; + parts = cmd_parts(bacon_sample); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_SAMPLE, parts); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + bacon_sample = "/Sample bacon 6.9"; + parts = cmd_parts(bacon_sample); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_SAMPLE, parts); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + char *bacon_mean = "/mean bacon"; + parts = cmd_parts(bacon_mean); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_MEAN, parts); + mu_assert(strcmp(msg, "Mean: 5.55\n") == 0, "call function failed"); + + char *bacon_dump = "/dump bacon"; + parts = cmd_parts(bacon_dump); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_DUMP, parts); + mu_assert(strcmp(msg, "sum: 11.100000, sumsq: 65.250000, n: 2, min: 4.200000, max: 6.900000, mean: 5.550000, stddev: 1.909188\n") == 0, "call function failed"); + + char *bacon_delete = "/delete bacon"; + parts = cmd_parts(bacon_delete); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_DELETE, parts); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + char *list = "/list"; + parts = cmd_parts(list); + mu_assert(parts != NULL, "cmd_parts failed"); + msg = call_function(NS_LIST, parts); + mu_assert(strcmp(msg, "ham\nbeef\n") == 0 + || strcmp(msg, "beef\nham\n") == 0, + "call function failed"); + + return NULL; +} + char *all_tests() { mu_suite_start(); @@ -217,6 +281,7 @@ char *all_tests() mu_run_test(test_check_cmd); mu_run_test(test_cmd_parts); mu_run_test(test_find_function); + mu_run_test(test_call_function); return NULL; }