Skip to content

Commit

Permalink
pq: change priority tested
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddri committed Jan 27, 2015
1 parent d75acd7 commit 7bf01e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
11 changes: 6 additions & 5 deletions data-structures/priority-queue/priority_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ func (pq *PQ) Extract() (el Item) {
return pq.data.Extract().(Item)
}

func (pq *PQ) changePriority(el Item, priority int) {
func (pq *PQ) ChangePriority(val interface{}, priority int) {
var storage = make([]Item, 0)

popped := pq.Extract()

for el != popped {
for val != popped.Value {
if pq.Len() == 0 {
panic("Item not found")
}
Expand All @@ -56,11 +56,12 @@ func (pq *PQ) changePriority(el Item, priority int) {
popped = pq.Extract()
}

el.Priority = priority
pq.data.Insert(el)
popped.Priority = priority
pq.data.Insert(popped)

for len(storage) > 0 {
for len(storage) > 1 {
pq.data.Insert(storage[0])
storage = storage[1 : len(storage)-1]
}
pq.data.Insert(storage[0])
}
24 changes: 23 additions & 1 deletion data-structures/priority-queue/priority_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func TestMinHeap(t *testing.T) {
func TestPriorityQueue(t *testing.T) {
h := New()

h.Insert(*NewItem(8, 10))
Expand All @@ -31,3 +31,25 @@ func TestMinHeap(t *testing.T) {
}
}
}

func TestChangePriority(t *testing.T) {
h := New()

h.Insert(*NewItem(8, 10))
h.Insert(*NewItem(7, 11))
h.Insert(*NewItem(6, 12))
h.Insert(*NewItem(3, 13))
h.Insert(*NewItem(1, 14))
h.Insert(*NewItem(0, 15))
h.Insert(*NewItem(2, 16))
h.Insert(*NewItem(4, 17))
h.Insert(*NewItem(9, 18))
h.Insert(*NewItem(5, 19))

h.ChangePriority(8, 66)
popped := h.Extract()

if popped.Value != 8 {
t.Error()
}
}

0 comments on commit 7bf01e7

Please sign in to comment.