Mercurial > lbo > hg > ylisp
changeset 133:2af75be12687
eval: Add test
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sun, 01 Sep 2019 19:24:33 +0200 |
parents | bad34d9a4443 |
children | ee13116349cd |
files | src/eval_test.c |
diffstat | 1 files changed, 63 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/eval_test.c Sun Sep 01 19:24:33 2019 +0200 @@ -0,0 +1,63 @@ +#include "eval.h" + +#include "parse.h" +#include "preprocess.h" +#include "value.h" + +void test_eval_edge_cases(void) { + fprintf(stderr, "test_eval_edge_cases ===========\n"); + + ystr_t input = ystr_new("() -- empty list\n"); + yexpr_t program = yexpr_new(); + ystr_t error = ystr_new(NULL); + assert(yparse_str(&input, &program, &error)); + + yeval_state_t state; + state.call_stack = YVEC_NEW(NULL, 4, yexpr_t); + yexpr_t result = yeval_list_return_last(&state, &program.value.list, 0); + assert(result.typ == YEXPR_LIST); + assert(result.value.list.len == 0); + + yexpr_destroy(&program); + ystr_destroy(&input); + ystr_destroy(&error); + yvec_destroy(&state.call_stack); + yexpr_destroy(&result); +} + +void test_eval_func_wrong_call(void) { + fprintf(stderr, "test_eval_func_wrong_call ===========\n"); + + ystr_t input = ystr_new( + "(defn my-func (first-arg) first-arg)" + "(my-func 'correct-number-of-args)" + "(my-func 'too 'many 'args)"); + yexpr_t program = yexpr_new(); + ystr_t error = ystr_new(NULL); + assert(yparse_str(&input, &program, &error)); + yexpr_debug(&program); + + ypreprocess(&program); + + yeval_state_t state; + state.call_stack = YVEC_NEW(NULL, 4, yexpr_t); + yexpr_t result = yeval_list_return_last(&state, &program.value.list, 0); + assert(result.typ == YEXPR_EXCEPTION); + assert(0 == ystr_cmp_str(&result.value.str, + "Unexpected number of arguments in call to " + "function my-func: Want 1, got 3")); + yexpr_debug(&result); + + yexpr_destroy(&program); + ystr_destroy(&input); + ystr_destroy(&error); + yvec_destroy(&state.call_stack); + yexpr_destroy(&result); +} + +int main(void) { + test_eval_edge_cases(); + test_eval_func_wrong_call(); + yvalue_free_all(); + return 0; +}