示例#1
0
 def test_add(self):
     lst = AvlTreeList()
     lst.append("January")
     lst.append("February")
     lst.append("March")
     lst.append("April")
     lst.append("May")
     lst.append("June")
     lst.check_structure()
     self.assertEqual(6, len(lst))
     self.assertEqual("January", lst[0])
     self.assertEqual("February", lst[1])
     self.assertEqual("March", lst[2])
     self.assertEqual("April", lst[3])
     self.assertEqual("May", lst[4])
     self.assertEqual("June", lst[5])
	def test_add(self):
		lst = AvlTreeList()
		lst.append("January")
		lst.append("February")
		lst.append("March")
		lst.append("April")
		lst.append("May")
		lst.append("June")
		lst.check_structure()
		self.assertEqual(6, len(lst))
		self.assertEqual("January" , lst[0])
		self.assertEqual("February", lst[1])
		self.assertEqual("March"   , lst[2])
		self.assertEqual("April"   , lst[3])
		self.assertEqual("May"     , lst[4])
		self.assertEqual("June"    , lst[5])
示例#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()