Esempio n. 1
0
    def test_clear(self):
        lst = AvlTreeList()
        for i in range(20):
            lst.append(i * i)

        lst.clear()
        self.assertEqual(0, len(lst))

        lst.append(-1)
        lst.append(-8)
        lst.append(-27)
        self.assertEqual(3, len(lst))
        self.assertEqual(-1, lst[0])
        self.assertEqual(-8, lst[1])
        self.assertEqual(-27, lst[2])
	def test_clear(self):
		lst = AvlTreeList()
		for i in range(20):
			lst.append(i * i)
		
		lst.clear()
		self.assertEqual(0, len(lst))
		
		lst.append(- 1)
		lst.append(- 8)
		lst.append(-27)
		self.assertEqual(3, len(lst))
		self.assertEqual(- 1, lst[0])
		self.assertEqual(- 8, lst[1])
		self.assertEqual(-27, lst[2])
Esempio n. 3
0
    def test_against_python_list_randomly(self):
        ITERATIONS = 3000
        list0 = []
        list1 = AvlTreeList()
        length = 0
        for i in range(ITERATIONS):
            op = random.randrange(100)

            if op < 1:  # Clear
                list1.check_structure()
                list0 = []
                list1.clear()
                length = 0

            elif op < 2:  # Set
                if length > 0:
                    index = random.randint(0, length - 1)
                    val = random.randrange(100000)
                    list0[index] = val
                    list1[index] = val

            elif op < 30:  # Random insertion
                n = random.randint(1, 100)
                for j in range(n):
                    index = random.randint(0, length)
                    val = random.randrange(100000)
                    list0.insert(index, val)
                    list1.insert(index, val)
                    length += 1

            elif op < 50:  # Ascending insertion
                n = random.randint(1, 100)
                offset = random.randint(0, length)
                for j in range(n):
                    val = random.randrange(100000)
                    list0.insert(offset + j, val)
                    list1.insert(offset + j, val)
                length += n

            elif op < 70:  # Descending insertion
                n = random.randint(1, 100)
                offset = random.randint(0, length)
                for j in range(n):
                    val = random.randrange(100000)
                    list0.insert(offset, val)
                    list1.insert(offset, val)
                length += n

            elif op < 80:  # Random deletion
                n = min(random.randint(1, 100), length)
                for j in range(n):
                    index = random.randint(0, length - 1)
                    del list0[index]
                    del list1[index]
                    length -= 1

            elif op < 90:  # Ascending deletion
                if length > 0:
                    offset = random.randint(0, length - 1)
                    n = min(random.randint(1, 100), length - offset)
                    for j in range(n):
                        del list0[offset]
                        del list1[offset]
                    length -= n

            elif op < 100:  # Descending deletion
                if length > 0:
                    offset = random.randint(0, length - 1)
                    n = min(random.randint(1, 100), offset + 1)
                    for j in range(n):
                        del list0[offset - j]
                        del list1[offset - j]
                    length -= n
            else:
                raise AssertionError()

            if len(list0) != length or len(list1) != length:
                raise AssertionError()
            if length > 0:
                for j in range(10):
                    index = random.randint(0, length - 1)
                    if list0[index] is not list1[index]:
                        raise AssertionError()
	def test_against_python_list_randomly(self):
		ITERATIONS = 3000
		list0 = []
		list1 = AvlTreeList()
		length = 0
		for i in range(ITERATIONS):
			op = random.randrange(100)
			
			if op < 1:  # Clear
				list1.check_structure()
				list0 = []
				list1.clear()
				length = 0
				
			elif op < 2:  # Set
				if length > 0:
					index = random.randint(0, length - 1)
					val = random.randrange(100000)
					list0[index] = val
					list1[index] = val
				
			elif op < 30:  # Random insertion
				n = random.randint(1, 100)
				for j in range(n):
					index = random.randint(0, length)
					val = random.randrange(100000)
					list0.insert(index, val)
					list1.insert(index, val)
					length += 1
				
			elif op < 50:  # Ascending insertion
				n = random.randint(1, 100)
				offset = random.randint(0, length)
				for j in range(n):
					val = random.randrange(100000)
					list0.insert(offset + j, val)
					list1.insert(offset + j, val)
				length += n
				
			elif op < 70:  # Descending insertion
				n = random.randint(1, 100)
				offset = random.randint(0, length)
				for j in range(n):
					val = random.randrange(100000)
					list0.insert(offset, val)
					list1.insert(offset, val)
				length += n
				
			elif op < 80:  # Random deletion
				n = min(random.randint(1, 100), length)
				for j in range(n):
					index = random.randint(0, length - 1)
					del list0[index]
					del list1[index]
					length -= 1
				
			elif op < 90:  # Ascending deletion
				if length > 0:
					offset = random.randint(0, length - 1)
					n = min(random.randint(1, 100), length - offset)
					for j in range(n):
						del list0[offset]
						del list1[offset]
					length -= n
				
			elif op < 100:  # Descending deletion
				if length > 0:
					offset = random.randint(0, length - 1)
					n = min(random.randint(1, 100), offset + 1)
					for j in range(n):
						del list0[offset - j]
						del list1[offset - j]
					length -= n
			else:
				raise AssertionError()
			
			if len(list0) != length or len(list1) != length:
				raise AssertionError()
			if length > 0:
				for j in range(10):
					index = random.randint(0, length - 1)
					if list0[index] is not list1[index]:
						raise AssertionError()