summaryrefslogtreecommitdiff
path: root/src/lib/container/array/array_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-01-28 13:32:31 -0800
committerRobert Griesemer <gri@golang.org>2009-01-28 13:32:31 -0800
commit0a513b6d8d408b977d25ae98f3458b17fcd22a8d (patch)
treeae4629b3aef8f9ac36dd2071fe85c94f79cf5870 /src/lib/container/array/array_test.go
parent7845d26bb8660cbb03740040b607f534705755e5 (diff)
downloadgolang-0a513b6d8d408b977d25ae98f3458b17fcd22a8d.tar.gz
additions to array container:
- added Slice, Cut, InsertArray, AppendArray - renamed Remove -> Delete (so we have: Insert, Delete, Cut) - more factoring of code - extra tests (could use some more) R=r,rsc DELTA=179 (127 added, 22 deleted, 30 changed) OCL=23648 CL=23685
Diffstat (limited to 'src/lib/container/array/array_test.go')
-rw-r--r--src/lib/container/array/array_test.go96
1 files changed, 77 insertions, 19 deletions
diff --git a/src/lib/container/array/array_test.go b/src/lib/container/array/array_test.go
index 6deaf4a6e..df9fee673 100644
--- a/src/lib/container/array/array_test.go
+++ b/src/lib/container/array/array_test.go
@@ -24,7 +24,7 @@ func TestNew(t *testing.T) {
}
-func Val(i int) int {
+func val(i int) int {
return i*991 - 1234
}
@@ -34,34 +34,34 @@ func TestAccess(t *testing.T) {
var a array.Array;
a.Init(n);
for i := 0; i < n; i++ {
- a.Set(i, Val(i));
+ a.Set(i, val(i));
}
for i := 0; i < n; i++ {
- if a.At(i).(int) != Val(i) { t.Error(i) }
+ if a.At(i).(int) != val(i) { t.Error(i) }
}
}
-func TestInsertRemoveClear(t *testing.T) {
+func TestInsertDeleteClear(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") }
+ 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.Last().(int) != val(0) { t.Error("C") }
+ if a.Delete(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()) }
+ 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.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()) }
@@ -70,17 +70,76 @@ func TestInsertRemoveClear(t *testing.T) {
for j := 0; j < m; j++ {
a.Push(j);
for i := 0; i < n; i++ {
- x := Val(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() != 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) }
+ if a.Len() != m { t.Errorf("L) wrong len %d (expected %d)", a.Len(), m) }
+}
+
+
+func verify_slice(t *testing.T, x *array.Array, elt, i, j int) {
+ for k := i; k < j; k++ {
+ if x.At(k).(int) != elt {
+ t.Errorf("M) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt)
+ }
+ }
+
+ s := x.Slice(i, j);
+ for k, n := 0, j-i; k < n; k++ {
+ if s.At(k).(int) != elt {
+ t.Errorf("N) wrong [%d] element %d (expected %d)", k, x.At(k).(int), elt)
+ }
+ }
+}
+
+
+func verify_pattern(t *testing.T, x *array.Array, a, b, c int) {
+ n := a + b + c;
+ if x.Len() != n {
+ t.Errorf("O) wrong len %d (expected %d)", x.Len(), n)
+ }
+ verify_slice(t, x, 0, 0, a);
+ verify_slice(t, x, 1, a, a + b);
+ verify_slice(t, x, 0, a + b, n);
+}
+
+
+func make_array(elt, len int) *array.Array {
+ x := array.New(len);
+ for i := 0; i < len; i++ {
+ x.Set(i, elt);
+ }
+ return x;
+}
+
+
+func TestInsertArray(t *testing.T) {
+ // 1
+ a := make_array(0, 0);
+ b := make_array(1, 10);
+ a.InsertArray(0, b);
+ verify_pattern(t, a, 0, 10, 0);
+ // 2
+ a = make_array(0, 10);
+ b = make_array(1, 0);
+ a.InsertArray(5, b);
+ verify_pattern(t, a, 5, 0, 5);
+ // 3
+ a = make_array(0, 10);
+ b = make_array(1, 3);
+ a.InsertArray(3, b);
+ verify_pattern(t, a, 3, 3, 7);
+ // 4
+ a = make_array(0, 10);
+ b = make_array(1, 1000);
+ a.InsertArray(8, b);
+ verify_pattern(t, a, 8, 1000, 2);
}
-/* currently doesn't compile due to linker bug
func TestSorting(t *testing.T) {
const n = 100;
a := array.NewIntArray(n);
@@ -89,4 +148,3 @@ func TestSorting(t *testing.T) {
}
if sort.IsSorted(a) { t.Error("not sorted") }
}
-*/