From 451f0ef4e2b9d7e8b3950bbbbd9e92764aa42b6a Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Sun, 19 Nov 2023 15:25:33 +0100 Subject: [PATCH] the embryo of test_all_solutions.py --- tests/problems.py | 44 +++++++++++++++++++++++++++++++++++++ tests/test_all_solutions.py | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 tests/test_all_solutions.py diff --git a/tests/problems.py b/tests/problems.py index ebfaf2b..2f8fda5 100644 --- a/tests/problems.py +++ b/tests/problems.py @@ -44,6 +44,10 @@ def small_trimino_problem(): data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY), solution1=[5, 13], solution_count=2, + all_solutions=[ + {5, 13}, + {6, 12}, + ], ) @@ -52,6 +56,10 @@ def small_trimino_problem_from_file(): data=np.load("tests/files/small_trimino_problem.npy"), solution1=[5, 13], solution_count=2, + all_solutions=[ + {5, 13}, + {6, 12}, + ], ) @@ -72,6 +80,9 @@ def detailed_wikipedia_problem(): ), solution1=[1, 3, 5], solution_count=1, + all_solutions=[ + {1, 3, 5}, + ], ) @@ -88,6 +99,10 @@ def bruteforce_problem1(): data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY), solution1=[0, 1, 2], solution_count=2, + all_solutions=[ + {0, 1, 2}, + {3, 4, 5}, + ], ) @@ -107,6 +122,17 @@ def bruteforce_problem2(): data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY), solution1=[0, 1, 2], solution_count=9, + all_solutions=[ + {0, 1, 2}, + {0, 1, 8}, + {0, 2, 7}, + {0, 7, 8}, + {1, 2, 6}, + {1, 6, 8}, + {2, 6, 7}, + {3, 4, 5}, + {6, 7, 8}, + ], ) @@ -129,4 +155,22 @@ def bruteforce_problem3(): data=np.array(to_cover, dtype=DTYPE_FOR_ARRAY), solution1=[0, 1, 2], solution_count=16, + all_solutions=[ + {0, 1, 2}, + {0, 1, 8}, + {0, 2, 7}, + {0, 7, 8}, + {1, 2, 6}, + {1, 6, 8}, + {2, 6, 7}, + {3, 4, 5}, + {3, 4, 11}, + {3, 5, 10}, + {3, 10, 11}, + {4, 5, 9}, + {4, 9, 11}, + {5, 9, 10}, + {6, 7, 8}, + {9, 10, 11}, + ] ) diff --git a/tests/test_all_solutions.py b/tests/test_all_solutions.py new file mode 100644 index 0000000..86e9acb --- /dev/null +++ b/tests/test_all_solutions.py @@ -0,0 +1,41 @@ +# xxx not yet available +# from exact_cover_impl import get_all_solutions + +import problems + + +def normalize(solution): + """ + transform the output of get_all_solutions + or the all_solutions key in a problem dict + into a frozenset of frozensets, so that we can + compare solutions for equality. + """ + return frozenset(frozenset(row) for row in solution) + +def test_from_problem(problem): + """ + generate a test function for a problem dict + """ + def test(): + # xxx not yet available + # result = normalize(get_all_solutions(problem["data"])) + result = frozenset() + expected = normalize(problem["all_solutions"]) + assert result == expected + return test + + +# define in the current module a test function +# for each problem in problems +# not all symbols in the problems module are considered: +# - need to contain '_problem' +# - and not be a private symbol (start with '_') + +for problem_name in dir(problems): + if ('_problem' not in problem_name + or problem_name.startswith('_')): + continue + test_name = 'test_' + problem_name + problem_function = getattr(problems, problem_name) + globals()[test_name] = test_from_problem(problem_function())