[pypy-svn] r23795 - pypy/dist/pypy/module/stackless/test

mwh at codespeak.net mwh at codespeak.net
Wed Mar 1 00:50:35 CET 2006


Author: mwh
Date: Wed Mar  1 00:50:32 2006
New Revision: 23795

Modified:
   pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py
Log:
(moshez, mwh)
Add a not-run test that uses coroutines to compare the 
preorder traversal of two trees (and is named to suggest
it does something else entirely)


Modified: pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py
==============================================================================
--- pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py	(original)
+++ pypy/dist/pypy/module/stackless/test/test_interp_coroutine.py	Wed Mar  1 00:50:32 2006
@@ -209,3 +209,96 @@
     
     data = wrap_stackless_function(f)
     assert int(data.strip()) == 4711
+
+def dont_test_tree_differ():
+    class Node:
+        def __init__(self, value, left=None, right=None):
+            self.value = value
+            self.left = left
+            self.right = right
+        def __repr__(self):
+            return 'Node(%r, %r, %r)'%(self.value, self.left, self.right)
+
+    tree1 = Node(1, Node(2))
+    tree2 = Node(1, Node(2))
+    
+    def eq(t1, t2):
+        if t1 is None:
+            return t2 is None
+        if t1.value != t2.value:
+            return False
+        if t1.left is None:
+            if t2.left is None:
+                return eq(t1.right, t2.right)
+            else:
+                return False
+        if t2.right is None:
+            if t1.right is None:
+                return eq(t1.left, t2.left)
+            else:
+                return False
+        else:
+            return eq(t1.left, t2.left) and eq(t1.right, t2.right)
+
+#    assert not eq(tree1, tree2)
+
+    class Super:
+        pass
+    class Producer(Super):
+        def __init__(self, tree, objects, consumer):
+            self.tree = tree
+            self.objects = objects
+            self.consumer = consumer
+        def produce(self, t):
+            if t is None:
+                return
+            self.objects.append(t.value)
+            self.consumer.switch()
+            self.produce(t.left)
+            self.produce(t.right)
+        def call(self):
+            self.produce(self.tree)
+            while 1:
+                self.consumer.switch()
+    class Consumer(Super):
+        def __init__(self, tree, objects, producer):
+            self.tree = tree
+            self.objects = objects
+            self.producer = producer
+        def consume(self, t):
+            if t is None:
+                return True
+            self.producer.switch()
+            if not self.objects:
+                return False
+            if self.objects.pop(0) != t.value:
+                return False
+            if not self.consume(t.left):
+                return False
+            return self.consume(t.right)
+            
+        def call(self):
+            self.result = self.consume(self.tree)
+            costate.main.switch()
+
+    def eq2(t1, t2):
+        objects = []
+        producer = Coroutine()
+        consumer = Coroutine()
+        
+        producer.bind(Producer(t1, objects, consumer))
+        cons = Consumer(t2, objects, producer)
+        consumer.bind(cons)
+
+        consumer.switch()
+
+        return cons.result
+
+    def ep():
+        return eq2(tree1, tree2)
+
+    output = wrap_stackless_function(ep)
+    print '!!!!!!', output
+    assert False
+            
+            



More information about the Pypy-commit mailing list