diff options
-rw-r--r-- | include/shell.h | 1 | ||||
-rw-r--r-- | lib/shell.c | 23 | ||||
-rw-r--r-- | tests/framework.h | 1 | ||||
-rw-r--r-- | tests/test_shell_shell.c | 24 |
4 files changed, 37 insertions, 12 deletions
diff --git a/include/shell.h b/include/shell.h index 44637e7..8b322cd 100644 --- a/include/shell.h +++ b/include/shell.h @@ -10,7 +10,6 @@ #define SHELL_BENCHMARK 1 << 2 typedef struct { - struct timespec start_time, stop_time; double time_elapsed; int returncode; char *output; diff --git a/lib/shell.c b/lib/shell.c index 6b28e64..18781c0 100644 --- a/lib/shell.c +++ b/lib/shell.c @@ -32,7 +32,8 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { va_list args; va_start(args, fmt); - clockid_t clkid = CLOCK_REALTIME; + clockid_t clockid = CLOCK_REALTIME; + struct timespec start, stop; FILE *proc = NULL; (*proc_info) = (Process *)calloc(1, sizeof(Process)); @@ -57,7 +58,7 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { vsnprintf(cmd, PATH_MAX, fmt, args); if (option & SHELL_BENCHMARK) { - if (clock_gettime(clkid, &(*proc_info)->start_time) == -1) { + if (clock_gettime(clockid, &start) < 0) { perror("clock_gettime"); exit(errno); } @@ -70,15 +71,6 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { return; } - if (option & SHELL_BENCHMARK) { - if (clock_gettime(clkid, &(*proc_info)->stop_time) == -1) { - perror("clock_gettime"); - exit(errno); - } - (*proc_info)->time_elapsed = ((*proc_info)->stop_time.tv_sec - (*proc_info)->start_time.tv_sec) - + ((*proc_info)->stop_time.tv_nsec - (*proc_info)->start_time.tv_nsec) / 1E9; - } - if (option & SHELL_OUTPUT) { size_t bytes_read = 0; size_t i = 0; @@ -99,6 +91,15 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { } } (*proc_info)->returncode = pclose(proc); + + if (option & SHELL_BENCHMARK) { + if (clock_gettime(clockid, &stop) < 0) { + perror("clock_gettime"); + exit(errno); + } + (*proc_info)->time_elapsed = (double)(stop.tv_sec - start.tv_sec) + (double)(stop.tv_nsec - start.tv_nsec) / 1E9;; + } + va_end(args); free(outbuf); free(cmd); diff --git a/tests/framework.h b/tests/framework.h index d317005..f1ce15c 100644 --- a/tests/framework.h +++ b/tests/framework.h @@ -18,6 +18,7 @@ struct TestCase { union TestValue caseValue; union TestValue inputValue; union TestValue truthValue; + union TestValue arg[10]; // when there are too many damn arguments }; char *array_to_string(char **array, const char *sep) { diff --git a/tests/test_shell_shell.c b/tests/test_shell_shell.c new file mode 100644 index 0000000..ade16a7 --- /dev/null +++ b/tests/test_shell_shell.c @@ -0,0 +1,24 @@ +#include "spm.h" +#include "shell.h" +#include "framework.h" + +const char *testFmt = "returned '%s', expected '%s'\n"; +struct TestCase testCase[] = { + {.arg[0].unsigned_integer = SHELL_OUTPUT|SHELL_BENCHMARK, .arg[1].sptr = "echo hello; sleep 3", .arg[2].floating = 3, .arg[3].sptr = "hello"}, +}; +size_t numCases = sizeof(testCase) / sizeof(struct TestCase); + +int main(int argc, char *argv[]) { + for (size_t i = 0; i < numCases; i++) { + char elapsed[100] = {0,}; + Process *result = NULL; + + shell(&result, testCase[i].arg[0].unsigned_integer, testCase[i].arg[1].sptr); + sprintf(elapsed, "%0.8lf", result->time_elapsed); + strip(result->output); + + myassert(strcmp(result->output, testCase[i].arg[3].sptr) == 0, testFmt, result->output, testCase[i].arg[3].sptr); + myassert(fabs(result->time_elapsed) >= fabs((double)testCase[i].arg[2].floating), testFmt, elapsed, "non-zero elapsed time"); + } + return 0; +}
\ No newline at end of file |