Пример #1
0
class BacktrackingSolverTest(unittest.TestCase):
    def setUp(self):
        self.s = BacktrackingSolver()
        self.s.set_conditions(
            variables={
                'x': range(3),
                'y': range(3),
            },
            constraints=[
                (lambda x, y: x != y, ['x', 'y'], ()),
            ]
        )
        self.expected = [
            {'x': 1, 'y': 0},
            {'x': 2, 'y': 0},
            {'x': 0, 'y': 1},
            {'x': 2, 'y': 1},
            {'x': 0, 'y': 2},
            {'x': 1, 'y': 2},
        ]

    def test_itersolution(self):
        assertIterEqual(self.expected, self.s)

    def test_solutions_seen_is_nonzero(self):
        list(self.s)
        self.assertNotEqual(self.s, 0)

    def test_solutions_at_points(self):
        list(self.s)
        assertIterEqual(self.expected, self.s.solutions_at_points.values())

    def test_reset(self):
        result1 = list(self.s)
        self.s.set_conditions(
            variables={
                'x': range(3),
                'y': range(3),
            },
            constraints=[
                (lambda x, y: x != y, ['x', 'y'], ()),
            ]
        )
        result2 = list(self.s)
        assertIterEqual(result1, result2)

    def test_save_and_restore(self):
        it = iter(self.s)
        values = next(it), next(it)
        ref = self.s.save_point()
        del it

        self.s.restore_point(ref)
        it = iter(self.s)
        value = next(it)
        self.assertFalse(value in values, 'Iterator was not restored properly.')
Пример #2
0
 def setUp(self):
     self.s = BacktrackingSolver()
     self.s.set_conditions(
         variables={"x": range(3), "y": range(3)}, constraints=[(lambda x, y: x != y, ["x", "y"], ())]
     )
     self.expected = [
         {"x": 1, "y": 0},
         {"x": 2, "y": 0},
         {"x": 0, "y": 1},
         {"x": 2, "y": 1},
         {"x": 0, "y": 2},
         {"x": 1, "y": 2},
     ]
Пример #3
0
 def setUp(self):
     self.s = BacktrackingSolver()
     self.s.set_conditions(
         variables={
             'x': range(3),
             'y': range(3),
         },
         constraints=[
             (lambda x, y: x != y, ['x', 'y'], ()),
         ]
     )
     self.expected = [
         {'x': 1, 'y': 0},
         {'x': 2, 'y': 0},
         {'x': 0, 'y': 1},
         {'x': 2, 'y': 1},
         {'x': 0, 'y': 2},
         {'x': 1, 'y': 2},
     ]