2 from basetest
import BaseTest
6 sys
.path
.insert(0, '..')
7 from zeroinstall
.injector
import model
, arch
, qdom
8 from zeroinstall
.injector
.namespaces
import XMLNS_IFACE
10 #from zeroinstall.injector.origsolver import DefaultSolver as Solver
11 #from zeroinstall.injector.pbsolver import PBSolver as Solver
12 #from zeroinstall.injector.sgsolver import DefaultSolver as Solver
13 from zeroinstall
.injector
.solver
import SATSolver
as Solver
14 from zeroinstall
.injector
import sat
17 logger
= logging
.getLogger()
20 def lookup_any(self
, digests
):
25 uri_prefix
= 'http://localhost/tests/'
28 def __init__(self
, n
):
33 def add_requires(self
, lib
, min_v
, max_v
):
34 self
.requires
.append((lib
, min_v
, max_v
))
37 def __init__(self
, name
):
41 def get_version(self
, version
):
42 if version
not in self
.versions
:
43 self
.versions
[version
] = Version(version
)
44 return self
.versions
[version
]
47 def child(parent
, name
, attrs
= None):
48 new
= qdom
.Element(XMLNS_IFACE
, name
, attrs
or {})
49 parent
.childNodes
.append(new
)
52 root
= qdom
.Element(XMLNS_IFACE
, 'interface', {'uri' : uri_prefix
+ self
.name
})
53 child(root
, 'name').content
= self
.name
54 child(root
, 'summary').content
= self
.name
57 for version
in self
.versions
.values():
60 'version': str(version
.n
),
63 attrs
['arch'] = version
.arch
64 impl
= child(root
, 'implementation', attrs
)
65 child(impl
, 'manifest-digest', {'sha1new': '1234'})
66 for lib
, min_v
, max_v
in version
.requires
:
67 req
= child(impl
, 'requires', {'interface': uri_prefix
+ lib
})
68 child(req
, 'version', {
69 'before': str(int(max_v
) + 1),
73 feed
= model
.ZeroInstallFeed(root
)
74 feed
.last_modified
= 1
82 def get_prog(self
, prog
):
83 if not prog
in self
.progs
:
84 self
.progs
[prog
] = Program(prog
)
85 return self
.progs
[prog
]
87 def get_interface(self
, uri
):
88 if uri
not in self
.interfaces
:
89 iface
= model
.Interface(uri
)
90 iface
._main
_feed
= self
.progs
[uri
.rsplit('/', 1)[1]].build_feed()
91 self
.interfaces
[uri
] = iface
92 return self
.interfaces
[uri
]
94 def assertSelection(expected
, repo
):
97 expected
= [tuple(e
.strip().split('-')) for e
in expected
.split(",")]
99 for line
in repo
.split('\n'):
101 if not line
: continue
103 prog
, versions
= line
.split(':')
106 prog
, prog_arch
= prog
.split()
109 for v
in versions
.split():
110 cache
.get_prog(prog
).get_version(v
).arch
= prog_arch
112 prog
, requires
= line
.split('=>')
113 prog
, version_range
= prog
.strip().split('[')
114 lib
, min_v
, max_v
= requires
.split()
115 assert version_range
.endswith(']')
116 version_range
= version_range
[:-1]
117 if ',' in version_range
:
118 min_p
, max_p
= map(int, version_range
.split(','))
119 prog_versions
= range(min_p
, max_p
+ 1)
121 prog_versions
= [int(version_range
)]
122 for prog_version
in prog_versions
:
123 cache
.get_prog(prog
).get_version(str(prog_version
)).add_requires(lib
, min_v
, max_v
)
125 root
= uri_prefix
+ expected
[0][0]
126 s
= Solver(model
.network_offline
, cache
, stores
)
127 s
.solve(root
, arch
.get_architecture('Linux', 'x86_64'))
129 if expected
[0][1] == 'FAIL':
135 for iface
, impl
in s
.selections
.iteritems():
136 actual
.append(((iface
.uri
.rsplit('/', 1)[1]), impl
.get_version()))
140 if expected
!= actual
:
141 raise Exception("Solve failed:\nExpected: %s\n Actual: %s" % (expected
, actual
))
144 class TestSAT(BaseTest
):
145 def testTrivial(self
):
146 assertSelection("prog-2", """
150 def testSimple(self
):
151 assertSelection("prog-5, liba-5", """
159 def testBestImpossible(self
):
160 assertSelection("prog-1", """
167 assertSelection("prog-1", """
168 prog: 1 2 3 4 5 6 7 8 9
169 liba: 1 2 3 4 5 6 7 8 9
170 libb: 1 2 3 4 5 6 7 8 9
171 libc: 1 2 3 4 5 6 7 8 9
172 libd: 1 2 3 4 5 6 7 8 9
174 prog[2,9] => liba 1 9
175 liba[1,9] => libb 1 9
176 libb[1,9] => libc 1 9
177 libc[1,9] => libd 1 9
178 libd[1,9] => libe 0 0
181 def testNoSolution(self
):
182 assertSelection("prog-FAIL", """
185 prog[1,3] => liba 2 3
188 def testBacktrackSimple(self
):
189 # We initially try liba-3 before learning that it
190 # is incompatible and backtracking.
191 # We learn that liba-3 doesn't work ever.
192 assertSelection("prog-1, liba-2", """
198 def testBacktrackLocal(self
):
199 # We initially try liba-3 before learning that it
200 # is incompatible and backtracking.
201 # We learn that liba-3 doesn't work with prog-1.
202 assertSelection("prog-2, liba-2", """
205 prog[1,2] => liba 1 2
208 def testLearning(self
):
209 # Prog-2 depends on libb and libz, but we can't have both
210 # at once. The learning means we don't have to explore every
211 # possible combination of liba and libb.
212 assertSelection("prog-1", """
215 libb Linux-i486: 1 2 3
216 libz Linux-x86_64: 1 2
219 liba[1,3] => libb 1 3
222 def testToplevelConflict(self
):
223 # We don't detect the conflict until we start solving, but the
224 # conflict is top-level so we abort immediately without
226 assertSelection("prog-FAIL", """
232 def testDiamondConflict(self
):
233 # prog depends on liba and libb, which depend on incompatible
235 assertSelection("prog-FAIL", """
246 def testCoverage(self
):
247 # Try to trigger some edge cases...
249 # An at_most_one clause must be analysed for causing
251 solver
= sat
.Solver()
252 v1
= solver
.add_variable("v1")
253 v2
= solver
.add_variable("v2")
254 v3
= solver
.add_variable("v3")
255 solver
.at_most_one([v1
, v2
])
256 solver
.add_clause([v1
, sat
.neg(v3
)])
257 solver
.add_clause([v2
, sat
.neg(v3
)])
258 solver
.add_clause([v1
, v3
])
259 solver
.run_solver(lambda: v3
)
261 def testFailState(self
):
262 # If we can't select a valid combination,
263 # try to select as many as we can.
264 s
= assertSelection("prog-FAIL", """
269 prog[1,2] => liba 1 2
270 liba[1,2] => libb 1 2
271 libb[1,2] => libc 0 0
275 for iface
, impl
in s
.selections
.iteritems():
276 if impl
is not None: impl
= impl
.get_version()
277 selected
[iface
.uri
.rsplit('/', 1)[1]] = impl
285 suite
= unittest
.makeSuite(TestSAT
)
286 if __name__
== '__main__':
287 sys
.argv
.append('-v')