Mercurial > lbo > hg > ylisp
view src/eval_test.c @ 147:1e9d534b903d
eval: Return type errors for undefined functions; test recursion
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Tue, 03 Sep 2019 12:16:23 +0200 |
parents | 2af75be12687 |
children | 3cd189bd595c |
line wrap: on
line source
#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); } void test_eval_recursion(void) { fprintf(stderr, "test_eval_recursion ===========\n"); // Calculate faculty values from 1 to 10. ystr_t input = ystr_new( "(defn fac (n) (if (== n 0) 1 ((* n (fac (- n 1))))))" "(for i (1 2 3 4 5 6 7 8 9 10) (fac i))"); yexpr_t program = yexpr_new(); ystr_t error = ystr_new(NULL); assert(yparse_str(&input, &program, &error)); yexpr_debug(&program); ypreprocess(&program); yexpr_debug(&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); yexpr_debug(&result); assert(YEXPR_LIST == result.typ); assert(362880 == YVEC_AT(&result.value.list, 8, yexpr_t)->value.n); yexpr_destroy(&program); yvec_destroy(&state.call_stack); ystr_destroy(&error); yexpr_destroy(&result); ystr_destroy(&input); } int main(void) { test_eval_edge_cases(); test_eval_func_wrong_call(); test_eval_recursion(); yvalue_free_all(); yatom_free_all(); return 0; }