| 1 |
(define (times2 x) (* x 2)) |
| 2 |
(define s5 (s16vector 1 2 3 4 5)) |
| 3 |
(define s4 (s16vector 1 2 3 4)) |
| 4 |
(define s5+ (s16vector 1 2 3 4 6)) |
| 5 |
|
| 6 |
(define (steady i x) (values x x)) |
| 7 |
(define (count-up i x) (values x (+ x 1))) |
| 8 |
(define (count-down i x) (values x (- x 1))) |
| 9 |
(define (odd+1 x) (if (odd? x) (+ 1 x) #f)) |
| 10 |
(define s16vector< (comparator-ordering-predicate s16vector-comparator)) |
| 11 |
(define s16vector-hash (comparator-hash-function s16vector-comparator)) |
| 12 |
|
| 13 |
(define g (make-s16vector-generator s5)) |
| 14 |
(define-syntax test-equiv |
| 15 |
(syntax-rules () |
| 16 |
((test-equiv expect expr) |
| 17 |
(test expect (s16vector->list expr))) |
| 18 |
((test-equiv name expect expr) |
| 19 |
(test name expect (s16vector->list expr))))) |
| 20 |
|
| 21 |
(test-group "s16vector" |
| 22 |
(test-group "s16vector/constructors" |
| 23 |
(test-equiv "make" '(3 3 3 3 3) (make-s16vector 5 3)) |
| 24 |
(test-equiv "s16vector" '(-2 -1 0 1 2) (s16vector -2 -1 0 1 2)) |
| 25 |
(test-equiv "unfold up" '(10 11 12 13 14) |
| 26 |
(s16vector-unfold count-up 5 10)) |
| 27 |
(test-equiv "unfold down" '(10 9 8 7 6) |
| 28 |
(s16vector-unfold count-down 5 10)) |
| 29 |
(test-equiv "unfold steady" '(10 10 10 10 10) |
| 30 |
(s16vector-unfold steady 5 10)) |
| 31 |
(test-equiv "unfold-right up" '(14 13 12 11 10) |
| 32 |
(s16vector-unfold-right count-up 5 10)) |
| 33 |
(test-equiv "unfold-right down" '(6 7 8 9 10) |
| 34 |
(s16vector-unfold-right count-down 5 10)) |
| 35 |
(test-equiv "unfold-right steady" '(10 10 10 10 10) |
| 36 |
(s16vector-unfold-right steady 5 10)) |
| 37 |
(test-equiv "copy" '(1 2 3 4 5) (s16vector-copy s5)) |
| 38 |
(test-assert "copy2" (not (eqv? s5 (s16vector-copy s5)))) |
| 39 |
(test-equiv "copy3" '(2 3) (s16vector-copy s5 1 3)) |
| 40 |
(test-equiv "reverse-copy" '(5 4 3 2 1) (s16vector-reverse-copy s5)) |
| 41 |
(test-equiv "append" '(1 2 3 4 5 1 2 3 4 5) |
| 42 |
(s16vector-append s5 s5)) |
| 43 |
(test-equiv "concatenate" '(1 2 3 4 5 1 2 3 4 5) |
| 44 |
(s16vector-concatenate (list s5 s5))) |
| 45 |
(test-equiv "append-subvectors" '(2 3 2 3) |
| 46 |
(s16vector-append-subvectors s5 1 3 s5 1 3)) |
| 47 |
) ; end s16vector/constructors |
| 48 |
|
| 49 |
(test-group "s16vector/predicates" |
| 50 |
(test-assert "s16?" (s16? 5)) |
| 51 |
(test-assert "not s16?" (not (s16? 65536))) |
| 52 |
(test-assert "s16vector?" (s16vector? s5)) |
| 53 |
(test-assert "not s16vector?" (not (s16vector? #t))) |
| 54 |
(test-assert "empty" (s16vector-empty? (s16vector))) |
| 55 |
(test-assert "not empty" (not (s16vector-empty? s5))) |
| 56 |
(test-assert "=" (s16vector= (s16vector 1 2 3) (s16vector 1 2 3))) |
| 57 |
(test-assert "not =" (not (s16vector= (s16vector 1 2 3) (s16vector 3 2 1)))) |
| 58 |
(test-assert "not =2" (not (s16vector= (s16vector 1 2 3) (s16vector 1 2)))) |
| 59 |
) ; end s16vector/predicates |
| 60 |
|
| 61 |
(test-group "s16vector/selectors" |
| 62 |
(test "ref" 1 (s16vector-ref (s16vector 1 2 3) 0)) |
| 63 |
(test "length" 3 (s16vector-length (s16vector 1 2 3))) |
| 64 |
) ; end s16vector/selectors |
| 65 |
|
| 66 |
(test-group "s16vector/iteration" |
| 67 |
(test-equiv "take" '(1 2) (s16vector-take s5 2)) |
| 68 |
(test-equiv "take-right" '(4 5) (s16vector-take-right s5 2)) |
| 69 |
(test-equiv "drop" '(3 4 5) (s16vector-drop s5 2)) |
| 70 |
(test-equiv "drop-right" '(1 2 3) (s16vector-drop-right s5 2)) |
| 71 |
(test "segment" (list (s16vector 1 2 3) (s16vector 4 5)) |
| 72 |
(s16vector-segment s5 3)) |
| 73 |
(test "fold" -6 (s16vector-fold - 0 (s16vector 1 2 3))) |
| 74 |
(test "fold" '(((0 1 4) 2 5) 3 6) |
| 75 |
(s16vector-fold list 0 (s16vector 1 2 3) (s16vector 4 5 6))) |
| 76 |
(test "fold-right" 2 (s16vector-fold-right - 0 (s16vector 1 2 3))) |
| 77 |
(test "fold-right" '(((0 3 6) 2 5) 1 4) |
| 78 |
(s16vector-fold-right list 0 (s16vector 1 2 3) (s16vector 4 5 6))) |
| 79 |
(test-equiv "map" '(-1 -2 -3 -4 -5) (s16vector-map - s5)) |
| 80 |
(test-equiv "map" '(-2 -4 -6 -8 -10) (s16vector-map - s5 s5 s5 s5)) |
| 81 |
(let ((v (s16vector 1 2 3 4 5))) |
| 82 |
(s16vector-map! - v) |
| 83 |
(test-equiv "map!" '(-1 -2 -3 -4 -5) v)) |
| 84 |
(let ((v (s16vector 1 2 3 4 5)) |
| 85 |
(v2 (s16vector 6 7 8 9 10))) |
| 86 |
(s16vector-map! + v v2) |
| 87 |
(test-equiv "map!" '(7 9 11 13 15) v)) |
| 88 |
(let ((list '())) |
| 89 |
(s16vector-for-each |
| 90 |
(lambda (e) (set! list (cons e list))) |
| 91 |
s5) |
| 92 |
;; stupid hack to shut up test egg about testing the value of a variable |
| 93 |
(test "for-each" '(5 4 3 2 1) (cons (car list) (cdr list)))) |
| 94 |
(let ((list '())) |
| 95 |
(s16vector-for-each |
| 96 |
(lambda (e1 e2) (set! list (cons (cons e1 e2) list))) |
| 97 |
s5 |
| 98 |
(s16vector 6 7 8 9 10)) |
| 99 |
;; stupid hack to shut up test egg about testing the value of a variable |
| 100 |
(test "for-each" '((5 . 10) (4 . 9) (3 . 8) (2 . 7) (1 . 6)) |
| 101 |
(cons (car list) (cdr list)))) |
| 102 |
(test "count" 3 (s16vector-count odd? s5)) |
| 103 |
(test "count" 2 (s16vector-count > s5 (s16vector 9 2 1 5 3))) |
| 104 |
(test-equiv "cumulate" '(1 3 6 10 15) |
| 105 |
(s16vector-cumulate + 0 s5)) |
| 106 |
) ; end s16vector/iteration |
| 107 |
|
| 108 |
(test-group "s16vector/searching" |
| 109 |
(test-equiv "take-while" '(1) (s16vector-take-while odd? s5)) |
| 110 |
(test-equiv "take-while-right" '(5) (s16vector-take-while-right odd? s5)) |
| 111 |
(test-equiv "drop-while" '(2 3 4 5) (s16vector-drop-while odd? s5)) |
| 112 |
(test-equiv "drop-while-right" '(1 2 3 4) (s16vector-drop-while-right odd? s5)) |
| 113 |
(test-equiv "degenerate take-while" '() (s16vector-take-while inexact? s5)) |
| 114 |
(test-equiv "degenerate take-while-right" '() (s16vector-take-while-right inexact? s5)) |
| 115 |
(test-equiv "degenerate drop-while" '(1 2 3 4 5) (s16vector-drop-while inexact? s5)) |
| 116 |
(test-equiv "degenerate drop-while-right" '(1 2 3 4 5) (s16vector-drop-while-right inexact? s5)) |
| 117 |
(test "index" 1 (s16vector-index even? s5)) |
| 118 |
(test "index" 2 (s16vector-index < s5 (s16vector 0 0 10 10 0))) |
| 119 |
(test "index-right" 3 (s16vector-index-right even? s5)) |
| 120 |
(test "index-right" 3 (s16vector-index-right < s5 (s16vector 0 0 10 10 0))) |
| 121 |
(test "skip" 1 (s16vector-skip odd? s5)) |
| 122 |
(test "skip" 2 (s16vector-skip > s5 (s16vector 0 0 10 10 0))) |
| 123 |
(test "skip-right" 3 (s16vector-skip-right odd? s5)) |
| 124 |
(test "skip-right" 3 (s16vector-skip-right > s5 (s16vector 0 0 10 10 0))) |
| 125 |
(test "any" 4 (s16vector-any (lambda (x) (and (even? x) (* x 2))) s5)) |
| 126 |
(test-assert "not any" (not (s16vector-any inexact? s5))) |
| 127 |
(test "any + 1" 2 (s16vector-any odd+1 s5)) |
| 128 |
(test-assert "every" (s16vector-every exact? s5)) |
| 129 |
(test-assert "not every" (not (s16vector-every odd? s5))) |
| 130 |
(test-assert "every + 1" (not (s16vector-every odd+1 s5))) |
| 131 |
(test "multi-any" 10 (s16vector-any (lambda (x y) (and (even? x) (even? y) (+ x y))) |
| 132 |
s5 (s16vector 0 1 2 6 4))) |
| 133 |
(test "multi-any 2" #f (s16vector-any (lambda (x y) (and (even? x) (even? y) (+ x y))) |
| 134 |
s5 (s16vector 0 1 2 5 4))) |
| 135 |
(test "multi-every" 10 (s16vector-every (lambda (x) (and (exact? x) (* x 2))) s5)) |
| 136 |
(test "multi-every-2" 10 (s16vector-every (lambda (x y) (and (exact? x) (exact? y) (+ x y))) |
| 137 |
s5 s5)) |
| 138 |
(test-assert "multi-not every" (not (s16vector-every < s5 (s16vector 10 10 10 10 0)))) |
| 139 |
(test-equiv "partition" '(1 3 5 2 4) (s16vector-partition odd? s5)) |
| 140 |
(test-equiv "filter" '(1 3 5) (s16vector-filter odd? s5)) |
| 141 |
(test-equiv "remove" '(2 4) (s16vector-remove odd? s5)) |
| 142 |
) ; end s16vector/searching |
| 143 |
|
| 144 |
(test-group "s16vector/mutators" |
| 145 |
(let ((v (s16vector 1 2 3))) |
| 146 |
(display "set!\n") |
| 147 |
(s16vector-set! v 0 10) |
| 148 |
(test-equiv "set!" '(10 2 3) v)) |
| 149 |
(let ((v (s16vector 1 2 3))) |
| 150 |
(display "swap!\n") |
| 151 |
(s16vector-swap! v 0 1) |
| 152 |
(test-equiv "swap!" '(2 1 3) v)) |
| 153 |
(let ((v (s16vector 1 2 3))) |
| 154 |
(display "fill!\n") |
| 155 |
(s16vector-fill! v 2) |
| 156 |
(test-equiv "fill!" '(2 2 2) v)) |
| 157 |
(let ((v (s16vector 1 2 3))) |
| 158 |
(display "fill2!\n") |
| 159 |
(s16vector-fill! v 10 0 2) |
| 160 |
(test-equiv "fill2!" '(10 10 3) v)) |
| 161 |
(let ((v (s16vector 1 2 3))) |
| 162 |
(display "reverse!\n") |
| 163 |
(s16vector-reverse! v) |
| 164 |
(test-equiv "reverse!" '(3 2 1) v)) |
| 165 |
(let ((v (s16vector 10 20 30 40 50))) |
| 166 |
(display "copy!\n") |
| 167 |
(s16vector-copy! v 1 s5 2 4) |
| 168 |
(test-equiv "copy!" '(10 3 4 40 50) v)) |
| 169 |
(let ((v (s16vector 10 20 30 40 50))) |
| 170 |
(display "reverse-copy!\n") |
| 171 |
(s16vector-reverse-copy! v 1 s5 2 4) |
| 172 |
(test-equiv "reverse-copy!" '(10 4 3 40 50) v)) |
| 173 |
(let ((v (s16vector 1 2 3 4 5 6 7 8))) |
| 174 |
(display "unfold!") |
| 175 |
(s16vector-unfold! (lambda (_ x) (values (* x 2) (* x 2))) |
| 176 |
v 1 6 -1) |
| 177 |
(test-equiv "vector-unfold!" '(1 -2 -4 -8 -16 -32 7 8) v)) |
| 178 |
(let ((v (s16vector 1 2 3 4 5 6 7 8))) |
| 179 |
(display "unfold-right!") |
| 180 |
(s16vector-unfold-right! (lambda (_ x) (values (* x 2) (* x 2))) |
| 181 |
v 1 6 -1) |
| 182 |
(test-equiv "vector-unfold!" '(1 -32 -16 -8 -4 -2 7 8) v)) |
| 183 |
) ; end s16vector/mutators |
| 184 |
|
| 185 |
(test-group "s16vector/conversion" |
| 186 |
(test "@vector->list 1" '(1 2 3 4 5) |
| 187 |
(s16vector->list s5)) |
| 188 |
(test "@vector->list 2" '(2 3 4 5) |
| 189 |
(s16vector->list s5 1)) |
| 190 |
(test "@vector->list 3" '(2 3 4) |
| 191 |
(s16vector->list s5 1 4)) |
| 192 |
(test "@vector->vector 1" #(1 2 3 4 5) |
| 193 |
(s16vector->vector s5)) |
| 194 |
(test "@vector->vector 2" #(2 3 4 5) |
| 195 |
(s16vector->vector s5 1)) |
| 196 |
(test "@vector->vector 3" #(2 3 4) |
| 197 |
(s16vector->vector s5 1 4)) |
| 198 |
(test-equiv "list->@vector" '(1 2 3 4 5) |
| 199 |
(list->s16vector '(1 2 3 4 5))) |
| 200 |
(test-equiv "reverse-list->@vector" '(5 4 3 2 1) |
| 201 |
(reverse-list->s16vector '(1 2 3 4 5))) |
| 202 |
(test-equiv "vector->@vector 1" '(1 2 3 4 5) |
| 203 |
(vector->s16vector #(1 2 3 4 5))) |
| 204 |
(test-equiv "vector->@vector 2" '(2 3 4 5) |
| 205 |
(vector->s16vector #(1 2 3 4 5) 1)) |
| 206 |
(test-equiv "vector->@vector 3" '(2 3 4) |
| 207 |
(vector->s16vector #(1 2 3 4 5) 1 4)) |
| 208 |
) ; end s16vector/conversion |
| 209 |
|
| 210 |
(test-group "s16vector/misc" |
| 211 |
(let ((port (open-output-string))) |
| 212 |
(write-s16vector s5 port) |
| 213 |
(test "write-@vector" "#s16(1 2 3 4 5)" (get-output-string port)) |
| 214 |
(close-output-port port)) |
| 215 |
|
| 216 |
(test-assert "@vector< short" (s16vector< s4 s5)) |
| 217 |
(test-assert "not @vector< short" (not (s16vector< s5 s4))) |
| 218 |
(test-assert "@vector< samelen" (s16vector< s5 s5+)) |
| 219 |
(test-assert "not @vector< samelen" (not (s16vector< s5+ s5+))) |
| 220 |
(test-assert "@vector=" (s16vector= s5+ s5+)) |
| 221 |
;(test "@vector-hash" 15 (s16vector-hash s5)) |
| 222 |
|
| 223 |
(test "@vector-gen 0" 1 (g)) |
| 224 |
(test "@vector-gen 1" 2 (g)) |
| 225 |
(test "@vector-gen 2" 3 (g)) |
| 226 |
(test "@vector-gen 3" 4 (g)) |
| 227 |
(test "@vector-gen 4" 5 (g)) |
| 228 |
(test-assert (eof-object? (g))) |
| 229 |
) ; end s16vector/misc |
| 230 |
|
| 231 |
) ; end s16vector |
| 232 |
(test-exit) |