[Python-checkins] r68351 - sandbox/trunk/io-c/test_io.py

antoine.pitrou python-checkins at python.org
Mon Jan 5 23:05:00 CET 2009


Author: antoine.pitrou
Date: Mon Jan  5 23:05:00 2009
New Revision: 68351

Log:
more tests for BufferedRandom



Modified:
   sandbox/trunk/io-c/test_io.py

Modified: sandbox/trunk/io-c/test_io.py
==============================================================================
--- sandbox/trunk/io-c/test_io.py	(original)
+++ sandbox/trunk/io-c/test_io.py	Mon Jan  5 23:05:00 2009
@@ -762,7 +762,7 @@
         rw.write(b"ddd")
         rw.write(b"eee")
         self.assertFalse(raw._write_stack) # Buffer writes
-        self.assertEqual(b"ghjk", rw.read()) # This read forces write flush
+        self.assertEqual(b"ghjk", rw.read())
         self.assertEquals(b"dddeee", raw._write_stack[0])
 
     def testSeekAndTell(self):
@@ -784,24 +784,45 @@
         self.assertEquals(7, rw.tell())
         self.assertEquals(b"fl", rw.read(11))
         self.assertRaises(TypeError, rw.seek, 0.0)
-    
-    def testFlushAndRead(self):
+
+    def check_flush_and_read(self, read_func):
         raw = io.BytesIO(b"abcdefghi")
         bufio = self.tp(raw)
 
-        self.assertEquals(b"ab", bufio.read(2))
+        self.assertEquals(b"ab", read_func(bufio, 2))
         bufio.write(b"12")
-        self.assertEquals(b"ef", bufio.read(2))
+        self.assertEquals(b"ef", read_func(bufio, 2))
         self.assertEquals(6, bufio.tell())
         bufio.flush()
         self.assertEquals(6, bufio.tell())
-        self.assertEquals(b"ghi", bufio.read())
+        self.assertEquals(b"ghi", read_func(bufio))
         raw.seek(0, 0)
         raw.write(b"XYZ")
         # flush() resets the read buffer
         bufio.flush()
         bufio.seek(0, 0)
-        self.assertEquals(b"XYZ", bufio.read(3))
+        self.assertEquals(b"XYZ", read_func(bufio, 3))
+
+    def testFlushAndRead(self):
+        self.check_flush_and_read(lambda bufio, *args: bufio.read(*args))
+
+    def testFlushAndReadinto(self):
+        def _readinto(bufio, n=-1):
+            b = bytearray(n if n >= 0 else 9999)
+            n = bufio.readinto(b)
+            return bytes(b[:n])
+        self.check_flush_and_read(_readinto)
+
+    def testFlushAndPeek(self):
+        def _peek(bufio, n=-1):
+            # This relies on the fact that the buffer can contain the whole
+            # raw stream, otherwise peek() can return less.
+            b = bufio.peek(n)
+            if n != -1:
+                b = b[:n]
+            bufio.seek(len(b), 1)
+            return b
+        self.check_flush_and_read(_peek)
 
     def testFlushAndWrite(self):
         raw = io.BytesIO(b"abcdefghi")


More information about the Python-checkins mailing list