def test(self): while True: print('n: %s' % StressTestCase.N) print('m: %s' % StressTestCase.M) table_sizes = \ [ randint(0, StressTestCase.ROWS) for i in range(StressTestCase.N) ] solver = Solver(StressTestCase.N, table_sizes) for i in range(0, StressTestCase.M): destination = randint(0, StressTestCase.N - 1) source = randint(0, StressTestCase.N - 1) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] solver.merge(destination, source) try: self.assert_result(old_rank, old_parent, solver, destination, source) print('.') except Exception as e: f = open('stress-test-failure.txt', 'w') f.write('old_parent: %s' % old_parent) f.write('old_rank: %s' % old_rank) f.write('destination: %s' % destination) f.write('source: %s' % source) f.write('parent: %s' % solver._parent) f.write('rank: %s' % solver._rank) f.close() raise e
def test(self): while True: print('n: %s' % StressTestCase.N) print('m: %s' % StressTestCase.M) table_sizes = \ [ randint(0, StressTestCase.ROWS) for i in range(StressTestCase.N) ] solver = Solver(StressTestCase.N, table_sizes) for i in range(0, StressTestCase.M): destination = randint(0, StressTestCase.N - 1) source = randint(0, StressTestCase.N - 1) old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) try: self.assert_result(old_nodes, solver, destination, source) print('.') except Exception as e: f = open('stress-test-failure.txt', 'w') f.write('max_table_size: %s\n' % solver.max_table_size) f.write('source: %s\n' % source) f.write('destination: %s\n' % destination) f.write('old_nodes: %s\n' % old_nodes) f.write('nodes: %s\n' % solver._nodes) f.close() raise e
def test_merge_with_6_tables_and_4_operations(self): n = 6 solver = Solver(n, [10, 0, 5, 0, 3, 3]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 3, 4, 5], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([10, 0, 5, 0, 3, 3], solver._rank) self.assertEqual(10, solver.max_table_size) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 5, 5 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 3, 4, 5], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([10, 0, 5, 0, 3, 3], solver._rank) self.assertEqual(10, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 5, 4 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 3, 5, 5], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([10, 0, 5, 0, 0, 6], solver._rank) self.assertEqual(10, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 4, 3 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 5, 5, 5], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([10, 0, 5, 0, 0, 6], solver._rank) self.assertEqual(10, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 3, 2 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 5, 5, 5, 5], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([10, 0, 0, 0, 0, 11], solver._rank) self.assertEqual(11, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source)
def test_merge_with_huge_tables(self): """ This test reproduces a stack overflow due to the recursive nature of Solver.get_parent(). """ n = 100000 solver = Solver(n, [i for i in range(n)]) m = n for i in range(m): solver.merge(m - 2 - i, m - 1 - i) sum = (n - 1) * n // 2 self.assertEqual(sum, solver.max_table_size)
def test_merge_with_6_tables_and_4_operations(self): n = 6 solver = Solver(n, [10, 0, 5, 0, 3, 3]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 10, 0), (1, 0, 1), (2, 5, 2), (3, 0, 3), (4, 3, 4), (5, 3, 5)], solver._nodes) self.assertEqual(10, solver.max_table_size) destination, source = 5, 5 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 10, 0), (1, 0, 1), (2, 5, 2), (3, 0, 3), (4, 3, 4), (5, 3, 5)], solver._nodes) self.assertEqual(10, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 5, 4 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 10, 0), (1, 0, 1), (2, 5, 2), (3, 0, 3), (4, 0, 5), (5, 6, 5)], solver._nodes) self.assertEqual(10, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 4, 3 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 10, 0), (1, 0, 1), (2, 5, 2), (3, 0, 5), (4, 0, 5), (5, 6, 5)], solver._nodes) self.assertEqual(10, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 3, 2 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 10, 0), (1, 0, 1), (2, 0, 5), (3, 0, 5), (4, 0, 5), (5, 11, 5)], solver._nodes) self.assertEqual(11, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_constructor_with_n_as_two_and_two_element_list(self): n = 2 solver = Solver(n, [1, 3]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 3, 1)], solver._nodes) self.assertEqual(3, solver.max_table_size)
def test_constructor_with_n_as_one_and_one_element_list(self): n = 1 solver = Solver(n, [2]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 2, 0)], solver._nodes) self.assertEqual(2, solver.max_table_size)
def test_constructor_with_n_as_three_and_three_element_list(self): n = 3 solver = Solver(n, [1, 3, 5]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 3, 1), (2, 5, 2)], solver._nodes) self.assertEqual(5, solver.max_table_size)
def test_merge_with_2_empty_tables_and_merge_first_and_second(self): n = 2 solver = Solver(n, [0, 0]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 0), (1, 0, 1)], solver._nodes) self.assertEqual(0, solver.max_table_size) destination, source = 0, 1 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 0), (1, 0, 0)], solver._nodes) self.assertEqual(0, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_merge_with_2_tables_and_merge_second_and_first(self): n = 2 solver = Solver(n, [1, 2]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 2, 1)], solver._nodes) self.assertEqual(2, solver.max_table_size) destination, source = 1, 0 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 1), (1, 3, 1)], solver._nodes) self.assertEqual(3, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_constructor_with_n_as_one_and_one_element_list(self): n = 1 solver = Solver(n, [2]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([2], solver._rank) self.assertEqual(2, solver.max_table_size)
def test_constructor_with_n_as_two_and_two_element_list(self): n = 2 solver = Solver(n, [1, 3]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 3], solver._rank) self.assertEqual(3, solver.max_table_size)
def test_constructor_with_n_as_three_and_three_element_list(self): n = 3 solver = Solver(n, [1, 3, 5]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 3, 5], solver._rank) self.assertEqual(5, solver.max_table_size)
def test_merge_with_3_tables(self): n = 3 solver = Solver(n, [2, 0, 0]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 2, 0), (1, 0, 1), (2, 0, 2)], solver._nodes) self.assertEqual(2, solver.max_table_size) destination, source = 1, 2 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 2, 0), (1, 0, 1), (2, 0, 1)], solver._nodes) self.assertEqual(2, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 0, 2 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 2, 0), (1, 0, 0), (2, 0, 1)], solver._nodes) self.assertEqual(2, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 1, 0 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 2, 0), (1, 0, 0), (2, 0, 1)], solver._nodes) self.assertEqual(2, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 2, 0 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 2, 0), (1, 0, 0), (2, 0, 0)], solver._nodes) self.assertEqual(2, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_merge_with_2_empty_tables_and_merge_first_and_second(self): n = 2 solver = Solver(n, [0, 0]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 0], solver._rank) self.assertEqual(0, solver.max_table_size) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 0, 1 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 0], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 0], solver._rank) self.assertEqual(0, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source)
def test_merge_with_2_tables_and_merge_second_and_first(self): n = 2 solver = Solver(n, [1, 2]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 2], solver._rank) self.assertEqual(2, solver.max_table_size) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 1, 0 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([1, 1], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 3], solver._rank) self.assertEqual(3, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source)
def test_merge_with_3_tables_and_merge_from_left(self): n = 3 solver = Solver(n, [1, 2, 3]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 2, 3], solver._rank) self.assertEqual(3, solver.max_table_size) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 1, 0 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([1, 1, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 3, 3], solver._rank) self.assertEqual(3, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 2, 0 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([1, 2, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 0, 6], solver._rank) self.assertEqual(6, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source)
def test_merge_with_3_tables_and_merge_from_left(self): n = 3 solver = Solver(n, [1, 2, 3]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 2, 1), (2, 3, 2)], solver._nodes) self.assertEqual(3, solver.max_table_size) destination, source = 1, 0 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 1), (1, 3, 1), (2, 3, 2)], solver._nodes) self.assertEqual(3, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 2, 0 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 1), (1, 0, 2), (2, 6, 2)], solver._nodes) self.assertEqual(6, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_merge_with_4_tables(self): n = 4 solver = Solver(n, [1, 2, 3, 4]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 3], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 2, 3, 4], solver._rank) self.assertEqual(4, solver.max_table_size) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 2, 3 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 2, 7, 0], solver._rank) self.assertEqual(7, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 1, 2 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 1, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 9, 0, 0], solver._rank) self.assertEqual(9, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 0, 3 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 0, 1, 1], solver._parent) self.assertEqual([10, 0, 0, 0], solver._rank) self.assertEqual(10, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source)
def test_merge_with_4_tables(self): n = 4 solver = Solver(n, [1, 2, 3, 4]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 2, 1), (2, 3, 2), (3, 4, 3)], solver._nodes) self.assertEqual(4, solver.max_table_size) destination, source = 2, 3 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 2, 1), (2, 7, 2), (3, 0, 2)], solver._nodes) self.assertEqual(7, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 1, 2 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 9, 1), (2, 0, 1), (3, 0, 2)], solver._nodes) self.assertEqual(9, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 0, 3 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 10, 0), (1, 0, 0), (2, 0, 1), (3, 0, 1)], solver._nodes) self.assertEqual(10, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_merge_with_5_tables_and_5_operations(self): n = 5 solver = Solver(n, [1, 1, 1, 1, 1]) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 1, 1), (2, 1, 2), (3, 1, 3), (4, 1, 4)], solver._nodes) self.assertEqual(1, solver.max_table_size) destination, source = 2, 4 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 1, 1), (2, 2, 2), (3, 1, 3), (4, 0, 2)], solver._nodes) self.assertEqual(2, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 1, 3 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 1, 0), (1, 2, 1), (2, 2, 2), (3, 0, 1), (4, 0, 2)], solver._nodes) self.assertEqual(2, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 0, 3 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 3, 0), (1, 0, 0), (2, 2, 2), (3, 0, 1), (4, 0, 2)], solver._nodes) self.assertEqual(3, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 4, 3 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 2), (1, 0, 0), (2, 5, 2), (3, 0, 0), (4, 0, 2)], solver._nodes) self.assertEqual(5, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source) destination, source = 4, 2 old_nodes = self.copy_nodes_as_tuples(solver, destination, source) solver.merge(destination, source) self.assertEqual(n, len(solver._nodes)) self.assert_nodes([(0, 0, 2), (1, 0, 0), (2, 5, 2), (3, 0, 0), (4, 0, 2)], solver._nodes) self.assertEqual(5, solver.max_table_size) self.assert_result(old_nodes, solver, destination, source)
def test_constructor_with_negative_n(self): with self.assertRaisesRegex(ValueError, 'n must be > 0.'): Solver(-1, [])
def test_constructor_with_n_as_zero_and_one_element_list(self): with self.assertRaisesRegex( ValueError, 'n must equal to the length of the list of tables.'): Solver(1, [1, 2])
def test_merge_with_5_tables_and_5_operations(self): n = 5 solver = Solver(n, [1, 1, 1, 1, 1]) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 3, 4], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 1, 1, 1, 1], solver._rank) self.assertEqual(1, solver.max_table_size) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 2, 4 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 3, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 1, 2, 1, 0], solver._rank) self.assertEqual(2, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 1, 3 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 1, 2, 1, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([1, 2, 2, 0, 0], solver._rank) self.assertEqual(2, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 0, 3 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([0, 0, 2, 1, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([3, 0, 2, 0, 0], solver._rank) self.assertEqual(3, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 4, 3 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([2, 0, 2, 0, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 0, 5, 0, 0], solver._rank) self.assertEqual(5, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source) old_rank = [x for x in solver._rank] old_parent = [x for x in solver._parent] destination, source = 4, 2 solver.merge(destination, source) self.assertEqual(n, len(solver._parent)) self.assertEqual([2, 0, 2, 0, 2], solver._parent) self.assertEqual(n, len(solver._rank)) self.assertEqual([0, 0, 5, 0, 0], solver._rank) self.assertEqual(5, solver.max_table_size) self.assert_result(old_rank, old_parent, solver, destination, source)