Add lots of utilties, initial builtins

This commit is contained in:
2022-10-27 16:51:21 -04:00
parent 07a486cd16
commit d81d8c5156
11 changed files with 603 additions and 10 deletions

215
test/test_utility.c Normal file
View File

@@ -0,0 +1,215 @@
#include <stdlib.h>
#include <unity.h>
#include <string.h>
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
static struct nl_object *input;
static struct nl_object *response;
#define TEST_ASSERT_OBJ_SYMBOL(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_SYMBOL)
#define TEST_ASSERT_OBJ_STRING(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_STRING)
#define TEST_ASSERT_OBJ_INT(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_INT)
#define TEST_ASSERT_OBJ_LIST(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_CELL)
#define TEST_ASSERT_LIST_LEN(list, len) \
do { \
TEST_ASSERT_OBJ_LIST(list); \
TEST_ASSERT_EQUAL(nl_list_length(list)->integer, len); \
} while(0)
#define TEST_ASSERT_NIL(obj) \
TEST_ASSERT_LIST_LEN(obj, 0)
#define TEST_ASSERT_T(obj) \
do { \
TEST_ASSERT_OBJ_SYMBOL(obj); \
TEST_ASSERT_EQUAL_STRING(obj->symbol, "t"); \
} while(0)
void setUp(void) {
input = NULL;
response = NULL;
}
void tearDown(void) {
// TODO: Implement GC so we can clean these both up
//nl_object_delete(input);
input = NULL;
nl_object_delete(response);
response = NULL;
}
static void test_nil_create(void) {
response = nl_nil_create();
TEST_ASSERT_NIL(response);
}
static void test_t_create(void) {
response = nl_t_create();
TEST_ASSERT_T(response);
}
static void test_predicate_true(void) {
response = nl_predicate(true);
TEST_ASSERT_T(response);
}
static void test_predicate_false(void) {
response = nl_predicate(false);
TEST_ASSERT_NIL(response);
}
static void test_car_nil(void) {
response = nl_car(nl_nil_create());
TEST_ASSERT_NIL(response);
}
static void test_cdr_nil(void) {
response = nl_cdr(nl_nil_create());
TEST_ASSERT_NIL(response);
}
static void test_car_t(void) {
response = nl_car(nl_t_create());
TEST_ASSERT_NULL(response);
}
static void test_cdr_t(void) {
response = nl_car(nl_t_create());
TEST_ASSERT_NULL(response);
}
static void test_car_list(void) {
input = nl_tuple_create(
nl_string_create(strdup("foo")),
nl_t_create());
response = nl_car(input);
TEST_ASSERT_OBJ_STRING(response);
}
static void test_cdr_list(void) {
input = nl_tuple_create(
nl_t_create(),
nl_string_create(strdup("foo")));
response = nl_cdr(input);
TEST_ASSERT_OBJ_STRING(nl_car(response));
}
static void test_list_length_nil() {
response = nl_list_length(nl_nil_create());
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 0);
}
static void test_list_length_1() {
response = nl_list_length(
nl_cell_create(
nl_t_create(), NULL));
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 1);
}
static void test_list_length_2() {
response = nl_list_length(
nl_tuple_create(
nl_t_create(),
nl_t_create()));
TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 2);
}
static void test_list_append_nil() {
input = nl_nil_create();
nl_list_append(input, nl_t_create());
TEST_ASSERT_EQUAL(nl_list_length(input)->integer, 1);
}
static void test_list_append_list() {
input = nl_tuple_create(nl_t_create(), nl_t_create());
nl_list_append(input, nl_t_create());
TEST_ASSERT_EQUAL(nl_list_length(input)->integer, 3);
}
static void test_list_nth_nil_0() {
response = nl_list_nth(nl_nil_create(), 0);
TEST_ASSERT_NULL(response);
}
static void test_list_nth_nil_1() {
response = nl_list_nth(nl_nil_create(), 1);
TEST_ASSERT_NULL(response);
}
static void test_list_nth_list_0() {
response = nl_list_nth(
nl_tuple_create(
nl_t_create(),
nl_string_create(strdup("foo"))),
0);
TEST_ASSERT_T(response);
}
static void test_list_nth_list_1() {
response = nl_list_nth(
nl_tuple_create(
nl_t_create(),
nl_string_create(strdup("foo"))),
1);
TEST_ASSERT_OBJ_STRING(response);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_nil_create);
RUN_TEST(test_t_create);
RUN_TEST(test_predicate_true);
RUN_TEST(test_predicate_false);
RUN_TEST(test_car_nil);
RUN_TEST(test_cdr_nil);
RUN_TEST(test_car_t);
RUN_TEST(test_cdr_t);
RUN_TEST(test_car_list);
RUN_TEST(test_cdr_list);
RUN_TEST(test_list_length_nil);
RUN_TEST(test_list_length_1);
RUN_TEST(test_list_length_2);
RUN_TEST(test_list_append_nil);
RUN_TEST(test_list_append_list);
RUN_TEST(test_list_nth_nil_0);
RUN_TEST(test_list_nth_nil_1);
RUN_TEST(test_list_nth_list_0);
RUN_TEST(test_list_nth_list_1);
return UNITY_END();
}