Files
uclisp/test/test_parse.c
2022-10-14 19:57:44 -04:00

264 lines
7.4 KiB
C

#include <stdlib.h>
#include <unity.h>
#include "nihilispm.h"
#include "nihilispm_internal.h"
/* static struct nl_parse_result *result; */
static struct nl_object *response;
void setUp(void) {
response = NULL;
}
void tearDown(void) {
nl_object_delete(response);
}
/* void test_parse_null() { */
/* result = nl_parse("()"); */
/* TEST_ASSERT_EQUAL(result->result, 0); */
/* TEST_ASSERT_NOT_NULL(result->statement); */
/* TEST_ASSERT_NULL(result->statement->car); */
/* TEST_ASSERT_NULL(result->statement->cdr); */
/* } */
/* void test_parse_error() { */
/* result = nl_parse("("); */
/* TEST_ASSERT_EQUAL(result->result, 0); */
/* TEST_ASSERT_NULL(result->statement); */
/* } */
void test_token_next_empty_str() {
const char *input = "";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr);
}
void test_token_next_only_whitespace() {
const char *input = " \n";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr);
}
void test_token_next_lparen() {
const char *input = "(";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
void test_token_next_rparen() {
const char *input = ")";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
void test_token_next_lrparen() {
const char *input = "()";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", response->cell.car->string);
TEST_ASSERT_EQUAL(')', *curr);
nl_object_delete(response);
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
void test_token_next_string() {
const char *input = "\"foo\"";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL('\0', *curr);
}
void test_token_next_string_w_whitespace() {
const char *input = " \"foo\" ";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_STRING, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING(" ", curr);
}
void test_token_next_symbol() {
const char *input = "foo";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING("", curr);
}
void test_token_next_symbol_w_whitespace() {
const char *input = " foo ";
const char *curr = input;
response = nl_token_next(&curr);
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->string);
TEST_ASSERT_EQUAL_STRING(" ", curr);
}
void test_tokenize_empty_str() {
response = nl_tokenize("");
TEST_ASSERT_NULL(response);
}
void test_tokenize_nil() {
response = nl_tokenize("()");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
const struct nl_object *token1 = response;
const struct nl_object *token2 = response->cell.cdr;
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token1->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string);
TEST_ASSERT_NOT_NULL(token2);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, token2->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token2->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", token2->cell.car->string);
TEST_ASSERT_NULL(token2->cell.cdr);
}
void test_tokenize_statement() {
response = nl_tokenize("(foo)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
const struct nl_object *token1 = response;
const struct nl_object *token2 = response->cell.cdr;
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token1->cell.car->type);
TEST_ASSERT_EQUAL_STRING("(", token1->cell.car->string);
TEST_ASSERT_NOT_NULL(token2);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, token2->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token2->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", token2->cell.car->string);
const struct nl_object *token3 = token2->cell.cdr;
TEST_ASSERT_NOT_NULL(token3);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, token3->type);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, token3->cell.car->type);
TEST_ASSERT_EQUAL_STRING(")", token3->cell.car->string);
}
void test_parse_empty_str() {
response = nl_parse("");
TEST_ASSERT_NULL(response);
}
void test_parse_symbol() {
response = nl_parse("foo");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->type);
TEST_ASSERT_EQUAL_STRING(NL_TYPE_SYMBOL, "foo");
}
void test_parse_nil() {
response = nl_parse("()");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_NULL(response->cell.car);
TEST_ASSERT_NULL(response->cell.cdr);
}
void test_parse_list_1elem() {
response = nl_parse("(foo)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->cell.car->symbol);
}
void test_parse_list_2elem() {
response = nl_parse("(foo bar)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(NL_TYPE_CELL, response->type);
TEST_ASSERT_NOT_NULL(response->cell.car);
TEST_ASSERT_EQUAL(NL_TYPE_SYMBOL, response->cell.car->type);
TEST_ASSERT_EQUAL_STRING("foo", response->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_statement);
return UNITY_END();
}