| 1 |
(import |
| 2 |
(scheme base) |
| 3 |
(srfi 64) |
| 4 |
(compounds)) |
| 5 |
|
| 6 |
(define (test-compound-equal c1 c2) |
| 7 |
(test-equal |
| 8 |
(compound-subobjects c1) |
| 9 |
(compound-subobjects c2))) |
| 10 |
|
| 11 |
(test-begin "Compounds") |
| 12 |
|
| 13 |
(test-group "make-compound, compound?, compound-subobjects" |
| 14 |
(define (test c) |
| 15 |
(test-assert (compound? c)) |
| 16 |
(test-equal (compound-subobjects c) '(1 2 3))) |
| 17 |
(test (make-compound 1 2 3)) |
| 18 |
(test (make-compound (make-compound 1) (make-compound 2) 3)) |
| 19 |
|
| 20 |
(test-assert (not (compound? (list '(1 2 3)))))) |
| 21 |
|
| 22 |
(test-group "compound-length" |
| 23 |
(test-equal 3 (compound-length (make-compound 1 2 3))) |
| 24 |
(test-equal 1 (compound-length 'test))) |
| 25 |
|
| 26 |
(test-group "compound-ref" |
| 27 |
(test-equal 1 (compound-ref (make-compound 1 2 3) 0)) |
| 28 |
(test-equal 1 (compound-ref 1 0))) |
| 29 |
|
| 30 |
(test-group "compound-map" |
| 31 |
(define c (make-compound 1 2 3)) |
| 32 |
|
| 33 |
(test-compound-equal |
| 34 |
(make-compound 2 3 4) |
| 35 |
(compound-map (lambda (e) (+ 1 e)) c)) |
| 36 |
|
| 37 |
(test-compound-equal |
| 38 |
(make-compound 0 2 0 3 0 4) |
| 39 |
(compound-map (lambda (e) (make-compound 0 (+ 1 e))) c)) |
| 40 |
|
| 41 |
(test-compound-equal |
| 42 |
(make-compound 2) |
| 43 |
(compound-map (lambda (e) (+ 1 e)) 1))) |
| 44 |
|
| 45 |
(test-group "compound-map->list" |
| 46 |
(define c (make-compound 1 2 3)) |
| 47 |
(test-equal |
| 48 |
(compound-map->list |
| 49 |
(lambda (e) (+ 1 e)) |
| 50 |
c) |
| 51 |
(list 2 3 4)) |
| 52 |
(test-equal |
| 53 |
(compound-map->list |
| 54 |
(lambda (e) (+ 1 e)) |
| 55 |
1) |
| 56 |
(list 2))) |
| 57 |
|
| 58 |
(test-group "compound-filter" |
| 59 |
(define c (make-compound 1 2 3)) |
| 60 |
(test-compound-equal |
| 61 |
(compound-filter (lambda (e) (= e 2)) c) |
| 62 |
(make-compound 2)) |
| 63 |
(test-compound-equal |
| 64 |
(compound-filter (lambda (e) (= e 2)) 2) |
| 65 |
(make-compound 2)) |
| 66 |
(test-compound-equal |
| 67 |
(compound-filter (lambda (e) (= e 2)) 1) |
| 68 |
(make-compound))) |
| 69 |
|
| 70 |
(test-group "compound-predicate" |
| 71 |
(define c1 (make-compound 1 2)) |
| 72 |
(define c2 (make-compound 3 4)) |
| 73 |
(define (pred1 obj) |
| 74 |
(equal? obj 'a)) |
| 75 |
(define (pred3 obj) |
| 76 |
(equal? obj 1)) |
| 77 |
|
| 78 |
(test-assert (compound-predicate pred1 'a)) |
| 79 |
(test-assert (not (compound-predicate pred1 c1))) |
| 80 |
(test-assert (not (compound-predicate pred1 c2))) |
| 81 |
|
| 82 |
(test-assert (not (compound-predicate pred3 'a))) |
| 83 |
(test-assert (compound-predicate pred3 c1)) |
| 84 |
(test-assert (not (compound-predicate pred3 c2)))) |
| 85 |
|
| 86 |
(test-group "compound-access" |
| 87 |
(define (pred obj) |
| 88 |
(and (number? obj) |
| 89 |
(= obj 2))) |
| 90 |
(define (accessor obj) |
| 91 |
(+ 1 obj)) |
| 92 |
(test-equal |
| 93 |
(compound-access pred accessor 0 (make-compound 1 2 3)) |
| 94 |
3) |
| 95 |
(test-equal |
| 96 |
(compound-access pred accessor 0 (make-compound 1 3)) |
| 97 |
0) |
| 98 |
(test-equal |
| 99 |
(compound-access pred accessor 0 1) |
| 100 |
0) |
| 101 |
(test-equal |
| 102 |
(compound-access pred accessor 0 2) |
| 103 |
3)) |
| 104 |
|
| 105 |
(test-group "examples in spec" |
| 106 |
|
| 107 |
;; The following definitions are referenced in later examples |
| 108 |
|
| 109 |
(define-record-type <student> |
| 110 |
(student admission-year gpa) |
| 111 |
student? |
| 112 |
(admission-year admission-year) |
| 113 |
(gpa gpa)) ; grade-point average |
| 114 |
|
| 115 |
(define-record-type <teacher> |
| 116 |
(teacher hire-year salary) |
| 117 |
teacher? |
| 118 |
(hire-year hire-year) ; integer year |
| 119 |
(salary salary)) ; annualized |
| 120 |
|
| 121 |
(define alyssa (student 1986 4.0)) |
| 122 |
|
| 123 |
(define guy (teacher 1981 25000)) |
| 124 |
|
| 125 |
;; These definitions are referenced in later examples |
| 126 |
|
| 127 |
(define george |
| 128 |
(make-compound |
| 129 |
'teaching-assistant |
| 130 |
(student 1979 3.8) |
| 131 |
(teacher 1983 1000))) |
| 132 |
|
| 133 |
(define (teaching-assistant? obj) |
| 134 |
(eq? obj 'teaching-assistant)) |
| 135 |
|
| 136 |
(test-assert (not (compound? alyssa))) |
| 137 |
(let ((subobjs (compound-subobjects alyssa))) |
| 138 |
(test-assert (list? subobjs)) |
| 139 |
(test-equal 1 (length subobjs)) |
| 140 |
(test-assert (student? (car subobjs))) |
| 141 |
(test-equal 1986 (admission-year (car subobjs))) |
| 142 |
(test-equal 4.0 (gpa (car subobjs)))) |
| 143 |
(let ((subobjs (compound-subobjects george))) |
| 144 |
(test-assert (list? subobjs)) |
| 145 |
(test-equal 3 (length subobjs)) |
| 146 |
(test-equal 'teaching-assistant (car subobjs)) |
| 147 |
(test-assert (student? (cadr subobjs))) |
| 148 |
(test-equal 1979 (admission-year (cadr subobjs))) |
| 149 |
(test-equal 3.8 (gpa (cadr subobjs))) |
| 150 |
(test-assert (teacher? (list-ref subobjs 2))) |
| 151 |
(test-equal 1983 (hire-year (list-ref subobjs 2))) |
| 152 |
(test-equal 1000 (salary (list-ref subobjs 2)))) |
| 153 |
|
| 154 |
(test-equal 1 (compound-length alyssa)) |
| 155 |
|
| 156 |
(test-assert (student? (compound-ref alyssa 0))) |
| 157 |
(test-assert (teacher? (compound-ref george 2))) |
| 158 |
|
| 159 |
(test-equal |
| 160 |
'(-1 -2 -3 -4 -5) |
| 161 |
(compound-subobjects (compound-map - (make-compound 1 2 3 4 5)))) |
| 162 |
(test-equal |
| 163 |
'(-1 -2 -3 -4 -5) |
| 164 |
(compound-map->list - (make-compound 1 2 3 4 5))) |
| 165 |
|
| 166 |
(test-equal |
| 167 |
'() |
| 168 |
(compound-subobjects (compound-filter teacher? alyssa))) |
| 169 |
(let ((subobjs (compound-subobjects (compound-filter teacher? george)))) |
| 170 |
(test-equal 1 (length subobjs)) |
| 171 |
(test-assert (teacher? (car subobjs)))) |
| 172 |
|
| 173 |
(test-assert (compound-predicate student? alyssa)) |
| 174 |
(test-assert (compound-predicate student? george)) |
| 175 |
(test-assert (compound-predicate teacher? george)) |
| 176 |
(test-assert (compound-predicate teacher? guy)) |
| 177 |
(test-assert (not (compound-predicate teaching-assistant? alyssa))) |
| 178 |
(test-assert (not (compound-predicate teaching-assistant? guy))) |
| 179 |
(test-assert (compound-predicate teaching-assistant? george)) |
| 180 |
|
| 181 |
(let () |
| 182 |
(define (uni-member-hire-year obj) |
| 183 |
(compound-access teacher? hire-year #f obj)) |
| 184 |
|
| 185 |
(test-equal #f (uni-member-hire-year alyssa)) |
| 186 |
(test-equal 1981 (uni-member-hire-year guy)) |
| 187 |
(test-equal 1983 (uni-member-hire-year george)) |
| 188 |
(test-equal #f (uni-member-hire-year (make-compound '(27 42 98) 'fire!))))) |
| 189 |
|
| 190 |
(test-end "Compounds") |