| 1 |
;; Tests generic comparison procedures |
| 2 |
;; NB: Type-specific comparison tests are in each type's test suite. |
| 3 |
|
| 4 |
(use gauche.test) |
| 5 |
|
| 6 |
(test-start "comparison procedures") |
| 7 |
|
| 8 |
(test-section "compare") |
| 9 |
|
| 10 |
(let () |
| 11 |
(define (test-compare* cmp data) |
| 12 |
(dolist [datum data] |
| 13 |
(apply (^[what a b eq] |
| 14 |
(test* #"compare ~what" (if eq '(0 0) '(-1 1)) |
| 15 |
(list (cmp a b) |
| 16 |
(cmp b a)))) |
| 17 |
datum))) |
| 18 |
|
| 19 |
(test-compare* |
| 20 |
compare |
| 21 |
'(("boolean" #t #t #t) |
| 22 |
("boolean" #f #f #t) |
| 23 |
("boolean" #f #t #f) |
| 24 |
|
| 25 |
("char" #\a #\a #t) |
| 26 |
("char" #\A #\a #f) |
| 27 |
|
| 28 |
("string" "ab" "ab" #t) |
| 29 |
("string" "ab" "abc" #f) |
| 30 |
("string" "aB" "ab" #f) |
| 31 |
|
| 32 |
("symbol" a a #t) |
| 33 |
("symbol" A a #f) |
| 34 |
("symbol" a ab #f) |
| 35 |
("symbol" a #:a #f) |
| 36 |
("symbol" #0=#:a #0# #t) |
| 37 |
|
| 38 |
("number" 1 1 #t) |
| 39 |
("number" 1 1.0 #t) |
| 40 |
("number" -1 1 #f) |
| 41 |
("number" 1 1+i #f) |
| 42 |
("number" 1-i 1 #f) |
| 43 |
("number" 1+i 1+2i #f) |
| 44 |
("number" 1+i 1+i #t) |
| 45 |
|
| 46 |
("null" () () #t) |
| 47 |
("null" () a #f) |
| 48 |
("pair" (a b c) (a b c) #t) |
| 49 |
("pair" (A b c) (a b c) #f) |
| 50 |
("pair" (a b) (a b c) #f) |
| 51 |
|
| 52 |
("vector" #(a b c) #(a b c) #t) |
| 53 |
("vector" #(a B c) #(a b c) #f) |
| 54 |
("vector" #(a b) #(a b c) #f) |
| 55 |
("vector" #() #() #t) |
| 56 |
|
| 57 |
("uvector" #u8(1 2 3) #u8(1 2 3) #t) |
| 58 |
("uvector" #u8(1 0 3) #u8(1 2 3) #f) |
| 59 |
("uvector" #u8(1 2) #u8(1 2 3) #f) |
| 60 |
("uvector" #u8() #u8() #t) |
| 61 |
|
| 62 |
)) |
| 63 |
|
| 64 |
(test-compare* |
| 65 |
(^[a b] (comparator-compare string-ci-comparator a b)) |
| 66 |
'(("string-ci" "abc" "ABC" #t) |
| 67 |
("string-ci" "ABC" "abd" #f) |
| 68 |
)) |
| 69 |
) |
| 70 |
|
| 71 |
(test-section "object-compare") |
| 72 |
|
| 73 |
;; comparing different types |
| 74 |
(define-class <other-type> () ()) |
| 75 |
(define-method object-compare ((a <other-type>) (b <other-type>)) 0) |
| 76 |
|
| 77 |
(let () |
| 78 |
;; NB: We haven't tested gauche.sequence, so no find-index yet. |
| 79 |
(define (precedence x) |
| 80 |
(let loop ([cls (list <null> <pair> <boolean> <char> <string> |
| 81 |
<symbol> <number> <vector> <u8vector> <s8vector> |
| 82 |
<u16vector> <s16vector> <u32vector> <s32vector> |
| 83 |
<u64vector> <s64vector> <f16vector> <f32vector> |
| 84 |
<f64vector> <hash-table>)] |
| 85 |
[n 0]) |
| 86 |
(cond [(null? cls) n] |
| 87 |
[(is-a? x (car cls)) n] |
| 88 |
[else (loop (cdr cls) (+ n 1))]))) |
| 89 |
(define (compare-matrix vals) |
| 90 |
(map (^[x] (map (^[y] (list (compare x y) (compare y x))) vals)) vals)) |
| 91 |
(define *data* |
| 92 |
`(() (a . b) #t #\a "a" 0 #(a) #u8(0) #s8(0) #u16(0) #s16(0) |
| 93 |
#u32(0) #s32(0) #u64(0) #s64(0) #f16(0) #f32(0) #f64(0) |
| 94 |
,(make <other-type>))) |
| 95 |
|
| 96 |
(test* "comparing different types" |
| 97 |
(compare-matrix (map precedence *data*)) |
| 98 |
(compare-matrix *data*))) |
| 99 |
|
| 100 |
;; comparing different user-defined types |
| 101 |
(define-module compare-test-a |
| 102 |
(define-class <compare-test-x> () ()) |
| 103 |
(define-class <compare-test-y> () ())) |
| 104 |
(define-module compare-test-b |
| 105 |
(define-class <compare-test-x> () ()) |
| 106 |
(define-class <compare-test-y> () ())) |
| 107 |
(let ([ax (make (with-module compare-test-a <compare-test-x>))] |
| 108 |
[ay (make (with-module compare-test-a <compare-test-y>))] |
| 109 |
[bx (make (with-module compare-test-b <compare-test-x>))] |
| 110 |
[by (make (with-module compare-test-b <compare-test-y>))]) |
| 111 |
(test* "comparing different user-defined types" |
| 112 |
'(-1 1 -1 1 1 -1) |
| 113 |
(list (compare ax ay) |
| 114 |
(compare ay ax) |
| 115 |
(compare ax bx) |
| 116 |
(compare bx ax) |
| 117 |
(compare ay bx) |
| 118 |
(compare bx ay)))) |
| 119 |
|
| 120 |
(test-section "builtin comparators") |
| 121 |
|
| 122 |
(let () |
| 123 |
(define tc (make-tuple-comparator integer-comparator |
| 124 |
(make-reverse-comparator real-comparator) |
| 125 |
string-comparator)) |
| 126 |
(test* "tuple comparator - type-test" '(#t #f #f #f) |
| 127 |
(list (comparator-test-type tc '(3 9.4 "abc")) |
| 128 |
(comparator-test-type tc '(3.1 9.4 "abc")) |
| 129 |
(comparator-test-type tc '(3 a "abc")) |
| 130 |
(comparator-test-type tc '(3 8 abc)))) |
| 131 |
(test* "tuple comparator - equality" '(#t #f #f #f) |
| 132 |
(list (=? tc '(1 2/3 "abc") '(1 2/3 "abc")) |
| 133 |
(=? tc '(1 2/3 "abc") '(2 2/3 "abc")) |
| 134 |
(=? tc '(1 2/3 "abc") '(1 1/3 "abc")) |
| 135 |
(=? tc '(1 2/3 "abc") '(1 2/3 "qbc")))) |
| 136 |
(test* "tuple comparator - compare" '(0 1 -1 -1 1 1 -1) |
| 137 |
(list (comparator-compare tc '(1 2/3 "abc") '(1 2/3 "abc")) |
| 138 |
(comparator-compare tc '(1 2/3 "abc") '(0 2/3 "abc")) |
| 139 |
(comparator-compare tc '(0 2/3 "abc") '(1 2/3 "abc")) |
| 140 |
(comparator-compare tc '(1 2/3 "abc") '(1 1/3 "abc")) |
| 141 |
(comparator-compare tc '(1 1/3 "abc") '(1 2/3 "abc")) |
| 142 |
(comparator-compare tc '(1 2/3 "abc") '(1 2/3 "aba")) |
| 143 |
(comparator-compare tc '(1 2/3 "aba") '(1 2/3 "abc")))) |
| 144 |
(test* "tuple comparator - hash" |
| 145 |
(fold-right combine-hash-value |
| 146 |
(comparator-hash string-comparator "abc") |
| 147 |
(list (comparator-hash integer-comparator 1) |
| 148 |
(comparator-hash real-comparator 2/3))) |
| 149 |
(comparator-hash tc '(1 2/3 "abc")))) |
| 150 |
|
| 151 |
;; comparator with no comparison proc / no hash proc |
| 152 |
|
| 153 |
(test* "comparator requires at least either equality or comparison" |
| 154 |
(test-error) |
| 155 |
(make-comparator/compare #t #t #f #f)) |
| 156 |
(test* "comparator equality fallback behavior" #t |
| 157 |
((comparator-equality-predicate (make-comparator/compare #t #t (^[a b] 0) #f)) |
| 158 |
1 2)) |
| 159 |
(test* "has comparison proc" #t |
| 160 |
(comparator-ordered? (make-comparator/compare #t eq? compare #f))) |
| 161 |
(test* "no comparison proc" #f |
| 162 |
(comparator-ordered? (make-comparator/compare #t eq? #f #f))) |
| 163 |
(test* "comparator fallback behavior" (test-error) |
| 164 |
((comparator-comparison-procedure (make-comparator/compare #t eq? #f #f)) |
| 165 |
'a 'b)) |
| 166 |
(test* "has hash function" #t |
| 167 |
(comparator-hashable? (make-comparator/compare #t eq? #f hash))) |
| 168 |
(test* "no hash function" #f |
| 169 |
(comparator-hashable? (make-comparator/compare #t eq? #f #f))) |
| 170 |
(test* "hash fallback behavior" (test-error) |
| 171 |
((comparator-hash-procedure (make-comparator/compare #t eq? #f #f)) 'a)) |
| 172 |
|
| 173 |
;; comparing comparators |
| 174 |
;; this is undefined in SRFI-114; we compare the slots. |
| 175 |
(test* "comparing comparator 1" #t |
| 176 |
(equal? (make-comparator/compare #t eq? #f #f) |
| 177 |
(make-comparator/compare #t eq? #f #f))) |
| 178 |
(test* "comparing comparator 2" #f |
| 179 |
(equal? (make-comparator/compare #t eq? #f #f) |
| 180 |
(make-comparator/compare #t eqv? #f #f))) |
| 181 |
(test* "comparing comparator 3" #t |
| 182 |
(equal? (make-comparator/compare #t #t compare #f) |
| 183 |
(make-comparator/compare #t #t compare #f))) |
| 184 |
(test* "comparing comparator 4" #f |
| 185 |
(equal? (make-comparator/compare #t #t compare #f) |
| 186 |
(make-comparator/compare #t #t compare hash))) |
| 187 |
(test* "comparing comparator 5" #t |
| 188 |
(let ([chk (^x (or (char? x) (string? x)))] |
| 189 |
[cmp (^[a b] (cond [(char? a) |
| 190 |
(if (char? b) |
| 191 |
(compare a b) |
| 192 |
-1)] |
| 193 |
[(string? a) |
| 194 |
(if (string? b) |
| 195 |
1 |
| 196 |
(compare a b))] |
| 197 |
[else 0]))] ; dummy |
| 198 |
[hash (^x (if (char? x) (eqv-hash x) (hash x)))]) |
| 199 |
(equal? (make-comparator/compare chk equal? cmp hash) |
| 200 |
(make-comparator/compare chk equal? cmp hash)))) |
| 201 |
(test* "comparing comparator 6" #t |
| 202 |
(equal? (make-comparator/compare #t equal? #f #f 'yo) |
| 203 |
(make-comparator/compare #t equal? #f #f 'yo))) |
| 204 |
(test* "comparing comparator 7" #f |
| 205 |
(equal? (make-comparator/compare #t equal? #f #f) |
| 206 |
(make-comparator/compare #t equal? #f #f 'yo))) |
| 207 |
(test* "comparing comparator 8" #f |
| 208 |
(equal? (make-comparator/compare #t equal? #f #f 'bo) |
| 209 |
(make-comparator/compare #t equal? #f #f 'yo))) |
| 210 |
|
| 211 |
(test-section "comparator utilities") |
| 212 |
|
| 213 |
(let () |
| 214 |
(define (t rcmp xss) |
| 215 |
(test* "=?" |
| 216 |
(map (^[xs] (apply = xs)) xss) |
| 217 |
(map (^[xs] (apply =? rcmp xs)) xss)) |
| 218 |
(test* "<?" |
| 219 |
(map (^[xs] (apply > xs)) xss) |
| 220 |
(map (^[xs] (apply <? rcmp xs)) xss)) |
| 221 |
(test* "<=?" |
| 222 |
(map (^[xs] (apply >= xs)) xss) |
| 223 |
(map (^[xs] (apply <=? rcmp xs)) xss)) |
| 224 |
(test* ">?" |
| 225 |
(map (^[xs] (apply < xs)) xss) |
| 226 |
(map (^[xs] (apply >? rcmp xs)) xss)) |
| 227 |
(test* ">=?" |
| 228 |
(map (^[xs] (apply <= xs)) xss) |
| 229 |
(map (^[xs] (apply >=? rcmp xs)) xss))) |
| 230 |
|
| 231 |
(dolist [rcmp (list |
| 232 |
(make-comparator #t = > #f) |
| 233 |
(make-comparator/compare #t = (^[a b] (compare b a)) #f))] |
| 234 |
(t rcmp '((1 1) |
| 235 |
(0 1) |
| 236 |
(1 0) |
| 237 |
(1 1 1) |
| 238 |
(0 1 1) |
| 239 |
(0 1 0) |
| 240 |
(1 1 0) |
| 241 |
(1 2 3) |
| 242 |
(2 1 3) |
| 243 |
(3 1 2) |
| 244 |
(3 2 1) |
| 245 |
(1 1 2 4 7 8 9) |
| 246 |
(0 1 2 4 7 8 9) |
| 247 |
(9 8 4 3 2 1 0) |
| 248 |
(9 8 4 4 2 1 1)))) |
| 249 |
) |
| 250 |
|
| 251 |
(let () |
| 252 |
(define (t maybe-compar lt eq gt) |
| 253 |
(test* `(comparator-if<=> ,maybe-compar ,@lt) 'lt (cif maybe-compar lt)) |
| 254 |
(test* `(comparator-if<=> ,maybe-compar ,@eq) 'eq (cif maybe-compar eq)) |
| 255 |
(test* `(comparator-if<=> ,maybe-compar ,@gt) 'gt (cif maybe-compar gt))) |
| 256 |
(define (cif maybe-compar args) |
| 257 |
(if maybe-compar |
| 258 |
(comparator-if<=> maybe-compar (car args) (cadr args) 'lt 'eq 'gt) |
| 259 |
(comparator-if<=> (car args) (cadr args) 'lt 'eq 'gt))) |
| 260 |
(t (make-comparator #t = > #f) '(1 0) '(1 1) '(0 1)) |
| 261 |
(t (make-comparator/compare #t = (^[a b] (compare b a)) #f) |
| 262 |
'(1 0) '(1 1) '(0 1)) |
| 263 |
(t #f '(0 1) '(1 1) '(1 0)) |
| 264 |
) |
| 265 |
|
| 266 |
(test-section "default comparator extension") |
| 267 |
|
| 268 |
(define-class <moo> () ((v :init-keyword :v))) |
| 269 |
|
| 270 |
(comparator-register-default! |
| 271 |
(make-comparator (^x (is-a? x <moo>)) |
| 272 |
(^[x y] (= (~ x'v) (~ y'v))) |
| 273 |
(^[x y] (< (~ x'v) (~ y'v))) |
| 274 |
(^x (default-hash (~ x'v))))) |
| 275 |
|
| 276 |
|
| 277 |
(let ([a (make <moo> :v 0)] |
| 278 |
[b (make <moo> :v 10)] |
| 279 |
[c (make <moo> :v 10)] |
| 280 |
[d (make <moo> :v -10)]) |
| 281 |
(define pairs `((,a ,b) (,a ,c) (,a ,d) (,b ,c) (,b ,d) (,c ,d))) |
| 282 |
(test* "extended default comparator comparison" |
| 283 |
(map (^p (let ([a (car p)] [b (cadr p)]) |
| 284 |
(list (= (~ a'v) (~ b'v)) |
| 285 |
(< (~ a'v) (~ b'v)) |
| 286 |
(<= (~ a'v) (~ b'v)) |
| 287 |
(> (~ a'v) (~ b'v)) |
| 288 |
(>= (~ a'v) (~ b'v))))) |
| 289 |
pairs) |
| 290 |
(map (^p (let ([a (car p)] [b (cadr p)]) |
| 291 |
(list (=? default-comparator a b) |
| 292 |
(<? default-comparator a b) |
| 293 |
(<=? default-comparator a b) |
| 294 |
(>? default-comparator a b) |
| 295 |
(>=? default-comparator a b)))) |
| 296 |
pairs)) |
| 297 |
|
| 298 |
(test* "extended default comparator hash" |
| 299 |
(map (^a (default-hash (~ a 'v))) (list a b c d)) |
| 300 |
(map (cut comparator-hash default-comparator <>) (list a b c d))) |
| 301 |
) |
| 302 |
|
| 303 |
(test-end) |