1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package array
import "array"
import "testing"
import "sort"
func TestInit(t *testing.T) {
var a array.Array;
if a.Init(0).Len() != 0 { t.Error("A") }
if a.Init(1).Len() != 1 { t.Error("B") }
if a.Init(10).Len() != 10 { t.Error("C") }
}
func TestNew(t *testing.T) {
if array.New(0).Len() != 0 { t.Error("A") }
if array.New(1).Len() != 1 { t.Error("B") }
if array.New(10).Len() != 10 { t.Error("C") }
}
func Val(i int) int {
return i*991 - 1234
}
func TestAccess(t *testing.T) {
const n = 100;
var a array.Array;
a.Init(n);
for i := 0; i < n; i++ {
a.Set(i, Val(i));
}
for i := 0; i < n; i++ {
if a.At(i).(int) != Val(i) { t.Error(i) }
}
}
func TestInsertRemoveClear(t *testing.T) {
const n = 100;
a := array.New(0);
for i := 0; i < n; i++ {
if a.Len() != i { t.Errorf("A wrong len %d (expected %d)", a.Len(), i) }
a.Insert(0, Val(i));
if a.Last().(int) != Val(0) { t.Error("B") }
}
for i := n-1; i >= 0; i-- {
if a.Last().(int) != Val(0) { t.Error("C") }
if a.Remove(0).(int) != Val(i) { t.Error("D") }
if a.Len() != i { t.Errorf("E wrong len %d (expected %d)", a.Len(), i) }
}
if a.Len() != 0 { t.Errorf("F wrong len %d (expected 0)", a.Len()) }
for i := 0; i < n; i++ {
a.Push(Val(i));
if a.Len() != i+1 { t.Errorf("G wrong len %d (expected %d)", a.Len(), i+1) }
if a.Last().(int) != Val(i) { t.Error("H") }
}
a.Init(0);
if a.Len() != 0 { t.Errorf("I wrong len %d (expected 0)", a.Len()) }
const m = 5;
for j := 0; j < m; j++ {
a.Push(j);
for i := 0; i < n; i++ {
x := Val(i);
a.Push(x);
if a.Pop().(int) != x { t.Error("J") }
if a.Len() != j+1 { t.Errorf("K wrong len %d (expected %d)", a.Len(), j+1) }
}
}
if a.Len() != m { t.Errorf("L wrong len %d (expected %d)", a.Len(), m) }
}
/* currently doesn't compile due to linker bug
func TestSorting(t *testing.T) {
const n = 100;
a := array.NewIntArray(n);
for i := n-1; i >= 0; i-- {
a.Set(i, n-1-i);
}
if sort.IsSorted(a) { t.Error("not sorted") }
}
*/
|