| 1 |
;; NB: This is taken from srfi-133 reference implementation. |
| 2 |
;; https://github.com/scheme-requests-for-implementation/srfi-133 |
| 3 |
(cond-expand |
| 4 |
(chicken (use test vectors)) |
| 5 |
(chibi (import (scheme base) (chibi test) (vectors))) |
| 6 |
(else)) |
| 7 |
(test-group "vectors" |
| 8 |
(test-group "vectors/basics" |
| 9 |
(define v (make-vector 3 3)) |
| 10 |
(test-assert (vector? #(1 2 3))) |
| 11 |
(test-assert (vector? (make-vector 10))) |
| 12 |
(test 3 (vector-ref v 0)) |
| 13 |
(test 3 (vector-ref v 1)) |
| 14 |
(test 3 (vector-ref v 2)) |
| 15 |
(test-error (vector-ref v -1)) |
| 16 |
(test-error (vector-ref v 3)) |
| 17 |
(vector-set! v 0 -32) |
| 18 |
(test -32 (vector-ref v 0)) |
| 19 |
(test 3 (vector-length v)) |
| 20 |
(test 0 (vector-length '#())) |
| 21 |
) ; end vectors/basics |
| 22 |
|
| 23 |
(test-group "vectors/constructors" |
| 24 |
(test '#(0 1 2 3 4) (vector 0 1 2 3 4)) |
| 25 |
(test '#(0 -1 -2 -3 -4 -5 -6 -7 -8 -9) |
| 26 |
(vector-unfold (lambda (i x) (values x (- x 1))) 10 0)) |
| 27 |
(test '#(0 1 2 3 4 5 6) (vector-unfold values 7)) |
| 28 |
(test '#((0 . 4) (1 . 3) (2 . 2) (3 . 1) (4 . 0)) |
| 29 |
(vector-unfold-right (lambda (i x) (values (cons i x) (+ x 1))) 5 0)) |
| 30 |
(define a2i '#(a b c d e f g h i)) |
| 31 |
(test a2i (vector-copy a2i)) |
| 32 |
(test-assert (not (eqv? a2i (vector-copy a2i)))) |
| 33 |
(test '#(g h i) (vector-copy a2i 6)) |
| 34 |
(test '#(d e f) (vector-copy a2i 3 6)) |
| 35 |
(test '#(1 2 3 4) (vector-reverse-copy '#(5 4 3 2 1 0) 1 5)) |
| 36 |
(test '#(x y) (vector-append '#(x) '#(y))) |
| 37 |
(test '#(a b c d) (vector-append '#(a) '#(b c d))) |
| 38 |
(test '#(a #(b) #(c)) (vector-append '#(a #(b)) '#(#(c)))) |
| 39 |
(test '#(a b c d) (vector-concatenate '(#(a b) #(c d)))) |
| 40 |
(test '#(a b h i) (vector-append-subvectors '#(a b c d e) 0 2 '#(f g h i j) 2 4)) |
| 41 |
) ; end vectors/constructors |
| 42 |
|
| 43 |
(test-group "vectors/predicates" |
| 44 |
(test #f (vector-empty? '#(a))) |
| 45 |
(test #f (vector-empty? '#(()))) |
| 46 |
(test #f (vector-empty? '#(#()))) |
| 47 |
(test-assert (vector-empty? '#())) |
| 48 |
(test-assert (vector= eq? '#(a b c d) '#(a b c d))) |
| 49 |
(test #f (vector= eq? '#(a b c d) '#(a b d c))) |
| 50 |
(test #f (vector= = '#(1 2 3 4 5) '#(1 2 3 4))) |
| 51 |
(test-assert (vector= eq?)) |
| 52 |
(test-assert (vector= eq? '#(a))) |
| 53 |
(test #f (vector= eq? (vector (vector 'a)) (vector (vector 'a)))) |
| 54 |
(test-assert (vector= equal? (vector (vector 'a)) (vector (vector 'a)))) |
| 55 |
) ; end vectors/predicates |
| 56 |
|
| 57 |
(test-group "vectors/iteration" |
| 58 |
(define vos '#("abc" "abcde" "abcd")) |
| 59 |
(define vec '#(0 1 2 3 4 5)) |
| 60 |
(define vec2 (vector 0 1 2 3 4)) |
| 61 |
(define vec3 (vector 1 2 3 4 5)) |
| 62 |
(define result '()) |
| 63 |
(define (sqr x) (* x x)) |
| 64 |
(test 5 (vector-fold (lambda (len str) (max (string-length str) len)) |
| 65 |
0 vos)) |
| 66 |
(test '(5 4 3 2 1 0) |
| 67 |
(vector-fold (lambda (tail elt) (cons elt tail)) '() vec)) |
| 68 |
(test 3 (vector-fold (lambda (ctr n) (if (even? n) (+ ctr 1) ctr)) 0 vec)) |
| 69 |
(test '(a b c d) (vector-fold-right (lambda (tail elt) (cons elt tail)) |
| 70 |
'() '#(a b c d))) |
| 71 |
(test '#(1 4 9 16) (vector-map sqr '#(1 2 3 4))) |
| 72 |
(test '#(5 8 9 8 5) (vector-map * '#(1 2 3 4 5) '#(5 4 3 2 1))) |
| 73 |
(vector-map! sqr vec2) |
| 74 |
(test '#(0 1 4 9 16) (vector-copy vec2)) |
| 75 |
(vector-map! * vec2 vec3) |
| 76 |
(test '#(0 2 12 36 80) (vector-copy vec2)) |
| 77 |
(vector-for-each (lambda (x) (set! result (cons x result))) vec) |
| 78 |
(test '(5 4 3 2 1 0) (cons (car result) (cdr result))) |
| 79 |
(test 3 (vector-count even? '#(3 1 4 1 5 9 2 5 6))) |
| 80 |
(test 2 (vector-count < '#(1 3 6 9) '#(2 4 6 8 10 12))) |
| 81 |
(test '#(3 4 8 9 14 23 25 30 36) (vector-cumulate + 0 '#(3 1 4 1 5 9 2 5 6))) |
| 82 |
) ; end vectors/iteration |
| 83 |
|
| 84 |
(test-group "vectors/searching" |
| 85 |
(define (cmp a b) |
| 86 |
(cond |
| 87 |
((< a b) -1) |
| 88 |
((= a b) 0) |
| 89 |
(else 1))) |
| 90 |
(define v '#(0 2 4 6 8 10 12)) |
| 91 |
(test 2 (vector-index even? '#(3 1 4 1 5 9 6))) |
| 92 |
(test 1 (vector-index < '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))) |
| 93 |
(test #f (vector-index = '#(3 1 4 1 5 9 2 5 6) '#(2 7 1 8 2))) |
| 94 |
(test 5 (vector-index-right odd? '#(3 1 4 1 5 9 6))) |
| 95 |
(test 3 (vector-index-right < '#(3 1 4 1 5) '#(2 7 1 8 2))) |
| 96 |
(test 2 (vector-skip number? '#(1 2 a b 3 4 c d))) |
| 97 |
(test 2 (vector-skip = '#(1 2 3 4 5) '#(1 2 -3 4))) |
| 98 |
(test 7 (vector-skip-right number? '#(1 2 a b 3 4 c d))) |
| 99 |
(test 3 (vector-skip-right = '#(1 2 3 4 5) '#(1 2 -3 -4 5))) |
| 100 |
(test 0 (vector-binary-search v 0 cmp)) |
| 101 |
(test 3 (vector-binary-search v 6 cmp)) |
| 102 |
(test #f (vector-binary-search v 1 cmp)) |
| 103 |
(test-assert (vector-any number? '#(1 2 x y z))) |
| 104 |
(test-assert (vector-any < '#(1 2 3 4 5) '#(2 1 3 4 5))) |
| 105 |
(test #f (vector-any number? '#(a b c d e))) |
| 106 |
(test #f (vector-any > '#(1 2 3 4 5) '#(1 2 3 4 5))) |
| 107 |
(test #f (vector-every number? '#(1 2 x y z))) |
| 108 |
(test-assert (vector-every number? '#(1 2 3 4 5))) |
| 109 |
(test #f (vector-every < '#(1 2 3) '#(2 3 3))) |
| 110 |
(test-assert (vector-every < '#(1 2 3) '#(2 3 4))) |
| 111 |
(test 'yes (vector-any (lambda (x) (if (number? x) 'yes #f)) '#(1 2 x y z))) |
| 112 |
(let-values (((new off) (vector-partition number? '#(1 x 2 y 3 z)))) |
| 113 |
(test '#(1 2 3 x y z) (vector-copy new)) |
| 114 |
(test 3 (+ off 0))) |
| 115 |
) ; end vectors-searching |
| 116 |
|
| 117 |
(test-group "vectors/mutation" |
| 118 |
(define vs (vector 1 2 3)) |
| 119 |
(define vf0 (vector 1 2 3)) |
| 120 |
(define vf1 (vector 1 2 3)) |
| 121 |
(define vf2 (vector 1 2 3)) |
| 122 |
(define vr0 (vector 1 2 3)) |
| 123 |
(define vr1 (vector 1 2 3)) |
| 124 |
(define vr2 (vector 1 2 3)) |
| 125 |
(define vc0 (vector 1 2 3 4 5)) |
| 126 |
(define vc1 (vector 1 2 3 4 5)) |
| 127 |
(define vc2 (vector 1 2 3 4 5)) |
| 128 |
(define vrc0 (vector 1 2 3 4 5)) |
| 129 |
(define vrc1 (vector 1 2 3 4 5)) |
| 130 |
(define vrc2 (vector 1 2 3 4 5)) |
| 131 |
(define vu0 (vector 1 2 3 4 5)) |
| 132 |
(define vu1 (vector 1 2 3 4 5)) |
| 133 |
(define vu2 (vector 1 2 3 4 5)) |
| 134 |
(define vur0 (vector 1 2 3 4 5)) |
| 135 |
(define vur1 (vector 1 2 3 4 5)) |
| 136 |
(define vur2 (vector 1 2 3 4 5)) |
| 137 |
(vector-swap! vs 0 1) |
| 138 |
(test '#(2 1 3) (vector-copy vs)) |
| 139 |
(vector-fill! vf0 0) |
| 140 |
(test '#(0 0 0) (vector-copy vf0)) |
| 141 |
(vector-fill! vf1 0 1) |
| 142 |
(test '#(1 0 0) (vector-copy vf1)) |
| 143 |
(vector-fill! vf2 0 0 1) |
| 144 |
(test '#(0 2 3) (vector-copy vf2)) |
| 145 |
(vector-reverse! vr0) |
| 146 |
(test '#(3 2 1) (vector-copy vr0)) |
| 147 |
(vector-reverse! vr1 1) |
| 148 |
(test '#(1 3 2) (vector-copy vr1)) |
| 149 |
(vector-reverse! vr2 0 2) |
| 150 |
(test '#(2 1 3) (vector-copy vr2)) |
| 151 |
(vector-copy! vc0 1 '#(10 20 30)) |
| 152 |
(test '#(1 10 20 30 5) (vector-copy vc0)) |
| 153 |
(vector-copy! vc1 1 '#(0 10 20 30 40) 1) |
| 154 |
(test '#(1 10 20 30 40) (vector-copy vc1)) |
| 155 |
(vector-copy! vc2 1 '#(0 10 20 30 40) 1 4) |
| 156 |
(test '#(1 10 20 30 5) (vector-copy vc2)) |
| 157 |
(vector-reverse-copy! vrc0 1 '#(10 20 30)) |
| 158 |
(test '#(1 30 20 10 5) (vector-copy vrc0)) |
| 159 |
(vector-reverse-copy! vrc1 1 '#(0 10 20 30 40) 1) |
| 160 |
(test '#(1 40 30 20 10) (vector-copy vrc1)) |
| 161 |
(vector-reverse-copy! vrc2 1 '#(0 10 20 30 40) 1 4) |
| 162 |
(test '#(1 30 20 10 5) (vector-copy vrc2)) |
| 163 |
(vector-unfold! (lambda (i) (+ 10 i)) vu0 1 4) |
| 164 |
(test '#(1 11 12 13 5) (vector-copy vu0)) |
| 165 |
(vector-unfold! (lambda (i x) (values (+ i x) (+ x 1))) vu1 1 4 0) |
| 166 |
(test '#(1 1 3 5 5) (vector-copy vu1)) |
| 167 |
(vector-unfold! (lambda (i x y) (values (+ i x y) (+ x 1) (+ x 1))) vu2 1 4 0 0) |
| 168 |
(test '#(1 1 4 7 5) (vector-copy vu2)) |
| 169 |
(vector-unfold-right! (lambda (i) (+ 10 i)) vur0 1 4) |
| 170 |
(test '#(1 11 12 13 5) (vector-copy vur0)) |
| 171 |
(vector-unfold-right! (lambda (i x) (values (+ i x) (+ x 1))) vur1 1 4 0) |
| 172 |
(test '#(1 3 3 3 5) (vector-copy vur1)) |
| 173 |
(vector-unfold-right! (lambda (i x y) (values (+ i x y) (+ x 1) (+ x 1))) vur2 1 4 0 0) |
| 174 |
(test '#(1 5 4 3 5) (vector-copy vur2)) |
| 175 |
|
| 176 |
) ; end vectors/mutation |
| 177 |
|
| 178 |
(test-group "vectors/conversion" |
| 179 |
(test '(1 2 3) (vector->list '#(1 2 3))) |
| 180 |
(test '(2 3) (vector->list '#(1 2 3) 1)) |
| 181 |
(test '(1 2) (vector->list '#(1 2 3) 0 2)) |
| 182 |
(test '#(1 2 3) (list->vector '(1 2 3))) |
| 183 |
(test '(3 2 1) (reverse-vector->list '#(1 2 3))) |
| 184 |
(test '(3 2) (reverse-vector->list '#(1 2 3) 1)) |
| 185 |
(test '(2 1) (reverse-vector->list '#(1 2 3) 0 2)) |
| 186 |
(test '#(3 2 1) (reverse-list->vector '(1 2 3))) |
| 187 |
(test "abc" (vector->string '#(#\a #\b #\c))) |
| 188 |
(test "bc" (vector->string '#(#\a #\b #\c) 1)) |
| 189 |
(test "ab" (vector->string '#(#\a #\b #\c) 0 2)) |
| 190 |
(test '#(#\a #\b #\c) (string->vector "abc")) |
| 191 |
(test '#(#\b #\c) (string->vector "abc" 1)) |
| 192 |
(test '#(#\a #\b) (string->vector "abc" 0 2)) |
| 193 |
) ; end vectors/conversion |
| 194 |
) ; end vectors |
| 195 |
(test-exit) |