| 1 |
(use gauche.test) |
| 2 |
|
| 3 |
(test-start "util.*") |
| 4 |
|
| 5 |
;;-------------------------------------------------------------- |
| 6 |
(test-section "util.match") |
| 7 |
(use util.match) |
| 8 |
(test-module 'util.match) |
| 9 |
|
| 10 |
(test* "primitive types" '(emptylist |
| 11 |
true |
| 12 |
false |
| 13 |
string |
| 14 |
number |
| 15 |
character |
| 16 |
symbol |
| 17 |
any) |
| 18 |
(map (^[exp] |
| 19 |
(match exp |
| 20 |
[(a b) (list 'huh? a b)] |
| 21 |
[() 'emptylist] |
| 22 |
[#t 'true] |
| 23 |
[#f 'false] |
| 24 |
["a" 'string] |
| 25 |
[1 'number] |
| 26 |
[#\a 'character] |
| 27 |
['a 'symbol] |
| 28 |
[_ 'any])) |
| 29 |
'(() #t #f "a" 1 #\a a #()))) |
| 30 |
|
| 31 |
(test* "pattern" '([5 x (y z)] |
| 32 |
[4 x y] |
| 33 |
[2 x (y z) (u v w)] |
| 34 |
[1 x y (u v)] |
| 35 |
[2 x () (y)] |
| 36 |
[3 x y z]) |
| 37 |
(map (^[exp] |
| 38 |
(match exp |
| 39 |
[((a b) c) (list 1 a b c)] |
| 40 |
[((a . b) c) (list 2 a b c)] |
| 41 |
[#(a b c) (list 3 a b c)] |
| 42 |
[(a b) (list 4 a b)] |
| 43 |
[(a . b) (list 5 a b)])) |
| 44 |
'((x y z) |
| 45 |
(x y) |
| 46 |
((x y z) (u v w)) |
| 47 |
((x y) (u v)) |
| 48 |
((x) (y)) |
| 49 |
#(x y z)))) |
| 50 |
|
| 51 |
|
| 52 |
(test* "repetition pattern" '((1 1 2 (3 4 5)) |
| 53 |
;(2 1 2 (3 4 5)) |
| 54 |
(3 1 2 (3 4)) |
| 55 |
(4 1 2 (3 4)) |
| 56 |
(3 1 2 ()) |
| 57 |
(4 1 2 ())) |
| 58 |
(map (^[exp] |
| 59 |
(match exp |
| 60 |
[(a b c ..3) (list 1 a b c)] |
| 61 |
[#(a b c ..3) (list 2 a b c)] |
| 62 |
[(a b c ...) (list 3 a b c)] |
| 63 |
[#(a b c ...) (list 4 a b c)] |
| 64 |
)) |
| 65 |
'((1 2 3 4 5) |
| 66 |
;#(1 2 3 4 5) ; doesn't work? |
| 67 |
(1 2 3 4) |
| 68 |
#(1 2 3 4) |
| 69 |
(1 2) |
| 70 |
#(1 2)))) |
| 71 |
|
| 72 |
(test* "nested pattern" '((1 4) (2 5) (3 6)) |
| 73 |
(match '((1 (2 3)) (4 (5 6))) |
| 74 |
[((a (b c)) ...) (list a b c)])) |
| 75 |
|
| 76 |
;; a bug pointed by Hira |
| 77 |
(test* "nested pattern, conditional" 'a |
| 78 |
(match '((1 a b) (4 e) (6 q u e r)) |
| 79 |
[(((? number?) (? symbol?) ...) ...) 'a])) |
| 80 |
|
| 81 |
;; the cause of https://github.com/shirok/Gauche/issues/47 |
| 82 |
(test* "... must only match a proper list" 'c |
| 83 |
(match '(1 . 2) |
| 84 |
[(x) 'a] |
| 85 |
[(x ...) 'b] |
| 86 |
[_ 'c])) |
| 87 |
|
| 88 |
;; examples shown in Wright&Duba |
| 89 |
(test* "xmap" '(2 4 6) |
| 90 |
(letrec ([xmap (^[f l] |
| 91 |
(match l |
| 92 |
[() ()] |
| 93 |
[(x . y) (cons (f x) (xmap f y))]))]) |
| 94 |
(xmap (cut * <> 2) '(1 2 3)))) |
| 95 |
|
| 96 |
(test* "Y?" '(#t #f) |
| 97 |
(letrec ([y? (match-lambda |
| 98 |
[('lambda (f1) |
| 99 |
('lambda (y1) |
| 100 |
((('lambda (x1) (f2 ('lambda (z1) ((x2 x3) z2)))) |
| 101 |
('lambda (a1) (f3 ('lambda (b1) ((a2 a3) b2))))) |
| 102 |
y2))) |
| 103 |
(and (symbol? f1) (symbol? y1) (symbol? x1) |
| 104 |
(symbol? z1) (symbol? a1) (symbol? b1) |
| 105 |
(eq? f1 f2) (eq? f1 f3) (eq? y1 y2) |
| 106 |
(eq? x1 x2) (eq? x1 x3) (eq? z1 z2) |
| 107 |
(eq? a1 a2) (eq? a1 a3) (eq? b1 b2))] |
| 108 |
[_ #f])]) |
| 109 |
(list |
| 110 |
(y? '(lambda (F) |
| 111 |
(lambda (Y) |
| 112 |
(((lambda (j) (F (lambda (k) ((j j) k)))) |
| 113 |
(lambda (l) (F (lambda (m) ((l l) m))))) |
| 114 |
Y)))) |
| 115 |
(y? '(lambda (F) |
| 116 |
(lambda (Y) |
| 117 |
(((lambda (j) (F (lambda (k) ((j j) k)))) |
| 118 |
(lambda (l) (F (lambda (m) ((l l) l))))) |
| 119 |
Y)))) |
| 120 |
))) |
| 121 |
|
| 122 |
;; interference with hygienic macro |
| 123 |
(let () |
| 124 |
(define-syntax define-registrar |
| 125 |
(syntax-rules () |
| 126 |
[(_ varname key default2-var default3-var) |
| 127 |
(define (varname proc . opts) |
| 128 |
(match opts |
| 129 |
[() (set! default3-var proc)] |
| 130 |
[(':perspective) (set! default3-var proc)] |
| 131 |
[(':orthographic) (set! default2-var proc)] |
| 132 |
[(name) |
| 133 |
(cond [(name->window name) => (^[win] (ref win'closure) 'key proc)] |
| 134 |
[else |
| 135 |
(errorf "~a: no such window with name: ~a" 'varname name)])] |
| 136 |
))])) |
| 137 |
|
| 138 |
(define a #f) |
| 139 |
(define b #f) |
| 140 |
(define-registrar z x a b) |
| 141 |
(test* "match in hygienic expansion" '(2 1) |
| 142 |
(begin (z 1 :perspective) |
| 143 |
(z 2 :orthographic) |
| 144 |
(list a b))) |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
;;-------------------------------------------------------------- |
| 149 |
|
| 150 |
(test* "pred" '(a b c) |
| 151 |
(match '("abc" a b c) |
| 152 |
[((? string?) x ...) x])) |
| 153 |
|
| 154 |
(test* "pred" "abc" |
| 155 |
(match '("abc" a b c) |
| 156 |
[((? string? k) x ...) k])) |
| 157 |
|
| 158 |
;;-------------------------------------------------------------- |
| 159 |
|
| 160 |
(define-class <foo> () |
| 161 |
((a :init-keyword :a :accessor a-of) |
| 162 |
(b :init-keyword :b :accessor b-of))) |
| 163 |
|
| 164 |
(test* "struct" '(0 "foo") |
| 165 |
(match (make <foo> :a 0 :b "foo") |
| 166 |
[($ <foo> x y) (list x y)])) |
| 167 |
|
| 168 |
(test* "field" 0 |
| 169 |
(match (make <foo> :a 0 :b "foo") |
| 170 |
[(= a-of aa) aa])) |
| 171 |
|
| 172 |
(test* "object" '(1 "bar") |
| 173 |
(match (make <foo> :a 1 :b "bar") |
| 174 |
[(object <foo> (b bb) (a aa)) (list aa bb)])) |
| 175 |
|
| 176 |
;; examples shown in Wright&Duba |
| 177 |
(define-class Lam () |
| 178 |
((args :init-keyword :args) |
| 179 |
(body :init-keyword :body))) |
| 180 |
(define-class Var () |
| 181 |
((s :init-keyword :s))) |
| 182 |
(define-class Const () |
| 183 |
((n :init-keyword :n))) |
| 184 |
(define-class App () |
| 185 |
((fun :init-keyword :fun) |
| 186 |
(args :init-keyword :args))) |
| 187 |
|
| 188 |
(define parse |
| 189 |
(match-lambda |
| 190 |
[(and s (? symbol?) (not 'lambda)) |
| 191 |
(make Var :s s)] |
| 192 |
[(? number? n) |
| 193 |
(make Const :n n)] |
| 194 |
[('lambda (and args ((? symbol?) ...) (not (? repeats?))) body) |
| 195 |
(make Lam :args args :body (parse body))] |
| 196 |
[(f args ...) |
| 197 |
(make App :fun (parse f) :args (map parse args))] |
| 198 |
[x |
| 199 |
(error "invalid expression" x)])) |
| 200 |
|
| 201 |
(define (repeats? l) |
| 202 |
(and (not (null? l)) |
| 203 |
(or (memq (car l) (cdr l)) (repeats? (cdr l))))) |
| 204 |
|
| 205 |
(define unparse |
| 206 |
(match-lambda |
| 207 |
[($ Var s) s] |
| 208 |
[($ Const n) n] |
| 209 |
[($ Lam args body) `(lambda ,args ,(unparse body))] |
| 210 |
[($ App f args) `(,(unparse f) ,@(map unparse args))])) |
| 211 |
|
| 212 |
(test* "parse-unparse" '(lambda (a b c) (map (lambda (d) (a d 3)) (list b c))) |
| 213 |
(unparse (parse '(lambda (a b c) |
| 214 |
(map (lambda (d) (a d 3)) (list b c)))))) |
| 215 |
|
| 216 |
(define unparse-obj |
| 217 |
(match-lambda |
| 218 |
[(@ Var (s symbol)) symbol] |
| 219 |
[(@ Const (n number)) number] |
| 220 |
[(@ Lam (body body-expr) (args lambda-list)) |
| 221 |
`(lambda ,lambda-list ,(unparse-obj body-expr))] |
| 222 |
[(@ App (fun f) (args args)) |
| 223 |
`(,(unparse-obj f) ,@(map unparse-obj args))])) |
| 224 |
|
| 225 |
(test* "parse-unparse-obj" |
| 226 |
'(lambda (a b c) (map (lambda (d) (a d 3)) (list b c))) |
| 227 |
(unparse-obj (parse '(lambda (a b c) |
| 228 |
(map (lambda (d) (a d 3)) (list b c)))))) |
| 229 |
|
| 230 |
;;-------------------------------------------------------------- |
| 231 |
|
| 232 |
(test* "get! / pair" 3 |
| 233 |
(let1 x (list 1 (list 2 3)) |
| 234 |
(match x |
| 235 |
[(_ (_ (get! getter))) (getter)]))) |
| 236 |
|
| 237 |
(test* "set! / pair" '(1 (2 4)) |
| 238 |
(rlet1 x (list 1 (list 2 3)) |
| 239 |
(match x |
| 240 |
[(_ (_ (set! setter))) (setter 4)]))) |
| 241 |
|
| 242 |
(test* "get! / vector" 3 |
| 243 |
(let1 x (vector 1 2 3 4 5) |
| 244 |
(match x |
| 245 |
[#(a b (get! getter) d e) (getter)]))) |
| 246 |
|
| 247 |
(test* "set! / vector" '#(1 2 o 4 5) |
| 248 |
(rlet1 x (vector 1 2 3 4 5) |
| 249 |
(match x |
| 250 |
[#(a b (set! setter) d e) (setter 'o)]))) |
| 251 |
|
| 252 |
(test* "get! / $" 'foo |
| 253 |
(let* ([v (make Var :s 'foo)] |
| 254 |
[x (list 0 0 (vector 0 0 v 0) 0)]) |
| 255 |
(match x |
| 256 |
[(_ _ #(_ _ ($ Var (get! getter)) _) _) (getter)]))) |
| 257 |
|
| 258 |
(test* "get! / @" 'foo |
| 259 |
(let* ([v (make Var :s 'foo)] |
| 260 |
[x (list 0 0 (vector 0 0 v 0) 0)]) |
| 261 |
(match x |
| 262 |
[(_ _ #(_ _ (@ Var (s (get! getter))) _) _) (getter)]))) |
| 263 |
|
| 264 |
(test* "set! / $" 'bar |
| 265 |
(let* ([v (make Var :s 'foo)] |
| 266 |
[x (list 0 0 (vector 0 0 v 0) 0)]) |
| 267 |
(match x |
| 268 |
[(_ _ #(_ _ ($ Var (set! setter)) _) _) |
| 269 |
(setter 'bar) |
| 270 |
(ref v 's)]))) |
| 271 |
|
| 272 |
(test* "set! / @" 'bar |
| 273 |
(let* ([v (make Var :s 'foo)] |
| 274 |
[x (list 0 0 (vector 0 0 v 0) 0)]) |
| 275 |
(match x |
| 276 |
[(_ _ #(_ _ (@ Var (s (set! setter))) _) _) |
| 277 |
(setter 'bar) |
| 278 |
(ref v 's)]))) |
| 279 |
|
| 280 |
;;-------------------------------------------------------------- |
| 281 |
|
| 282 |
;; test case derived from the bug report from Tatsuya BIZENN. |
| 283 |
|
| 284 |
(define (test-match ls) |
| 285 |
(match ls |
| 286 |
[(a b . c) (=> next) (values a b c)] |
| 287 |
[(a . b) (=> next) (values #f a b)] |
| 288 |
[() (=> next) (values #f #f '())])) |
| 289 |
|
| 290 |
(test* "failure continuation #1" '(1 2 (3)) |
| 291 |
(values->list (test-match '(1 2 3)))) |
| 292 |
|
| 293 |
(test* "failure continuation #2" '(#f 1 ()) |
| 294 |
(values->list (test-match '(1)))) |
| 295 |
|
| 296 |
(test* "failure continuation #2" '(#f #f ()) |
| 297 |
(values->list (test-match '()))) |
| 298 |
|
| 299 |
(test* "failure continuation #2" (test-error) |
| 300 |
(test-match 1)) |
| 301 |
|
| 302 |
|
| 303 |
;;-------------------------------------------------------------- |
| 304 |
|
| 305 |
(test* "match-let1" '((a b c) (1 2 3)) |
| 306 |
(match-let1 ((ca . cd) ...) '((a . 1) (b . 2) (c . 3)) |
| 307 |
(list ca cd))) |
| 308 |
|
| 309 |
;;-------------------------------------------------------------- |
| 310 |
|
| 311 |
;; Interaction between hygienic macro and match |
| 312 |
(define-syntax gen-match |
| 313 |
(syntax-rules () [(_) (match 1 [(and 1 x) x])])) |
| 314 |
|
| 315 |
(test* "match in hygienic macro" 1 (gen-match)) |
| 316 |
|
| 317 |
;;-------------------------------------------------------------- |
| 318 |
|
| 319 |
(when (symbol? :a) |
| 320 |
(test* "ensure we're using RnRS lambda" 1 |
| 321 |
(match '(1) |
| 322 |
[(:a) :a])) |
| 323 |
(test* "ensure we're using RnRS lambda" '(1) |
| 324 |
(match '(1) |
| 325 |
[:a :a]))) |
| 326 |
|
| 327 |
(test-end) |