(use vector-lib test) (test "make-vector" '#(3 3 3 3 3) (make-vector 5 3)) (test "vector" '#(0 1 2 3 4) (vector 0 1 2 3 4)) ;;; fixed; the original has #(0 -1 -2 -3 -4 -5 -6 -7 -8 -8) (test "vector-unfold" '#(0 -1 -2 -3 -4 -5 -6 -7 -8 -9) (vector-unfold (lambda (i x) (values x (- x 1))) 10 0)) (let ((copy-vector (lambda (vector) (vector-unfold (lambda (i) (vector-ref vector i)) (vector-length vector))))) (test "vector-unfold (copy-vector)" '#(1 2 3) (copy-vector '#(1 2 3)))) (let ((reverse-vector (lambda (vector) (vector-unfold-right (lambda (i x) (values (vector-ref vector x) (+ x 1))) (vector-length vector) 0)))) (test "vector-unfold-right (reverse-vector)" '#(3 2 1) (reverse-vector '#(1 2 3)))) (test "vector-copy" '#(a b c d e f g h i) (vector-copy '#(a b c d e f g h i))) (test "vector-copy with start" '#(g h i) (vector-copy '#(a b c d e f g h i) 6)) (test "vector-copy with start, end" '#(d e f) (vector-copy '#(a b c d e f g h i) 3 6)) (test "vector-copy with start, end, fill" '#(g h i x x x) (vector-copy '#(a b c d e f g h i) 6 12 'x)) (test "vector-reverse-copy" '#(1 2 3 4) (vector-reverse-copy '#(5 4 3 2 1 0) 1 5)) (test "vector-append" '#(a b c d) (vector-append '#(a) '#(b c d))) (test "vector-append with subvectors" '#(a #(b) #(c)) (vector-append '#(a #(b)) '#(#(c)))) (test "vector-concatenate" '#(a b c d) (vector-concatenate '(#(a b) #(c d)))) (test-assert "vector?" (vector? '#(a b c))) (test "vector? on list" #f (vector? '(a b c))) (test "vector? on boolean" #f (vector? #t)) (test-assert "vector? on null-vector" (vector? '#())) (test "vector? on null-list" #f (vector? '())) (test "vector-empty? on non-empty vector" #f (vector-empty? '#(a))) (test "vector-empty? on vector with sub-list" #f (vector-empty? '#(()))) (test "vector-empty? on vector with sub-vector" #f (vector-empty? '#(#()))) (test-assert "vector-empty? on empty vector" (vector-empty? '#())) (test-assert "vector= with eq?" (vector= eq? '#(a b c d) '#(a b c d))) (test "vector= with eq? on unequal vectors" #f (vector= eq? '#(a b c d) '#(a b d c))) (test "vector= with = on unequal vectors" #f (vector= = '#(1 2 3 4 5) '#(1 2 3 4))) (test-assert "vector= with =" (vector= = '#(1 2 3 4) '#(1 2 3 4))) (test-assert "vector= trivial medadic" (vector= eq?)) (test-assert "vector= trivial monadic" (vector= eq? '#(a))) (test "vector= with eq? and vector (unequal)" #f (vector= eq? (vector (vector 'a)) (vector (vector 'a)))) (test-assert "vector= with eq? and vector (equal?)" (vector= equal? (vector (vector 'a)) (vector (vector 'a)))) (test "vector-ref" 'c (vector-ref '#(a b c d) 2)) (test "vector-length" 3 (vector-length '#(a b c))) (let ((longest-string-length (lambda (vector-of-strings) (vector-fold (lambda (index len str) (max (string-length str) len)) 0 vector-of-strings)))) (test "vector-fold (longest-string-length)" 3 (longest-string-length '#("a" "aa" "aaa")))) (let ((vector->list (lambda (vector) (vector-fold-right (lambda (index tail elt) (cons elt tail)) '() vector)))) (test "vector-fold-right (vector->list)" '(a b c d) (vector->list '#(a b c d)))) (test "vector-map" '#(1 4 9 16) (vector-map (lambda (i x) (* x x)) (vector-unfold (lambda (i x) (values x (+ x 1))) 4 1))) (test "vector-for-each" "foo\nbar\nbaz\nquux\nzot\n" (with-output-to-string (lambda () (vector-for-each (lambda (i x) (display x) (newline)) '#("foo" "bar" "baz" "quux" "zot"))))) (test "vector-count" 3 (vector-count (lambda (i elt) (even? elt)) '#(3 1 4 1 5 9 2 5 6))) (unless (zero? (test-failure-count)) (exit 1))