nserver

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

commit 6dac5422ab8b390b8e4d6ecf0313c9c9f448f54a
parent 09f04a6bdd80450ab8a972262cc07c5cbf0aa0fa
Author: rsiddharth <s@ricketyspace.net>
Date:   Tue, 14 Apr 2020 20:53:33 -0400

nserver: ncmd.h: Add NS_LOAD.

* nserver/src/ncmd.c (find_function): Add handling to find NS_LOAD.
(call_function): Add handling for NS_LOAD.
* nserver/src/ncmd.h (FUNCTIONS): Add NS_LOAD
* nserver/tests/ncmd_tests.c (test_find_function)
(test_call_function, test_process): Update tests to check the
correctness of NS_LOAD.

Diffstat:
nserver/src/ncmd.c | 17+++++++++++++++++
nserver/src/ncmd.h | 1+
nserver/tests/ncmd_tests.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/nserver/src/ncmd.c b/nserver/src/ncmd.c @@ -109,6 +109,7 @@ int find_function(struct bstrList *cmd_parts) struct tagbstring fdelete = bsStatic("/delete"); struct tagbstring flist = bsStatic("/list"); struct tagbstring fstore = bsStatic("/store"); + struct tagbstring fload = bsStatic("/load"); check(cmd_parts != NULL, "cmd_parts is NULL"); check(cmd_parts->qty > 0, "qty check failed"); @@ -135,6 +136,8 @@ int find_function(struct bstrList *cmd_parts) return NS_LIST; } else if (bstricmp(cmd_name, &fstore) == 0) { return NS_STORE; + } else if (bstricmp(cmd_name, &fload) == 0) { + return NS_LOAD; } else { return NS_NOP; } @@ -291,6 +294,20 @@ int call_function(int func, struct bstrList *cmd_parts, char *out) } strncpy(out, "OK\n", RSP_SIZE); break; + case NS_LOAD: + if(check_args(cmd_parts, 3) != 0) { + strncpy(out, "error: command invalid\n", RSP_SIZE); + + return -1; + } + if (ssload(bdata(cmd_parts->entry[1]), + bdata(cmd_parts->entry[2])) < 0) { + strncpy(out, "error: load failed\n", RSP_SIZE); + + return -1; + } + strncpy(out, "OK\n", RSP_SIZE); + break; default: strncpy(out, "error: function not found\n", RSP_SIZE); diff --git a/nserver/src/ncmd.h b/nserver/src/ncmd.h @@ -19,6 +19,7 @@ enum FUNCTIONS { NS_DELETE, NS_LIST, NS_STORE, + NS_LOAD, NS_NOP = -1 }; diff --git a/nserver/tests/ncmd_tests.c b/nserver/tests/ncmd_tests.c @@ -214,6 +214,18 @@ char *test_find_function() funk = find_function(parts); mu_assert(funk == NS_STORE, "find function list failed"); + char *load = "/load jowl ears"; + parts = cmd_parts(load); + mu_assert(parts != NULL, "cmp_parts failed"); + funk = find_function(parts); + mu_assert(funk == NS_LOAD, "find function list failed"); + + load = "/LoAD jowl ears"; + parts = cmd_parts(load); + mu_assert(parts != NULL, "cmp_parts failed"); + funk = find_function(parts); + mu_assert(funk == NS_LOAD, "find function list failed"); + char *nop = "/meant bacon"; parts = cmd_parts(nop); mu_assert(parts != NULL, "cmd_parts failed"); @@ -311,7 +323,22 @@ char *test_call_function() mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); - // delete ham and beef. + char *roastedham_load = "/load ham roastedham"; + parts = cmd_parts(roastedham_load); + mu_assert(parts != NULL, "cmd_parts failed"); + rc = call_function(NS_LOAD, parts, msg); + mu_assert(rc == 0, "call function failed"); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + char *grilledbeef_load = "/load beef grilledbeef"; + parts = cmd_parts(grilledbeef_load); + mu_assert(parts != NULL, "cmd_parts failed"); + rc = call_function(NS_LOAD, parts, msg); + printf("LOAD FIAAAL %d %s\n", rc, msg); + mu_assert(rc == 0, "call function failed"); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + + // delete ham, roastedham, beef, and roastedbeef. char *ham_delete = "/delete ham"; parts = cmd_parts(ham_delete); mu_assert(parts != NULL, "cmd_parts failed"); @@ -319,6 +346,13 @@ char *test_call_function() mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + char *roastedham_delete = "/delete roastedham"; + parts = cmd_parts(roastedham_delete); + mu_assert(parts != NULL, "cmd_parts failed"); + rc = call_function(NS_DELETE, parts, msg); + mu_assert(rc == 0, "call function failed"); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + char *beef_delete = "/delete beef"; parts = cmd_parts(beef_delete); mu_assert(parts != NULL, "cmd_parts failed"); @@ -326,6 +360,13 @@ char *test_call_function() mu_assert(rc == 0, "call function failed"); mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + char *grilledbeef_delete = "/delete grilledbeef"; + parts = cmd_parts(grilledbeef_delete); + mu_assert(parts != NULL, "cmd_parts failed"); + rc = call_function(NS_DELETE, parts, msg); + mu_assert(rc == 0, "call function failed"); + mu_assert(strcmp(msg, "OK\n") == 0, "call function failed"); + return NULL; } @@ -349,6 +390,16 @@ char *test_process() mu_assert(rc == 0, "process failed"); mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + char *ham_sample = "/sample ham 4.3"; + rc = process(ham_sample, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "Mean: 4.30\n") == 0, "process failed"); + + ham_sample = "/sample ham 6.0"; + rc = process(ham_sample, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "Mean: 5.15\n") == 0, "process failed"); + char *bacon_sample = "/sample bacon 4.2"; rc = process(bacon_sample, out); mu_assert(rc == 0, "process failed"); @@ -386,7 +437,6 @@ char *test_process() mu_assert(rc == 0, "process failed"); mu_assert(strcmp(out, "OK\n") == 0, "process failed"); - char *beef_store = "/store beef"; rc = process(beef_store, out); mu_assert(rc == 0, "process failed"); @@ -397,6 +447,26 @@ char *test_process() mu_assert(rc == -1, "process failed"); mu_assert(strcmp(out, "error: store failed\n") == 0, "process failed"); + char *roastedham_load = "/load ham roastedham"; + rc = process(roastedham_load, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + + char *roastedham_delete = "/delete roastedham"; + rc = process(roastedham_delete, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + + roastedham_load = "/load ham roastedham"; + rc = process(roastedham_load, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "OK\n") == 0, "process failed"); + + char *roastedham_dump = "/dump roastedham"; + rc = process(roastedham_dump, out); + mu_assert(rc == 0, "process failed"); + mu_assert(strcmp(out, "sum: 10.300000, sumsq: 54.490000, n: 2, min: 4.300000, max: 6.000000, mean: 5.150000, stddev: 1.202082\n") == 0, "process failed"); + return NULL; }