Debian Codegraph @

test/vector.scm

raw
1 ;;
2 ;; tests for vectors
3 ;;
4
5 (use gauche.test)
6
7 (test-start "vectors")
8
9 ;;-------------------------------------------------------------------
10 (test-section "builtins")
11
12 (test* "vector" '#(a b c d e f g) (vector 'a 'b 'c 'd 'e 'f 'g))
13 (test* "vector" '#() (vector))
14
15 (test* "list->vector" `(#(a b c d e f g) #(b c d e) #(c))
16 (let1 data '(a b c d e f g)
17 (map (^i (list->vector data i (- (length data) (* 2 i))))
18 (iota 3))))
19 (test* "list->vector" (test-error) (list->vector '(a) 3))
20 (test* "list->vector" (test-error) (list->vector '(a b c) 2 1))
21 (test* "list->vector" '#() (list->vector '(a b c) 2 2))
22 (test* "reverse-list->vector" `(#(a b c d e f g) #(c d e f) #(e))
23 (let1 data '(g f e d c b a)
24 (map (^i (reverse-list->vector data i (- (length data) (* 2 i))))
25 (iota 3))))
26 (test* "reverse-list->vector" (test-error) (reverse-list->vector '(a) 3))
27 (test* "reverse-list->vector" (test-error) (reverse-list->vector '(a b c) 2 1))
28 (test* "reverse-list->vector" '#() (reverse-list->vector '(a b c) 2 2))
29
30 (test* "vector-tabulate" '#(0 2 4 6 8)
31 (vector-tabulate 5 (cut * <> 2)))
32 (test* "vector-tabulate with restart"
33 '(#(0 1 yo 6 8) #(0 1 2 3 4))
34 (let ([v0 #f]
35 [v1 #f]
36 [cont #f])
37 (set! v0 (vector-tabulate 5 (^i (cond [v0 (* i 2)]
38 [(= i 2) (let/cc c
39 (set! cont c)
40 i)]
41 [else i]))))
42 (if v1
43 (list v0 v1)
44 (begin (set! v1 v0) (cont 'yo)))))
45
46 (test* "vector-copy 0" '#(a b c d e f g h i)
47 (vector-copy '#(a b c d e f g h i)))
48 (test* "vector-copy 1" '#(g h i)
49 (vector-copy '#(a b c d e f g h i) 6))
50 (test* "vector-copy 2" '#(d e f)
51 (vector-copy '#(a b c d e f g h i) 3 6))
52 (test* "vector-copy 3" '#(g h i x x x)
53 (vector-copy '#(a b c d e f g h i) 6 12 'x))
54 (test* "vector-copy 4" '#()
55 (vector-copy '#(a b c d e f g h i) 6 6))
56 (test* "vector-copy 5" (test-error)
57 (vector-copy '#(a b c d e f g h i) 4 2))
58
59 (test* "vector-copy! 0" '#(1 2 3 d e)
60 (rlet1 v (vector 'a 'b 'c 'd 'e)
61 (vector-copy! v 0 '#(1 2 3))))
62 (test* "vector-copy! 1" '#(a b 1 2 3)
63 (rlet1 v (vector 'a 'b 'c 'd 'e)
64 (vector-copy! v 2 '#(1 2 3))))
65 (test* "vector-copy! 2" '#(a b 2 3 e)
66 (rlet1 v (vector 'a 'b 'c 'd 'e)
67 (vector-copy! v 2 '#(1 2 3) 1)))
68 (test* "vector-copy! 3" '#(a b 3 4 5)
69 (rlet1 v (vector 'a 'b 'c 'd 'e)
70 (vector-copy! v 2 '#(1 2 3 4 5) 2 5)))
71 (test* "vector-copy! 4" '#(a b c d e)
72 (rlet1 v (vector 'a 'b 'c 'd 'e)
73 (vector-copy! v 2 '#(1 2 3) 1 1)))
74 (test* "vector-copy! self0" '#(b c c d e)
75 (rlet1 v (vector 'a 'b 'c 'd 'e)
76 (vector-copy! v 0 v 1 3)))
77 (test* "vector-copy! self1" '#(a b b c d)
78 (rlet1 v (vector 'a 'b 'c 'd 'e)
79 (vector-copy! v 2 v 1 4)))
80 (test* "vector-copy! self2" '#(a b c d e)
81 (rlet1 v (vector 'a 'b 'c 'd 'e)
82 (vector-copy! v 0 v 0)))
83 (test* "vector-copy! e0" (test-error) (vector-copy! (vector 1 2) 3 '#(1 2 3)))
84 (test* "vector-copy! e1" (test-error) (vector-copy! (vector 1 2) 0 '#(1 2 3)))
85 (test* "vector-copy! e2" (test-error) (vector-copy! (vector 1 2) 1 '#(1 2 3) 1))
86
87 (test* "vector-append 0" '#(x y)
88 (vector-append '#(x) '#(y)))
89 (test* "vector-append 1" '#(x y x y x y)
90 (let ((v '#(x y)))
91 (vector-append v v v)))
92 (test* "vector-append 2" '#(x y)
93 (vector-append '#(x) '#() '#(y)))
94 (test* "vector-append 3" '#()
95 (vector-append))
96 (test* "vector-append 4" (test-error) (vector-append '#() 'b 'c))
97
98 ;; immutable vectors
99 ;; literal vectors are immutable only when compiled with API_VERSION >= 1000
100 (when (vector-immutable? '#(1 2 3))
101 (test* "vector-immutable?" #f
102 (vector-immutable? (vector 1 2 3)))
103 (test* "set! to immutalbe vector" (test-error)
104 (vector-set! '#(1 2 3) 0 3))
105 (test* "set! to immutalbe vector" (test-error)
106 (set! (vector-ref '#(1 2 3) 0) 3))
107 (test* "fill! to immutalbe vector" (test-error)
108 (vector-fill!! '#(1 2 3) 0)))
109
110 ;;-------------------------------------------------------------------
111 (test-section "bitvector builtins")
112
113 ;; we have comprehensive tests in srfi-178-tests. here we cover
114 ;; non-srfi procedures.
115
116 (test* "bitvector-any-value? #t 1" #t (bitvector-any-value? #*0001000 1))
117 (test* "bitvector-any-value? #t 2" #t
118 (bitvector-any-value? #*000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000 #t))
119 (test* "bitvector-any-value? #t 3" #t
120 (bitvector-any-value? #*000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1))
121 (test* "bitvector-any-value? #t 4" #f
122 (bitvector-any-value? #*00000000000 1))
123 (test* "bitvector-any-value? #t 5" #f
124 (bitvector-any-value? #* 1))
125 (test* "bitvector-any-value? #t 6" #f
126 (bitvector-any-value? #*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 1))
127
128 (test* "bitvector-any-value? #f 1" #t (bitvector-any-value? #*1110111 0))
129 (test* "bitvector-any-value? #f 2" #f (bitvector-any-value? #*1111111 0))
130 (test* "bitvector-any-value? #f 3" #f (bitvector-any-value? #* 0))
131 (test* "bitvector-any-value? #f 4" #t
132 (bitvector-any-value? #*1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111011 0))
133 (test* "bitvector-any-value? #f 5" #f
134 (bitvector-any-value? #*1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 0))
135
136 (test* "bitvector-every-value? #t 1" #t (bitvector-every-value? #*11111 1))
137 (test* "bitvector-every-value? #t 2" #t (bitvector-every-value? #* #t))
138 (test* "bitvector-every-value? #t 3" #t
139 (bitvector-every-value? #*11111111111111111111111111111111111111111111111111111111111111111111111111111111111111 1))
140 (test* "bitvector-every-value? #t 4" #f (bitvector-every-value? #*11101 1))
141 (test* "bitvector-every-value? #t 5" #f
142 (bitvector-every-value? #*11111111111111111111111111111111111111111111111111111111111111111111111111111111111110 1))
143 (test* "bitvector-every-value? #t 6" #f
144 (bitvector-every-value? #*11111111101111111111111111111111111111111111111111111111111111111111111111111111111111 1))
145
146 (test* "bitvector-every-value? #f 1" #t (bitvector-every-value? #*00000 0))
147 (test* "bitvector-every-value? #f 2" #t (bitvector-every-value? #* #f))
148 (test* "bitvector-every-value? #f 3" #t
149 (bitvector-every-value? #*00000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0))
150 (test* "bitvector-every-value? #f 4" #f
151 (bitvector-every-value? #*00000000000000000000000000000000000000000000000000000000000000000000000000000000000010 #f))
152 (test* "bitvector-every-value? #f 5" #f
153 (bitvector-every-value? #*00000001000000000000000000000000000000000000000000000000000000000000000000000000000000 0))
154
155 (test-end)