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