File: test_validation.py

package info (click to toggle)
logilab-constraint 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: python: 2,926; xml: 165; makefile: 25
file content (109 lines) | stat: -rw-r--r-- 3,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# copyright 2002-2021 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of logilab-constraint.
#
# logilab-constraint is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# logilab-constraint is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with logilab-constraint. If not, see <http://www.gnu.org/licenses/>.
"""Validation testing for constraint propagation module"""

import sys
from io import StringIO

from logilab.common.testlib import TestCase, unittest_main, Tags

from logilab.constraint import fd, Repository, Solver
from logilab.constraint.propagation import quiet_printer
from logilab.constraint.distributors import EnumeratorDistributor


class Queens8_TC(TestCase):
    tags = Tags("slow")

    size = 8
    nb_sols = 92
    verbose = 0

    def setUp(self):
        variables = []
        domains = {}
        constraints = []
        for i in range(self.size):
            name = "Q%d" % i
            variables.append(name)
            domains[name] = fd.FiniteDomain([(i, j) for j in range(self.size)])

        for q1 in variables:
            for q2 in variables:
                if q1 < q2:
                    c = fd.make_expression(
                        (q1, q2),
                        "%(q1)s[0] < %(q2)s[0] and "
                        "%(q1)s[1] != %(q2)s[1] and "
                        "abs(%(q1)s[0]-%(q2)s[0]) != "
                        "abs(%(q1)s[1]-%(q2)s[1])" % {"q1": q1, "q2": q2},
                    )
                    constraints.append(c)
        self.repo = Repository(variables, domains, constraints, printer=quiet_printer)
        sys.stdout = StringIO()

    def tearDown(self):
        sys.stdout = sys.__stdout__

    def testQueensWithEnumerator(self):
        solver = Solver(EnumeratorDistributor(), printer=quiet_printer)
        solutions = solver.solve(self.repo, verbose=self.verbose)
        self.assertEqual(len(solutions), self.nb_sols)

    def testQueensWithDefaultDistributor(self):
        solver = Solver(printer=quiet_printer)
        solutions = solver.solve(self.repo, verbose=self.verbose)
        self.assertEqual(len(solutions), self.nb_sols)


class Queens4_TC(Queens8_TC):
    size = 4
    nb_sols = 2


class Queens5_TC(Queens8_TC):
    size = 5
    nb_sols = 10


class Queens6_TC(Queens8_TC):
    size = 6
    nb_sols = 4


class Queens7_TC(Queens8_TC):
    size = 7
    nb_sols = 40


class Queens6Verbose_TC(Queens6_TC):
    verbose = 3


class Queens9_TC(Queens8_TC):
    size = 9
    nb_sols = 352


class Queens10_TC(Queens8_TC):
    size = 10
    nb_sols = 724


if __name__ == "__main__":
    unittest_main()