Skip to content

Commit

Permalink
fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrasca committed Jan 6, 2017
1 parent adbb88d commit 6ad5cb5
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 55 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ help/build

resources.py
distances-aligned.csv

af.qm
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

#Add iso code for any locales you want to support here (space separated)
# default is no locales
# LOCALES = af
LOCALES =
LOCALES = af
# LOCALES =

# If locales are enabled, set the name of the lrelease binary on your system. If
# you have trouble compiling the translations, you may have to specify the full path to
# lrelease
#LRELEASE = lrelease
#LRELEASE = lrelease-qt4
LRELEASE = lrelease-qt4


# translation
Expand Down
52 changes: 0 additions & 52 deletions ghini_tree_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,6 @@ def __init__(self, elems):
highest priority values goes to front of array.
>>> i = Heap([{'prio':1},{'prio':2},{'prio':3}])
>>> i.priorities()
[3, 1, 2]
>>> i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5}])
>>> i.priorities()
[5, 4, 2, 1, 3]
>>> i = Heap([{'prio':1},{'prio':3},{'prio':2},{'prio':4},{'prio':5}])
>>> i.priorities()
[5, 4, 2, 1, 3]
>>> i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7}])
>>> i.priorities()
[7, 4, 5, 1, 3, 2]
"""
self.heap = list(elems)
self.heap[0]['heappos'] = 0
Expand All @@ -315,16 +302,6 @@ def __init__(self, elems):
def push(self, elem):
"""push new element into heap
>>> i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7}])
>>> i.push({'prio': 8})
>>> i.pop()
{'prio': 8}
>>> i.push({'prio': 6})
>>> i.pop()
{'prio': 7}
>>> i.pop()
{'prio': 6}
"""
k = len(self.heap)
elem['heappos'] = k
Expand All @@ -335,35 +312,6 @@ def pop(self):
"""pop highest priority from heap
element with highest priority value is removed from heap.
>>> i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7}])
>>> i.pop()
{'prio': 7}
>>> i.pop()
{'prio': 5}
>>> i.pop()
{'prio': 4}
>>> i.pop()
{'prio': 3}
>>> i.pop()
{'prio': 2}
>>> i.pop()
{'prio': 1}
>>> len(i.priorities())
0
>>> a, b, c, d, e, f = ({'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7})
>>> i = Heap([f, e, b, c, d, a])
>>> i.priorities()
[7, 5, 2, 3, 4, 1]
>>> i.pop()
{'prio': 7}
>>> i.priorities()
[5, 4, 2, 3, 1]
>>> i.pop()
{'prio': 5}
>>> i.priorities()
[4, 3, 2, 1]
"""
k = len(self.heap) - 1
self._swap(0, k)
Expand Down
91 changes: 91 additions & 0 deletions test/test_heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import unittest

from ghini_tree_position import Heap

class HeapTest(unittest.TestCase):
def test_init(self):
i = Heap([{'prio':1},{'prio':2},{'prio':3}])
print(dir(self))
self.assertEquals(i.priorities(), [3, 1, 2])
i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5}])
self.assertEquals(i.priorities(), [5, 4, 2, 1, 3])
i = Heap([{'prio':1},{'prio':3},{'prio':2},{'prio':4},{'prio':5}])
self.assertEquals(i.priorities(), [5, 4, 2, 1, 3])
i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7}])
self.assertEquals(i.priorities(), [7, 4, 5, 1, 3, 2])

def test_push(self):

i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7}])
i.push({'prio': 8})
self.assertEquals(i.pop(), {'prio': 8})
i.push({'prio': 6})
self.assertEquals(i.pop(), {'prio': 7})
self.assertEquals(i.pop(), {'prio': 6})

def test_pop(self):

i = Heap([{'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7}])
self.assertEquals(i.pop(), {'prio': 7})
self.assertEquals(i.pop(), {'prio': 5})
self.assertEquals(i.pop(), {'prio': 4})
self.assertEquals(i.pop(), {'prio': 3})
self.assertEquals(i.pop(), {'prio': 2})
self.assertEquals(i.pop(), {'prio': 1})
self.assertEquals(len(i.priorities()), 0)

a, b, c, d, e, f = ({'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7})
i = Heap([f, e, b, c, d, a])
self.assertEquals(i.priorities(), [7, 5, 2, 3, 4, 1])
self.assertEquals(i.pop(), {'prio': 7})
self.assertEquals(i.priorities(), [5, 4, 2, 3, 1])
self.assertEquals(i.pop(), {'prio': 5})
self.assertEquals(i.priorities(), [4, 3, 2, 1])

def test_reprioritize(self):

a, b, c, d, e, f = ({'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7})
i = Heap([a, b, c, d, e, f])

## nothing happens if the priority change is zero
self.assertEquals(i.priorities(), [7, 4, 5, 1, 3, 2])
i.reprioritize(a, 0)
self.assertEquals(i.priorities(), [7, 4, 5, 1, 3, 2])

## the priority change default value is the positive unit. you specify
## the object of which the priority has to be altered.
a, b, c, d, e, f = ({'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7})
i = Heap([a, b, c, d, e, f])
self.assertEquals(i.priorities(), [7, 4, 5, 1, 3, 2])
i.reprioritize(a)
self.assertEquals(i.priorities(), [7, 4, 5, 2, 3, 2])
i.reprioritize(a)
self.assertEquals(i.priorities(), [7, 4, 5, 3, 3, 2])
i.reprioritize(a)
self.assertEquals(i.priorities(), [7, 4, 5, 4, 3, 2])
i.reprioritize(a)
self.assertEquals(i.priorities(), [7, 5, 5, 4, 3, 2])
self.assertEquals(a, {'heappos': 1, 'prio': 5})

## you can give any value for the desired priority change.
a, b, c, d, e, f = ({'prio':1},{'prio':2},{'prio':3},{'prio':4},{'prio':5},{'prio':7})
i = Heap([a, b, c, d, e, f])
self.assertEquals(i.priorities(), [7, 4, 5, 1, 3, 2])
i.reprioritize(c, 8)
self.assertEquals(i.priorities(), [11, 7, 5, 1, 4, 2])
i.reprioritize(e, 15)
self.assertEquals(i.priorities(), [20, 7, 11, 1, 4, 2])

## a negative priority change will sink the object into the heap
i.reprioritize(e, -20)
self.assertEquals(i.priorities(), [11, 7, 2, 1, 4, 0])
i.reprioritize(c, -12)
self.assertEquals(i.priorities(), [7, 4, 2, 1, -1, 0])

def test_swap(self):
i = Heap([{'prio':1},{'prio':2},{'prio':3}])
self.assertEquals(i.priorities(), [3, 1, 2])
i._swap(0, 1)
self.assertEquals(i.priorities(), [1, 3, 2])


0 comments on commit 6ad5cb5

Please sign in to comment.