forked from scheme-requests-for-implementation/srfi-128
-
Notifications
You must be signed in to change notification settings - Fork 2
/
comparators-test.scm
257 lines (228 loc) · 11.8 KB
/
comparators-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
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
(use test)
(use srfi-128)
(load "../comparators/r7rs-shim.scm")
(define (print x) (display x) (newline))
(test-group "comparators"
(define (vector-cdr vec)
(let* ((len (vector-length vec))
(result (make-vector (- len 1))))
(let loop ((n 1))
(cond
((= n len) result)
(else (vector-set! result (- n 1) (vector-ref vec n))
(loop (+ n 1)))))))
(test '#(2 3 4) (vector-cdr '#(1 2 3 4)))
(test '#() (vector-cdr '#(1)))
(print "default-comparator")
(define default-comparator (make-default-comparator))
(print "real-comparator")
(define real-comparator (make-comparator real? = < number-hash))
(print "degenerate comparator")
(define degenerate-comparator (make-comparator (lambda (x) #t) equal? #f #f))
(print "boolean comparator")
(define boolean-comparator
(make-comparator boolean? eq? (lambda (x y) (and (not x) y)) boolean-hash))
(print "bool-pair-comparator")
(define bool-pair-comparator (make-pair-comparator boolean-comparator boolean-comparator))
(print "num-list-comparator")
(define num-list-comparator
(make-list-comparator real-comparator list? null? car cdr))
(print "num-vector-comparator")
(define num-vector-comparator
(make-vector-comparator real-comparator vector? vector-length vector-ref))
(print "vector-qua-list comparator")
(define vector-qua-list-comparator
(make-list-comparator
real-comparator
vector?
(lambda (vec) (= 0 (vector-length vec)))
(lambda (vec) (vector-ref vec 0))
vector-cdr))
(print "list-qua-vector-comparator")
(define list-qua-vector-comparator
(make-vector-comparator default-comparator list? length list-ref))
(print "eq-comparator")
(define eq-comparator (make-eq-comparator))
(print "eqv-comparator")
(define eqv-comparator (make-eqv-comparator))
(print "equal-comparator")
(define equal-comparator (make-equal-comparator))
(print "symbol-comparator")
(define symbol-comparator
(make-comparator
symbol?
eq?
(lambda (a b) (string<? (symbol->string a) (symbol->string b)))
symbol-hash))
(test-group "comparators/predicates"
(test-assert (comparator? real-comparator))
(test-assert (not (comparator? =)))
(test-assert (comparator-ordered? real-comparator))
(test-assert (comparator-hashable? real-comparator))
(test-assert (not (comparator-ordered? degenerate-comparator)))
(test-assert (not (comparator-hashable? degenerate-comparator)))
) ; end comparators/predicates
(test-group "comparators/constructors"
(test-assert (=? boolean-comparator #t #t))
(test-assert (not (=? boolean-comparator #t #f)))
(test-assert (<? boolean-comparator #f #t))
(test-assert (not (<? boolean-comparator #t #t)))
(test-assert (not (<? boolean-comparator #t #f)))
(test-assert (comparator-test-type bool-pair-comparator '(#t . #f)))
(test-assert (not (comparator-test-type bool-pair-comparator 32)))
(test-assert (not (comparator-test-type bool-pair-comparator '(32 . #f))))
(test-assert (not (comparator-test-type bool-pair-comparator '(#t . 32))))
(test-assert (not (comparator-test-type bool-pair-comparator '(32 . 34))))
(test-assert (=? bool-pair-comparator '(#t . #t) '(#t . #t)))
(test-assert (not (=? bool-pair-comparator '(#t . #t) '(#f . #t))))
(test-assert (not (=? bool-pair-comparator '(#t . #t) '(#t . #f))))
(test-assert (<? bool-pair-comparator '(#f . #t) '(#t . #t)))
(test-assert (<? bool-pair-comparator '(#t . #f) '(#t . #t)))
(test-assert (not (<? bool-pair-comparator '(#t . #t) '(#t . #t))))
(test-assert (not (<? bool-pair-comparator '(#t . #t) '(#f . #t))))
(test-assert (not (<? bool-pair-comparator '(#f . #t) '(#f . #f))))
(test-assert (comparator-test-type num-vector-comparator '#(1 2 3)))
(test-assert (comparator-test-type num-vector-comparator '#()))
(test-assert (not (comparator-test-type num-vector-comparator 1)))
(test-assert (not (comparator-test-type num-vector-comparator '#(a 2 3))))
(test-assert (not (comparator-test-type num-vector-comparator '#(1 b 3))))
(test-assert (not (comparator-test-type num-vector-comparator '#(1 2 c))))
(test-assert (=? num-vector-comparator '#(1 2 3) '#(1 2 3)))
(test-assert (not (=? num-vector-comparator '#(1 2 3) '#(4 5 6))))
(test-assert (not (=? num-vector-comparator '#(1 2 3) '#(1 5 6))))
(test-assert (not (=? num-vector-comparator '#(1 2 3) '#(1 2 6))))
(test-assert (<? num-vector-comparator '#(1 2) '#(1 2 3)))
(test-assert (<? num-vector-comparator '#(1 2 3) '#(2 3 4)))
(test-assert (<? num-vector-comparator '#(1 2 3) '#(1 3 4)))
(test-assert (<? num-vector-comparator '#(1 2 3) '#(1 2 4)))
(test-assert (<? num-vector-comparator '#(3 4) '#(1 2 3)))
(test-assert (not (<? num-vector-comparator '#(1 2 3) '#(1 2 3))))
(test-assert (not (<? num-vector-comparator '#(1 2 3) '#(1 2))))
(test-assert (not (<? num-vector-comparator '#(1 2 3) '#(0 2 3))))
(test-assert (not (<? num-vector-comparator '#(1 2 3) '#(1 1 3))))
(test-assert (not (<? vector-qua-list-comparator '#(3 4) '#(1 2 3))))
(test-assert (<? list-qua-vector-comparator '(3 4) '(1 2 3)))
(define bool-pair (cons #t #f))
(define bool-pair-2 (cons #t #f))
(define reverse-bool-pair (cons #f #t))
(test-assert (=? eq-comparator #t #t))
(test-assert (not (=? eq-comparator #f #t)))
(test-assert (=? eqv-comparator bool-pair bool-pair))
(test-assert (not (=? eqv-comparator bool-pair bool-pair-2)))
(test-assert (=? equal-comparator bool-pair bool-pair-2))
(test-assert (not (=? equal-comparator bool-pair reverse-bool-pair)))
) ; end comparators/constructors
(test-group "comparators/hash"
(test-assert (exact-integer? (boolean-hash #f)))
(test-assert (not (negative? (boolean-hash #t))))
(test-assert (exact-integer? (char-hash #\a)))
(test-assert (not (negative? (char-hash #\b))))
(test-assert (exact-integer? (char-ci-hash #\a)))
(test-assert (not (negative? (char-ci-hash #\b))))
(test-assert (= (char-ci-hash #\a) (char-ci-hash #\A)))
(test-assert (exact-integer? (string-hash "f")))
(test-assert (not (negative? (string-hash "g"))))
(test-assert (exact-integer? (string-ci-hash "f")))
(test-assert (not (negative? (string-ci-hash "g"))))
(test-assert (= (string-ci-hash "f") (string-ci-hash "F")))
(test-assert (exact-integer? (symbol-hash 'f)))
(test-assert (not (negative? (symbol-hash 't))))
(test-assert (exact-integer? (number-hash 3)))
(test-assert (not (negative? (number-hash 3))))
(test-assert (exact-integer? (number-hash -3)))
(test-assert (not (negative? (number-hash -3))))
(test-assert (exact-integer? (number-hash 3.0)))
(test-assert (not (negative? (number-hash 3.0))))
) ; end comparators/hash
(test-group "comparators/default"
(test-assert (<? default-comparator '() '(a)))
(test-assert (not (=? default-comparator '() '(a))))
(test-assert (=? default-comparator #t #t))
(test-assert (not (=? default-comparator #t #f)))
(test-assert (<? default-comparator #f #t))
(test-assert (not (<? default-comparator #t #t)))
(test-assert (=? default-comparator #\a #\a))
(test-assert (<? default-comparator #\a #\b))
(test-assert (comparator-test-type default-comparator '()))
(test-assert (comparator-test-type default-comparator #t))
(test-assert (comparator-test-type default-comparator #\t))
(test-assert (comparator-test-type default-comparator '(a)))
(test-assert (comparator-test-type default-comparator 'a))
(test-assert (comparator-test-type default-comparator (make-bytevector 10)))
(test-assert (comparator-test-type default-comparator 10))
(test-assert (comparator-test-type default-comparator 10.0))
(test-assert (comparator-test-type default-comparator "10.0"))
(test-assert (comparator-test-type default-comparator '#(10)))
(test-assert (=? default-comparator '(#t . #t) '(#t . #t)))
(test-assert (not (=? default-comparator '(#t . #t) '(#f . #t))))
(test-assert (not (=? default-comparator '(#t . #t) '(#t . #f))))
(test-assert (<? default-comparator '(#f . #t) '(#t . #t)))
(test-assert (<? default-comparator '(#t . #f) '(#t . #t)))
(test-assert (not (<? default-comparator '(#t . #t) '(#t . #t))))
(test-assert (not (<? default-comparator '(#t . #t) '(#f . #t))))
(test-assert (not (<? default-comparator '#(#f #t) '#(#f #f))))
(test-assert (=? default-comparator '#(#t #t) '#(#t #t)))
(test-assert (not (=? default-comparator '#(#t #t) '#(#f #t))))
(test-assert (not (=? default-comparator '#(#t #t) '#(#t #f))))
(test-assert (<? default-comparator '#(#f #t) '#(#t #t)))
(test-assert (<? default-comparator '#(#t #f) '#(#t #t)))
(test-assert (not (<? default-comparator '#(#t #t) '#(#t #t))))
(test-assert (not (<? default-comparator '#(#t #t) '#(#f #t))))
(test-assert (not (<? default-comparator '#(#f #t) '#(#f #f))))
(test-assert (= (comparator-hash default-comparator #t) (boolean-hash #t)))
(test-assert (= (comparator-hash default-comparator #\t) (char-hash #\t)))
(test-assert (= (comparator-hash default-comparator "t") (string-hash "t")))
(test-assert (= (comparator-hash default-comparator 't) (symbol-hash 't)))
(test-assert (= (comparator-hash default-comparator 10) (number-hash 10)))
(test-assert (= (comparator-hash default-comparator 10.0) (number-hash 10.0)))
(comparator-register-default!
(make-comparator procedure? (lambda (a b) #t) (lambda (a b) #f) (lambda (obj) 200)))
(test-assert (=? default-comparator (lambda () #t) (lambda () #f)))
(test-assert (not (<? default-comparator (lambda () #t) (lambda () #f))))
(test 200 (comparator-hash default-comparator (lambda () #t)))
) ; end comparators/default
(test-group "comparators/accessors"
(define ttp (lambda (x) #t))
(define eqp (lambda (x y) #t))
(define orp (lambda (x y) #t))
(define hf (lambda (x) 0))
(define comp (make-comparator ttp eqp orp hf))
(test ttp (comparator-type-test-predicate comp))
(test eqp (comparator-equality-predicate comp))
(test orp (comparator-ordering-predicate comp))
(test hf (comparator-hash-function comp))
) ; end comparators/accessors
(test-group "comparators/invokers"
(test-assert (comparator-test-type real-comparator 3))
(test-assert (comparator-test-type real-comparator 3.0))
(test-assert (not (comparator-test-type real-comparator "3.0")))
(test-assert (comparator-check-type boolean-comparator #t))
(test-error (comparator-check-type boolean-comparator 't))
) ; end comparators/invokers
(test-group "comparators/comparison"
(test-assert (=? real-comparator 2 2.0 2))
(test-assert (<? real-comparator 2 3.0 4))
(test-assert (>? real-comparator 4.0 3.0 2))
(test-assert (<=? real-comparator 2.0 2 3.0))
(test-assert (>=? real-comparator 3 3.0 2))
(test-assert (not (=? real-comparator 1 2 3)))
(test-assert (not (<? real-comparator 3 1 2)))
(test-assert (not (>? real-comparator 1 2 3)))
(test-assert (not (<=? real-comparator 4 3 3)))
(test-assert (not (>=? real-comparator 3 4 4.0)))
) ; end comparators/comparison
(test-group "comparators/syntax"
(test 'less (comparator-if<=> real-comparator 1 2 'less 'equal 'greater))
(test 'equal (comparator-if<=> real-comparator 1 1 'less 'equal 'greater))
(test 'greater (comparator-if<=> real-comparator 2 1 'less 'equal 'greater))
(test 'less (comparator-if<=> "1" "2" 'less 'equal 'greater))
(test 'equal (comparator-if<=> "1" "1" 'less 'equal 'greater))
(test 'greater (comparator-if<=> "2" "1" 'less 'equal 'greater))
) ; end comparators/syntax
(test-group "comparators/bound-salt"
(test-assert (exact-integer? (hash-bound)))
(test-assert (exact-integer? (hash-salt)))
(test-assert (< (hash-salt) (hash-bound)))
) ; end comparators/bound-salt
) ; end comparators
(test-exit)