-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
47 lines (35 loc) · 1.55 KB
/
test.py
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
import unittest
import pytest
australia_names = ['WA', 'NT', 'Q', 'NSW', 'V', 'SA', 'T']
australia_neighbors = [
('WA', 'NT'), ('NT', 'SA'), ('WA', 'SA'), ('NT', 'Q'), ('SA', 'Q'),
('Q', 'NSW'), ('SA', 'NSW'), ('SA', 'V'), ('NSW', 'V')
]
@pytest.fixture
def australia():
import csp
australia = csp.ConstraintSatisfactionProblem()
class AustraliaVariable(csp.BaseVariable):
def __init__(self, name):
csp.BaseVariable.__init__(self, australia, name)
self.domain = {'red', 'blue', 'green'}
for name in australia_names:
australia.variables[name] = AustraliaVariable(name)
for constraint in australia_neighbors:
vars = map(lambda t: australia.variables[t], constraint)
australia.constraints.add(csp.AllDifferentConstraint(vars))
return australia
class ConstraintSatisfactionProblemTestCase(unittest.TestCase):
def test_solve(self):
solution = australia().solve()
self.assertTrue(all(variable.value for variable in solution.variables.values()))
for pair in australia_neighbors:
vars = [solution.variables.get(p) for p in pair]
self.assertNotEqual(vars[0].value, vars[1].value, "Failed: {}".format(solution.variables))
class BaseVariableTestCase(unittest.TestCase):
def test_neighbors(self):
csp = australia()
self.assertSetEqual(set(), csp.variables['T'].neighbors)
self.assertSetEqual({csp.variables[x] for x in ['WA', 'SA', 'Q']}, csp.variables['NT'].neighbors)
if __name__ == '__main__':
unittest.main()