diff --git a/src/lib/container/array/array.go b/src/lib/container/array/array.go index 97f2c43970a..95ed6c2ece4 100644 --- a/src/lib/container/array/array.go +++ b/src/lib/container/array/array.go @@ -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] diff --git a/src/lib/container/array/testarray.go b/src/lib/container/array/testarray.go index be4928301af..2c56ba8f02d 100644 --- a/src/lib/container/array/testarray.go +++ b/src/lib/container/array/testarray.go @@ -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") } +} +*/