view src/eval_test.c @ 133:2af75be12687

eval: Add test
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 01 Sep 2019 19:24:33 +0200
parents
children 1e9d534b903d
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);
}

int main(void) {
    test_eval_edge_cases();
    test_eval_func_wrong_call();
    yvalue_free_all();
    return 0;
}