From e7d0eefb6f766968fe0de26556da1353ec9bd0ab Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sun, 9 Jan 2022 11:16:17 -0800 Subject: Add a library for pattern matching. --- match-test.csc | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 match-test.csc (limited to 'match-test.csc') diff --git a/match-test.csc b/match-test.csc new file mode 100644 index 0000000..3fed862 --- /dev/null +++ b/match-test.csc @@ -0,0 +1,42 @@ +(import (scheme base) + (only (csc testing) define-test errorf subtest) + (csc match)) + + +(define-test (test-match t) + (define-record-type + (test-case name expr want) + test-case? + (name name) + (expr expr) + (want want)) + (let ((tests (list + (test-case + "simple" + (match 3 + (0 0) + (1 1) + (2 2) + (3 3) + (else 4)) + 3) + (test-case + "match-list" + (match '(1 2 3) + ('() 0) + ((1 2) 1) + ((1 2 3) 2) + ((1 2 3 4) 3) + (else 4)) + 2) + (test-case + "binding" + (match '(1 2 3) + ((1 (! x) 3) x)) + 2)))) + (for-each + (lambda (tc) + (subtest t (name tc) + (unless (equal? (expr tc) (want tc)) + (errorf t "match = {}, want {}." (expr tc) (want tc))))) + tests))) -- cgit v1.3.1