1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // A vector.Vector implements sort.Interface except for Less,
15 // and it implements Push and Pop as required for heap.Interface.
20 func (h *myHeap) Less(i, j int) bool { return h.At(i).(int) < h.At(j).(int) }
23 func (h *myHeap) verify(t *testing.T, i int) {
29 t.Errorf("heap invariant invalidated [%d] = %d > [%d] = %d", i, h.At(i), j1, h.At(j1))
36 t.Errorf("heap invariant invalidated [%d] = %d > [%d] = %d", i, h.At(i), j1, h.At(j2))
44 func TestInit0(t *testing.T) {
46 for i := 20; i > 0; i-- {
47 h.Push(0) // all elements are the same
52 for i := 1; h.Len() > 0; i++ {
56 t.Errorf("%d.th pop got %d; want %d", i, x, 0)
62 func TestInit1(t *testing.T) {
64 for i := 20; i > 0; i-- {
65 h.Push(i) // all elements are different
70 for i := 1; h.Len() > 0; i++ {
74 t.Errorf("%d.th pop got %d; want %d", i, x, i)
80 func Test(t *testing.T) {
84 for i := 20; i > 10; i-- {
90 for i := 10; i > 0; i-- {
95 for i := 1; h.Len() > 0; i++ {
102 t.Errorf("%d.th pop got %d; want %d", i, x, i)
108 func TestRemove0(t *testing.T) {
110 for i := 0; i < 10; i++ {
117 x := Remove(h, i).(int)
119 t.Errorf("Remove(%d) got %d; want %d", i, x, i)
126 func TestRemove1(t *testing.T) {
128 for i := 0; i < 10; i++ {
133 for i := 0; h.Len() > 0; i++ {
134 x := Remove(h, 0).(int)
136 t.Errorf("Remove(0) got %d; want %d", x, i)
143 func TestRemove2(t *testing.T) {
147 for i := 0; i < N; i++ {
152 m := make(map[int]bool)
154 m[Remove(h, (h.Len()-1)/2).(int)] = true
159 t.Errorf("len(m) = %d; want %d", len(m), N)
161 for i := 0; i < len(m); i++ {
163 t.Errorf("m[%d] doesn't exist", i)