[pypy-svn] r22896 - pypy/dist/pypy/lib/logic

auc at codespeak.net auc at codespeak.net
Tue Jan 31 15:35:49 CET 2006


Author: auc
Date: Tue Jan 31 15:35:45 2006
New Revision: 22896

Modified:
   pypy/dist/pypy/lib/logic/test_computationspace.py
Log:
don't use global stuff from computationspace.py


Modified: pypy/dist/pypy/lib/logic/test_computationspace.py
==============================================================================
--- pypy/dist/pypy/lib/logic/test_computationspace.py	(original)
+++ pypy/dist/pypy/lib/logic/test_computationspace.py	Tue Jan 31 15:35:45 2006
@@ -2,13 +2,12 @@
 
 import variable as v
 import constraint as c
-import computationspace as cs
+import computationspace as space
 import distributor as di
-from problems import *
+import problems
 from py.test import raises
 
-
-#-- meat ------------------------
+#-- utility ---------------------
 
 class FunThread(Thread):
 
@@ -20,127 +19,139 @@
     def run(self):
         self.fun(self, *self.args)
 
+#-- meat ------------------------
+
 class TestStoreUnification:
     
-    def setup_method(self, meth):
-        cs._cs = cs.ComputationSpace(dummy_problem)
 
     def test_already_in_store(self):
-        x = cs.var('x')
-        raises(v.AlreadyInStore, cs.var, 'x')
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x = sp.var('x')
+        raises(v.AlreadyInStore, sp.var, 'x')
 
     def test_already_bound(self):
-        x = cs.var('x')
-        cs.bind(x, 42)
-        raises(cs.AlreadyBound, cs.bind, x, 42)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x = sp.var('x')
+        sp.bind(x, 42)
+        raises(space.AlreadyBound, sp.bind, x, 42)
 
     def test_bind_var_var(self):
-        x = cs.var('x')
-        y = cs.var('y')
-        z = cs.var('z')
-        cs.bind(x, z)
-        assert x.val == cs.EqSet([x, z])
-        assert y.val == cs.EqSet([y])
-        assert z.val == cs.EqSet([x, z])
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x = sp.var('x')
+        y = sp.var('y')
+        z = sp.var('z')
+        sp.bind(x, z)
+        assert x.val == space.EqSet([x, z])
+        assert y.val == space.EqSet([y])
+        assert z.val == space.EqSet([x, z])
 
     def test_bind_var_val(self):
-        x, y, z = cs.var('x'), cs.var('y'), cs.var('z')
-        cs.bind(x, z)
-        cs.bind(y, 42)
-        cs.bind(z, 3.14)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x, y, z = sp.var('x'), sp.var('y'), sp.var('z')
+        sp.bind(x, z)
+        sp.bind(y, 42)
+        sp.bind(z, 3.14)
         assert x.val == 3.14
         assert y.val == 42
         assert z.val == 3.14
 
     def test_unify_same(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        cs.bind(x, [42, z])
-        cs.bind(y, [z, 42])
-        cs.bind(w, [z, 43])
-        raises(cs.UnificationFailure, cs.unify, x, w)
-        cs.unify(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        sp.bind(x, [42, z])
+        sp.bind(y, [z, 42])
+        sp.bind(w, [z, 43])
+        raises(space.UnificationFailure, sp.unify, x, w)
+        sp.unify(x, y)
         assert z.val == 42
 
     def test_double_unification(self):
-        x, y, z = (cs.var('x'), cs.var('y'),
-                   cs.var('z'))
-        cs.bind(x, 42)
-        cs.bind(y, z)
-        cs.unify(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x, y, z = (sp.var('x'), sp.var('y'),
+                   sp.var('z'))
+        sp.bind(x, 42)
+        sp.bind(y, z)
+        sp.unify(x, y)
         assert z.val == 42
-        cs.unify(x, y)
+        sp.unify(x, y)
         assert (z.val == x.val) and (x.val == y.val)
 
 
     def test_unify_values(self):
-        x, y = cs.var('x'), cs.var('y')
-        cs.bind(x, [1, 2, 3])
-        cs.bind(y, [1, 2, 3])
-        cs.unify(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x, y = sp.var('x'), sp.var('y')
+        sp.bind(x, [1, 2, 3])
+        sp.bind(y, [1, 2, 3])
+        sp.unify(x, y)
         assert x.val == [1, 2, 3]
         assert y.val == [1, 2, 3]
 
     def test_unify_lists_success(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        cs.bind(x, [42, z])
-        cs.bind(y, [w, 44])
-        cs.unify(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        sp.bind(x, [42, z])
+        sp.bind(y, [w, 44])
+        sp.unify(x, y)
         assert x.val == [42, z]
         assert y.val == [w, 44]
         assert z.val == 44
         assert w.val == 42
 
     def test_unify_dicts_success(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        cs.bind(x, {1:42, 2:z})
-        cs.bind(y, {1:w,  2:44})
-        cs.unify(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        sp.bind(x, {1:42, 2:z})
+        sp.bind(y, {1:w,  2:44})
+        sp.unify(x, y)
         assert x.val == {1:42, 2:z}
         assert y.val == {1:w,  2:44}
         assert z.val == 44
         assert w.val == 42
 
     def test_unify_failure(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
-        cs.bind(x, [42, z])
-        cs.bind(y, [z, 44])
-        raises(cs.UnificationFailure, cs.unify, x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
+        sp.bind(x, [42, z])
+        sp.bind(y, [z, 44])
+        raises(space.UnificationFailure, sp.unify, x, y)
         # check store consistency
         assert x.val == [42, z]
         assert y.val == [z, 44]
-        assert z.val == cs.EqSet([z])
+        assert z.val == space.EqSet([z])
 
     def test_unify_failure2(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        cs.bind(x, [42, z])
-        cs.bind(y, [w, 44])
-        cs.bind(z, w)
-        assert cs._cs.in_transaction == False
-        raises(cs.UnificationFailure, cs.unify, x, y)
-        assert cs._cs.in_transaction == False
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        sp.bind(x, [42, z])
+        sp.bind(y, [w, 44])
+        sp.bind(z, w)
+        assert sp.in_transaction == False
+        raises(space.UnificationFailure, sp.unify, x, y)
+        assert sp.in_transaction == False
         # check store consistency
         assert x.val == [42, z]
         assert y.val == [w, 44]
-        assert z.val == cs.EqSet([z,w])
-        assert w.val == cs.EqSet([z,w])
+        assert z.val == space.EqSet([z,w])
+        assert w.val == space.EqSet([z,w])
 
     def test_unify_circular(self):
-        x, y, z, w, a, b = (cs.var('x'), cs.var('y'),
-                            cs.var('z'), cs.var('w'),
-                            cs.var('a'), cs.var('b'))
-        cs.bind(x, [y])
-        cs.bind(y, [x])
-        raises(cs.UnificationFailure, cs.unify, x, y)
-        cs.bind(z, [1, w])
-        cs.bind(w, [z, 2])
-        raises(cs.UnificationFailure, cs.unify, z, w)
-        cs.bind(a, {1:42, 2:b})
-        cs.bind(b, {1:a,  2:42})
-        raises(cs.UnificationFailure, cs.unify, a, b)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x, y, z, w, a, b = (sp.var('x'), sp.var('y'),
+                            sp.var('z'), sp.var('w'),
+                            sp.var('a'), sp.var('b'))
+        sp.bind(x, [y])
+        sp.bind(y, [x])
+        raises(space.UnificationFailure, sp.unify, x, y)
+        sp.bind(z, [1, w])
+        sp.bind(w, [z, 2])
+        raises(space.UnificationFailure, sp.unify, z, w)
+        sp.bind(a, {1:42, 2:b})
+        sp.bind(b, {1:a,  2:42})
+        raises(space.UnificationFailure, sp.unify, a, b)
         # check store consistency
         assert x.val == [y]
         assert y.val == [x]
@@ -151,11 +162,12 @@
         
         
     def test_threads_creating_vars(self):
+        sp = space.ComputationSpace(problems.dummy_problem)
         def create_var(thread, *args):
-            x = cs.var('x')
+            x = sp.var('x')
 
         def create_var2(thread, *args):
-            raises(v.AlreadyExists, cs.var, 'x')
+            raises(v.AlreadyExists, sp.var, 'x')
 
         t1, t2 = (FunThread(create_var),
                   FunThread(create_var2))
@@ -164,27 +176,28 @@
 
 
     def test_threads_binding_vars(self):
+        sp = space.ComputationSpace(problems.dummy_problem)
 
         def do_stuff(thread, var, val):
             thread.raised = False
             try:
                 # pb. with TLS (thread-local-stuff) in
                 # cs class
-                cs.bind(var, val)
+                sp.bind(var, val)
             except Exception, e:
                 print e
                 thread.raised = True
-                assert isinstance(e, cs.AlreadyBound)
+                assert isinstance(e, space.AlreadyBound)
             
-        x = cs.var('x')
+        x = sp.var('x')
         vars_ = []
         for nvar in range(1000):
-            v = cs.var('x-'+str(nvar))
-            cs.bind(x, v)
+            v = sp.var('x-'+str(nvar))
+            sp.bind(x, v)
             vars_.append(v)
             
         for var in vars_:
-            assert var in cs._cs.vars
+            assert var in sp.vars
             assert var.val == x.val
 
         t1, t2 = (FunThread(do_stuff, x, 42),
@@ -195,144 +208,183 @@
         t2.join()
         #check that every var is really bound to 42 or 43
         for var in vars_:
-            assert var in cs._cs.vars
+            assert var in sp.vars
             assert var.val == x.val
         assert (t2.raised and not t1.raised) or \
                (t1.raised and not t2.raised)
     
 
+    def test_threads_waiting_for_unbound_var(self):
+        sp = space.ComputationSpace(problems.dummy_problem)
+        import time
+        
+        def near(v1, v2, err):
+            return abs(v1 - v2) < err
+        
+        start_time = time.time()
+
+        def wait_on_unbound(thread, var, start_time):
+            thread.val = var.get()
+            thread.waited = time.time() - start_time
+
+        x = sp.var('x')
+        t1, t2 = (FunThread(wait_on_unbound, x, start_time),
+                  FunThread(wait_on_unbound, x, start_time))
+        t1.start()
+        t2.start()
+        time.sleep(1)
+        sp.bind(x, 42)
+        t1.join()
+        t2.join()
+        assert t1.val == 42
+        assert t2.val == 42
+        assert near(t1.waited, 1, .1)
+        assert near(t2.waited, 1, .1)
+
+
     def test_set_var_domain(self):
-        x = cs.var('x')
-        cs.set_domain(x, [1, 3, 5])
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 3, 5])
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x = sp.var('x')
+        sp.set_domain(x, [1, 3, 5])
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 3, 5])
 
     def test_bind_with_domain(self):
-        x = cs.var('x')
-        cs.set_domain(x, [1, 2, 3])
-        raises(cs.OutOfDomain, cs.bind, x, 42)
-        cs.bind(x, 3)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x = sp.var('x')
+        sp.set_domain(x, [1, 2, 3])
+        raises(space.OutOfDomain, sp.bind, x, 42)
+        sp.bind(x, 3)
         assert x.val == 3
 
     def test_bind_with_incompatible_domains(self):
-        x, y = cs.var('x'), cs.var('y')
-        cs.set_domain(x, [1, 2])
-        cs.set_domain(y, [3, 4])
-        raises(cs.IncompatibleDomains, cs.bind, x, y)
-        cs.set_domain(y, [2, 4])
-        cs.bind(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x, y = sp.var('x'), sp.var('y')
+        sp.set_domain(x, [1, 2])
+        sp.set_domain(y, [3, 4])
+        raises(space.IncompatibleDomains, sp.bind, x, y)
+        sp.set_domain(y, [2, 4])
+        sp.bind(x, y)
         # check x and y are in the same equiv. set
         assert x.val == y.val
 
 
     def test_unify_with_domains(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
-        cs.bind(x, [42, z])
-        cs.bind(y, [z, 42])
-        cs.set_domain(z, [1, 2, 3])
-        raises(cs.UnificationFailure, cs.unify, x, y)
-        cs.set_domain(z, [41, 42, 43])
-        cs.unify(x, y)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
+        sp.bind(x, [42, z])
+        sp.bind(y, [z, 42])
+        sp.set_domain(z, [1, 2, 3])
+        raises(space.UnificationFailure, sp.unify, x, y)
+        sp.set_domain(z, [41, 42, 43])
+        sp.unify(x, y)
         assert z.val == 42
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([41, 42, 43])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([41, 42, 43])
 
     def test_add_constraint(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
         raises(c.DomainlessVariables,
-               c.Expression, cs._cs, [x, y, z], 'x == y + z')
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        cs.add_constraint(k)
-        assert k in cs._cs.constraints
+               c.Expression, sp, [x, y, z], 'x == y + z')
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        k = c.Expression(sp, [x, y, z], 'x == y + z')
+        sp.add_constraint(k)
+        assert k in sp.constraints
 
     def test_narrowing_domains_failure(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        k = c.Expression(sp, [x, y, z], 'x == y + z')
         raises(c.ConsistencyFailure, k.narrow)
 
     def test_narrowing_domains_success(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        k = c.Expression(sp, [x, y, z], 'x == y + z')
         k.narrow()
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3])
+        assert x.cs_get_dom(sp) == c.FiniteDomain([5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3])
 
     def test_compute_dependant_vars(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        w.cs_set_dom(cs._cs, c.FiniteDomain([1, 4, 5]))
-        k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        k2 = c.Expression(cs._cs, [z, w], 'z < w')
-        cs.add_constraint(k1)
-        cs.add_constraint(k2)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        w.cs_set_dom(sp, c.FiniteDomain([1, 4, 5]))
+        k1 = c.Expression(sp, [x, y, z], 'x == y + z')
+        k2 = c.Expression(sp, [z, w], 'z < w')
+        sp.add_constraint(k1)
+        sp.add_constraint(k2)
         varset = set()
         constset = set()
-        cs._cs._compute_dependant_vars(k1, varset, constset)
+        sp._compute_dependant_vars(k1, varset, constset)
         assert varset == set([x, y, z, w])
         assert constset == set([k1, k2])
 
     def test_store_satisfiable_success(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        cs.add_constraint(k)
-        assert cs.satisfiable(k) == True
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        k = c.Expression(sp, [x, y, z], 'x == y + z')
+        sp.add_constraint(k)
+        assert sp.satisfiable(k) == True
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
         
     def test_store_satisfiable_failure(self):
-        x,y,z = cs.var('x'), cs.var('y'), cs.var('z')
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        k = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        cs.add_constraint(k)
-        assert cs.satisfiable(k) == False
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z = sp.var('x'), sp.var('y'), sp.var('z')
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        k = c.Expression(sp, [x, y, z], 'x == y + z')
+        sp.add_constraint(k)
+        assert sp.satisfiable(k) == False
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
 
     def test_satisfiable_many_const_success(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        w.cs_set_dom(cs._cs, c.FiniteDomain([1, 4, 5]))
-        k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        k2 = c.Expression(cs._cs, [z, w], 'z < w')
-        cs.add_constraint(k1)
-        cs.add_constraint(k2)
-        assert cs.satisfiable(k1) == True
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1, 4, 5])
-        assert cs.satisfiable(k2) == True
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1, 4, 5])
-        narrowed_doms = cs.get_satisfying_domains(k1)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        w.cs_set_dom(sp, c.FiniteDomain([1, 4, 5]))
+        k1 = c.Expression(sp, [x, y, z], 'x == y + z')
+        k2 = c.Expression(sp, [z, w], 'z < w')
+        sp.add_constraint(k1)
+        sp.add_constraint(k2)
+        assert sp.satisfiable(k1) == True
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([1, 4, 5])
+        assert sp.satisfiable(k2) == True
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([1, 4, 5])
+        narrowed_doms = sp.get_satisfying_domains(k1)
         assert narrowed_doms == {x:c.FiniteDomain([5]),
                                  y:c.FiniteDomain([2]),
                                  z:c.FiniteDomain([3]),
                                  w:c.FiniteDomain([4, 5])}
-        narrowed_doms = cs.get_satisfying_domains(k2)
+        narrowed_doms = sp.get_satisfying_domains(k2)
         assert narrowed_doms == {x:c.FiniteDomain([5]),
                                  y:c.FiniteDomain([2]),
                                  z:c.FiniteDomain([3]),
@@ -340,70 +392,73 @@
 
 
     def test_satisfiable_many_const_failure(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        w.cs_set_dom(cs._cs, c.FiniteDomain([1]))
-        k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        k2 = c.Expression(cs._cs, [z, w], 'z < w')
-        cs.add_constraint(k1)
-        cs.add_constraint(k2)
-        assert cs.satisfiable(k1) == False
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
-        assert cs.satisfiable(k2) == False
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
-        narrowed_doms = cs.get_satisfying_domains(k1)
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        w.cs_set_dom(sp, c.FiniteDomain([1]))
+        k1 = c.Expression(sp, [x, y, z], 'x == y + z')
+        k2 = c.Expression(sp, [z, w], 'z < w')
+        sp.add_constraint(k1)
+        sp.add_constraint(k2)
+        assert sp.satisfiable(k1) == False
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([1])
+        assert sp.satisfiable(k2) == False
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([1])
+        narrowed_doms = sp.get_satisfying_domains(k1)
         assert narrowed_doms == {}
-        narrowed_doms = cs.get_satisfying_domains(k2)
+        narrowed_doms = sp.get_satisfying_domains(k2)
         assert narrowed_doms == {}
 
     def test_satisfy_many_const_failure(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        w.cs_set_dom(cs._cs, c.FiniteDomain([1]))
-        k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        k2 = c.Expression(cs._cs, [z, w], 'z < w')
-        cs.add_constraint(k1)
-        cs.add_constraint(k2)
-        raises(cs.ConsistencyFailure, cs.satisfy, k1)
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
-        raises(cs.ConsistencyFailure, cs.satisfy, k2)
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([1, 2, 5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2, 3])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3, 4])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([1])
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        w.cs_set_dom(sp, c.FiniteDomain([1]))
+        k1 = c.Expression(sp, [x, y, z], 'x == y + z')
+        k2 = c.Expression(sp, [z, w], 'z < w')
+        sp.add_constraint(k1)
+        sp.add_constraint(k2)
+        raises(space.ConsistencyFailure, sp.satisfy, k1)
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([1])
+        raises(space.ConsistencyFailure, sp.satisfy, k2)
+        assert x.cs_get_dom(sp) == c.FiniteDomain([1, 2, 5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2, 3])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3, 4])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([1])
         
     def test_satisfy_many_const_success(self):
-        x,y,z,w = (cs.var('x'), cs.var('y'),
-                   cs.var('z'), cs.var('w'))
-        x.cs_set_dom(cs._cs, c.FiniteDomain([1, 2, 5]))
-        y.cs_set_dom(cs._cs, c.FiniteDomain([2, 3]))
-        z.cs_set_dom(cs._cs, c.FiniteDomain([3, 4]))
-        w.cs_set_dom(cs._cs, c.FiniteDomain([1, 4, 5]))
-        k1 = c.Expression(cs._cs, [x, y, z], 'x == y + z')
-        k2 = c.Expression(cs._cs, [z, w], 'z < w')
-        cs.add_constraint(k1)
-        cs.add_constraint(k2)
-        cs.satisfy(k2)
-        print x.cs_get_dom(cs._cs)
-        assert x.cs_get_dom(cs._cs) == c.FiniteDomain([5])
-        assert y.cs_get_dom(cs._cs) == c.FiniteDomain([2])
-        assert z.cs_get_dom(cs._cs) == c.FiniteDomain([3])
-        assert w.cs_get_dom(cs._cs) == c.FiniteDomain([4, 5])
+        sp = space.ComputationSpace(problems.dummy_problem)
+        x,y,z,w = (sp.var('x'), sp.var('y'),
+                   sp.var('z'), sp.var('w'))
+        x.cs_set_dom(sp, c.FiniteDomain([1, 2, 5]))
+        y.cs_set_dom(sp, c.FiniteDomain([2, 3]))
+        z.cs_set_dom(sp, c.FiniteDomain([3, 4]))
+        w.cs_set_dom(sp, c.FiniteDomain([1, 4, 5]))
+        k1 = c.Expression(sp, [x, y, z], 'x == y + z')
+        k2 = c.Expression(sp, [z, w], 'z < w')
+        sp.add_constraint(k1)
+        sp.add_constraint(k2)
+        sp.satisfy(k2)
+        print x.cs_get_dom(sp)
+        assert x.cs_get_dom(sp) == c.FiniteDomain([5])
+        assert y.cs_get_dom(sp) == c.FiniteDomain([2])
+        assert z.cs_get_dom(sp) == c.FiniteDomain([3])
+        assert w.cs_get_dom(sp) == c.FiniteDomain([4, 5])
 
 
 class TestComputationSpace:
@@ -412,30 +467,30 @@
         pass
 
     def test_bind_cs_root(self):
-        spc = cs.ComputationSpace(satisfiable_problem)
+        spc = space.ComputationSpace(problems.satisfiable_problem)
         assert '__root__' in spc.names
         assert set(['x', 'y', 'w']) == \
                set([var.name for var in spc.root.val])
 
     def test_ask_success(self):
-        spc = cs.ComputationSpace(one_solution_problem)
-        assert spc.ask() == cs.Succeeded
+        spc = space.ComputationSpace(problems.one_solution_problem)
+        assert spc.ask() == space.Succeeded
 
     def test_double_ask(self):
-        spc = cs.ComputationSpace(one_solution_problem)
-        assert spc.ask() == cs.Succeeded
-        assert spc.ask() == cs.Succeeded
+        spc = space.ComputationSpace(problems.one_solution_problem)
+        assert spc.ask() == space.Succeeded
+        assert spc.ask() == space.Succeeded
         
     def test_ask_failure(self):
-        spc = cs.ComputationSpace(unsatisfiable_problem)
-        assert spc.ask() == cs.Failed
+        spc = space.ComputationSpace(problems.unsatisfiable_problem)
+        assert spc.ask() == space.Failed
 
     def test_ask_alternatives(self):
-        spc = cs.ComputationSpace(satisfiable_problem)
-        assert spc.ask() == cs.Alternatives(2)
+        spc = space.ComputationSpace(problems.satisfiable_problem)
+        assert spc.ask() == space.Alternatives(2)
 
     def test_distribute(self):
-        spc = cs.ComputationSpace(satisfiable_problem)
+        spc = space.ComputationSpace(problems.satisfiable_problem)
         new_domains = [tuple(d.items()) for d in
                        spc.distributor.distribute()]
         x, y, z, w = (spc.get_var_by_name('x'),
@@ -464,14 +519,14 @@
         #assert set(new_domains) == set(expected_domains)
 
     def test_clone(self):
-        spc = cs.ComputationSpace(satisfiable_problem)
+        spc = space.ComputationSpace(problems.satisfiable_problem)
         w = spc.get_var_by_name('w')
-        assert spc.ask() == cs.Alternatives(2)
+        assert spc.ask() == space.Alternatives(2)
         new_spc = spc.clone()
         new_spc.add_constraint(c.Expression(new_spc, [w], 'w == 5'))
         new_spc._process()
-        assert spc.ask() == cs.Alternatives(2)
-        assert new_spc.ask() == cs.Succeeded
+        assert spc.ask() == space.Alternatives(2)
+        assert new_spc.ask() == space.Succeeded
         assert w.cs_get_dom(spc) == c.FiniteDomain([5, 6, 7])
         assert w.cs_get_dom(new_spc) == c.FiniteDomain([5])
         spc.commit(0)



More information about the Pypy-commit mailing list