Ejemplo n.º 1
0
    def test_quantiles(self):
        case_class = namedtuple('case_class', 'array subset_count key expected_res')
        cases = (
            case_class(array=[100], subset_count=1, key=None, expected_res=[100]),
            case_class(array=[1, 3, 5, 4, 2, 7, 6], subset_count=3, key=None, expected_res=[2, 5, 7]),
            case_class(array=[1, 3, 5, 4, 2, 7, 6], subset_count=4, key=None, expected_res=[2, 4, 5, 7]),
            case_class(array=[1, 3, 5, 4, 2, 7, 6], subset_count=7, key=None, expected_res=[1, 2, 3, 4, 5, 6, 7]),
            case_class(array=[8, 2, 1, 3, 7, 5, 4, 6], subset_count=1, key=None, expected_res=[8]),
            case_class(array=[8, 2, 1, 3, 7, 5, 4, 6], subset_count=2, key=None, expected_res=[4, 8]),
            case_class(array=[8, 2, 1, 3, 7, 5, 4, 6], subset_count=3, key=None, expected_res=[3, 5, 8]),
            case_class(array=[8, 2, 1, 3, 7, 5, 4, 6], subset_count=3, key=lambda x: -x, expected_res=[6, 4, 1]),
            case_class(array=[8, 2, 1, 3, 7, 5, 4, 6], subset_count=4, key=None, expected_res=[2, 4, 6, 8]),
        )

        for case in cases:
            # print(case.array, case.subset_count)
            res = calc_quantiles(case.array, case.subset_count, case.key)
            self.assertEqual(case.expected_res, res)

        for length in range(1, 100):
            subset_count = randint(1, length)
            array = [x * 2 for x in range(0, length)]
            rand_permutate(array)
            # print(array, subset_count)
            res = calc_quantiles(array, subset_count)
            # print(res)
            self.assertEqual(subset_count, len(res))
            if subset_count > 1:
                diffs = []
                for i in range(0, subset_count - 1):
                    self.assertEqual(0, res[i] % 2)
                    diffs.append(res[i + 1] - res[i])
                min_diff, max_diff = min_max(diffs)
                self.assertTrue(max_diff - min_diff <= 2)
            self.assertEqual(array[length - 1], res[subset_count - 1])
Ejemplo n.º 2
0
 def test_rand_insert_delete(self):
     rbt = RBTree()
     insertion_seq = list(range(1000))
     rand_permutate(insertion_seq)
     values = []
     i = 0
     while i < len(insertion_seq):
         if len(values) > 0:
             rand = uniform(0, 1)
         else:
             rand = 0
         if rand > 0.5:
             rand_index = randint(0, len(values) - 1)
             value = values[rand_index]
             # print("delete %d" % value)
             values.pop(rand_index)
             node = rb_search(rbt, value)
             rb_pop(rbt, node)
         else:
             value = insertion_seq[i]
             i += 1
             # print("insert %d" % value)
             values.append(value)
             values.sort()
             rb_insert(rbt, value)
         rb_assert_properties(rbt)
         node_count = rb_node_count(rbt, rbt.root)
         self.assertEqual(len(values), node_count)
         self.assertSequenceEqual(values, list(rb_iter(rbt)))
Ejemplo n.º 3
0
 def test_rand_insert_delete(self):
     avl = AVLTree()
     insertion_seq = list(range(1000))
     rand_permutate(insertion_seq)
     values = []
     i = 0
     while i < len(insertion_seq):
         if len(values) > 0:
             rand = uniform(0, 1)
         else:
             rand = 0
         if rand > 0.5:
             rand_index = randint(0, len(values) - 1)
             value = values[rand_index]
             # print("delete %d" % value)
             values.pop(rand_index)
             avl_pop(avl, value)
         else:
             value = insertion_seq[i]
             i += 1
             # print("insert %d" % value)
             values.append(value)
             values.sort()
             avl_insert(avl, value)
         self._assert_avl_properties(avl)
         # print(len(values))
         self.assertSequenceEqual(values, list(bst_iter(avl)))
Ejemplo n.º 4
0
    def test_selection(self):
        case_class = namedtuple('case_class', 'array i key expected_res')
        for select_method in (
                rand_select,
                select,
                select_variant,
        ):
            cases = (
                case_class(array=[1], i=0, key=None, expected_res=1),
                case_class(array=[3, 2, 1], i=0, key=None, expected_res=1),
                case_class(array=[1, 3, 5, 4, 2, 7, 6],
                           i=4,
                           key=None,
                           expected_res=5),
                case_class(array=[1, 3, 5, 4, 2, 7, 6],
                           i=2,
                           key=None,
                           expected_res=3),
                case_class(array=[1, 3, 5, 4, 2, 7, 6],
                           i=6,
                           key=lambda x: -x,
                           expected_res=1),
                case_class(array=[8, 3, 2, 4, 6, 9, 7, 5, 1],
                           i=0,
                           key=None,
                           expected_res=1),
                case_class(array=[
                    16, 196, 64, 121, 144, 9, 36, 0, 49, 100, 4, 81, 169, 1, 25
                ],
                           i=4,
                           key=None,
                           expected_res=16),
                case_class(array=[1, 16, 4, 9, 49, 100, 25, 36, 81, 64, 0],
                           i=0,
                           key=None,
                           expected_res=0),
            )

            for case in cases:
                # print(case.array, case.i)
                self.assertEqual(case.expected_res,
                                 select_method(case.array, case.i, case.key))

            for length in range(1, 100):
                i = randint(0, length - 1)
                array = [x * x for x in range(0, length)]
                rand_permutate(array)
                case = case_class(array=array,
                                  i=i,
                                  key=None,
                                  expected_res=i * i)
                # print(case.array, case.i)
                self.assertEqual(case.expected_res,
                                 select_method(case.array, case.i, case.key))
Ejemplo n.º 5
0
 def test_rand_insertions(self):
     avl = AVLTree()
     insertion_seq = list(range(1, 129))
     rand_permutate(insertion_seq)
     cnt = 0
     for i in insertion_seq:
         avl_insert(avl, i)
         self._assert_avl_properties(avl)
         self.assertSequenceEqual(sorted(insertion_seq[:cnt + 1]), list(bst_iter(avl)))
         cnt += 1
     self.assertSequenceEqual(sorted(insertion_seq), list(bst_iter(avl)))
Ejemplo n.º 6
0
 def test_heap_delete_rand(self):
     from Common.common import rand_permutate
     from random import randint
     for i in range(100):
         len = randint(10, 100)
         array = list(range(1, len + 1))
         rand_permutate(array)
         build_max_heap(array, 0)
         self.assertTrue(check_max_heap(array, 0, 0))
         for j in range(len):
             array_copy = list(array)
             heap_delete(array_copy, j)
             self.assertTrue(
                 check_max_heap(array_copy, 0, 0),
                 msg='Heap is not max %r. from %r deleting element at %d' %
                 (array_copy, array, j))
Ejemplo n.º 7
0
    def test_two_smallest(self):
        case_class = namedtuple('case_class',
                                'array offset length key min second_min')
        cases = (
            case_class(array=(1, 1),
                       offset=0,
                       length=None,
                       key=None,
                       min=1,
                       second_min=1),
            case_class(array=(1, 2),
                       offset=0,
                       length=None,
                       key=lambda x: -x,
                       min=2,
                       second_min=1),
            case_class(array=(2, 1),
                       offset=0,
                       length=None,
                       key=lambda x: -x,
                       min=2,
                       second_min=1),
            case_class(array=(1, 3, 2),
                       offset=0,
                       length=None,
                       key=None,
                       min=1,
                       second_min=2),
            case_class(array=(2, 3, 4, 5, 1),
                       offset=0,
                       length=None,
                       key=None,
                       min=1,
                       second_min=2),
            case_class(array=(3, 8, 2, 5, 1, 7, 6, 4),
                       offset=0,
                       length=6,
                       key=None,
                       min=1,
                       second_min=2),
            case_class(array=(3, 8, 2, 5, 1, 7, 6, 4),
                       offset=0,
                       length=None,
                       key=None,
                       min=1,
                       second_min=2),
            case_class(array=(3, 8, 2, 5, 1, 7, 6, 4),
                       offset=0,
                       length=7,
                       key=None,
                       min=1,
                       second_min=2),
            case_class(array=(2, 1, 3, 3, 2, 3, 1),
                       offset=0,
                       length=None,
                       key=None,
                       min=1,
                       second_min=1),
            case_class(array=(1, 2, 3, 4, 5, 6, 7, 8, 9),
                       offset=0,
                       length=None,
                       key=None,
                       min=1,
                       second_min=2),
        )

        for case in cases:
            self.assertEqual((case.min, case.second_min),
                             two_smallest(case.array, case.offset, case.length,
                                          case.key))

        for length in range(2, 129):
            array = [x for x in range(1, length + 1)]
            rand_permutate(array)
            # print(array)
            case = case_class(array=array,
                              offset=0,
                              length=None,
                              key=None,
                              min=1,
                              second_min=2)
            self.assertEqual((case.min, case.second_min),
                             two_smallest(case.array, case.offset, case.length,
                                          case.key))