Skip to content

Commit

Permalink
add test cases for unconvential formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnaceBleukx committed Mar 3, 2024
1 parent 82e1398 commit c9fb69f
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions tests/test_tool_dimacs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
from cpmpy.transformations.get_variables import get_variables_model
class CNFTool(unittest.TestCase):

def test_read_cnf(self):

tmp_file = tempfile.NamedTemporaryFile()
def setUp(self) -> None:
self.tmpfile = tempfile.NamedTemporaryFile()

def test_read_cnf(self):

"""
a | b | c,
~b | ~c,
~a
"""
cnf_txt = "p cnf \n-2 -3 0\n3 2 1 0\n-1 0\n"
with open(tmp_file.name, "w") as f:
with open(self.tmpfile.name, "w") as f:
f.write(cnf_txt)

model = read_dimacs(tmp_file.name)
model = read_dimacs(self.tmpfile.name)
vars = sorted(get_variables_model(model), key=str)

sols = set()
Expand All @@ -28,6 +30,21 @@ def test_read_cnf(self):
self.assertEqual(model.solveAll(display=addsol), 2)
self.assertSetEqual(sols, {(False, False, True), (False, True, False)})


def test_badly_formatted(self):

cases = [
"p cnf 2 1\n1\n2\n0", "p cnf 2 1\n\n1 2 0"
]

for cnf_txt in cases:
with open(self.tmpfile.name, "w") as f:
f.write(cnf_txt)

m = read_dimacs(self.tmpfile.name)
self.assertEqual(len(m.constraints), 1)
self.assertEqual(m.solveAll(), 3)

def test_write_cnf(self):

a,b,c = [cp.boolvar(name=n) for n in "abc"]
Expand Down

0 comments on commit c9fb69f

Please sign in to comment.