]> Cypherpunks repositories - gostls13.git/commitdiff
- full support for sorting (assumes array elements implement LessInterface
authorRobert Griesemer <gri@golang.org>
Thu, 20 Nov 2008 00:23:45 +0000 (16:23 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 20 Nov 2008 00:23:45 +0000 (16:23 -0800)
- better test reporting

R=r
DELTA=43  (24 added, 0 deleted, 19 changed)
OCL=19641
CL=19645

src/lib/container/array/array.go
src/lib/container/array/testarray.go

index 97f2c43970ae4499936c18a814603238b23760d6..95ed6c2ece4cd5ec08aebf87a18e19a7a2dbb3fa 100644 (file)
@@ -111,6 +111,17 @@ func (p *Array) Pop() Element {
 
 
 // Partial SortInterface support
+
+export type LessInterface interface {
+       Less(y Element) bool
+}
+
+
+func (p *Array) Less(i, j int) bool {
+       return p.a[i].(LessInterface).Less(p.a[j])
+}
+
+
 func (p *Array) Swap(i, j int) {
        a := p.a;
        a[i], a[j] = a[j], a[i]
index be4928301af0a70e02dc2701c6b0aa3a37f20b28..2c56ba8f02d6325e0bbe12b4dbafcea2d326452d 100644 (file)
@@ -8,18 +8,19 @@ import "array"
 import "testing"
 import "sort"
 
+
 export func TestInit(t *testing.T) {
        var a array.Array;
-       if a.Init(0).Len() != 0 { t.FailNow() }
-       if a.Init(1).Len() != 1 { t.FailNow() }
-       if a.Init(10).Len() != 10 { t.FailNow() }
+       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") }
 }
 
 
 export func TestNew(t *testing.T) {
-       if array.New(0).Len() != 0 { t.FailNow() }
-       if array.New(1).Len() != 1 { t.FailNow() }
-       if array.New(10).Len() != 10 { t.FailNow() }
+       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") }
 }
 
 
@@ -36,7 +37,7 @@ export func TestAccess(t *testing.T) {
                a.Set(i, Val(i));
        }
        for i := 0; i < n; i++ {
-               if a.At(i).(int) != Val(i) { t.FailNow() }
+               if a.At(i).(int) != Val(i) { t.Error(i) }
        }
 }
 
@@ -46,24 +47,24 @@ export func TestInsertRemoveClear(t *testing.T) {
        a := array.New(0);
 
        for i := 0; i < n; i++ {
-               if a.Len() != i { t.FailNow() }
+               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.FailNow() }
+               if a.Last().(int) != Val(0) { t.Error("B") }
        }
        for i := n-1; i >= 0; i-- {
-               if a.Last().(int) != Val(0) { t.FailNow() }
-               if a.Remove(0).(int) != Val(i) { t.FailNow() }
-               if a.Len() != i { t.FailNow() }
+               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.FailNow() }
+       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.FailNow() }
-               if a.Last().(int) != Val(i) { t.FailNow() }
+               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.FailNow() }
+       if a.Len() != 0 { t.Errorf("I wrong len %d (expected 0)", a.Len()) }
 
        const m = 5;
        for j := 0; j < m; j++ {
@@ -71,9 +72,21 @@ export func TestInsertRemoveClear(t *testing.T) {
                for i := 0; i < n; i++ {
                        x := Val(i);
                        a.Push(x);
-                       if a.Pop().(int) != x { t.FailNow() }
-                       if a.Len() != j+1 { t.FailNow() }
+                       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.FailNow() }
+       if a.Len() != m { t.Errorf("L wrong len %d (expected %d)", a.Len(), m) }
+}
+
+
+/* currently doesn't compile due to linker bug
+export 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") }
 }
+*/