Add error types and simple state

This commit is contained in:
2022-10-28 16:08:40 -04:00
parent fd91e66b8a
commit ed173bd17a
14 changed files with 302 additions and 89 deletions

66
test/test_state.c Normal file
View File

@@ -0,0 +1,66 @@
#include <unity.h>
#include <stdlib.h>
#include <string.h>
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_state.h"
#include "nihilispm_utility.h"
#include "testing_helpers.h"
/* static struct nl_parse_result *result; */
static struct nl_state *state;
static struct nl_object *response;
void setUp(void) {
state = nl_state_create();
}
void tearDown(void) {
nl_state_delete(state);
state = NULL;
}
static void test_get_empty(void) {
response = nl_state_get(state, "foo");
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_put_get(void) {
nl_state_put(state, "foo", nl_t_create());
response = nl_state_get(state, "foo");
TEST_ASSERT_T(response);
}
static void test_put2_get(void) {
nl_state_put(state, "foo1", nl_t_create());
nl_state_put(state, "foo2", nl_nil_create());
response = nl_state_get(state, "foo1");
TEST_ASSERT_T(response);
}
static void test_put_modify_get(void) {
struct nl_object *obj = nl_tuple_create(
nl_string_create(strdup("bar")),
nl_string_create(strdup("baz")));
nl_state_put(state, "foo", obj);
nl_list_append(obj, nl_string_create(strdup("quux")));
response = nl_state_get(state, "foo");
TEST_ASSERT_OBJ_STRING(nl_list_nth(response, 2));
TEST_ASSERT_EQUAL_STRING(nl_list_nth(response, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(nl_list_nth(obj, 2));
TEST_ASSERT_EQUAL_STRING(nl_list_nth(obj, 2)->string, "quux");
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_get_empty);
RUN_TEST(test_put_get);
RUN_TEST(test_put2_get);
RUN_TEST(test_put_modify_get);
return UNITY_END();
}

View File

@@ -5,38 +5,11 @@
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "testing_helpers.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;
@@ -89,13 +62,13 @@ static void test_cdr_nil(void) {
static void test_car_t(void) {
response = nl_car(nl_t_create());
TEST_ASSERT_NULL(response);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_cdr_t(void) {
response = nl_car(nl_t_create());
TEST_ASSERT_NULL(response);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_car_list(void) {
@@ -160,13 +133,13 @@ static void test_list_append_list() {
static void test_list_nth_nil_0() {
response = nl_list_nth(nl_nil_create(), 0);
TEST_ASSERT_NULL(response);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_nil_1() {
response = nl_list_nth(nl_nil_create(), 1);
TEST_ASSERT_NULL(response);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_list_0() {
@@ -189,9 +162,42 @@ static void test_list_nth_list_1() {
TEST_ASSERT_OBJ_STRING(response);
}
static void test_list_nth_t_0() {
response = nl_list_nth(nl_t_create(), 1);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_bounds_0() {
response = nl_list_nth(nl_nil_create(), 0);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_bounds_1() {
response = nl_list_nth(
nl_cell_create(
nl_nil_create(),
NULL),
1);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_list_nth_bounds_2() {
response = nl_list_nth(
nl_tuple_create(
nl_nil_create(),
nl_nil_create()),
2);
TEST_ASSERT_OBJ_ERROR(response);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_nil_create);
RUN_TEST(test_t_create);
RUN_TEST(test_predicate_true);
@@ -211,5 +217,10 @@ int main(void) {
RUN_TEST(test_list_nth_nil_1);
RUN_TEST(test_list_nth_list_0);
RUN_TEST(test_list_nth_list_1);
RUN_TEST(test_list_nth_t_0);
RUN_TEST(test_list_nth_bounds_0);
RUN_TEST(test_list_nth_bounds_1);
RUN_TEST(test_list_nth_bounds_2);
return UNITY_END();
}

38
test/testing_helpers.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef _TESTING_HELPERS_H_
#define _TESTING_HELPERS_H_
#include <unity.h>
#include "nihilispm.h"
#define TEST_ASSERT_OBJ_ERROR(obj) \
TEST_ASSERT_EQUAL(obj->type, NL_TYPE_ERROR)
#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)
#endif