1 ;;;; vectors.test --- test suite for Guile's vector functions -*- scheme -*-
3 ;;;; Copyright (C) 2003, 2006, 2010, 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-suite vectors)
20 :use-module (test-suite lib))
22 ;; FIXME: As soon as guile supports immutable vectors, this has to be
23 ;; replaced with the appropriate error type and message.
24 (define exception:immutable-vector
25 (cons 'some-error-type "^trying to modify an immutable vector"))
28 (with-test-prefix "vector-set!"
30 (expect-fail-exception "vector constant"
31 exception:immutable-vector
32 (vector-set! '#(1 2 3) 0 4)))
34 (with-test-prefix "vector->list"
36 (pass-if "simple vector"
37 (equal? '(1 2 3) (vector->list #(1 2 3))))
39 (pass-if "string vector 1"
40 (equal? '("abc" "def" "ghi") (vector->list #("abc" "def" "ghi"))))
42 (pass-if "string-vector 2"
43 (equal? '("abc\u0100" "def\u0101" "ghi\u0102")
44 (vector->list #("abc\u0100" "def\u0101" "ghi\u0102"))))
46 (pass-if "shared array"
47 (let ((b (make-shared-array #(1) (lambda (x) '(0)) 2)))
48 (equal? b (list->vector (vector->list b))))))
50 (with-test-prefix "make-vector"
53 (equal? #() (make-vector 0)))
55 (pass-if "fill with num"
56 (equal? #(1 1 1) (make-vector 3 1)))
58 (pass-if "fill with string"
59 (equal? #("abc" "abc" "abc") (make-vector 3 "abc")))
61 (pass-if "fill with string 2"
62 (equal? #("ab\u0100" "ab\u0100" "ab\u0100")
63 (make-vector 3 "ab\u0100"))))
65 (with-test-prefix "vector-move-left!"
67 (pass-if-exception "before start" exception:out-of-range
68 (let ((a (vector 1 2 3 4 5 6 7 8 9))
69 (b (vector 10 20 30 40 50 60 70 80 90)))
70 (vector-move-left! a 3 5 b -1)))
73 (let ((a (vector 1 2 3 4 5 6 7 8 9))
74 (b (vector 10 20 30 40 50 60 70 80 90)))
75 (vector-move-left! a 3 5 b 0)
76 (equal? b #(4 5 30 40 50 60 70 80 90))))
79 (let ((a (vector 1 2 3 4 5 6 7 8 9))
80 (b (vector 10 20 30 40 50 60 70 80 90)))
81 (vector-move-left! a 3 5 b 2)
82 (equal? b #(10 20 4 5 50 60 70 80 90))))
85 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
86 (vector-move-left! a 3 5 a 2)
87 (equal? a #(1 2 4 5 5 6 7 8 9))))
90 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
91 (vector-move-left! a 3 5 a 4)
92 (equal? a #(1 2 3 4 4 4 7 8 9))))
95 (let ((a (vector 1 2 3 4 5 6 7 8 9))
96 (b (vector 10 20 30 40 50 60 70 80 90)))
97 (vector-move-left! a 3 5 b 7)
98 (equal? b #(10 20 30 40 50 60 70 4 5))))
100 (pass-if "whole thing"
101 (let ((a (vector 1 2 3 4 5 6 7 8 9))
102 (b (vector 10 20 30 40 50 60 70 80 90)))
103 (vector-move-left! a 0 9 b 0)
104 (equal? b #(1 2 3 4 5 6 7 8 9))))
106 (pass-if-exception "past end" exception:out-of-range
107 (let ((a (vector 1 2 3 4 5 6 7 8 9))
108 (b (vector 10 20 30 40 50 60 70 80 90)))
109 (vector-move-left! a 3 5 b 8))))
111 (with-test-prefix "vector-move-right!"
113 (pass-if-exception "before start" exception:out-of-range
114 (let ((a (vector 1 2 3 4 5 6 7 8 9))
115 (b (vector 10 20 30 40 50 60 70 80 90)))
116 (vector-move-right! a 3 5 b -1)))
119 (let ((a (vector 1 2 3 4 5 6 7 8 9))
120 (b (vector 10 20 30 40 50 60 70 80 90)))
121 (vector-move-right! a 3 5 b 0)
122 (equal? b #(4 5 30 40 50 60 70 80 90))))
125 (let ((a (vector 1 2 3 4 5 6 7 8 9))
126 (b (vector 10 20 30 40 50 60 70 80 90)))
127 (vector-move-right! a 3 5 b 2)
128 (equal? b #(10 20 4 5 50 60 70 80 90))))
131 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
132 (vector-move-right! a 3 5 a 2)
133 (equal? a #(1 2 5 5 5 6 7 8 9))))
136 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
137 (vector-move-right! a 3 5 a 4)
138 (equal? a #(1 2 3 4 4 5 7 8 9))))
141 (let ((a (vector 1 2 3 4 5 6 7 8 9))
142 (b (vector 10 20 30 40 50 60 70 80 90)))
143 (vector-move-right! a 3 5 b 7)
144 (equal? b #(10 20 30 40 50 60 70 4 5))))
146 (pass-if "whole thing"
147 (let ((a (vector 1 2 3 4 5 6 7 8 9))
148 (b (vector 10 20 30 40 50 60 70 80 90)))
149 (vector-move-right! a 0 9 b 0)
150 (equal? b #(1 2 3 4 5 6 7 8 9))))
152 (pass-if-exception "past end" exception:out-of-range
153 (let ((a (vector 1 2 3 4 5 6 7 8 9))
154 (b (vector 10 20 30 40 50 60 70 80 90)))
155 (vector-move-right! a 3 5 b 8))))