From 6ad5cb57afce55f806843fb36ca8ac4c23557a53 Mon Sep 17 00:00:00 2001 From: Mario Frasca Date: Fri, 6 Jan 2017 11:12:23 -0500 Subject: [PATCH] fixing unit tests --- .gitignore | 2 + Makefile | 6 +-- ghini_tree_position.py | 52 ------------------------ test/test_heap.py | 91 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 test/test_heap.py diff --git a/.gitignore b/.gitignore index 4df9c32..fb2bb79 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ help/build resources.py distances-aligned.csv + +af.qm diff --git a/Makefile b/Makefile index 972af29..640a045 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/ghini_tree_position.py b/ghini_tree_position.py index 62ae9c9..b92a434 100644 --- a/ghini_tree_position.py +++ b/ghini_tree_position.py @@ -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 @@ -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 @@ -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) diff --git a/test/test_heap.py b/test/test_heap.py new file mode 100644 index 0000000..79abab1 --- /dev/null +++ b/test/test_heap.py @@ -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]) + +