[pypy-svn] r25415 - in pypy/dist/pypy/objspace/constraint: . test

afayolle at codespeak.net afayolle at codespeak.net
Wed Apr 5 20:19:23 CEST 2006


Author: afayolle
Date: Wed Apr  5 20:19:22 2006
New Revision: 25415

Modified:
   pypy/dist/pypy/objspace/constraint/domain.py
   pypy/dist/pypy/objspace/constraint/test/test_fd.py
Log:
wrapped most useful methods in domain.py

Modified: pypy/dist/pypy/objspace/constraint/domain.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/domain.py	(original)
+++ pypy/dist/pypy/objspace/constraint/domain.py	Wed Apr  5 20:19:22 2006
@@ -6,7 +6,6 @@
 
 from pypy.objspace.std.objspace import W_Object
 
-# ?
 from pypy.objspace.std.listobject import W_ListObject, W_TupleObject
 
 
@@ -21,19 +20,23 @@
 
     def __init__(self, space):
         self._space = space
-        self.__changed = 0
+        self.__changed = self._space.newbool(False)
 
     def w_reset_flags(self):
-        self.__changed = 0
+        self.__changed = self._space.newbool(False)
     
     def w_has_changed(self):
         return self.__changed
 
+    def w_size(self):
+        pass
+    
     def _value_removed(self):
         """The implementation of remove_value should call this method"""
-        self.__changed = 1
-        if self.size() == 0:
-            raise ConsistencyFailure()
+        self.__changed = self._space.newbool(True)
+        if self._space.eq_w(self.w_size(), self._space.newint(0)):
+            raise  OperationError(self._space.w_RuntimeError,
+                             self._space.wrap('ConsistencyFailure'))
 
 W_AbstractDomain.typedef = typedef.TypeDef("W_AbstractDomain",
     reset_flags = interp2app(W_AbstractDomain.w_reset_flags),
@@ -61,8 +64,9 @@
 
     def w_remove_values(self, w_values):
         """Remove values of domain and check for consistency"""
-        if w_values:
-            for val in w_values :
+        if self._space.is_true(self._space.gt(self._space.len(w_values),
+                                              self._space.newint(0))) :
+            for val in w_values.wrappeditems :
                 self._values.remove(val)
             self._value_removed()
     __delitem__ = w_remove_value
@@ -82,25 +86,28 @@
     
     def w_copy(self):
         """clone the domain"""
-        return W_FiniteDomain(self, self._space)
+        return W_FiniteDomain(self._space, self.w_get_values())
     
     def __repr__(self):
         return '<FD %s>' % str(self.get_values())
 
-    def __eq__(self, other):
-        if other is NoDom: return False
-        return self._values == other._values
-
-    def __ne__(self, other):
-        return not self == other
-
-    def intersection(self, other):
-        if other is None: return self.get_values()
-        return self._values & other._values
+    def __eq__(self, w_other):
+        if w_other is NoDom: return False
+        return self._values == w_other._values
+
+    def __ne__(self, w_other):
+        return not self == w_other
+
+    # FIXME: this does not work, but we don't need it yet
+    def w_intersection(self, w_other):
+        assert isinstance(w_other, W_FiniteDomain)
+        return self._space.newlist([x for x in (set(self.w_get_values()) & set(w_other.w_get_values()))])
 
 W_FiniteDomain.typedef = typedef.TypeDef("W_FiniteDomain",
+    W_AbstractDomain.typedef,
     remove_value = interp2app(W_FiniteDomain.w_remove_value),
     remove_values = interp2app(W_FiniteDomain.w_remove_values),
     get_values = interp2app(W_FiniteDomain.w_get_values),
     copy = interp2app(W_FiniteDomain.w_copy),
+#    intersection = interp2app(W_FiniteDomain.w_intersection),
     size = interp2app(W_FiniteDomain.w_size))

Modified: pypy/dist/pypy/objspace/constraint/test/test_fd.py
==============================================================================
--- pypy/dist/pypy/objspace/constraint/test/test_fd.py	(original)
+++ pypy/dist/pypy/objspace/constraint/test/test_fd.py	Wed Apr  5 20:19:22 2006
@@ -1,7 +1,5 @@
 from pypy.conftest import gettestobjspace
 
-class UnificationFailure(Exception):  pass
-
 class AppTest_FD(object):
 
     def setup_class(cls):
@@ -12,3 +10,55 @@
         assert fd.size() == 3
         assert set(fd.get_values()) == set([1, 2, 3])
         #fd2 = fd.copy()
+
+    def test_copy(self):
+        fd = FiniteDomain([1, 2, 3])
+        clone = fd.copy()
+        assert set(clone.get_values()) == set([1, 2, 3])
+
+    def test_remove_value(self):
+        fd = FiniteDomain([1, 2, 3])
+        fd.remove_value(2)
+        assert fd.size() == 2
+        assert set(fd.get_values()) == set([1, 3])
+        assert fd.has_changed()
+
+    def test_remove_all_values(self):
+        fd = FiniteDomain([3])
+        # FIXME: check this is a ConsistencyFailure
+        raises(Exception, fd.remove_value, 3) 
+        
+        
+    def test_remove_values(self):
+        fd = FiniteDomain([1, 2, 3])
+        fd.remove_values([1, 2])
+        assert fd.size() == 1
+        assert set(fd.get_values()) == set([3,])
+        assert fd.has_changed()
+
+    def test_remove_values_empty_list(self):
+        fd = FiniteDomain([1, 2, 3])
+        assert not(fd.has_changed())
+        fd.remove_values([])
+        assert fd.size() == 3
+        assert not(fd.has_changed())
+
+
+    def notest_logical_variable_in_domain(self):
+        '''Logical variables do not play well with sets, skip for now'''
+        X = newvar()
+        fd = FiniteDomain([X, 2])
+        assert fd.size() == 2
+        unify(X,42)
+        fd.remove_value(42) 
+        assert fd.size() == 1
+        assert fd.has_changed()
+
+    def notest_intersection(self):
+        """not used for now"""
+        fd1 = FiniteDomain([1, 2, 3])
+        fd2 = FiniteDomain([2,3,4])
+        assert fd1.intersection(fd2)  == set([2,3])
+        assert fd2.intersection(fd1)  == set([3,2])
+
+



More information about the Pypy-commit mailing list