Example #1
0
	def test_custom_comparator(self):
		# Max heap using custom comparator
		h = Heap(lambda a,b: cmp(b, a))
		for i in range(10):
			h.add(i)

		for i in range(9, -1, -1):
			assert h.remove() == i
Example #2
0
	def test_remove(self):
		l = [5,4,3,2,1,6,7,8,9,0]
		h = Heap()

		with self.assertRaises(HeapEmptyError):
			h.peek()

		caught_exception = False
		try:
			assert h.remove() == None
		except HeapEmptyError as e:
			assert str(e) == "HeapEmptyError: 'remove(): Heap is empty'"
			caught_exception = True
		assert caught_exception == True


		for i in xrange(len(l)):
			h.add(l[i])

		assert len(h) == 10
		for i in xrange(10):
			item = h.remove()
			self.assertEqual(len(h), 10-i-1)
			self.assertEqual(item, i)