Exemple #1
0
def duplication_behavior_unique():
	#t = duptreap.duptreap(allow_duplicates=False)
	t = duptreap.duptreap()
	t[1] = 1
	t[2] = 2
	t[2] = 2
	t[3] = 3
	myut.assertTrue(list(t) == [1, 2, 2, 3])
Exemple #2
0
def iterator_test():
	lst = range(n)
	random_lst = random_swaps(lst[:])
	t = duptreap.duptreap()
	for x in random_lst:
		t[x] = x
	lst2 = list(t.iterator())
	myut.assertTrue(lst == lst2)
Exemple #3
0
def reverse_iterator_test():
	lst = range(n)
	lst.reverse()
	random_lst = random_swaps(lst[:])
	t = treap.treap()
	for x in random_lst:
		t[x] = x
	lst2 = list(t.reverse_iterator())
	myut.assertTrue(lst == lst2)
Exemple #4
0
def depth_test():
	# O(n^2), so we don't use n - we use something small.
	# We assume very little about the resulting depths - in particular, even though this datastructure should very nearly always be log2(n) deep, we assume that
	# worst case behavior is possible - IE that depth can be as high as n.  We also don't make any effort to show that an empty treap has a depth of 0
	my_n = min(n, 100)
	for i in xrange(my_n):
		t = duptreap.duptreap()
		for j in xrange(i):
			t[j] = j
		myut.assertTrue(0 <= t.depth() <= i)
Exemple #5
0
def remove_max_test():
	# O(n^2) test!
	lst = range(n)
	t = duptreap.duptreap()
	for i in lst:
		t[i] = 0
	# taking advantage of the fact that the keys are the same as our lst indices
	for i in lst:
		t.remove_max()
		myut.assertTrue(list(t) == lst[:-(i+1)])
Exemple #6
0
def string_test():
	def random_char():
		return chr(97 + int(random.random() * 32))

	t = duptreap.duptreap()
	dict = {}
	for i in xrange(n):
		strng = '%s%s' % (random_char(), random_char())
		t[strng] = None
		dict[strng] = None
	lst = dict.keys()
	lst.sort()
	myut.assertTrue(list(t) == lst)
Exemple #7
0
def remove_sequence_test(reverse):
	t = duptreap.duptreap()
	lst = range(n)
	for item in lst:
		t[item] = 0
	if reverse:
		lst.reverse()
		pop = t.remove_max
	else:
		pop = t.remove_min
	for i in xrange(len(lst)):
		value_from_treap = pop()
		myut.assertEqual((lst[i], 0), value_from_treap)
	myut.assertTrue(not bool(t))
Exemple #8
0
def remove_min_test():
	# O(n^2) test!
	lst = range(n)
	t = duptreap.duptreap()
	for i in lst:
		t[i] = 0
	# taking advantage of the fact that the keys are the same as our lst indices
	for i in lst:
		if i % (n / 5) == 0:
			myut.assertTrue(t.check_heap_invariant())
			myut.assertTrue(t.check_tree_invariant())
		t.remove_min()
		myut.assertTrue(list(t) == lst[i+1:])
Exemple #9
0
	def runTest(self):
		self.create(randoms(n))
		expected_direction = [0, 1]
		for i in xrange(len(self.content)-1):
			actual_direction = sign(self.content[i+1] - self.content[i])
			myut.assertTrue(actual_direction in expected_direction)
Exemple #10
0
def nonempty_test():
	t = duptreap.duptreap()
	t[1] = 1
	myut.assertTrue(bool(t) == True)
Exemple #11
0
def empty_test():
	t = duptreap.duptreap()
	myut.assertTrue(not (bool(t) == True))