Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the embryo of test_all_solutions.py #79

Open
wants to merge 2 commits into
base: all_solutions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
],
)


Expand All @@ -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},
],
)


Expand All @@ -72,6 +80,9 @@ def detailed_wikipedia_problem():
),
solution1=[1, 3, 5],
solution_count=1,
all_solutions=[
{1, 3, 5},
],
)


Expand All @@ -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},
],
)


Expand All @@ -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},
],
)


Expand All @@ -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},
]
)
41 changes: 41 additions & 0 deletions tests/test_all_solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# xxx not yet available
# from exact_cover_impl import get_all_solutions

from . 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())
Loading