[pypy-svn] r48717 - in pypy/branch/ropes-unicode/pypy/objspace/std: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Thu Nov 15 19:01:12 CET 2007


Author: cfbolz
Date: Thu Nov 15 19:01:12 2007
New Revision: 48717

Modified:
   pypy/branch/ropes-unicode/pypy/objspace/std/rope.py
   pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py
Log:
tests and more fixes for unicode ops with ropes


Modified: pypy/branch/ropes-unicode/pypy/objspace/std/rope.py
==============================================================================
--- pypy/branch/ropes-unicode/pypy/objspace/std/rope.py	(original)
+++ pypy/branch/ropes-unicode/pypy/objspace/std/rope.py	Thu Nov 15 19:01:12 2007
@@ -109,6 +109,7 @@
 
 class LiteralStringNode(LiteralNode):
     def __init__(self, s):
+        assert isinstance(s, str)
         self.s = s
         is_ascii = True
         for c in s:
@@ -190,8 +191,9 @@
 del i
 
 
-class LiteralUnicodeNode(StringNode):
+class LiteralUnicodeNode(LiteralNode):
     def __init__(self, u):
+        assert isinstance(u, unicode)
         self.u = u
     
     def length(self):
@@ -228,7 +230,7 @@
         return LiteralUnicodeNode(self.u[start:stop])
 
     def find_int(self, what, start, stop):
-        result = node.u.find(unichr(what), start, stop)
+        result = self.u.find(unichr(what), start, stop)
         if result == -1:
             return -1
         return result
@@ -236,7 +238,7 @@
     def literal_concat(self, other):
         if (isinstance(other, LiteralUnicodeNode) and
             len(other.u) + len(self.u) < NEW_NODE_WHEN_LENGTH):
-            return LiteralStringNode(self.u + other.u)
+            return LiteralUnicodeNode(self.u + other.u)
         elif (isinstance(other, LiteralStringNode) and
               len(other.s) + len(self.u) < NEW_NODE_WHEN_LENGTH and
               len(other.s) < CONVERT_WHEN_SMALLER):
@@ -247,11 +249,11 @@
         if self in seen:
             return
         seen[self] = True
-        addinfo = str(self.s).replace('"', "'") or "_"
+        addinfo = repr(self.u).replace('"', "'") or "_"
         if len(addinfo) > 10:
             addinfo = addinfo[:3] + "..." + addinfo[-3:]
         yield ('"%s" [shape=box,label="length: %s\\n%s"];' % (
-            id(self), len(self.s),
+            id(self), len(self.u),
             repr(addinfo).replace('"', '').replace("\\", "\\\\")))
 
 class BinaryConcatNode(StringNode):
@@ -322,7 +324,7 @@
 
     def flatten_unicode(self):
         f = fringe(self)
-        return "".join([node.flatten_string() for node in f])
+        return "".join([node.flatten_unicode() for node in f])
  
     def hash_part(self):
         h = self.hash_cache
@@ -1038,8 +1040,6 @@
             self.restart_positions = None
         else:
             self.restart_positions = construct_restart_positions_node(sub)
-            # XXX
-            assert self.restart_positions == construct_restart_positions(sub.flatten_string())
         self.start = start
         if stop == -1 or stop > len1:
             stop = len1

Modified: pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py
==============================================================================
--- pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py	(original)
+++ pypy/branch/ropes-unicode/pypy/objspace/std/test/test_rope.py	Thu Nov 15 19:01:12 2007
@@ -380,8 +380,31 @@
             assert r1 == r2
             start = r2 + max(len(searchstring), 1)
 
+
+def test_find_iterator_unicode():
+    for searchstring in [
+        u"\uAAAA\uBBBB\uCCCC", u"\uAAAA", u"", u"\u6666",
+        u"\u6666\u7777\u8888",
+        u"\uAAAA\uBBBB\uAAAA\uBBBB\uAAAA\uBBBB\uCCCC\uAAAA\uBBBB\uCCCC\uAAAA\uBBBB\uBBBB"]:
+        node = join(LiteralUnicodeNode(searchstring),
+                    [LiteralUnicodeNode(u"\ucccc\udddd" * i)
+                        for i in range(1, 10)])
+        iter = FindIterator(node, LiteralUnicodeNode(searchstring))
+        s = node.flatten_unicode()
+        assert s == searchstring.join([u"\ucccc\udddd" * i for i in range(1, 10)])
+        start = 0
+        while 1:
+            r2 = s.find(searchstring, start)
+            try:
+                r1 = iter.next()
+            except StopIteration:
+                assert r2 == -1
+                break
+            assert r1 == r2
+            start = r2 + max(len(searchstring), 1)
+
 def test_hash():
-    from pypy.rlib.rarithmetic import _hash_string, intmask
+    from pypy.rlib.rarithmetic import intmask
     for i in range(10):
         rope, _ = make_random_string()
         if rope.length() == 0:
@@ -489,13 +512,26 @@
             assert s.hash_part() == h
             assert s.hash_part() == h
 
+def test_hash_part_unicode():
+    a, st = make_random_string(unicode=True)
+    h = a.hash_part()
+    for split in range(1, len(st) - 1):
+        s1 = LiteralUnicodeNode(st[:split])
+        s2 = LiteralUnicodeNode(st[split:])
+        s = BinaryConcatNode(s1, s2)
+        if h is None:
+            h = s.hash_part()
+        else:
+            # try twice due to caching reasons
+            assert s.hash_part() == h
+            assert s.hash_part() == h
+
 def test_hash_part_more():
     for i in range(100):
         rope, st = make_random_string()
         h = rope.hash_part()
         assert LiteralStringNode(st).hash_part() == h
 
-
 def test_equality():
     l = [make_random_string() for i in range(3)]
     l.append((LiteralStringNode(""), ""))
@@ -503,6 +539,13 @@
         for rope2, st2 in l:
             assert eq(rope1, rope2) == (st1 == st2)
 
+def test_equality_unicode():
+    l = [make_random_string() for i in range(3)]
+    l.append((LiteralStringNode(""), ""))
+    for rope1, st1 in l:
+        for rope2, st2 in l:
+            assert eq(rope1, rope2) == (st1 == st2)
+
 def test_compare_random():
     l = [make_random_string() for i in range(3)]
     l.append((LiteralStringNode(""), ""))
@@ -513,6 +556,17 @@
                 c = c // abs(c)
             assert c == cmp(st1, st2)
 
+def test_compare_random_unicode():
+    l = [make_random_string() for i in range(3)]
+    l.append((LiteralStringNode(""), ""))
+    for rope1, st1 in l:
+        for rope2, st2 in l:
+            c = compare(rope1, rope2)
+            if c:
+                c = c // abs(c)
+            assert c == cmp(st1, st2)
+
+
 def test_power():
     for i in range(0, 60, 13):
         print i



More information about the Pypy-commit mailing list