OSDN Git Service

b825af912218e979e89a06115dc41d9fd1fd2008
[pf3gnuchains/gcc-fork.git] / libgo / go / container / vector / intvector_test.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // CAUTION: If this file is not vector_test.go, it was generated
6 // automatically from vector_test.go - DO NOT EDIT in that case!
7
8 package vector
9
10 import "testing"
11
12 func TestIntZeroLen(t *testing.T) {
13         a := new(IntVector)
14         if a.Len() != 0 {
15                 t.Errorf("%T: B1) expected 0, got %d", a, a.Len())
16         }
17         if len(*a) != 0 {
18                 t.Errorf("%T: B2) expected 0, got %d", a, len(*a))
19         }
20         var b IntVector
21         if b.Len() != 0 {
22                 t.Errorf("%T: B3) expected 0, got %d", b, b.Len())
23         }
24         if len(b) != 0 {
25                 t.Errorf("%T: B4) expected 0, got %d", b, len(b))
26         }
27 }
28
29 func TestIntResize(t *testing.T) {
30         var a IntVector
31         checkSize(t, &a, 0, 0)
32         checkSize(t, a.Resize(0, 5), 0, 5)
33         checkSize(t, a.Resize(1, 0), 1, 5)
34         checkSize(t, a.Resize(10, 0), 10, 10)
35         checkSize(t, a.Resize(5, 0), 5, 10)
36         checkSize(t, a.Resize(3, 8), 3, 10)
37         checkSize(t, a.Resize(0, 100), 0, 100)
38         checkSize(t, a.Resize(11, 100), 11, 100)
39 }
40
41 func TestIntResize2(t *testing.T) {
42         var a IntVector
43         checkSize(t, &a, 0, 0)
44         a.Push(int2IntValue(1))
45         a.Push(int2IntValue(2))
46         a.Push(int2IntValue(3))
47         a.Push(int2IntValue(4))
48         checkSize(t, &a, 4, 4)
49         checkSize(t, a.Resize(10, 0), 10, 10)
50         for i := 4; i < a.Len(); i++ {
51                 if a.At(i) != intzero {
52                         t.Errorf("%T: expected a.At(%d) == %v; found %v!", a, i, intzero, a.At(i))
53                 }
54         }
55         for i := 4; i < len(a); i++ {
56                 if a[i] != intzero {
57                         t.Errorf("%T: expected a[%d] == %v; found %v", a, i, intzero, a[i])
58                 }
59         }
60 }
61
62 func checkIntZero(t *testing.T, a *IntVector, i int) {
63         for j := 0; j < i; j++ {
64                 if a.At(j) == intzero {
65                         t.Errorf("%T: 1 expected a.At(%d) == %d; found %v", a, j, j, a.At(j))
66                 }
67                 if (*a)[j] == intzero {
68                         t.Errorf("%T: 2 expected (*a)[%d] == %d; found %v", a, j, j, (*a)[j])
69                 }
70         }
71         for ; i < a.Len(); i++ {
72                 if a.At(i) != intzero {
73                         t.Errorf("%T: 3 expected a.At(%d) == %v; found %v", a, i, intzero, a.At(i))
74                 }
75                 if (*a)[i] != intzero {
76                         t.Errorf("%T: 4 expected (*a)[%d] == %v; found %v", a, i, intzero, (*a)[i])
77                 }
78         }
79 }
80
81 func TestIntTrailingElements(t *testing.T) {
82         var a IntVector
83         for i := 0; i < 10; i++ {
84                 a.Push(int2IntValue(i + 1))
85         }
86         checkIntZero(t, &a, 10)
87         checkSize(t, &a, 10, 16)
88         checkSize(t, a.Resize(5, 0), 5, 16)
89         checkSize(t, a.Resize(10, 0), 10, 16)
90         checkIntZero(t, &a, 5)
91 }
92
93 func TestIntAccess(t *testing.T) {
94         const n = 100
95         var a IntVector
96         a.Resize(n, 0)
97         for i := 0; i < n; i++ {
98                 a.Set(i, int2IntValue(val(i)))
99         }
100         for i := 0; i < n; i++ {
101                 if elem2IntValue(a.At(i)) != int2IntValue(val(i)) {
102                         t.Error(i)
103                 }
104         }
105         var b IntVector
106         b.Resize(n, 0)
107         for i := 0; i < n; i++ {
108                 b[i] = int2IntValue(val(i))
109         }
110         for i := 0; i < n; i++ {
111                 if elem2IntValue(b[i]) != int2IntValue(val(i)) {
112                         t.Error(i)
113                 }
114         }
115 }
116
117 func TestIntInsertDeleteClear(t *testing.T) {
118         const n = 100
119         var a IntVector
120
121         for i := 0; i < n; i++ {
122                 if a.Len() != i {
123                         t.Errorf("%T: A) wrong Len() %d (expected %d)", a, a.Len(), i)
124                 }
125                 if len(a) != i {
126                         t.Errorf("%T: A) wrong len() %d (expected %d)", a, len(a), i)
127                 }
128                 a.Insert(0, int2IntValue(val(i)))
129                 if elem2IntValue(a.Last()) != int2IntValue(val(0)) {
130                         t.Errorf("%T: B", a)
131                 }
132         }
133         for i := n - 1; i >= 0; i-- {
134                 if elem2IntValue(a.Last()) != int2IntValue(val(0)) {
135                         t.Errorf("%T: C", a)
136                 }
137                 if elem2IntValue(a.At(0)) != int2IntValue(val(i)) {
138                         t.Errorf("%T: D", a)
139                 }
140                 if elem2IntValue(a[0]) != int2IntValue(val(i)) {
141                         t.Errorf("%T: D2", a)
142                 }
143                 a.Delete(0)
144                 if a.Len() != i {
145                         t.Errorf("%T: E) wrong Len() %d (expected %d)", a, a.Len(), i)
146                 }
147                 if len(a) != i {
148                         t.Errorf("%T: E) wrong len() %d (expected %d)", a, len(a), i)
149                 }
150         }
151
152         if a.Len() != 0 {
153                 t.Errorf("%T: F) wrong Len() %d (expected 0)", a, a.Len())
154         }
155         if len(a) != 0 {
156                 t.Errorf("%T: F) wrong len() %d (expected 0)", a, len(a))
157         }
158         for i := 0; i < n; i++ {
159                 a.Push(int2IntValue(val(i)))
160                 if a.Len() != i+1 {
161                         t.Errorf("%T: G) wrong Len() %d (expected %d)", a, a.Len(), i+1)
162                 }
163                 if len(a) != i+1 {
164                         t.Errorf("%T: G) wrong len() %d (expected %d)", a, len(a), i+1)
165                 }
166                 if elem2IntValue(a.Last()) != int2IntValue(val(i)) {
167                         t.Errorf("%T: H", a)
168                 }
169         }
170         a.Resize(0, 0)
171         if a.Len() != 0 {
172                 t.Errorf("%T: I wrong Len() %d (expected 0)", a, a.Len())
173         }
174         if len(a) != 0 {
175                 t.Errorf("%T: I wrong len() %d (expected 0)", a, len(a))
176         }
177
178         const m = 5
179         for j := 0; j < m; j++ {
180                 a.Push(int2IntValue(j))
181                 for i := 0; i < n; i++ {
182                         x := val(i)
183                         a.Push(int2IntValue(x))
184                         if elem2IntValue(a.Pop()) != int2IntValue(x) {
185                                 t.Errorf("%T: J", a)
186                         }
187                         if a.Len() != j+1 {
188                                 t.Errorf("%T: K) wrong Len() %d (expected %d)", a, a.Len(), j+1)
189                         }
190                         if len(a) != j+1 {
191                                 t.Errorf("%T: K) wrong len() %d (expected %d)", a, len(a), j+1)
192                         }
193                 }
194         }
195         if a.Len() != m {
196                 t.Errorf("%T: L) wrong Len() %d (expected %d)", a, a.Len(), m)
197         }
198         if len(a) != m {
199                 t.Errorf("%T: L) wrong len() %d (expected %d)", a, len(a), m)
200         }
201 }
202
203 func verify_sliceInt(t *testing.T, x *IntVector, elt, i, j int) {
204         for k := i; k < j; k++ {
205                 if elem2IntValue(x.At(k)) != int2IntValue(elt) {
206                         t.Errorf("%T: M) wrong [%d] element %v (expected %v)", x, k, elem2IntValue(x.At(k)), int2IntValue(elt))
207                 }
208         }
209
210         s := x.Slice(i, j)
211         for k, n := 0, j-i; k < n; k++ {
212                 if elem2IntValue(s.At(k)) != int2IntValue(elt) {
213                         t.Errorf("%T: N) wrong [%d] element %v (expected %v)", x, k, elem2IntValue(x.At(k)), int2IntValue(elt))
214                 }
215         }
216 }
217
218 func verify_patternInt(t *testing.T, x *IntVector, a, b, c int) {
219         n := a + b + c
220         if x.Len() != n {
221                 t.Errorf("%T: O) wrong Len() %d (expected %d)", x, x.Len(), n)
222         }
223         if len(*x) != n {
224                 t.Errorf("%T: O) wrong len() %d (expected %d)", x, len(*x), n)
225         }
226         verify_sliceInt(t, x, 0, 0, a)
227         verify_sliceInt(t, x, 1, a, a+b)
228         verify_sliceInt(t, x, 0, a+b, n)
229 }
230
231 func make_vectorInt(elt, len int) *IntVector {
232         x := new(IntVector).Resize(len, 0)
233         for i := 0; i < len; i++ {
234                 x.Set(i, int2IntValue(elt))
235         }
236         return x
237 }
238
239 func TestIntInsertVector(t *testing.T) {
240         // 1
241         a := make_vectorInt(0, 0)
242         b := make_vectorInt(1, 10)
243         a.InsertVector(0, b)
244         verify_patternInt(t, a, 0, 10, 0)
245         // 2
246         a = make_vectorInt(0, 10)
247         b = make_vectorInt(1, 0)
248         a.InsertVector(5, b)
249         verify_patternInt(t, a, 5, 0, 5)
250         // 3
251         a = make_vectorInt(0, 10)
252         b = make_vectorInt(1, 3)
253         a.InsertVector(3, b)
254         verify_patternInt(t, a, 3, 3, 7)
255         // 4
256         a = make_vectorInt(0, 10)
257         b = make_vectorInt(1, 1000)
258         a.InsertVector(8, b)
259         verify_patternInt(t, a, 8, 1000, 2)
260 }
261
262 func TestIntDo(t *testing.T) {
263         const n = 25
264         const salt = 17
265         a := new(IntVector).Resize(n, 0)
266         for i := 0; i < n; i++ {
267                 a.Set(i, int2IntValue(salt*i))
268         }
269         count := 0
270         a.Do(func(e int) {
271                 i := intf2IntValue(e)
272                 if i != int2IntValue(count*salt) {
273                         t.Error(tname(a), "value at", count, "should be", count*salt, "not", i)
274                 }
275                 count++
276         })
277         if count != n {
278                 t.Error(tname(a), "should visit", n, "values; did visit", count)
279         }
280
281         b := new(IntVector).Resize(n, 0)
282         for i := 0; i < n; i++ {
283                 (*b)[i] = int2IntValue(salt * i)
284         }
285         count = 0
286         b.Do(func(e int) {
287                 i := intf2IntValue(e)
288                 if i != int2IntValue(count*salt) {
289                         t.Error(tname(b), "b) value at", count, "should be", count*salt, "not", i)
290                 }
291                 count++
292         })
293         if count != n {
294                 t.Error(tname(b), "b) should visit", n, "values; did visit", count)
295         }
296
297         var c IntVector
298         c.Resize(n, 0)
299         for i := 0; i < n; i++ {
300                 c[i] = int2IntValue(salt * i)
301         }
302         count = 0
303         c.Do(func(e int) {
304                 i := intf2IntValue(e)
305                 if i != int2IntValue(count*salt) {
306                         t.Error(tname(c), "c) value at", count, "should be", count*salt, "not", i)
307                 }
308                 count++
309         })
310         if count != n {
311                 t.Error(tname(c), "c) should visit", n, "values; did visit", count)
312         }
313
314 }
315
316 func TestIntVectorCopy(t *testing.T) {
317         // verify Copy() returns a copy, not simply a slice of the original vector
318         const Len = 10
319         var src IntVector
320         for i := 0; i < Len; i++ {
321                 src.Push(int2IntValue(i * i))
322         }
323         dest := src.Copy()
324         for i := 0; i < Len; i++ {
325                 src[i] = int2IntValue(-1)
326                 v := elem2IntValue(dest[i])
327                 if v != int2IntValue(i*i) {
328                         t.Error(tname(src), "expected", i*i, "got", v)
329                 }
330         }
331 }