Exemplo n.º 1
0
	def test_issorted(self):
		collection = utils.sorted_list(1, 1000)
		result = sorting.isSorted(collection)
		self.assertEqual(True, result)
		collection = utils.reversed_sorted_list(1, 1000)
		result = sorting.isSorted(collection)
		self.assertEqual(False, result)
Exemplo n.º 2
0
 def test_quick_sort_rec(self):
     collection = utils.random_list(10000, 1, 1000)
     result = sorting.qs(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.reversed_sorted_list(1, 10000)
     result = sorting.quick_sort_rec(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.sorted_list(1, 10000)
     result = sorting.quick_sort_rec(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
Exemplo n.º 3
0
 def test_merge_sort(self):
     collection = utils.random_list(10000, 1, 1000)
     result = sorting.merge_sort(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.reversed_sorted_list(1, 10000)
     result = sorting.merge_sort(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
     collection = utils.sorted_list(1, 10000)
     result = sorting.merge_sort(collection)
     is_sorted = sorting.isSorted(result)
     self.assertEqual(True, is_sorted)
Exemplo n.º 4
0
 def test_quick_sort_inplace(self):
     collection = utils.random_list(10000, 1, 1000)
     sorting.qs_inplace(collection)
     is_sorted = sorting.isSorted(collection)
     self.assertEqual(True, is_sorted)
     collection = utils.reversed_sorted_list(1, 10000)
     sorting.qs_inplace(collection)
     is_sorted = sorting.isSorted(collection)
     self.assertEqual(True, is_sorted)
     collection = utils.sorted_list(1, 10000)
     sorting.qs_inplace(collection)
     is_sorted = sorting.isSorted(collection)
     self.assertEqual(True, is_sorted)
Exemplo n.º 5
0
 def test_heap(self):
     # Randomization
     shuffled = [16, 12, 15, 11, 8, 7, 13, 7, 6, 1, 2, 4, 5, 9, 0, -1]
     shuffle(shuffled)
     # Creation
     h = heap.Heap(shuffled)
     self.assertEqual(False, h.isMaxHeap())
     h.buildMaxHeap()
     self.assertEqual(True, h.isMaxHeap())
     self.assertEqual(16, h.maximum())
     self.assertEqual(-1, h.minimum())
     # increaseKey
     h.insert(19)
     self.assertEqual(19, h.maximum())
     self.assertEqual(True, h.isMaxHeap())
     # Merge
     h2 = heap.Heap(shuffled)
     self.assertEqual(False, h2.isMaxHeap())
     h2.buildMaxHeap()
     self.assertEqual(True, h2.isMaxHeap())
     h.merge(h2)
     self.assertEqual(True, h.isMaxHeap())
     self.assertEqual(19, h.maximum())
     self.assertEqual(-1, h.minimum())
     # extractMaximum
     m = h.extractMaximum()
     self.assertEqual(16, h.maximum())
     self.assertEqual(19, m)
     # Insertion
     h.insert(17)
     self.assertEqual(17, h.maximum())
     m = h.extractMaximum()
     self.assertEqual(17, m)
     self.assertEqual(16, h.maximum())
     # Immutable Heapsort
     result = h.heapSort()
     is_sorted = isSorted(result)
     self.assertEqual(True, is_sorted)
     self.assertEqual(True, h.isMaxHeap())
     self.assertEqual(16, h.maximum())
     self.assertEqual(-1, h.minimum())
Exemplo n.º 6
0
 def test_binary_tree(self):
     l = utils.sorted_list(0, 7)
     # Creation
     b = btree.BinTree()
     while l:
         b.tinsert(utils.takeRdm(l))
     # Functions
     result = b.treeSort()
     is_sorted = isSorted(result)
     self.assertEqual(True, is_sorted)
     b.treeMap(b.getRoot(), lambda x: x + 1)
     for idx, elem in enumerate(b.treeSort()):
         self.assertTrue(result[idx] < elem)
     # Search/Min/Max
     b.tinsert(16)
     searched = b.search(b.getRoot(), 16)
     self.assertEqual(btree.Node(16), searched)
     mi = b.minimum(b.getRoot())
     self.assertEqual(1, mi)
     ma = b.maximum(b.getRoot())
     self.assertEqual(16, ma)
from sorting import quickSort, isSorted

L1 = [-5, -4, -2, 0, 1, 5, 19]
L2 = reversed(L1)
L3 = [-5, 10, 3, -5, 3, 2, 1, 1]

# Fully sorted list
assert isSorted(quickSort(L1)) and len(quickSort(L1)) == len(L1)

# List that is sorted in reverse order
assert isSorted(quickSort(L2)) and len(quickSort(L2)) == len(L2)

# List that is not sorted
assert isSorted(quickSort(L3)) and len(quickSort(L3)) == len(L3)