Debian Codegraph @

test-suite/tests/vectors.test

raw
1 ;;;; vectors.test --- test suite for Guile's vector functions -*- scheme -*-
2 ;;;;
3 ;;;; Copyright (C) 2003, 2006, 2010, 2011 Free Software Foundation, Inc.
4 ;;;;
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.
9 ;;;;
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.
14 ;;;;
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
18
19 (define-module (test-suite vectors)
20 :use-module (test-suite lib))
21
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"))
26
27
28 (with-test-prefix "vector-set!"
29
30 (expect-fail-exception "vector constant"
31 exception:immutable-vector
32 (vector-set! '#(1 2 3) 0 4)))
33
34 (with-test-prefix "vector-copy"
35
36 (pass-if "defaults"
37 (equal? #(1 2 3) (vector-copy #(1 2 3))))
38 (pass-if "default end"
39 (equal? #(2 3) (vector-copy #(1 2 3) 1)))
40 (pass-if "start end"
41 (equal? #(2) (vector-copy #(1 2 3) 1 2))))
42
43 (with-test-prefix "vector->list"
44
45 (pass-if "plain vector"
46 (equal? '(1 2 3) (vector->list #(1 2 3))))
47
48 (pass-if "string vector 1"
49 (equal? '("abc" "def" "ghi") (vector->list #("abc" "def" "ghi"))))
50
51 (pass-if "string-vector 2"
52 (equal? '("abc\u0100" "def\u0101" "ghi\u0102")
53 (vector->list #("abc\u0100" "def\u0101" "ghi\u0102"))))
54
55 (pass-if "shared array"
56 (let ((b (make-shared-array #(1) (lambda (x) '(0)) 2)))
57 (equal? b (list->vector (vector->list b))))))
58
59 (with-test-prefix "make-vector"
60
61 (pass-if "null"
62 (equal? #() (make-vector 0)))
63
64 (pass-if "fill with num"
65 (equal? #(1 1 1) (make-vector 3 1)))
66
67 (pass-if "fill with string"
68 (equal? #("abc" "abc" "abc") (make-vector 3 "abc")))
69
70 (pass-if "fill with string 2"
71 (equal? #("ab\u0100" "ab\u0100" "ab\u0100")
72 (make-vector 3 "ab\u0100"))))
73
74 (with-test-prefix "vector-move-left!"
75
76 (pass-if-exception "before start" exception:out-of-range
77 (let ((a (vector 1 2 3 4 5 6 7 8 9))
78 (b (vector 10 20 30 40 50 60 70 80 90)))
79 (vector-move-left! a 3 5 b -1)))
80
81 (pass-if "beginning"
82 (let ((a (vector 1 2 3 4 5 6 7 8 9))
83 (b (vector 10 20 30 40 50 60 70 80 90)))
84 (vector-move-left! a 3 5 b 0)
85 (equal? b #(4 5 30 40 50 60 70 80 90))))
86
87 (pass-if "middle"
88 (let ((a (vector 1 2 3 4 5 6 7 8 9))
89 (b (vector 10 20 30 40 50 60 70 80 90)))
90 (vector-move-left! a 3 5 b 2)
91 (equal? b #(10 20 4 5 50 60 70 80 90))))
92
93 (pass-if "overlap -"
94 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
95 (vector-move-left! a 3 5 a 2)
96 (equal? a #(1 2 4 5 5 6 7 8 9))))
97
98 (pass-if "overlap +"
99 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
100 (vector-move-left! a 3 5 a 4)
101 (equal? a #(1 2 3 4 4 4 7 8 9))))
102
103 (pass-if "end"
104 (let ((a (vector 1 2 3 4 5 6 7 8 9))
105 (b (vector 10 20 30 40 50 60 70 80 90)))
106 (vector-move-left! a 3 5 b 7)
107 (equal? b #(10 20 30 40 50 60 70 4 5))))
108
109 (pass-if "whole thing"
110 (let ((a (vector 1 2 3 4 5 6 7 8 9))
111 (b (vector 10 20 30 40 50 60 70 80 90)))
112 (vector-move-left! a 0 9 b 0)
113 (equal? b #(1 2 3 4 5 6 7 8 9))))
114
115 (pass-if-exception "past end" exception:out-of-range
116 (let ((a (vector 1 2 3 4 5 6 7 8 9))
117 (b (vector 10 20 30 40 50 60 70 80 90)))
118 (vector-move-left! a 3 5 b 8))))
119
120 (with-test-prefix "vector-move-right!"
121
122 (pass-if-exception "before start" exception:out-of-range
123 (let ((a (vector 1 2 3 4 5 6 7 8 9))
124 (b (vector 10 20 30 40 50 60 70 80 90)))
125 (vector-move-right! a 3 5 b -1)))
126
127 (pass-if "beginning"
128 (let ((a (vector 1 2 3 4 5 6 7 8 9))
129 (b (vector 10 20 30 40 50 60 70 80 90)))
130 (vector-move-right! a 3 5 b 0)
131 (equal? b #(4 5 30 40 50 60 70 80 90))))
132
133 (pass-if "middle"
134 (let ((a (vector 1 2 3 4 5 6 7 8 9))
135 (b (vector 10 20 30 40 50 60 70 80 90)))
136 (vector-move-right! a 3 5 b 2)
137 (equal? b #(10 20 4 5 50 60 70 80 90))))
138
139 (pass-if "overlap -"
140 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
141 (vector-move-right! a 3 5 a 2)
142 (equal? a #(1 2 5 5 5 6 7 8 9))))
143
144 (pass-if "overlap +"
145 (let ((a (vector 1 2 3 4 5 6 7 8 9)))
146 (vector-move-right! a 3 5 a 4)
147 (equal? a #(1 2 3 4 4 5 7 8 9))))
148
149 (pass-if "end"
150 (let ((a (vector 1 2 3 4 5 6 7 8 9))
151 (b (vector 10 20 30 40 50 60 70 80 90)))
152 (vector-move-right! a 3 5 b 7)
153 (equal? b #(10 20 30 40 50 60 70 4 5))))
154
155 (pass-if "whole thing"
156 (let ((a (vector 1 2 3 4 5 6 7 8 9))
157 (b (vector 10 20 30 40 50 60 70 80 90)))
158 (vector-move-right! a 0 9 b 0)
159 (equal? b #(1 2 3 4 5 6 7 8 9))))
160
161 (pass-if-exception "past end" exception:out-of-range
162 (let ((a (vector 1 2 3 4 5 6 7 8 9))
163 (b (vector 10 20 30 40 50 60 70 80 90)))
164 (vector-move-right! a 3 5 b 8))))