summaryrefslogtreecommitdiff
path: root/test-suite/tests/match.test.upstream
blob: 8dd73b56628aa63a74c640c302c5aa9232f15b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

(cond-expand
 (modules (import (chibi match) (only (chibi test) test-begin test test-end)))
 (else (load "lib/chibi/match/match.scm")))

(test-begin "match")

(test "any" 'ok (match 'any (_ 'ok)))
(test "symbol" 'ok (match 'ok (x x)))
(test "number" 'ok (match 28 (28 'ok)))
(test "string" 'ok (match "good" ("bad" 'fail) ("good" 'ok)))
(test "literal symbol" 'ok (match 'good ('bad 'fail) ('good 'ok)))
(test "null" 'ok (match '() (() 'ok)))
(test "pair" 'ok (match '(ok) ((x) x)))
(test "vector" 'ok (match '#(ok) (#(x) x)))
(test "any doubled" 'ok (match '(1 2) ((_ _) 'ok)))
(test "and empty" 'ok (match '(o k) ((and) 'ok)))
(test "and single" 'ok (match 'ok ((and x) x)))
(test "and double" 'ok (match 'ok ((and (? symbol?) y) 'ok)))
(test "or empty" 'ok (match '(o k) ((or) 'fail) (else 'ok)))
(test "or single" 'ok (match 'ok ((or x) 'ok)))
(test "or double" 'ok (match 'ok ((or (? symbol? y) y) y)))
(test "not" 'ok (match 28 ((not (a . b)) 'ok)))
(test "pred" 'ok (match 28 ((? number?) 'ok)))
(test "named pred" 29 (match 28 ((? number? x) (+ x 1))))

(test "duplicate symbols pass" 'ok (match '(ok . ok) ((x . x) x)))
(test "duplicate symbols fail" 'ok (match '(ok . bad) ((x . x) 'bad) (else 'ok)))
(test "duplicate symbols samth" 'ok (match '(ok . ok) ((x . 'bad) x) (('ok . x) x)))
(test "duplicate symbols bound" 3 (let ((a '(1 2))) (match a ((and (a 2) (1 b)) (+ a b)) (_ #f))))
(test "duplicate quasiquote" 'ok (match '(a b) ((or `(a ,x) `(,x b)) 'ok) (_ #f)))

(test "duplicate before ellipsis" #f
  (match '(1 2) ((a a ...) a) (else #f)))
(test "duplicate ellipsis pass" '(1 2)
  (match '((1 2) (1 2)) (((x ...) (x ...)) x) (else #f)))
(test "duplicate ellipsis fail" #f
  (match '((1 2) (1 2 3)) (((x ...) (x ...)) x) (else #f)))
(test "duplicate ellipsis trailing" '(1 2)
  (match '((1 2 3) (1 2 3)) (((x ... 3) (x ... 3)) x) (else #f)))
(test "duplicate ellipsis trailing fail" #f
  (match '((1 2 3) (1 1 3)) (((x ... 3) (x ... 3)) x) (else #f)))
(test "duplicate ellipsis fail trailing" #f
  (match '((1 2 3) (1 2 4)) (((x ... 3) (x ... 3)) x) (else #f)))
(test "ellipsis trailing" '(3 1 2)
  (match '(1 2 3) ((x ... y) (cons y x)) (else #f)))


(test "ellipses" '((a b c) (1 2 3))
  (match '((a . 1) (b . 2) (c . 3))
    (((x . y) ___) (list x y))))

(test "real ellipses" '((a b c) (1 2 3))
  (match '((a . 1) (b . 2) (c . 3))
    (((x . y) ...) (list x y))))

(test "vector ellipses" '(1 2 3 (a b c) (1 2 3))
  (match '#(1 2 3 (a . 1) (b . 2) (c . 3))
    (#(a b c (hd . tl) ...) (list a b c hd tl))))

(test "pred ellipses" '(1 2 3)
  (match '(1 2 3)
    (((? odd? n) ___) n)
    (((? number? n) ___) n)))

(test "failure continuation" 'ok
  (match '(1 2)
    ((a . b) (=> next) (if (even? a) 'fail (next)))
    ((a . b) 'ok)))

(test "let" '(o k)
  (match-let ((x 'ok) (y '(o k))) y))

(test "let*" '(f o o f)
  (match-let* ((x 'f) (y 'o) ((z w) (list y x))) (list x y z w)))

(test "getter car" '(1 2)
  (match '(1 . 2) (((get! a) . b) (list (a) b))))

(test "getter cdr" '(1 2)
  (match '(1 . 2) ((a . (get! b)) (list a (b)))))

(test "getter vector" '(1 2 3)
  (match '#(1 2 3) (#((get! a) b c) (list (a) b c))))

(test "setter car" '(3 . 2)
  (let ((x (cons 1 2)))
    (match x (((set! a) . b) (a 3)))
    x))

(test "setter cdr" '(1 . 3)
  (let ((x (cons 1 2)))
    (match x ((a . (set! b)) (b 3)))
    x))

(test "setter vector" '#(1 0 3)
  (let ((x (vector 1 2 3)))
    (match x (#(a (set! b) c) (b 0)))
    x))

(test "single tail" '((a b) (1 2) (c . 3))
  (match '((a . 1) (b . 2) (c . 3))
    (((x . y) ... last) (list x y last))))

(test "single tail 2" '((a b) (1 2) 3)
  (match '((a . 1) (b . 2) 3)
    (((x . y) ... last) (list x y last))))

(test "multiple tail" '((a b) (1 2) (c . 3) (d . 4) (e . 5))
  (match '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5))
    (((x . y) ... u v w) (list x y u v w))))

(test "tail against improper list" #f
  (match '(a b c d e f . g)
    ((x ... y u v w) (list x y u v w))
    (else #f)))

(test "Riastradh quasiquote" '(2 3)
  (match '(1 2 3) (`(1 ,b ,c) (list b c))))

(test "trivial tree search" '(1 2 3)
  (match '(1 2 3) ((_ *** (a b c)) (list a b c))))

(test "simple tree search" '(1 2 3)
  (match '(x (1 2 3)) ((_ *** (a b c)) (list a b c))))

(test "deep tree search" '(1 2 3)
  (match '(x (x (x (1 2 3)))) ((_ *** (a b c)) (list a b c))))

(test "non-tail tree search" '(1 2 3)
  (match '(x (x (x a b c (1 2 3) d e f))) ((_ *** (a b c)) (list a b c))))

(test "restricted tree search" '(1 2 3)
  (match '(x (x (x a b c (1 2 3) d e f))) (('x *** (a b c)) (list a b c))))

(test "fail restricted tree search" #f
  (match '(x (y (x a b c (1 2 3) d e f)))
    (('x *** (a b c)) (list a b c))
    (else #f)))

(test "sxml tree search" '(((href . "http://synthcode.com/")) ("synthcode"))
  (match '(p (ul (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f)))
    (((or 'p 'ul 'li 'b) *** ('a ('^ attrs ...) text ...))
     (list attrs text))
    (else #f)))

(test "failed sxml tree search" #f
  (match '(p (ol (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f)))
    (((or 'p 'ul 'li 'b) *** ('a ('^ attrs ...) text ...))
     (list attrs text))
    (else #f)))

(test "collect tree search"
    '((p ul li) ((href . "http://synthcode.com/")) ("synthcode"))
  (match '(p (ul (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f)))
    (((and tag (or 'p 'ul 'li 'b)) *** ('a ('^ attrs ...) text ...))
     (list tag attrs text))
    (else #f)))

(test "anded tail pattern" '(1 2)
      (match '(1 2 3) ((and (a ... b) x) a)))

(test "anded search pattern" '(a b c)
      (match '(a (b (c d))) ((and (p *** 'd) x) p)))

(test "joined tail" '(1 2)
      (match '(1 2 3) ((and (a ... b) x) a)))

(test "list ..1" '(a b c)
    (match '(a b c) ((x ..1) x)))

(test "list ..1 failed" #f
    (match '()
      ((x ..1) x)
      (else #f)))

(test "list ..1 with predicate" '(a b c)
    (match '(a b c)
      (((and x (? symbol?)) ..1) x)))

(test "list ..1 with failed predicate" #f
    (match '(a b 3)
      (((and x (? symbol?)) ..1) x)
      (else #f)))

(test "match-named-let" 6
    (match-let loop (((x . rest) '(1 2 3))
                     (sum 0))
      (let ((sum (+ x sum)))
        (if (null? rest)
            sum
            (loop rest sum)))))

(test-end)