nendo 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,186 @@
1
+ ;;-*- mode: nendo; syntax: scheme -*-;;
2
+ ;;
3
+ ;; match-test.nnd - test suite for util.match
4
+ ;;
5
+ ;; This file ported from chibi-scheme 0.3 by Alex Shinn.
6
+ ;;
7
+
8
+ (use nendo.test)
9
+ (use util.match)
10
+ ;;(use util.match-expanded)
11
+
12
+ (test-start "match")
13
+ (test-section "match")
14
+
15
+ (define-syntax match-test*
16
+ (syntax-rules ()
17
+ ((_ title code result)
18
+ (test* title result code))))
19
+
20
+ (define-syntax pending*
21
+ (syntax-rules ()
22
+ ((_ title code result)
23
+ (printf "(pending) [%s]\n" title))))
24
+
25
+
26
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27
+ ;; run tests
28
+ (match-test* "any" (match 'any (_ 'ok)) 'ok)
29
+ (match-test* "symbol" (match 'ok (x x)) 'ok)
30
+ (match-test* "number" (match 28 (28 'ok)) 'ok)
31
+ (match-test* "string" (match "good" ("bad" 'fail) ("good" 'ok)) 'ok)
32
+ (match-test* "literal symbol" (match 'good ('bad 'fail) ('good 'ok)) 'ok)
33
+ (match-test* "null" (match '() (() 'ok)) 'ok)
34
+ (match-test* "pair" (match '(ok) ((x) x)) 'ok)
35
+ (match-test* "vector" (match '#(ok) (#(x) x)) 'ok)
36
+ (match-test* "any doubled" (match '(1 2) ((_ _) 'ok)) 'ok)
37
+ (match-test* "and empty" (match '(o k) ((and) 'ok)) 'ok)
38
+ (match-test* "and single" (match 'ok ((and x) x)) 'ok)
39
+ (match-test* "and double" (match 'ok ((and (? symbol?) y) 'ok)) 'ok)
40
+ (match-test* "or empty" (match '(o k) ((or) 'fail) (else 'ok)) 'ok)
41
+ (match-test* "or single" (match 'ok ((or x) 'ok)) 'ok)
42
+ (match-test* "or double" (match 'ok ((or (? symbol? y) y) y)) 'ok)
43
+ (match-test* "not" (match 28 ((not (a . b)) 'ok)) 'ok)
44
+ (match-test* "pred" (match 28 ((? number?) 'ok)) 'ok)
45
+ (match-test* "named pred" (match 28 ((? number? x) (+ x 1))) 29)
46
+ (match-test* "duplicate symbols pass" (match '(ok . ok) ((x . x) x)) 'ok)
47
+ (match-test* "duplicate symbols fail" (match '(ok . bad) ((x . x) 'bad) (else 'ok)) 'ok)
48
+ (match-test* "duplicate symbols samth" (match '(ok . ok) ((x . 'bad) x) (('ok . x) x)) 'ok)
49
+
50
+ (match-test* "ellipses"
51
+ (match '((a . 1) (b . 2) (c . 3))
52
+ (((x . y) ___) (list x y)))
53
+ '((a b c) (1 2 3)))
54
+
55
+ (match-test* "real ellipses"
56
+ (match '((a . 1) (b . 2) (c . 3))
57
+ (((x . y) ...) (list x y)))
58
+ '((a b c) (1 2 3)))
59
+
60
+ (match-test* "vector ellipses"
61
+ (match '#(1 2 3 (a . 1) (b . 2) (c . 3))
62
+ (#(a b c (hd . tl) ...) (list a b c hd tl)))
63
+ '(1 2 3 (a b c) (1 2 3)))
64
+
65
+ (match-test* "pred ellipses"
66
+ (match '(1 2 3)
67
+ (((? odd? n) ___) n)
68
+ (((? number? n) ___) n))
69
+ '(1 2 3))
70
+
71
+ (match-test* "failure continuation"
72
+ (match '(1 2)
73
+ ((a . b) (=> next) (if (even? a) 'fail (next)))
74
+ ((a . b) 'ok))
75
+ 'ok)
76
+
77
+ (match-test* "let"
78
+ (match-let ((x 'ok) (y '(o k)))
79
+ y)
80
+ '(o k))
81
+
82
+ (match-test* "let*"
83
+ (match-let* ((x 'f) (y 'o) ((z w) (list y x)))
84
+ (list x y z w))
85
+ '(f o o f))
86
+
87
+ (match-test* "getter car"
88
+ (match '(1 . 2) (((get! a) . b) (list (a) b)))
89
+ '(1 2))
90
+
91
+ (match-test* "getter cdr"
92
+ (match '(1 . 2) ((a . (get! b)) (list a (b))))
93
+ '(1 2))
94
+
95
+ (match-test* "getter vector"
96
+ (match '#(1 2 3) (#((get! a) b c) (list (a) b c)))
97
+ '(1 2 3))
98
+
99
+ (match-test* "setter car"
100
+ (let ((x (cons 1 2)))
101
+ (match x (((set! a) . b) (a 3)))
102
+ x)
103
+ '(3 . 2))
104
+
105
+ (match-test* "setter cdr"
106
+ (let ((x (cons 1 2)))
107
+ (match x ((a . (set! b)) (b 3)))
108
+ x)
109
+ '(1 . 3))
110
+
111
+ (match-test* "setter vector"
112
+ (let ((x (vector 1 2 3)))
113
+ (match x (#(a (set! b) c) (b 0)))
114
+ x)
115
+ '#(1 0 3))
116
+
117
+ (match-test* "single tail"
118
+ (match '((a . 1) (b . 2) (c . 3))
119
+ (((x . y) ... last) (list x y last)))
120
+ '((a b) (1 2) (c . 3)))
121
+
122
+ (match-test* "single tail 2"
123
+ (match '((a . 1) (b . 2) 3)
124
+ (((x . y) ... last) (list x y last)))
125
+ '((a b) (1 2) 3))
126
+
127
+ (match-test* "multiple tail"
128
+ (match '((a . 1) (b . 2) (c . 3) (d . 4) (e . 5))
129
+ (((x . y) ... u v w) (list x y u v w)))
130
+ '((a b) (1 2) (c . 3) (d . 4) (e . 5)))
131
+
132
+ (match-test* "Riastradh quasiquote"
133
+ (match '(1 2 3) (`(1 ,b ,c) (list b c)))
134
+ '(2 3))
135
+
136
+ (match-test* "trivial tree search"
137
+ (match '(1 2 3) ((_ *** (a b c)) (list a b c)))
138
+ '(1 2 3))
139
+
140
+ (match-test* "simple tree search"
141
+ (match '(x (1 2 3)) ((_ *** (a b c)) (list a b c)))
142
+ '(1 2 3))
143
+
144
+ (match-test* "deep tree search"
145
+ (match '(x (x (x (1 2 3)))) ((_ *** (a b c)) (list a b c)))
146
+ '(1 2 3))
147
+
148
+ (match-test* "non-tail tree search"
149
+ (match '(x (x (x a b c (1 2 3) d e f))) ((_ *** (a b c)) (list a b c)))
150
+ '(1 2 3))
151
+
152
+ (match-test* "restricted tree search"
153
+ (match '(x (x (x a b c (1 2 3) d e f))) (('x *** (a b c)) (list a b c)))
154
+ '(1 2 3))
155
+
156
+ (match-test* "fail restricted tree search"
157
+ (match '(x (y (x a b c (1 2 3) d e f)))
158
+ (('x *** (a b c)) (list a b c))
159
+ (else #f))
160
+ #f)
161
+
162
+ ;; /home2/home/kiyoka/work/github/nendo/lib/nendo.rb:2317: stack level too deep (SystemStackError)
163
+ (match-test* "sxml tree search"
164
+ (match '(p (ul (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f)))
165
+ (((or 'p 'ul 'li 'b) *** ('a ('^ attrs ...) text ...))
166
+ (list attrs text))
167
+ (else #f))
168
+ '(((href . "http://synthcode.com/")) ("synthcode")))
169
+
170
+ ;; /home2/home/kiyoka/work/github/nendo/lib/nendo.rb:2317: stack level too deep (SystemStackError)
171
+ (match-test* "failed sxml tree search"
172
+ (match '(p (ol (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f)))
173
+ (((or 'p 'ul 'li 'b) *** ('a ('^ attrs ...) text ...))
174
+ (list attrs text))
175
+ (else #f))
176
+ #f)
177
+
178
+ ;; ./test/match-test.nnd:192: Error: undefined variable _tag (NameError)
179
+ (match-test* "collect tree search"
180
+ (match '(p (ul (li a (b c) (a (^ (href . "http://synthcode.com/")) "synthcode") d e f)))
181
+ (((and tag (or 'p 'ul 'li 'b)) *** ('a ('^ attrs ...) text ...))
182
+ (list tag attrs text))
183
+ (else #f))
184
+ '((p ul li) ((href . "http://synthcode.com/")) ("synthcode")))
185
+
186
+ (test-end)
@@ -40,13 +40,11 @@
40
40
  (let1 var
41
41
  (+ arg1 1) var))
42
42
  expanded:
43
- (define
44
- (' dummy-function)
43
+ (define dummy-function
45
44
  (lambda
46
- ('
47
- (arg1))
48
- (let
49
- (((' var)
45
+ (arg1)
46
+ (%let
47
+ ((var
50
48
  (+ arg1 1))) var)))
51
49
  "
52
50
  (disasm 'dummy-function 'info))
@@ -54,7 +52,7 @@
54
52
  '(define (dummy-function arg1) (let1 var (+ arg1 1) var))
55
53
  (disasm 'dummy-function 'source))
56
54
  (test* "disasm expanded"
57
- '(define 'dummy-function (lambda '(arg1) (let (('var (+ arg1 1))) var)))
55
+ '(define dummy-function (lambda (arg1) (%let ((var (+ arg1 1))) var)))
58
56
  (disasm 'dummy-function 'expanded))
59
57
  (test* "disasm ruby-code"
60
58
  "