aboutsummaryrefslogtreecommitdiffstats
path: root/list-test.csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-01-09 08:40:09 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-01-09 08:40:09 -0800
commit3ff7aac2d2eb2cbf2f854793fc0d7bc6f1f7d927 (patch)
treecef8ca0e77c40a70daaca40af25572437d563105 /list-test.csc
downloadchromatopelma-3ff7aac2d2eb2cbf2f854793fc0d7bc6f1f7d927.tar.zst
Initial commit.
Not sure if everything here will be needed eventually, but we have a working bytecode interpreter. Next I will write the linker, then the core compiler, and finish with the macro expander.
Diffstat (limited to 'list-test.csc')
-rw-r--r--list-test.csc127
1 files changed, 127 insertions, 0 deletions
diff --git a/list-test.csc b/list-test.csc
new file mode 100644
index 0000000..ec4b4ed
--- /dev/null
+++ b/list-test.csc
@@ -0,0 +1,127 @@
+(import (scheme base)
+ (only (csc testing) define-test errorf)
+ (csc list))
+
+
+(define-test (test-take t)
+ (define-record-type <test-case>
+ (test-case desc n xs want)
+ test-case?
+ (desc desc)
+ (n n)
+ (xs xs)
+ (want want))
+ (let ((tests (list
+ (test-case
+ "simple"
+ 3
+ '(1 2 3 4 5)
+ '(1 2 3))
+ (test-case
+ "negative"
+ -5
+ '(1 2 3)
+ '())
+ (test-case
+ "zero"
+ 0
+ '(1 2 3)
+ '())
+ (test-case
+ "short list"
+ 5
+ '(1 2 3)
+ '(1 2 3))
+ (test-case
+ "take whole list"
+ 3
+ '(1 2 3)
+ '(1 2 3)))))
+ (for-each
+ (lambda (tc)
+ (let ((got (take (n tc) (xs tc))))
+ (unless (equal? got (want tc))
+ (errorf t "(got {} {}) = {}, want {}." (n tc) (xs tc) got (want tc)))))
+ tests)))
+
+
+(define-test (test-split-at t)
+ (define-record-type <test-case>
+ (test-case desc n xs want-a want-b)
+ test-case?
+ (desc desc)
+ (n n)
+ (xs xs)
+ (want-a want-a)
+ (want-b want-b))
+ (let ((tests (list
+ (test-case
+ "simple"
+ 2
+ '(1 2 3 4)
+ '(1 2)
+ '(3 4))
+ (test-case
+ "negative"
+ -5
+ '(1 2 3)
+ '()
+ '(1 2 3))
+ (test-case
+ "zero"
+ 0
+ '(1 2 3)
+ '()
+ '(1 2 3))
+ (test-case
+ "short list"
+ 5
+ '(1 2 3)
+ '(1 2 3)
+ '())
+ (test-case
+ "whole list"
+ 3
+ '(1 2 3)
+ '(1 2 3)
+ '()))))
+ (for-each
+ (lambda (tc)
+ (let-values (((got-a got-b) (split-at (n tc) (xs tc))))
+ (unless (equal? got-a (want-a tc))
+ (errorf t "(split-at {} {}) = (values {}, _), want {}." (n tc) (xs tc) got-a (want-a tc)))
+ (unless (equal? got-b (want-b tc))
+ (errorf t "(split-at {} {}) = (values _, {}), want {}." (n tc) (xs tc) got-b (want-b tc)))))
+ tests)))
+
+
+(define-test (test-revappend t)
+ (define-record-type <test-case>
+ (test-case desc a b want)
+ test-case?
+ (desc desc)
+ (a a)
+ (b b)
+ (want want))
+ (let ((tests (list
+ (test-case
+ "threes"
+ '(3 2 1)
+ '(4 5 6)
+ '(1 2 3 4 5 6))
+ (test-case
+ "empty first list"
+ '()
+ '(1 2 3)
+ '(1 2 3))
+ (test-case
+ "empty second list"
+ '(3 2 1)
+ '()
+ '(1 2 3)))))
+ (for-each
+ (lambda (tc)
+ (let ((got (revappend (a tc) (b tc))))
+ (unless (equal? got (want tc))
+ (errorf t "(revappend {} {}) = {}, want {}." (a tc) (b tc) got (want tc)))))
+ tests)))