gitee.com/quant1x/gox@v1.21.2/util/binaryheap/binaryheap_test.go (about)

     1  // Copyright (c) 2015, Emir Pasic. 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.
     4  
     5  package binaryheap
     6  
     7  import (
     8  	"math/rand"
     9  	"testing"
    10  )
    11  
    12  func TestBinaryHeapPush(t *testing.T) {
    13  	heap := NewWithIntComparator()
    14  
    15  	if actualValue := heap.Empty(); actualValue != true {
    16  		t.Errorf("Got %v expected %v", actualValue, true)
    17  	}
    18  
    19  	heap.Push(3) // [3]
    20  	heap.Push(2) // [2,3]
    21  	heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
    22  
    23  	if actualValue := heap.Values(); actualValue[0].(int) != 1 || actualValue[1].(int) != 3 || actualValue[2].(int) != 2 {
    24  		t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
    25  	}
    26  	if actualValue := heap.Empty(); actualValue != false {
    27  		t.Errorf("Got %v expected %v", actualValue, false)
    28  	}
    29  	if actualValue := heap.Size(); actualValue != 3 {
    30  		t.Errorf("Got %v expected %v", actualValue, 3)
    31  	}
    32  	if actualValue, ok := heap.Peek(); actualValue != 1 || !ok {
    33  		t.Errorf("Got %v expected %v", actualValue, 1)
    34  	}
    35  }
    36  
    37  func TestBinaryHeapPushBulk(t *testing.T) {
    38  	heap := NewWithIntComparator()
    39  
    40  	heap.Push(15, 20, 3, 1, 2)
    41  
    42  	if actualValue := heap.Values(); actualValue[0].(int) != 1 || actualValue[1].(int) != 2 || actualValue[2].(int) != 3 {
    43  		t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
    44  	}
    45  	if actualValue, ok := heap.Pop(); actualValue != 1 || !ok {
    46  		t.Errorf("Got %v expected %v", actualValue, 1)
    47  	}
    48  }
    49  
    50  func TestBinaryHeapPop(t *testing.T) {
    51  	heap := NewWithIntComparator()
    52  
    53  	if actualValue := heap.Empty(); actualValue != true {
    54  		t.Errorf("Got %v expected %v", actualValue, true)
    55  	}
    56  
    57  	heap.Push(3) // [3]
    58  	heap.Push(2) // [2,3]
    59  	heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
    60  	heap.Pop()   // [3,2]
    61  
    62  	if actualValue, ok := heap.Peek(); actualValue != 2 || !ok {
    63  		t.Errorf("Got %v expected %v", actualValue, 2)
    64  	}
    65  	if actualValue, ok := heap.Pop(); actualValue != 2 || !ok {
    66  		t.Errorf("Got %v expected %v", actualValue, 2)
    67  	}
    68  	if actualValue, ok := heap.Pop(); actualValue != 3 || !ok {
    69  		t.Errorf("Got %v expected %v", actualValue, 3)
    70  	}
    71  	if actualValue, ok := heap.Pop(); actualValue != nil || ok {
    72  		t.Errorf("Got %v expected %v", actualValue, nil)
    73  	}
    74  	if actualValue := heap.Empty(); actualValue != true {
    75  		t.Errorf("Got %v expected %v", actualValue, true)
    76  	}
    77  	if actualValue := heap.Values(); len(actualValue) != 0 {
    78  		t.Errorf("Got %v expected %v", actualValue, "[]")
    79  	}
    80  }
    81  
    82  func TestBinaryHeapRandom(t *testing.T) {
    83  	heap := NewWithIntComparator()
    84  
    85  	rand.Seed(3)
    86  	for i := 0; i < 10000; i++ {
    87  		r := int(rand.Int31n(30))
    88  		heap.Push(r)
    89  	}
    90  
    91  	prev, _ := heap.Pop()
    92  	for !heap.Empty() {
    93  		curr, _ := heap.Pop()
    94  		if prev.(int) > curr.(int) {
    95  			t.Errorf("Heap property invalidated. prev: %v current: %v", prev, curr)
    96  		}
    97  		prev = curr
    98  	}
    99  }
   100  
   101  func TestBinaryHeapIteratorOnEmpty(t *testing.T) {
   102  	heap := NewWithIntComparator()
   103  	it := heap.Iterator()
   104  	for it.Next() {
   105  		t.Errorf("Shouldn't iterate on empty heap")
   106  	}
   107  }
   108  
   109  func TestBinaryHeapIteratorNext(t *testing.T) {
   110  	heap := NewWithIntComparator()
   111  	heap.Push(3) // [3]
   112  	heap.Push(2) // [2,3]
   113  	heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
   114  
   115  	it := heap.Iterator()
   116  	count := 0
   117  	for it.Next() {
   118  		count++
   119  		index := it.Index()
   120  		value := it.Value()
   121  		switch index {
   122  		case 0:
   123  			if actualValue, expectedValue := value, 1; actualValue != expectedValue {
   124  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   125  			}
   126  		case 1:
   127  			if actualValue, expectedValue := value, 3; actualValue != expectedValue {
   128  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   129  			}
   130  		case 2:
   131  			if actualValue, expectedValue := value, 2; actualValue != expectedValue {
   132  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   133  			}
   134  		default:
   135  			t.Errorf("Too many")
   136  		}
   137  		if actualValue, expectedValue := index, count-1; actualValue != expectedValue {
   138  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   139  		}
   140  	}
   141  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   142  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   143  	}
   144  }
   145  
   146  func TestBinaryHeapIteratorPrev(t *testing.T) {
   147  	heap := NewWithIntComparator()
   148  	heap.Push(3) // [3]
   149  	heap.Push(2) // [2,3]
   150  	heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
   151  
   152  	it := heap.Iterator()
   153  	for it.Next() {
   154  	}
   155  	count := 0
   156  	for it.Prev() {
   157  		count++
   158  		index := it.Index()
   159  		value := it.Value()
   160  		switch index {
   161  		case 0:
   162  			if actualValue, expectedValue := value, 1; actualValue != expectedValue {
   163  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   164  			}
   165  		case 1:
   166  			if actualValue, expectedValue := value, 3; actualValue != expectedValue {
   167  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   168  			}
   169  		case 2:
   170  			if actualValue, expectedValue := value, 2; actualValue != expectedValue {
   171  				t.Errorf("Got %v expected %v", actualValue, expectedValue)
   172  			}
   173  		default:
   174  			t.Errorf("Too many")
   175  		}
   176  		if actualValue, expectedValue := index, 3-count; actualValue != expectedValue {
   177  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   178  		}
   179  	}
   180  	if actualValue, expectedValue := count, 3; actualValue != expectedValue {
   181  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   182  	}
   183  }
   184  
   185  func TestBinaryHeapIteratorBegin(t *testing.T) {
   186  	heap := NewWithIntComparator()
   187  	it := heap.Iterator()
   188  	it.Begin()
   189  	heap.Push(2)
   190  	heap.Push(3)
   191  	heap.Push(1)
   192  	for it.Next() {
   193  	}
   194  	it.Begin()
   195  	it.Next()
   196  	if index, value := it.Index(), it.Value(); index != 0 || value != 1 {
   197  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1)
   198  	}
   199  }
   200  
   201  func TestListIteratorEnd(t *testing.T) {
   202  	heap := NewWithIntComparator()
   203  	it := heap.Iterator()
   204  
   205  	if index := it.Index(); index != -1 {
   206  		t.Errorf("Got %v expected %v", index, -1)
   207  	}
   208  
   209  	it.End()
   210  	if index := it.Index(); index != 0 {
   211  		t.Errorf("Got %v expected %v", index, 0)
   212  	}
   213  
   214  	heap.Push(3) // [3]
   215  	heap.Push(2) // [2,3]
   216  	heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
   217  	it.End()
   218  	if index := it.Index(); index != heap.Size() {
   219  		t.Errorf("Got %v expected %v", index, heap.Size())
   220  	}
   221  
   222  	it.Prev()
   223  	if index, value := it.Index(), it.Value(); index != heap.Size()-1 || value != 2 {
   224  		t.Errorf("Got %v,%v expected %v,%v", index, value, heap.Size()-1, 2)
   225  	}
   226  }
   227  
   228  func TestStackIteratorFirst(t *testing.T) {
   229  	heap := NewWithIntComparator()
   230  	it := heap.Iterator()
   231  	if actualValue, expectedValue := it.First(), false; actualValue != expectedValue {
   232  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   233  	}
   234  	heap.Push(3) // [3]
   235  	heap.Push(2) // [2,3]
   236  	heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
   237  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   238  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   239  	}
   240  	if index, value := it.Index(), it.Value(); index != 0 || value != 1 {
   241  		t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1)
   242  	}
   243  }
   244  
   245  func TestBinaryHeapIteratorLast(t *testing.T) {
   246  	tree := NewWithIntComparator()
   247  	it := tree.Iterator()
   248  	if actualValue, expectedValue := it.Last(), false; actualValue != expectedValue {
   249  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   250  	}
   251  	tree.Push(2)
   252  	tree.Push(3)
   253  	tree.Push(1) // [1,3,2](2 swapped with 1, hence last)
   254  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   255  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   256  	}
   257  	if index, value := it.Index(), it.Value(); index != 2 || value != 2 {
   258  		t.Errorf("Got %v,%v expected %v,%v", index, value, 2, 2)
   259  	}
   260  }
   261  
   262  func TestBinaryHeapSerialization(t *testing.T) {
   263  	heap := NewWithStringComparator()
   264  
   265  	heap.Push("c") // ["c"]
   266  	heap.Push("b") // ["b","c"]
   267  	heap.Push("a") // ["a","c","b"]("b" swapped with "a", hence last)
   268  
   269  	var err error
   270  	assert := func() {
   271  		if actualValue := heap.Values(); actualValue[0].(string) != "a" || actualValue[1].(string) != "c" || actualValue[2].(string) != "b" {
   272  			t.Errorf("Got %v expected %v", actualValue, "[1,3,2]")
   273  		}
   274  		if actualValue := heap.Size(); actualValue != 3 {
   275  			t.Errorf("Got %v expected %v", actualValue, 3)
   276  		}
   277  		if actualValue, ok := heap.Peek(); actualValue != "a" || !ok {
   278  			t.Errorf("Got %v expected %v", actualValue, "a")
   279  		}
   280  		if err != nil {
   281  			t.Errorf("Got error %v", err)
   282  		}
   283  	}
   284  
   285  	assert()
   286  
   287  	json, err := heap.ToJSON()
   288  	assert()
   289  
   290  	err = heap.FromJSON(json)
   291  	assert()
   292  }
   293  
   294  func benchmarkPush(b *testing.B, heap *Heap, size int) {
   295  	for i := 0; i < b.N; i++ {
   296  		for n := 0; n < size; n++ {
   297  			heap.Push(n)
   298  		}
   299  	}
   300  }
   301  
   302  func benchmarkPop(b *testing.B, heap *Heap, size int) {
   303  	for i := 0; i < b.N; i++ {
   304  		for n := 0; n < size; n++ {
   305  			heap.Pop()
   306  		}
   307  	}
   308  }
   309  
   310  func BenchmarkBinaryHeapPop100(b *testing.B) {
   311  	b.StopTimer()
   312  	size := 100
   313  	heap := NewWithIntComparator()
   314  	for n := 0; n < size; n++ {
   315  		heap.Push(n)
   316  	}
   317  	b.StartTimer()
   318  	benchmarkPop(b, heap, size)
   319  }
   320  
   321  func BenchmarkBinaryHeapPop1000(b *testing.B) {
   322  	b.StopTimer()
   323  	size := 1000
   324  	heap := NewWithIntComparator()
   325  	for n := 0; n < size; n++ {
   326  		heap.Push(n)
   327  	}
   328  	b.StartTimer()
   329  	benchmarkPop(b, heap, size)
   330  }
   331  
   332  func BenchmarkBinaryHeapPop10000(b *testing.B) {
   333  	b.StopTimer()
   334  	size := 10000
   335  	heap := NewWithIntComparator()
   336  	for n := 0; n < size; n++ {
   337  		heap.Push(n)
   338  	}
   339  	b.StartTimer()
   340  	benchmarkPop(b, heap, size)
   341  }
   342  
   343  func BenchmarkBinaryHeapPop100000(b *testing.B) {
   344  	b.StopTimer()
   345  	size := 100000
   346  	heap := NewWithIntComparator()
   347  	for n := 0; n < size; n++ {
   348  		heap.Push(n)
   349  	}
   350  	b.StartTimer()
   351  	benchmarkPop(b, heap, size)
   352  }
   353  
   354  func BenchmarkBinaryHeapPush100(b *testing.B) {
   355  	b.StopTimer()
   356  	size := 100
   357  	heap := NewWithIntComparator()
   358  	b.StartTimer()
   359  	benchmarkPush(b, heap, size)
   360  }
   361  
   362  func BenchmarkBinaryHeapPush1000(b *testing.B) {
   363  	b.StopTimer()
   364  	size := 1000
   365  	heap := NewWithIntComparator()
   366  	for n := 0; n < size; n++ {
   367  		heap.Push(n)
   368  	}
   369  	b.StartTimer()
   370  	benchmarkPush(b, heap, size)
   371  }
   372  
   373  func BenchmarkBinaryHeapPush10000(b *testing.B) {
   374  	b.StopTimer()
   375  	size := 10000
   376  	heap := NewWithIntComparator()
   377  	for n := 0; n < size; n++ {
   378  		heap.Push(n)
   379  	}
   380  	b.StartTimer()
   381  	benchmarkPush(b, heap, size)
   382  }
   383  
   384  func BenchmarkBinaryHeapPush100000(b *testing.B) {
   385  	b.StopTimer()
   386  	size := 100000
   387  	heap := NewWithIntComparator()
   388  	for n := 0; n < size; n++ {
   389  		heap.Push(n)
   390  	}
   391  	b.StartTimer()
   392  	benchmarkPush(b, heap, size)
   393  }