tests
This commit is contained in:
parent
2e2bafcde8
commit
36135005d0
3 changed files with 81 additions and 5 deletions
5
Makefile
5
Makefile
|
@ -1,4 +1,4 @@
|
|||
all: lisp
|
||||
all: test
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra
|
||||
|
@ -16,6 +16,9 @@ OBJS = \
|
|||
run: lisp
|
||||
./lisp
|
||||
|
||||
test: lisp
|
||||
./lisp --test
|
||||
|
||||
clean:
|
||||
rm -f lisp $(OBJS)
|
||||
|
||||
|
|
|
@ -104,7 +104,8 @@ struct Object *func_eval(struct Object *const args)
|
|||
|
||||
struct Object *func_quote(struct Object *const args)
|
||||
{
|
||||
assert(args);
|
||||
if (!args) return NULL;
|
||||
|
||||
assert(args->type == TYPE_PAIR);
|
||||
assert(args->pair.b == NULL);
|
||||
|
||||
|
|
78
main.c
78
main.c
|
@ -5,12 +5,32 @@
|
|||
#include "parser.h"
|
||||
#include "tokens.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
static void run();
|
||||
static void test();
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
assert(argc == 1 || argc == 2);
|
||||
|
||||
if (argc == 1) {
|
||||
run();
|
||||
} else if (argc == 2 && strcmp(argv[1], "--test") == 0) {
|
||||
test();
|
||||
} else {
|
||||
abort();
|
||||
}
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
char chr;
|
||||
while ((chr = getchar()) != EOF) {
|
||||
|
@ -36,6 +56,58 @@ int main()
|
|||
|
||||
printf("\nResult:\n");
|
||||
Object_print(result, 0);
|
||||
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
void test()
|
||||
{
|
||||
struct Object *const sym_foo = Object_new_symbol("foo");
|
||||
struct Object *const sharp_true = Object_new_boolean(true);
|
||||
struct Object *const num_123 = Object_new_number(123);
|
||||
|
||||
// #t
|
||||
// #t
|
||||
assert(builtins_eval(sharp_true) == sharp_true);
|
||||
|
||||
// 123
|
||||
// 123
|
||||
assert(builtins_eval(num_123) == num_123);
|
||||
|
||||
// (quote foo)
|
||||
// 'foo
|
||||
assert(
|
||||
builtins_eval(
|
||||
Object_new_pair(
|
||||
Object_new_symbol("quote"),
|
||||
Object_new_pair(sym_foo, NULL)
|
||||
)
|
||||
) == sym_foo
|
||||
);
|
||||
|
||||
// (quote #t)
|
||||
// #t
|
||||
assert(
|
||||
builtins_eval(
|
||||
Object_new_pair(
|
||||
Object_new_symbol("quote"),
|
||||
Object_new_pair(sharp_true, NULL)
|
||||
)
|
||||
) == sharp_true
|
||||
);
|
||||
|
||||
// (quote ())
|
||||
// '()
|
||||
assert(
|
||||
builtins_eval(Object_new_pair(Object_new_symbol("quote"), NULL)) == NULL
|
||||
);
|
||||
|
||||
// (quote 123)
|
||||
// 123
|
||||
assert(
|
||||
builtins_eval(
|
||||
Object_new_pair(
|
||||
Object_new_symbol("quote"),
|
||||
Object_new_pair(num_123, NULL)
|
||||
)
|
||||
) == num_123
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue