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
16 logger
= logging
.getLogger()
19 def lookup_any(self
, digests
):
24 uri_prefix
= 'http://localhost/tests/'
27 def __init__(self
, n
):
32 def add_requires(self
, lib
, min_v
, max_v
):
33 self
.requires
.append((lib
, min_v
, max_v
))
36 def __init__(self
, name
):
40 def get_version(self
, version
):
41 if version
not in self
.versions
:
42 self
.versions
[version
] = Version(version
)
43 return self
.versions
[version
]
46 def child(parent
, name
, attrs
= None):
47 new
= qdom
.Element(XMLNS_IFACE
, name
, attrs
or {})
48 parent
.childNodes
.append(new
)
51 root
= qdom
.Element(XMLNS_IFACE
, 'interface', {'uri' : uri_prefix
+ self
.name
})
52 child(root
, 'name').content
= self
.name
53 child(root
, 'summary').content
= self
.name
56 for version
in self
.versions
.values():
59 'version': str(version
.n
),
62 attrs
['arch'] = version
.arch
63 impl
= child(root
, 'implementation', attrs
)
64 child(impl
, 'manifest-digest', {'sha1new': '1234'})
65 for lib
, min_v
, max_v
in version
.requires
:
66 req
= child(impl
, 'requires', {'interface': uri_prefix
+ lib
})
67 child(req
, 'version', {
68 'before': str(int(max_v
) + 1),
72 feed
= model
.ZeroInstallFeed(root
)
73 feed
.last_modified
= 1
81 def get_prog(self
, prog
):
82 if not prog
in self
.progs
:
83 self
.progs
[prog
] = Program(prog
)
84 return self
.progs
[prog
]
86 def get_interface(self
, uri
):
87 if uri
not in self
.interfaces
:
88 iface
= model
.Interface(uri
)
89 iface
._main
_feed
= self
.progs
[uri
.rsplit('/', 1)[1]].build_feed()
90 self
.interfaces
[uri
] = iface
91 return self
.interfaces
[uri
]
93 def assertSelection(expected
, repo
):
96 expected
= [tuple(e
.strip().split('-')) for e
in expected
.split(",")]
98 for line
in repo
.split('\n'):
100 if not line
: continue
102 prog
, versions
= line
.split(':')
105 prog
, prog_arch
= prog
.split()
108 for v
in versions
.split():
109 cache
.get_prog(prog
).get_version(v
).arch
= prog_arch
111 prog
, requires
= line
.split('=>')
112 prog
, version_range
= prog
.strip().split('[')
113 lib
, min_v
, max_v
= requires
.split()
114 assert version_range
.endswith(']')
115 version_range
= version_range
[:-1]
116 if ',' in version_range
:
117 min_p
, max_p
= map(int, version_range
.split(','))
118 prog_versions
= range(min_p
, max_p
+ 1)
120 prog_versions
= [int(version_range
)]
121 for prog_version
in prog_versions
:
122 cache
.get_prog(prog
).get_version(str(prog_version
)).add_requires(lib
, min_v
, max_v
)
124 root
= uri_prefix
+ expected
[0][0]
125 s
= Solver(model
.network_offline
, cache
, stores
)
126 s
.solve(root
, arch
.get_architecture('Linux', 'x86_64'))
128 if expected
[0][1] == 'FAIL':
134 for iface
, impl
in s
.selections
.iteritems():
135 actual
.append(((iface
.uri
.rsplit('/', 1)[1]), impl
.get_version()))
139 if expected
!= actual
:
140 raise Exception("Solve failed:\nExpected: %s\n Actual: %s" % (expected
, actual
))
142 class TestSAT(BaseTest
):
143 def testTrivial(self
):
144 assertSelection("prog-2", """
148 def testSimple(self
):
149 assertSelection("prog-5, liba-5", """
157 def testBestImpossible(self
):
158 assertSelection("prog-1", """
165 assertSelection("prog-1", """
166 prog: 1 2 3 4 5 6 7 8 9
167 liba: 1 2 3 4 5 6 7 8 9
168 libb: 1 2 3 4 5 6 7 8 9
169 libc: 1 2 3 4 5 6 7 8 9
170 libd: 1 2 3 4 5 6 7 8 9
172 prog[2,9] => liba 1 9
173 liba[1,9] => libb 1 9
174 libb[1,9] => libc 1 9
175 libc[1,9] => libd 1 9
176 libd[1,9] => libe 0 0
179 def testNoSolution(self
):
180 assertSelection("prog-FAIL", """
183 prog[1,3] => liba 2 3
186 def testBacktrackSimple(self
):
187 # We initially try liba-3 before learning that it
188 # is incompatible and backtracking.
189 # We learn that liba-3 doesn't work ever.
190 assertSelection("prog-1, liba-2", """
196 def testBacktrackLocal(self
):
197 # We initially try liba-3 before learning that it
198 # is incompatible and backtracking.
199 # We learn that liba-3 doesn't work with prog-1.
200 assertSelection("prog-2, liba-2", """
203 prog[1,2] => liba 1 2
206 def testLearning(self
):
207 # Prog-2 depends on libb and libz, but we can't have both
208 # at once. The learning means we don't have to explore every
209 # possible combination of liba and libb.
210 assertSelection("prog-1", """
213 libb Linux-i486: 1 2 3
214 libz Linux-x86_64: 1 2
217 liba[1,3] => libb 1 3
220 suite
= unittest
.makeSuite(TestSAT
)
221 if __name__
== '__main__':
222 sys
.argv
.append('-v')