| 1 |
|
| 2 |
(cond-expand |
| 3 |
(modules (import (chibi match) (only (chibi test) test-begin test test-end))) |
| 4 |
(else (load "lib/chibi/match/match.scm"))) |
| 5 |
|
| 6 |
(test-begin "match") |
| 7 |
|
| 8 |
(test "any" 'ok (match 'any (_ 'ok))) |
| 9 |
(test "symbol" 'ok (match 'ok (x x))) |
| 10 |
(test "number" 'ok (match 28 (28 'ok))) |
| 11 |
(test "string" 'ok (match "good" ("bad" 'fail) ("good" 'ok))) |
| 12 |
(test "literal symbol" 'ok (match 'good ('bad 'fail) ('good 'ok))) |
| 13 |
(test "null" 'ok (match '() (() 'ok))) |
| 14 |
(test "pair" 'ok (match '(ok) ((x) x))) |
| 15 |
(test "vector" 'ok (match '#(ok) (#(x) x))) |
| 16 |
(test "any doubled" 'ok (match '(1 2) ((_ _) 'ok))) |
| 17 |
(test "and empty" 'ok (match '(o k) ((and) 'ok))) |
| 18 |
(test "and single" 'ok (match 'ok ((and x) x))) |
| 19 |
(test "and double" 'ok (match 'ok ((and (? symbol?) y) 'ok))) |
| 20 |
(test "or empty" 'ok (match '(o k) ((or) 'fail) (else 'ok))) |
| 21 |
(test "or single" 'ok (match 'ok ((or x) 'ok))) |
| 22 |
(test "or double" 'ok (match 'ok ((or (? symbol? y) y) y))) |
| 23 |
(test "not" 'ok (match 28 ((not (a . b)) 'ok))) |
| 24 |
(test "pred" 'ok (match 28 ((? number?) 'ok))) |
| 25 |
(test "named pred" 29 (match 28 ((? number? x) (+ x 1)))) |
| 26 |
|
| 27 |
(test "duplicate symbols pass" 'ok (match '(ok . ok) ((x . x) x))) |
| 28 |
(test "duplicate symbols fail" 'ok (match '(ok . bad) ((x . x) 'bad) (else 'ok))) |
| 29 |
(test "duplicate symbols samth" 'ok (match '(ok . ok) ((x . 'bad) x) (('ok . x) x))) |
| 30 |
(test "duplicate symbols bound" 3 (let ((a '(1 2))) (match a ((and (a 2) (1 b)) (+ a b)) (_ #f)))) |
| 31 |
(test "duplicate quasiquote" 'ok (match '(a b) ((or `(a ,x) `(,x b)) 'ok) (_ #f))) |
| 32 |
|
| 33 |
(test "duplicate before ellipsis" #f |
| 34 |
(match '(1 2) ((a a ...) a) (else #f))) |
| 35 |
(test "duplicate ellipsis pass" '(1 2) |
| 36 |
(match '((1 2) (1 2)) (((x ...) (x ...)) x) (else #f))) |
| 37 |
(test "duplicate ellipsis fail" #f |
| 38 |
(match '((1 2) (1 2 3)) (((x ...) (x ...)) x) (else #f))) |
| 39 |
(test "duplicate ellipsis trailing" '(1 2) |
| 40 |
(match '((1 2 3) (1 2 3)) (((x ... 3) (x ... 3)) x) (else #f))) |
| 41 |
(test "duplicate ellipsis trailing fail" #f |
| 42 |
(match '((1 2 3) (1 1 3)) (((x ... 3) (x ... 3)) x) (else #f))) |
| 43 |
(test "duplicate ellipsis fail trailing" #f |
| 44 |
(match '((1 2 3) (1 2 4)) (((x ... 3) (x ... 3)) x) (else #f))) |
| 45 |
(test "ellipsis trailing" '(3 1 2) |
| 46 |
(match '(1 2 3) ((x ... y) (cons y x)) (else #f))) |
| 47 |
|
| 48 |
|
| 49 |
(test "ellipses" '((a b c) (1 2 3)) |
| 50 |
(match '((a . 1) (b . 2) (c . 3)) |
| 51 |
(((x . y) ___) (list x y)))) |
| 52 |
|
| 53 |
(test "real ellipses" '((a b c) (1 2 3)) |
| 54 |
(match '((a . 1) (b . 2) (c . 3)) |
| 55 |
(((x . y) ...) (list x y)))) |
| 56 |
|
| 57 |
(test "vector ellipses" '(1 2 3 (a b c) (1 2 3)) |
| 58 |
(match '#(1 2 3 (a . 1) (b . 2) (c . 3)) |
| 59 |
(#(a b c (hd . tl) ...) (list a b c hd tl)))) |
| 60 |
|
| 61 |
(test "pred ellipses" '(1 2 3) |
| 62 |
(match '(1 2 3) |
| 63 |
(((? odd? n) ___) n) |
| 64 |
(((? number? n) ___) n))) |
| 65 |
|
| 66 |
(test "failure continuation" 'ok |
| 67 |
(match '(1 2) |
| 68 |
((a . b) (=> next) (if (even? a) 'fail (next))) |
| 69 |
((a . b) 'ok))) |
| 70 |
|
| 71 |
(test "let" '(o k) |
| 72 |
(match-let ((x 'ok) (y '(o k))) y)) |
| 73 |
|
| 74 |
(test "let*" '(f o o f) |
| 75 |
(match-let* ((x 'f) (y 'o) ((z w) (list y x))) (list x y z w))) |
| 76 |
|
| 77 |
(test "getter car" '(1 2) |
| 78 |
(match '(1 . 2) (((get! a) . b) (list (a) b)))) |
| 79 |
|
| 80 |
(test "getter cdr" '(1 2) |
| 81 |
(match '(1 . 2) ((a . (get! b)) (list a (b))))) |
| 82 |
|
| 83 |
(test "getter vector" '(1 2 3) |
| 84 |
(match '#(1 2 3) (#((get! a) b c) (list (a) b c)))) |
| 85 |
|
| 86 |
(test "setter car" '(3 . 2) |
| 87 |
(let ((x (cons 1 2))) |
| 88 |
(match x (((set! a) . b) (a 3))) |
| 89 |
x)) |
| 90 |
|
| 91 |
(test "setter cdr" '(1 . 3) |
| 92 |
(let ((x (cons 1 2))) |
| 93 |
(match x ((a . (set! b)) (b 3))) |
| 94 |
x)) |
| 95 |
|
| 96 |
(test "setter vector" '#(1 0 3) |
| 97 |
(let ((x (vector 1 2 3))) |
| 98 |
(match x (#(a (set! b) c) (b 0))) |
| 99 |
x)) |
| 100 |
|
| 101 |
(test "single tail" '((a b) (1 2) (c . 3)) |
| 102 |
(match '((a . 1) (b . 2) (c . 3)) |
| 103 |
(((x . y) ... last) (list x y last)))) |
| 104 |
|
| 105 |
(test "single tail 2" '((a b) (1 2) 3) |
| 106 |
(match '((a . 1) (b . 2) 3) |
| 107 |
(((x . y) ... last) (list x y last)))) |
| 108 |
|
| 109 |
(test "multiple tail" '((a b) (1 2) (c . 3) (d . 4) (e . 5)) |
| 110 |
(match '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5)) |
| 111 |
(((x . y) ... u v w) (list x y u v w)))) |
| 112 |
|
| 113 |
(test "tail against improper list" #f |
| 114 |
(match '(a b c d e f . g) |
| 115 |
((x ... y u v w) (list x y u v w)) |
| 116 |
(else #f))) |
| 117 |
|
| 118 |
(test "Riastradh quasiquote" '(2 3) |
| 119 |
(match '(1 2 3) (`(1 ,b ,c) (list b c)))) |
| 120 |
|
| 121 |
(test "trivial tree search" '(1 2 3) |
| 122 |
(match '(1 2 3) ((_ *** (a b c)) (list a b c)))) |
| 123 |
|
| 124 |
(test "simple tree search" '(1 2 3) |
| 125 |
(match '(x (1 2 3)) ((_ *** (a b c)) (list a b c)))) |
| 126 |
|
| 127 |
(test "deep tree search" '(1 2 3) |
| 128 |
(match '(x (x (x (1 2 3)))) ((_ *** (a b c)) (list a b c)))) |
| 129 |
|
| 130 |
(test "non-tail tree search" '(1 2 3) |
| 131 |
(match '(x (x (x a b c (1 2 3) d e f))) ((_ *** (a b c)) (list a b c)))) |
| 132 |
|
| 133 |
(test "restricted tree search" '(1 2 3) |
| 134 |
(match '(x (x (x a b c (1 2 3) d e f))) (('x *** (a b c)) (list a b c)))) |
| 135 |
|
| 136 |
(test "fail restricted tree search" #f |
| 137 |
(match '(x (y (x a b c (1 2 3) d e f))) |
| 138 |
(('x *** (a b c)) (list a b c)) |
| 139 |
(else #f))) |
| 140 |
|
| 141 |
(test "sxml tree search" '(((href . "http://synthcode.com/")) ("synthcode")) |
| 142 |
(match '(p (ul (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f))) |
| 143 |
(((or 'p 'ul 'li 'b) *** ('a ('^ attrs ...) text ...)) |
| 144 |
(list attrs text)) |
| 145 |
(else #f))) |
| 146 |
|
| 147 |
(test "failed sxml tree search" #f |
| 148 |
(match '(p (ol (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f))) |
| 149 |
(((or 'p 'ul 'li 'b) *** ('a ('^ attrs ...) text ...)) |
| 150 |
(list attrs text)) |
| 151 |
(else #f))) |
| 152 |
|
| 153 |
(test "collect tree search" |
| 154 |
'((p ul li) ((href . "http://synthcode.com/")) ("synthcode")) |
| 155 |
(match '(p (ul (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f))) |
| 156 |
(((and tag (or 'p 'ul 'li 'b)) *** ('a ('^ attrs ...) text ...)) |
| 157 |
(list tag attrs text)) |
| 158 |
(else #f))) |
| 159 |
|
| 160 |
(test "anded tail pattern" '(1 2) |
| 161 |
(match '(1 2 3) ((and (a ... b) x) a))) |
| 162 |
|
| 163 |
(test "anded search pattern" '(a b c) |
| 164 |
(match '(a (b (c d))) ((and (p *** 'd) x) p))) |
| 165 |
|
| 166 |
(test "joined tail" '(1 2) |
| 167 |
(match '(1 2 3) ((and (a ... b) x) a))) |
| 168 |
|
| 169 |
(test "list ..1" '(a b c) |
| 170 |
(match '(a b c) ((x ..1) x))) |
| 171 |
|
| 172 |
(test "list ..1 failed" #f |
| 173 |
(match '() |
| 174 |
((x ..1) x) |
| 175 |
(else #f))) |
| 176 |
|
| 177 |
(test "list ..1 with predicate" '(a b c) |
| 178 |
(match '(a b c) |
| 179 |
(((and x (? symbol?)) ..1) x))) |
| 180 |
|
| 181 |
(test "list ..1 with failed predicate" #f |
| 182 |
(match '(a b 3) |
| 183 |
(((and x (? symbol?)) ..1) x) |
| 184 |
(else #f))) |
| 185 |
|
| 186 |
(test "match-named-let" 6 |
| 187 |
(match-let loop (((x . rest) '(1 2 3)) |
| 188 |
(sum 0)) |
| 189 |
(let ((sum (+ x sum))) |
| 190 |
(if (null? rest) |
| 191 |
sum |
| 192 |
(loop rest sum))))) |
| 193 |
|
| 194 |
(test-end) |