Ejemplo n.º 1
0
 def test_goodsize(self):
     data = b"How long is a piece of string?"
     src = io.BytesIO(data)
     # default buffer is larger than src
     b = BufferedStreamWrapper(src)
     self.assertTrue(isinstance(b, io.RawIOBase))
     self.assertTrue(b.readable())
     self.assertFalse(b.writable())
     self.assertTrue(b.seekable())
     self.assertTrue(b.length == len(data))
     pos = b.tell()
     self.assertTrue(pos == 0, "buffer starts at beginning of stream")
     for i in range3(100):
         # check seek and read
         newpos = random.randint(0, len(data) + 1)
         rlen = random.randint(0, len(data) + 1)
         whence = random.choice((io.SEEK_SET, io.SEEK_CUR, io.SEEK_END))
         if whence == io.SEEK_CUR:
             adj = newpos - pos
         elif whence == io.SEEK_END:
             adj = newpos - len(data)
         elif whence == io.SEEK_SET:
             adj = newpos
         b.seek(adj, whence)
         self.assertTrue(b.tell() == newpos,
                         "Expected %i found %i" % (newpos, b.tell()))
         pos = newpos
         xlen = max(0, min(len(data) - pos, rlen))
         rdata = b.read(rlen)
         self.assertTrue(len(rdata) == xlen)
         self.assertTrue(rdata == data[pos:pos + rlen])
         pos += xlen
Ejemplo n.º 2
0
 def test_goodsize(self):
     data = b"How long is a piece of string?"
     src = io.BytesIO(data)
     # default buffer is larger than src
     b = BufferedStreamWrapper(src)
     self.assertTrue(isinstance(b, io.RawIOBase))
     self.assertTrue(b.readable())
     self.assertFalse(b.writable())
     self.assertTrue(b.seekable())
     self.assertTrue(b.length == len(data))
     pos = b.tell()
     self.assertTrue(pos == 0, "buffer starts at beginning of stream")
     for i in range3(100):
         # check seek and read
         newpos = random.randint(0, len(data) + 1)
         rlen = random.randint(0, len(data) + 1)
         whence = random.choice((io.SEEK_SET, io.SEEK_CUR, io.SEEK_END))
         if whence == io.SEEK_CUR:
             adj = newpos - pos
         elif whence == io.SEEK_END:
             adj = newpos - len(data)
         elif whence == io.SEEK_SET:
             adj = newpos
         b.seek(adj, whence)
         self.assertTrue(b.tell() == newpos,
                         "Expected %i found %i" % (newpos, b.tell()))
         pos = newpos
         xlen = max(0, min(len(data) - pos, rlen))
         rdata = b.read(rlen)
         self.assertTrue(len(rdata) == xlen)
         self.assertTrue(rdata == data[pos:pos + rlen])
         pos += xlen