| 1 |
(cond-expand |
| 2 |
(chibi |
| 3 |
(import (chibi test) |
| 4 |
(scheme base) |
| 5 |
(srfi 1) |
| 6 |
(srfi 128) |
| 7 |
(only (srfi 132) list-sort))) |
| 8 |
(else)) |
| 9 |
|
| 10 |
(define-record-type Person |
| 11 |
(make-person first-name last-name) |
| 12 |
person? |
| 13 |
(first-name person-first-name) |
| 14 |
(last-name person-last-name)) |
| 15 |
|
| 16 |
(define person-name-comparator |
| 17 |
(make-product-comparator |
| 18 |
(make-wrapper-comparator person? person-last-name string-ci-comparator) |
| 19 |
(make-wrapper-comparator person? person-first-name string-ci-comparator))) |
| 20 |
|
| 21 |
|
| 22 |
(test-group "base cases" |
| 23 |
|
| 24 |
(test-equal eqv? #t (comparator? comparator-one)) |
| 25 |
(test-equal eqv? #t (comparator? comparator-zero)) |
| 26 |
|
| 27 |
(define data `(a 1 "a" (a) #(a) ,(make-person "John" "Doe"))) |
| 28 |
|
| 29 |
(test-equal equal? |
| 30 |
'(#t #t #t #t #t #t) |
| 31 |
(map (lambda (d) (comparator-test-type comparator-one d)) data)) |
| 32 |
(test-equal equal? |
| 33 |
'(#t #t #t #t #t #t) |
| 34 |
(map (lambda (d) (comparator-test-type (make-product-comparator) d)) data)) |
| 35 |
|
| 36 |
(test-equal equal? |
| 37 |
'(#f #f #f #f #f #f) |
| 38 |
(map (lambda (d) (comparator-test-type comparator-zero d)) data)) |
| 39 |
(test-equal equal? |
| 40 |
'(#f #f #f #f #f #f) |
| 41 |
(map (lambda (d) (comparator-test-type (make-sum-comparator) d)) data)) |
| 42 |
|
| 43 |
(test-equal equal? |
| 44 |
'(#f #f #f #f #f #f) |
| 45 |
(map (lambda (d) (<? comparator-one d d)) data)) |
| 46 |
(test-equal equal? |
| 47 |
'(#f #f #f #f #f #f) |
| 48 |
(map (lambda (d) (<? (make-product-comparator) d d)) data)) |
| 49 |
|
| 50 |
(test-equal eqv? #f (comparator-ordered? comparator-zero)) |
| 51 |
(test-equal eqv? #f (comparator-ordered? (make-sum-comparator))) |
| 52 |
|
| 53 |
(test-equal equal? |
| 54 |
'(0 0 0 0 0 0) |
| 55 |
(map (lambda (d) (comparator-hash comparator-one d)) data)) |
| 56 |
(test-equal equal? |
| 57 |
'(0 0 0 0 0 0) |
| 58 |
(map (lambda (d) (comparator-hash (make-product-comparator) d)) data)) |
| 59 |
|
| 60 |
(test-equal eqv? #f (comparator-hashable? comparator-zero)) |
| 61 |
(test-equal eqv? #f (comparator-hashable? (make-sum-comparator))) |
| 62 |
) |
| 63 |
|
| 64 |
(test-group "simple" |
| 65 |
(test-equal eq? |
| 66 |
#t |
| 67 |
(<? person-name-comparator |
| 68 |
(make-person "John" "Cowan") |
| 69 |
(make-person "Daphne" "Preston-Kendal"))) |
| 70 |
|
| 71 |
(test-equal eq? |
| 72 |
#t |
| 73 |
(>? person-name-comparator |
| 74 |
(make-person "Tom" "Smith") |
| 75 |
(make-person "John" "Smith")))) |
| 76 |
|
| 77 |
(define-record-type Book |
| 78 |
(make-book author title) |
| 79 |
book? |
| 80 |
(author book-author) |
| 81 |
(title book-title)) |
| 82 |
|
| 83 |
(define book-comparator |
| 84 |
(make-product-comparator |
| 85 |
(make-wrapper-comparator book? book-author person-name-comparator) |
| 86 |
(make-wrapper-comparator book? book-title string-ci-comparator))) |
| 87 |
|
| 88 |
(define-record-type CD |
| 89 |
(make-cd artist title) |
| 90 |
cd? |
| 91 |
(artist cd-artist) |
| 92 |
(title cd-title)) |
| 93 |
|
| 94 |
(define cd-comparator |
| 95 |
(make-product-comparator |
| 96 |
(make-wrapper-comparator cd? cd-artist person-name-comparator) |
| 97 |
(make-wrapper-comparator cd? cd-title string-ci-comparator))) |
| 98 |
|
| 99 |
(define item-comparator |
| 100 |
(make-sum-comparator book-comparator cd-comparator)) |
| 101 |
|
| 102 |
(test-group "nested" |
| 103 |
(let* ((beatles (make-person "The" "Beatles")) |
| 104 |
(abbey-road (make-cd beatles "Abbey Road")) |
| 105 |
(deutsche-grammatik |
| 106 |
(make-book (make-person "Jacob" "Grimm") "Deutsche Grammatik")) |
| 107 |
(sonnets (make-book (make-person "William" "Shakespeare") "Sonnets")) |
| 108 |
(mnd (make-book (make-person "William" "Shakespeare") |
| 109 |
"A Midsummer Night’s Dream")) |
| 110 |
(bob (make-cd (make-person "Bob" "Dylan") "Blonde on Blonde")) |
| 111 |
(revolver (make-cd (make-person "The" "Beatles") "Revolver"))) |
| 112 |
(test-equal |
| 113 |
equal? |
| 114 |
(list-sort |
| 115 |
(lambda (a b) (<? item-comparator a b)) |
| 116 |
(list abbey-road |
| 117 |
deutsche-grammatik |
| 118 |
sonnets |
| 119 |
mnd |
| 120 |
bob |
| 121 |
revolver)) |
| 122 |
(list deutsche-grammatik |
| 123 |
mnd |
| 124 |
sonnets |
| 125 |
abbey-road |
| 126 |
revolver |
| 127 |
bob)))) |