-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc22-tests.scm
263 lines (221 loc) · 9.68 KB
/
c22-tests.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
(load "./c22.scm")
(load "./test.scm")
(run-tests '(
; ex. 2.2
(=? '(y-point (make-point 1 9)) 9)
(=? '(x-point (make-point 1 9)) 1)
(=? '(showpoint (make-point 5 7)) "5,7")
(=? '(showsegment (make-segment (make-point 0 0)(make-point 2 2)))
"0,0:2,2")
(=? '(showpoint (midpoint-segment (make-segment (make-point 0 0)(make-point 2 2))))
"1.0,1.0")
(=? '(showpoint (midpoint-segment (make-segment (make-point -2 -4)(make-point 2 2))))
"0.0,-1.0")
; ex. 2.3
(=? '(len-segment (make-segment (make-point 0 0)(make-point 2 2))) 2)
(=? '(showsegment (horiz-rect t-rect1)) "0,2:2,2")
(=? '(showsegment (vert-rect t-rect1)) "0,2:0,0")
(=? '(len-rect t-rect1) 2)
(=? '(hei-rect t-rect1) 2)
(=? '(hei-rect (make-rect (make-point 1 5)(make-point 5 1))) 4)
(=?~ '(area-rect (make-rect
(make-point 0 2)(make-point 5 0)))
10.0)
(=?~ '(perim-rect (make-rect (make-point 0 2)(make-point 5 0))) 14.0)
;ex 2.4
(=? '(kar (kons 1 2)) 1)
(=? '(kdr (kons 1 2)) 2)
;ex 2.5
(=? '(which-power? 3 3) 1)
(=? '(which-power? 9 3) 2)
(=? '(which-power? 27 3) 3)
(=? '(which-power? 2 2) 1)
(=? '(which-power? 4 2) 2)
(=? '(which-power? 32 2) 5)
(=? '(which-power? 64 2) 6)
(=? '(qar (qons 1 2)) 1)
(=? '(qdr (qons 1 2)) 2)
(=? '(qar (qons 2 3)) 2)
(=? '(qdr (qons 2 3)) 3)
(=? '(qar (qons 5 8)) 5)
(=? '(qdr (qons 5 8)) 8)
; ex 2.17
(=? '(last-pair (list 4 5 6 7)) 7)
(=? '(last-pair (list 1)) 1)
(=? '(last-pair '()) '())
; ex 2.18
(=? '(reverse_ (list 1 2 3 4)) '(4 3 2 1))
(=? '(reverse_ '()) '())
(=? '(reverse__ (list 1 2 3 4)) '(4 3 2 1))
(=? '(reverse__ '()) '())
; ex 2.19
(=? '(cc 100 us-coins) 292)
(=? '(cc 100 us-rev-coins) 292)
; ex 2.20
(=? '(filter_ even? (list 2 4 6 7)) '(2 4 6))
(=? '(filter_ odd? (list 1 2 4 6 7)) '(1 7))
(=? '(same-parity 1 2 3 4 5 6 7) '(1 3 5 7))
(=? '(same-parity 2 3 4 5 6 7) '(2 4 6))
; ex 2.21
(=? '(square-list_ (list 1 2 3 4)) '(1 4 9 16))
(=? '(square-list__ (list 1 2 3 4)) '(1 4 9 16))
; ex 2.22
(=? '(square-list3-broken (list 1 2 3 4)) '(16 9 4 1))
(=? '(square-list3-fixed (list 1 2 3 4)) '(1 4 9 16))
(=? '(square-list3-fixed_ (list 1 2 3 4)) '(1 4 9 16))
; ex 2.23
(=? '(for-each_ (lambda(x) (* x x)) (list 1 2 3)) '())
; ex 2.25
(=? '(expr1 (list 1 2 (list 5 7) 9)) 7)
(=? '(expr2 (list (list 7))) 7)
(=? '(expr3 (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7))))))) 7)
; ex 2.26
(=? '(append x26 y26) (list 1 2 3 4 5 6))
(=? '(cons x26 y26) (list (list 1 2 3) 4 5 6))
(=? '(list x26 y26) (list (list 1 2 3) (list 4 5 6)))
; ex 2.27
(=? '(reverse_ x27) (list (list 3 4) (list 1 2)))
(=? '(deep-reverse x27) (list (list 4 3) (list 2 1)))
; ex 2.28
(=? '(fringe x28) (list 1 2 3 4))
(=? '(fringe (list x28 x28)) (list 1 2 3 4 1 2 3 4))
; ex 2.29
(=? '(right-branch (make-mobile 1 2)) 2)
(=? '(left-branch (make-mobile 1 2)) 1)
(=? '(is-branch? (make-branch 1 (make-mobile (make-branch 1 1)
(make-branch 1 1)))) #t)
(=? '(is-branch? '()) #f)
(=? '(is-branch? (make-mobile (make-branch 1 1) (make-branch 1 1))) #f)
(=? '(is-structure? '()) #f)
(=? '(is-structure? (make-mobile (make-branch 1 1) (make-branch 1 1))) #t)
(=? '(is-structure? (make-branch 1 (make-mobile (make-branch 1 1)
(make-branch 1 1)))) #f)
(=? '(total-weight (make-mobile (make-branch 1 3)
(make-branch 5 (make-mobile (make-branch 3 7)
(make-branch 6 11))))) 21)
(=? '(is-balanced? (make-mobile (make-branch 4 8)
(make-branch 16 2)))
#t)
(=? '(is-balanced? (make-mobile (make-branch 4 8)
(make-branch 16 1)))
#f)
(=? '(is-balanced? (make-mobile (make-branch 5 (make-mobile (make-branch 2 10)
(make-branch 2 10)))
(make-branch 10 10)))
#t)
(=? '(is-balanced? (make-mobile (make-branch 5 (make-mobile (make-branch 3 10)
(make-branch 1 10)))
(make-branch 10 10)))
#f)
; ex 2.30
(=? '(skuare-tree (list 1
(list 2 (list 3 4) 5)
(list 6 7)))
'(1 (4 (9 16) 25) (36 49)))
(=? '(scuare-tree (list 1
(list 2 (list 3 4) 5)
(list 6 7)))
'(1 (4 (9 16) 25) (36 49)))
; ex 2.31
(=? '(m-square-tree (list 1
(list 2 (list 3 4) 5)
(list 6 7)))
'(1 (4 (9 16) 25) (36 49)))
; ex 2.32
(=? '(subsets '(1 2 3))
'(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)))
; accumulate (fold) test
(=? '(accumulate + 0 '(1 2 3 4 5)) 15)
(=? '(accumulate * 1 '(1 2 3 4 5)) 120)
(=? '(accumulate cons '() '(1 2 3 4 5)) '(1 2 3 4 5))
; ex 2.33
(=? '(map32 square '(1 2 3)) '(1 4 9))
(=? '(append32 '(1 2 3) '(4 5 6)) '(1 2 3 4 5 6))
(=? '(length32 '(1 2 3)) 3)
; ex 2.43
(=? '(horner-eval 2 '(1 3 0 5 0 1)) 79)
; ex 2.35
(=? '(count-leaves-acc (list (list 1 2) (list 3 4) (list 5 6)))
6)
(=? '(count-leaves-acc (list (list 1 (list 11 12)) 2 (list 3 4) (list 5 6)))
8)
; ex 2.36
(=? '(accumulate-n + 0 (list (list 1 2 3) (list 4 5 6) (list 7 8 9) (list 10 11 12)))
(list 22 26 30))
; ex 2.37
(=? '(dot-product (list 1 2 3) (list 3 4 5)) 26)
(=? '(matrix-*-vector '((1 2 3) (1 2 3) (1 2 3)) '(3 4 5)) (list 26 26 26))
(=? '(transpose '((1 2 3))) '((1) (2) (3)))
(=? '(transpose '((1 2 3) (1 2 3))) '((1 1) (2 2) (3 3)))
(=? '(matrix-*-matrix
'((1 2) (1 2)) '((1 0) (0 1))) '((1 2) (1 2)))
(=? '(matrix-*-matrix
'((1 2 3) (4 5 6)) '((1 2) (1 2) (1 2))) '((6 12) (15 30)))
; ex 2.38
(=? '(accumulate + 0 '(1 2 3)) (foldr + 0 '(1 2 3)))
(=? '(foldl + 0 '(1 2 3)) (foldr + 0 '(1 2 3)))
(=?~ '(foldr / 1 '(1 2 3)) 1.5)
(=?~ '(foldl / 1 '(1 2 3)) 0.166)
(=? '(foldr list '() '(1 2 3)) (list 1 (list 2 (list 3 '()))))
(=? '(foldl list '() '(1 2 3)) (list (list (list '() 1) 2) 3))
; ex 2.39
(=? '(fr-rev '(1 2 3 4)) '(4 3 2 1))
(=? '(fl-rev '(1 2 3 4)) '(4 3 2 1))
; ex 2.40
; needed functions
(=? '(prime? 2) #t)
(=? '(prime? 3) #t)
(=? '(prime? 23) #t)
(=? '(prime? 24) #f)
(=? '(flatmap (lambda(list)(map
(lambda(y) (+ y 1))
list))
(list (list 1 2)(list 3 4)))
'(2 3 4 5))
(=? '(enumerate-interval 0 0) '(0))
(=? '(enumerate-interval 0 1) '(0 1))
(=? '(enumerate-interval 0 5) '(0 1 2 3 4 5))
(=? '(enumerate-interval -2 2) '(-2 -1 0 1 2))
(=? '(prime-sum? (list 1 2)) #t)
(=? '(prime-sum? (list 2 2)) #f)
(=? '(make-pair-sum (list 1 2)) '(1 2 3))
(=? '(prime-sum-pairs 2) '((2 1 3)))
(=? '(remove 1 '(1 2 3)) '(2 3))
(=? '(permutations '(1 2 3)) '((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1)))
; 2.40
(=? '(unique-pairs 3) '((2 1) (3 1) (3 2)))
; 2.41
(=? '(unique-3-tuples 4) '((3 2 1) (4 2 1) (4 3 1) (4 3 2)))
(=? '(sum-equal? '(1 2 3) 6) #t)
(=? '(sum-equal? '(1 2 4) 6) #f)
(=? '(triple-sums 5 10) '((5 3 2) (5 4 1)))
(=? '(triple-sums 5 12) '((5 4 3)))
(=? '(triple-sums 5 13) '())
; 2.42
(=? '(all (lambda(x) (= x 3)) '(1 2 3)) #f)
(=? '(all (lambda(x) (= x 3)) '(3 3 3)) #t)
(=? '(uniques '(1 2 3)) '(1 2 3))
(=? '(uniques '(1 2 1)) '(2 1))
(=? '(uniques '()) '())
(=? '(p-row (mk-p 2 8)) 2)
(=? '(p-col (mk-p 7 6)) 6)
(=? '(last? (list (mk-p 1 1))) #t)
(=? '(safe-row? (list (mk-p 3 4) (mk-p 2 3))) #t)
(=? '(safe-row? (list (mk-p 2 3) (mk-p 3 2) (mk-p 2 1))) #f)
(=? '(safe? 1 '()) #t)
(=? '(safe? 2 (list (mk-p 1 2) (mk-p 1 1))) #f)
(=? '(safe? 2 (list (mk-p 3 2) (mk-p 1 1))) #t)
(=? '(safe? 2 (list (mk-p 2 2) (mk-p 1 1))) #f)
(=? '(safe? 2 (list (mk-p 1 2) (mk-p 2 1))) #f)
(=? '(safe? 3 (list (mk-p 1 3) (mk-p 4 2) (mk-p 2 1))) #t)
(=? '(safe? 3 (list (mk-p 1 3) (mk-p 5 2) (mk-p 3 1))) #f)
(=? '(safe? 3 (list (mk-p 3 3) (mk-p 4 2) (mk-p 2 1))) #f)
(=? '(length (queens 1)) 1)
(=? '(length (queens 2)) 0)
(=? '(length (queens 3)) 0)
(=? '(length (queens 4)) 2)
(=? '(length (queens 5)) 10)
(=? '(length (queens 6)) 4)
; (=? '(length (queens 7)) 40)
; (=? '(length (queens 8)) 92)
))