aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-06-19 07:06:29 +0800
committerMistivia <i@mistivia.com>2025-06-19 07:06:29 +0800
commitd96ab7d81aeb9676779faa7c1380dd48f5440b07 (patch)
tree5170d5cae8d3f506f2ec0be27074c24949d85d2c /tests
parent0b335dc24e76cace44e748e62d5cbbc40c4355f5 (diff)
parse list test
Diffstat (limited to 'tests')
-rw-r--r--tests/test_parser.c113
1 files changed, 112 insertions, 1 deletions
diff --git a/tests/test_parser.c b/tests/test_parser.c
index c0587cb..28c30e0 100644
--- a/tests/test_parser.c
+++ b/tests/test_parser.c
@@ -43,7 +43,113 @@ int main() {
parser.ctx = &interp;
ParseResult res;
- SExp sexp;
+ SExp sexp, a, b, c;
+
+ res = parse_str(&parser, "((1 2)\n . 3)");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kPairSExp);
+ a = *Interp_ref(&interp, sexp.pair.car);
+ b = *Interp_ref(&interp, sexp.pair.cdr);
+ assert(a.type == kPairSExp);
+ c = *Interp_ref(&interp, a.pair.cdr);
+ a = *Interp_ref(&interp, a.pair.car);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 1);
+ a = *Interp_ref(&interp, c.pair.car);
+ c = *Interp_ref(&interp, c.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 2);
+ assert(c.type == kNilSExp);
+ assert(b.type == kIntegerSExp);
+ assert(b.integer == 3);
+
+ res = parse_str(&parser, "((1 2) . 3)");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kPairSExp);
+ a = *Interp_ref(&interp, sexp.pair.car);
+ b = *Interp_ref(&interp, sexp.pair.cdr);
+ assert(a.type == kPairSExp);
+ c = *Interp_ref(&interp, a.pair.cdr);
+ a = *Interp_ref(&interp, a.pair.car);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 1);
+ a = *Interp_ref(&interp, c.pair.car);
+ c = *Interp_ref(&interp, c.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 2);
+ assert(c.type == kNilSExp);
+ assert(b.type == kIntegerSExp);
+ assert(b.integer == 3);
+
+ res = parse_str(&parser, "((1 2) 3)");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kPairSExp);
+ a = *Interp_ref(&interp, sexp.pair.car);
+ b = *Interp_ref(&interp, sexp.pair.cdr);
+ assert(a.type == kPairSExp);
+ c = *Interp_ref(&interp, a.pair.cdr);
+ a = *Interp_ref(&interp, a.pair.car);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 1);
+ a = *Interp_ref(&interp, c.pair.car);
+ c = *Interp_ref(&interp, c.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 2);
+ assert(c.type == kNilSExp);
+ a = *Interp_ref(&interp, b.pair.car);
+ b = *Interp_ref(&interp, b.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 3);
+ assert(b.type == kNilSExp);
+
+ ERROR_TEST("(1 2 . 3 4)");
+ ERROR_TEST("(1 2 . )");
+ ERROR_TEST("(1 2 .)");
+
+ res = parse_str(&parser, "(1 2 . 3)");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kPairSExp);
+ a = *Interp_ref(&interp, sexp.pair.car);
+ b = *Interp_ref(&interp, sexp.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 1);
+ assert(b.type == kPairSExp);
+ a = *Interp_ref(&interp, b.pair.car);
+ b = *Interp_ref(&interp, b.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 2);
+ assert(b.type == kIntegerSExp);
+ assert(b.integer == 3);
+
+ res = parse_str(&parser, "(1 . 2)");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kPairSExp);
+ a = *Interp_ref(&interp, sexp.pair.car);
+ b = *Interp_ref(&interp, sexp.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 1);
+ assert(b.type == kIntegerSExp);
+ assert(b.integer == 2);
+
+ res = parse_str(&parser, "(1 2)");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kPairSExp);
+ a = *Interp_ref(&interp, sexp.pair.car);
+ b = *Interp_ref(&interp, sexp.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 1);
+ a = *Interp_ref(&interp, b.pair.car);
+ b = *Interp_ref(&interp, b.pair.cdr);
+ assert(a.type == kIntegerSExp);
+ assert(a.integer == 2);
+ assert(b.type == kNilSExp);
+
ATOM_TEST("1.11", kRealSExp, real, 1.11);
ATOM_TEST("-1.11", kRealSExp, real, -1.11);
@@ -76,6 +182,11 @@ int main() {
ERROR_TEST("@1");
ERROR_TEST("a|");
+ res = parse_str(&parser, "()");
+ assert(!ParseResult_is_err(res));
+ sexp = *Interp_ref(&interp, res.val);
+ assert(sexp.type == kNilSExp);
+
Interp_free(&interp);
Parser_free(&parser);
printf("[PASS] parser\n");