-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopy-test.scm
197 lines (163 loc) · 5.01 KB
/
loopy-test.scm
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
195
196
(import (only chicken.format printf)
(only chicken.time current-process-milliseconds)
chicken.load)
(load-relative "loopy-loop.scm")
(load-relative "matchable.scm")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SRFI-64 subset + test-approx=
(define *pass* 0)
(define *fail* 0)
(define *start* 0)
(define (run-test name thunk expect eq pass-msg fail-msg)
(let ((result (thunk)))
(cond
((eq expect result)
(set! *pass* (+ *pass* 1))
(format-result pass-msg name expect result))
(else
(set! *fail* (+ *fail* 1))
(format-result fail-msg name expect result)))))
(define (format-result ls name expect result)
(let lp ((ls ls))
(cond
((null? ls) (newline))
((eq? (car ls) 'expect) (display expect) (lp (cdr ls)))
((eq? (car ls) 'result) (display result) (lp (cdr ls)))
((eq? (car ls) 'name) (if name (begin (display #\space) (display name))) (lp (cdr ls)))
(else (display (car ls)) (lp (cdr ls))))))
(define (test-begin . o)
(set! *pass* 0)
(set! *fail* 0)
(set! *start* (current-process-milliseconds)))
(define (format-float n prec)
(let* ((str (number->string n))
(len (string-length str)))
(let lp ((i (- len 1)))
(cond
((negative? i)
(string-append str "." (make-string prec #\0)))
((eqv? #\. (string-ref str i))
(let ((diff (+ 1 (- prec (- len i)))))
(cond
((positive? diff)
(string-append str (make-string diff #\0)))
((negative? diff)
(substring str 0 (+ i prec 1)))
(else
str))))
(else
(lp (- i 1)))))))
(define (format-percent num denom)
(let ((x (if (zero? denom) num (exact->inexact (/ num denom)))))
(format-float (* 100 x) 2)))
(define (test-end . o)
(let ((end (current-process-milliseconds))
(total (+ *pass* *fail*)))
(printf " ~A tests completed in ~A seconds\n"
total (format-float (exact->inexact (/ (- end *start*) 1000)) 3))
(printf " ~A (~A%) tests passed\n"
*pass* (format-percent *pass* total))
(printf " ~A (~A%) tests failed\n"
*fail* (format-percent *fail* total))))
(define-syntax test-assert
(syntax-rules ()
((_ x opt)
(run-assert x (lambda () opt)))
((_ x ) (run-assert 'x (lambda () x)))))
(define (run-equal name thunk expect eq)
(run-test name thunk expect eq
'("(PASS)" name)
'("(FAIL)" name ": expected " expect " but got " result)))
(define-syntax test-equal
(syntax-rules ()
((_ x y opt)
(run-equal x (lambda () y) opt equal?))
((_ x y) (run-equal 'x (lambda () x) y equal?))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; run tests
(test-begin "loop")
(test-equal
"stepping"
(loop lp ((i 0 (+ i 1)) (res '() (cons i res)))
(if (= i 3)
(reverse res)
(lp)))
'(0 1 2))
(test-equal
"basic in-list"
(let ((res '()))
(loop ((x <- in-list '(a b c)))
(set! res (cons x res)))
res)
'(c b a))
(test-equal
"in-list with result"
(loop ((x <- in-list '(a b c))
(res '() (cons x res)))
=> res)
'(c b a))
(test-equal
"in-list with collecting"
(loop ((x <- in-list '(a b c)) (res <- collecting x)) => res)
'(a b c))
(test-equal
"uneven length in-list's"
(loop ((x <- in-list '(a b c))
(y <- in-list '(1 2 3 4))
(res <- collecting (cons x y)))
=> res)
'((a . 1) (b . 2) (c . 3)))
(test-equal
"in-lists"
(loop ((ls <- in-lists '((a b c) (1 2 3)))
(res <- collecting ls))
=> res)
'((a 1) (b 2) (c 3)))
(define (flatten ls)
(reverse
(loop lp ((x ls <- in-list ls) (res '()))
=> res
(if (pair? x)
(lp res <- (lp ls <- x))
(lp res <- (cons x res))))))
(test-equal
"flatten (recursion test)"
(flatten '(1 (2) (3 (4 (5)) 6) 7))
'(1 2 3 4 5 6 7))
(test-equal
"in-string"
(loop ((c <- in-string "hello") (res <- collecting c)) => res)
'(#\h #\e #\l #\l #\o))
(test-equal
"in-string with start"
(loop ((c <- in-string "hello" 3) (res <- collecting c)) => res)
'(#\l #\o))
(test-equal
"in-string with start and end"
(loop ((c <- in-string "hello" 0 4) (res <- collecting c)) => res)
'(#\h #\e #\l #\l))
(test-equal
"in-string with start, end and step"
(loop ((c <- in-string "hello" 1 4 2) (res <- collecting c)) => res)
'(#\e #\l))
(test-equal
"in-string-reverse"
(loop ((c <- in-string-reverse "hello") (res <- collecting c)) => res)
'(#\o #\l #\l #\e #\h))
(test-equal
"in-vector"
(loop ((x <- in-vector '#(1 2 3)) (res <- collecting x)) => res)
'(1 2 3))
(test-equal
"in-permutations"
(loop ((p <- in-permutations '(a b c)) (res <- collecting p)) => res)
'((a b c) (a c b) (b a c) (b c a) (c a b) (c b a)))
(test-equal
"in-permutations with length"
(loop ((p <- in-permutations '(a b c) 2) (res <- collecting p)) => res)
'((a b) (a c) (b a) (b c) (c a) (c b)))
(test-equal
"in-combinations"
(loop ((p <- in-combinations '(a b c) 2) (res <- collecting p)) => res)
'((a b) (a c) (b c)))
(test-end "loop")