コード例 #1
0
ファイル: test_heap.py プロジェクト: sahebsunny/Development
	def test_isHeap_i(self):
		h = Heap()
		isHeap = h.isHeap_i

		h.items = []
		assert isHeap() == True

		h.items = [1, 1]
		assert isHeap() == True

		h.items = [1, 1, 1]
		assert isHeap() == True

		'''
		  1
		 / \
		2   3
	   / \
	  4   5
		'''
		h.items = [1,2,3,4,5]
		assert isHeap() == True

		'''
		  2
		 /
		1
		'''
		h.items = [2, 1]
		assert isHeap() == False

		'''
		  2
		 / \
		3   1
		'''
		h.items = [2, 3, 1]
		assert isHeap() == False

		'''
		   1
		 /   \
		2     3
	   / \   /
	  4   5 1
		'''
		h.items = [1, 2, 3, 4, 5, 1]
		assert isHeap() == False

		h.items = range(10, -1, -1)
		assert isHeap() == False

		h.items = range(10)
		assert isHeap() == True
コード例 #2
0
ファイル: test_heap.py プロジェクト: sahebsunny/Development
	def test_increaseKey(self):
		l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10]
		h = Heap()
		h.items = l

		'''
              0
            /   \
           1     4
         /  \   /  \
        2    6 5    8
       / \  / \
      3   7 9 10
		'''
		caught_exception = False
		try:
			h.increaseKey(4, 5)
		except ValueError as v:
			assert v.message == "ValueError: increaseKey() - New key should be greater than current value"
			caught_exception = True
		assert caught_exception == True

		h.increaseKey(1, 11)
		'''
              0
            /   \
           2     4
         /  \   /  \
        3    6 5    8
       / \  / \
     11   7 9 10
	  '''
		assert h.items == [0, 2, 4, 3, 6, 5, 8, 11, 7, 9, 10]
コード例 #3
0
ファイル: test_heap.py プロジェクト: sahebsunny/Development
	def test_sorted(self):
		l = range(10, -1, -1)
		Heap.build_heap(l)
		h = Heap()
		h.items = l

		new_l = []
		h.sorted(lambda i, l: l.append(i), new_l)

		# TODO: Fix assert fail here owing to bug in remove()
		# when bubble_down() is incorrect due to one/both of L-R not present
		assert new_l == range(11)
コード例 #4
0
ファイル: test_heap.py プロジェクト: sahebsunny/Development
	def test_decreaseKey(self):
		l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10]
		h = Heap()
		h.items = l

		caught_exception = False
		try:
			h.decreaseKey(4, 7)
		except ValueError as v:
			assert v.message == "ValueError: decreaseKey() - New key should be less than current value"
			caught_exception = True
		assert caught_exception == True

		h.decreaseKey(4, 0)
		assert h.items == [0, 0, 4, 2, 1, 5, 8, 3, 7, 9, 10]
コード例 #5
0
ファイル: test_heap.py プロジェクト: sahebsunny/Development
	def test_setitem(self):
		l = [0, 1, 4, 2, 6, 5, 8, 3, 7, 9, 10]
		h = Heap()
		h.items = l

		'''
              0
            /   \
           1     4
         /  \   /  \
        2    6 5    8
       / \  / \
      3   7 9 10
		'''

		assert h[4] == 6
		h[4] = 0 # calls decreaseKey
		'''
              0
            /   \
           0     4
         /  \   /  \
        2    1 5    8
       / \  / \
      3   7 9 10
		'''
		assert h.items == [0, 0, 4, 2, 1, 5, 8, 3, 7, 9, 10]

		assert h[1] ==  0
		h[1] = 11 # calls increaseKey
		'''
              0
            /   \
           1     4
         /  \   /  \
        2    9 5    8
       / \  / \
      3   7 11 10
		'''
		assert h.items == [0, 1, 4, 2, 9, 5, 8, 3, 7, 11, 10]