Files
uclisp/test/test_parse.c
2022-11-15 00:15:48 -05:00

362 lines
11 KiB
C

#include <stdlib.h>
#include <unity.h>
#include <string.h>
#include "uclisp.h"
#include "internal.h"
/* static struct ucl_parse_result *result; */
static struct ucl_object *response;
void setUp(void) {
response = NULL;
}
void tearDown(void) {
ucl_gc();
response = NULL;
}
static void test_token_next_empty_str(void) {
const char *input = "";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr);
}
static void test_token_next_only_whitespace(void) {
const char *input = " \n";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr);
}
static void test_token_next_lparen(void) {
const char *input = "(";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
static void test_token_next_rparen(void) {
const char *input = ")";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
static void test_token_next_lrparen(void) {
const char *input = "()";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string);
TEST_ASSERT_EQUAL(')', *curr);
ucl_object_delete(response);
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
static void test_token_next_string(void) {
const char *input = "\"foo\"";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
static void test_token_next_string_w_whitespace(void) {
const char *input = " \"foo\" ";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING(" ", curr);
}
static void test_token_next_symbol(void) {
const char *input = "foo";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING("", curr);
}
static void test_token_next_symbol_w_whitespace(void) {
const char *input = " foo ";
const char *curr = input;
response = ucl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING(" ", curr);
}
static void test_tokenize_empty_str(void) {
response = ucl_tokenize("");
TEST_ASSERT_NULL(response);
}
static void test_tokenize_nil(void) {
response = ucl_tokenize("()");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
const struct ucl_object *token1 = response;
const struct ucl_object *token2 = response->cell.cdr;
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token1->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string);
TEST_ASSERT_NOT_NULL(token2);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, token2->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token2->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", token2->cell.car->string);
TEST_ASSERT_NULL(token2->cell.cdr);
}
static void test_tokenize_scopement(void) {
response = ucl_tokenize("(foo)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
const struct ucl_object *token1 = response;
const struct ucl_object *token2 = response->cell.cdr;
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token1->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string);
TEST_ASSERT_NOT_NULL(token2);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, token2->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token2->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", token2->cell.car->string);
const struct ucl_object *token3 = token2->cell.cdr;
TEST_ASSERT_NOT_NULL(token3);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, token3->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, token3->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", token3->cell.car->string);
}
static void test_parse_atom_symbol(void) {
response = ucl_parse_token_atom(ucl_symbol_create("foo"));
TEST_ASSERT_EQUAL(response->type, UCL_TYPE_SYMBOL);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
}
static void test_parse_atom_lparen(void) {
response = ucl_parse_token_atom(ucl_symbol_create("("));
TEST_ASSERT_NULL(response);
}
static void test_parse_atom_rparen(void) {
response = ucl_parse_token_atom(ucl_symbol_create(")"));
TEST_ASSERT_NULL(response);
}
static void test_parse_empty_str(void) {
response = ucl_parse("");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr);
}
static void test_parse_symbol(void) {
response = ucl_parse("foo");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(response->cell.car->type, UCL_TYPE_SYMBOL);
TEST_ASSERT_EQUAL_STRING(response->cell.car->symbol, "foo");
}
static void test_parse_nil(void) {
response = ucl_parse("()");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.car);
TEST_ASSERT_NULL(response->cell.car->cell.cdr);
}
static void test_parse_list_1elem(void) {
response = ucl_parse("(foo)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->symbol);
}
static void test_parse_list_2elem(void) {
response = ucl_parse("(foo bar)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NOT_NULL(response->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->symbol);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->cell.cdr->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.cdr->cell.car->type);
TEST_ASSERT_EQUAL_STRING("bar", response->cell.car->cell.cdr->cell.car->string);
}
static void test_parse_2elem(void) {
response = ucl_parse("foo bar");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->symbol);
TEST_ASSERT_NOT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.cdr->type);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.cdr->cell.car->type);
TEST_ASSERT_EQUAL_STRING("bar", response->cell.cdr->cell.car->symbol);
}
static void test_parse_2elem_str(void) {
response = ucl_parse("\"foo\" \"bar\"");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_NOT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.cdr->type);
TEST_ASSERT_EQUAL(UCL_TYPE_STRING, response->cell.cdr->cell.car->type);
TEST_ASSERT_EQUAL_STRING("bar", response->cell.cdr->cell.car->string);
}
static void test_parse_nested(void) {
response = ucl_parse("((foo))");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->cell.car->cell.car->type);
TEST_ASSERT_NULL(response->cell.car->cell.car->cell.cdr);
TEST_ASSERT_EQUAL(UCL_TYPE_SYMBOL, response->cell.car->cell.car->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->cell.car->cell.car->symbol);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_token_next_empty_str);
RUN_TEST(test_token_next_only_whitespace);
RUN_TEST(test_token_next_lparen);
RUN_TEST(test_token_next_rparen);
RUN_TEST(test_token_next_lrparen);
RUN_TEST(test_token_next_string);
RUN_TEST(test_token_next_string_w_whitespace);
RUN_TEST(test_token_next_symbol);
RUN_TEST(test_token_next_symbol_w_whitespace);
RUN_TEST(test_tokenize_empty_str);
RUN_TEST(test_tokenize_nil);
RUN_TEST(test_tokenize_scopement);
RUN_TEST(test_parse_atom_symbol);
RUN_TEST(test_parse_atom_lparen);
RUN_TEST(test_parse_atom_rparen);
RUN_TEST(test_parse_empty_str);
RUN_TEST(test_parse_symbol);
RUN_TEST(test_parse_nil);
RUN_TEST(test_parse_list_1elem);
RUN_TEST(test_parse_list_2elem);
RUN_TEST(test_parse_2elem);
RUN_TEST(test_parse_2elem_str);
RUN_TEST(test_parse_nested);
return UNITY_END();
}