File: minheap_test.go

package info (click to toggle)
golang-github-mailgun-minheap 0.0~git20170619.3dbe6c6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 92 kB
  • sloc: makefile: 18
file content (78 lines) | stat: -rw-r--r-- 1,349 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package minheap

import (
	. "launchpad.net/gocheck"
	"testing"
)

func Test(t *testing.T) { TestingT(t) }

type MinHeapSuite struct{}

var _ = Suite(&MinHeapSuite{})

func toEl(i int) interface{} {
	return &i
}

func fromEl(i interface{}) int {
	return *(i.(*int))
}

func (s *MinHeapSuite) TestPeek(c *C) {
	mh := NewMinHeap()

	el := &Element{
		Value:    toEl(1),
		Priority: 5,
	}

	mh.PushEl(el)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 1)
	c.Assert(mh.Len(), Equals, 1)

	el = &Element{
		Value:    toEl(2),
		Priority: 1,
	}
	mh.PushEl(el)
	c.Assert(mh.Len(), Equals, 2)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 2)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 2)
	c.Assert(mh.Len(), Equals, 2)

	el = mh.PopEl()

	c.Assert(fromEl(el.Value), Equals, 2)
	c.Assert(mh.Len(), Equals, 1)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 1)

	mh.PopEl()
	c.Assert(mh.Len(), Equals, 0)
}

func (s *MinHeapSuite) TestUpdate(c *C) {
	mh := NewMinHeap()
	x := &Element{
		Value:    toEl(1),
		Priority: 4,
	}
	y := &Element{
		Value:    toEl(2),
		Priority: 3,
	}
	z := &Element{
		Value:    toEl(3),
		Priority: 8,
	}
	mh.PushEl(x)
	mh.PushEl(y)
	mh.PushEl(z)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 2)

	mh.UpdateEl(z, 1)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 3)

	mh.UpdateEl(x, 0)
	c.Assert(fromEl(mh.PeekEl().Value), Equals, 1)
}