Skip to content

Commit

Permalink
Completes doc and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kc596 committed Nov 3, 2020
1 parent 261d324 commit 1eafa16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Method | Return Type | Description
`Insert(*Node)` | `void` | Adds a new node to the priority queue
`Max()` | `*Node,error` | Returns highest priority node of the priority queue
`Pop()` | `*Node,error` | Returns highest priority node and deletes it from priority queue
`Clear()` | `void` | Clears the priority queue


#### maxpq.Node
Expand Down
28 changes: 28 additions & 0 deletions maxpq/pq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,31 @@ func TestPQ_Clear(t *testing.T) {
assert.Equal(len(nodes)-1, pq.Size())
assert.False(pq.IsEmpty())
}

func TestPQ_ClearEmptyPQ(t *testing.T) {
assert := assert.New(t)
pq := New()
pq.Clear()
var nodes []*Node
for i := 0; i < 100; i++ {
priority := 10*i - i*i // 0, 9, 16, 21, 24, 25, 24, 21, ... Max value at 4
nodeVal := nodeValue{K1: priority, K2: "Index" + fmt.Sprint(i), K3: struct{ K4 int }{K4: rand.Int()}}
nodes = append(nodes, NewNode(nodeVal, float64(priority)))
}
for _, node := range nodes {
go pq.Insert(node)
}
for pq.Size() < len(nodes) {
time.Sleep(10 * time.Millisecond)
}
max, err := pq.Max()
assert.Nil(err)
assert.Equal(nodes[5], max)
for range nodes {
go pq.Pop()
}
for pq.Size() > 0 {
time.Sleep(10 * time.Millisecond)
}
assert.Zero(pq.Size())
}

0 comments on commit 1eafa16

Please sign in to comment.