From c9fb69fab4129f9aebaf876d23e60d929d79df44 Mon Sep 17 00:00:00 2001 From: Ignace Bleukx Date: Sun, 3 Mar 2024 17:56:52 +0100 Subject: [PATCH] add test cases for unconvential formatted --- tests/test_tool_dimacs.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/tests/test_tool_dimacs.py b/tests/test_tool_dimacs.py index aa1d2c303..7e28d59cd 100644 --- a/tests/test_tool_dimacs.py +++ b/tests/test_tool_dimacs.py @@ -6,9 +6,11 @@ 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, @@ -16,10 +18,10 @@ def test_read_cnf(self): ~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() @@ -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"]