mirror of
https://github.com/golang/go
synced 2024-11-26 11:48:03 -07:00
- full support for sorting (assumes array elements implement LessInterface
- better test reporting R=r DELTA=43 (24 added, 0 deleted, 19 changed) OCL=19641 CL=19645
This commit is contained in:
parent
88daac7862
commit
9af3ee5471
@ -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]
|
||||
|
@ -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") }
|
||||
}
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user