From 502b9349fe924f71df98c3cd7e3e5b2ebac4e126 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Fri, 25 Mar 2022 16:11:20 -0400 Subject: [PATCH] Fix some busted tests. Better styling of native test printing. Add `detail` parameter to runTests(). --- src/Makefile | 2 +- src/env.c | 15 +++++++++++---- src/env.h | 2 +- src/pebblisp.c | 13 +++++++++++-- src/plfunc.c | 14 +++++++++++++- src/plfunc.h | 2 +- src/tests.sh | 6 ++++++ 7 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Makefile b/src/Makefile index c4b14d8..37c64a3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,7 +12,7 @@ mkfile_dir := $(dir $(mkfile_path)) GCC_COM ?= gcc -g -O0 -Wall -o $(exe) -D WEBSERVER -D STANDALONE -DSCRIPTDIR=\"$(SCRIPTDIR)\" all: - $(GCC_COM) $(files) $(libs) && echo && ./tests.sh && ./pl --run-tests + $(GCC_COM) $(file_libs) && echo && ./tests.sh notest: $(GCC_COM) $(file_libs) diff --git a/src/env.c b/src/env.c index 86c3f39..76db6a1 100644 --- a/src/env.c +++ b/src/env.c @@ -346,7 +346,7 @@ fnn(segfault, "seg", "Induces a segfault.") } // Returns number of failures -int runTests() +int runTests(int detailed) { printf("::NATIVE TESTS::\n"); int failureCount = 0; @@ -355,9 +355,9 @@ int runTests() struct helpText h = helpTexts[hi]; if (h.tests) { char result[1024]; - // if (h.testCount == 0) { - // printf("`%s` is untested.\n", h.symbol); - // } + if (detailed && h.testCount > 0) { + printf(" `%s` ", h.symbol); + } for (int ti = 0; ti < h.testCount; ti += 2) { const char* test = h.tests[ti]; const char* expected = h.tests[ti + 1]; @@ -371,8 +371,15 @@ int runTests() printf("%s\n", test); printf("Expected '%s' but received '%s'\n", expected, result); } else { + if (detailed) { + printf("✓"); + } passCount++; } + deleteEnv(&env); + } + if (detailed && h.testCount > 0) { + printf("\n"); } } } diff --git a/src/env.h b/src/env.h index eaf4147..fe7eec2 100644 --- a/src/env.h +++ b/src/env.h @@ -48,6 +48,6 @@ char* getHelp(const char* symbol); void printColored(const char* code); -int runTests(); +int runTests(int detailed); #endif diff --git a/src/pebblisp.c b/src/pebblisp.c index c5f0293..4edb681 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -771,8 +771,17 @@ int main(int argc, const char* argv[]) struct Environment env = defaultEnv(); setGlobal(&env); - if (argc == 2 && strcmp(argv[1], "--run-tests") == 0) { - return runTests(); + int ret = -1; + if (argc == 2) { + if (strcmp(argv[1], "--run-tests") == 0) { + ret = runTests(0); + } else if (strcmp(argv[1], "--run-tests=detailed") == 0) { + ret = runTests(1); + } + } + if (ret != -1) { + deleteEnv(global()); + return ret; } struct sigaction action; diff --git a/src/plfunc.c b/src/plfunc.c index c2cf18a..bf12a31 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -26,8 +26,17 @@ Object charAt(Object* params, int length, struct Environment* env) Object at = params[1]; char* c = malloc(sizeof(char) * 2); - c[0] = string.string[at.number]; c[1] = '\0'; + + for (int i = 0; i < at.number; i++) { + if (string.string[i] == '\0') { + c[0] = '\0'; + string.string = c; + return string; + } + } + + c[0] = string.string[at.number]; string.string = c; return string; } @@ -89,6 +98,9 @@ Object at(Object* params, int length, struct Environment* env) Object rest(Object* params, int length, struct Environment* env) { Object list = params[0]; + if (!isListy(list)) { + return errorObject(NOT_A_LIST); + } Object ret = listObject(); Object* l = &list; diff --git a/src/plfunc.h b/src/plfunc.h index 08f5a53..3087596 100644 --- a/src/plfunc.h +++ b/src/plfunc.h @@ -85,7 +85,7 @@ fn(rest, "Get the tail of a list. All but the first element.", "(rest (1 2 3))", "( 2 3 )", "(rest ())", "( )", - "(rest \"string\")", "( )", + "(rest \"string\")", "NOT_A_LIST", ); /// LIST => LIST diff --git a/src/tests.sh b/src/tests.sh index 185178e..d0a31fb 100755 --- a/src/tests.sh +++ b/src/tests.sh @@ -269,3 +269,9 @@ fi echo "$TOTAL_FAILS Tests Failed" echo "" + +if $VALGRIND; then + $VALCOM ./pl --run-tests=detailed +else + ./pl --run-tests +fi