Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 1 | """Unit tests for io.py.""" |
| 2 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 3 | import unittest |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 4 | from itertools import chain |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 5 | from test import test_support |
| 6 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 7 | import io |
| 8 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 10 | class MockIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 12 | def __init__(self, read_stack=()): |
| 13 | self._read_stack = list(read_stack) |
| 14 | self._write_stack = [] |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 15 | |
| 16 | def read(self, n=None): |
| 17 | try: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 18 | return self._read_stack.pop(0) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 19 | except: |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 20 | return b"" |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 22 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 23 | self._write_stack.append(b[:]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 24 | return len(b) |
| 25 | |
| 26 | def writable(self): |
| 27 | return True |
| 28 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 29 | def fileno(self): |
| 30 | return 42 |
| 31 | |
| 32 | def readable(self): |
| 33 | return True |
| 34 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 35 | def seekable(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 36 | return True |
| 37 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 38 | def seek(self, pos, whence): |
| 39 | pass |
| 40 | |
| 41 | def tell(self): |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 42 | return 42 |
| 43 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 45 | class MockFileIO(io.BytesIO): |
| 46 | |
| 47 | def __init__(self, data): |
| 48 | self.read_history = [] |
| 49 | io.BytesIO.__init__(self, data) |
| 50 | |
| 51 | def read(self, n=None): |
| 52 | res = io.BytesIO.read(self, n) |
| 53 | self.read_history.append(None if res is None else len(res)) |
| 54 | return res |
| 55 | |
| 56 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 57 | class MockNonBlockWriterIO(io.RawIOBase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 59 | def __init__(self, blockingScript): |
| 60 | self.bs = list(blockingScript) |
| 61 | self._write_stack = [] |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 63 | def write(self, b): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 64 | self._write_stack.append(b[:]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 65 | n = self.bs.pop(0) |
| 66 | if (n < 0): |
Guido van Rossum | 141f767 | 2007-04-10 00:22:16 +0000 | [diff] [blame^] | 67 | raise io.BlockingIOError(0, "test blocking", -n) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 68 | else: |
| 69 | return n |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 71 | def writable(self): |
| 72 | return True |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 75 | class IOTest(unittest.TestCase): |
| 76 | |
Guido van Rossum | 4d0f5a4 | 2007-03-07 22:59:39 +0000 | [diff] [blame] | 77 | def tearDown(self): |
| 78 | test_support.unlink(test_support.TESTFN) |
| 79 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 80 | def write_ops(self, f): |
| 81 | f.write(b"blah.") |
| 82 | f.seek(0) |
| 83 | f.write(b"Hello.") |
| 84 | self.assertEqual(f.tell(), 6) |
| 85 | f.seek(-1, 1) |
| 86 | self.assertEqual(f.tell(), 5) |
| 87 | f.write(" world\n\n\n") |
| 88 | f.seek(0) |
| 89 | f.write("h") |
| 90 | f.seek(-2, 2) |
| 91 | f.truncate() |
| 92 | |
| 93 | def read_ops(self, f): |
| 94 | data = f.read(5) |
| 95 | self.assertEqual(data, b"hello") |
Guido van Rossum | 00efead | 2007-03-07 05:23:25 +0000 | [diff] [blame] | 96 | n = f.readinto(data) |
| 97 | self.assertEqual(n, 5) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 98 | self.assertEqual(data, b" worl") |
Guido van Rossum | 00efead | 2007-03-07 05:23:25 +0000 | [diff] [blame] | 99 | n = f.readinto(data) |
| 100 | self.assertEqual(n, 2) |
| 101 | self.assertEqual(len(data), 5) |
| 102 | self.assertEqual(data[:2], b"d\n") |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 103 | f.seek(0) |
| 104 | self.assertEqual(f.read(20), b"hello world\n") |
| 105 | f.seek(-6, 2) |
| 106 | self.assertEqual(f.read(5), b"world") |
| 107 | f.seek(-6, 1) |
| 108 | self.assertEqual(f.read(5), b" worl") |
| 109 | self.assertEqual(f.tell(), 10) |
| 110 | |
| 111 | def test_raw_file_io(self): |
| 112 | f = io.open(test_support.TESTFN, "wb", buffering=0) |
| 113 | self.assertEqual(f.readable(), False) |
| 114 | self.assertEqual(f.writable(), True) |
| 115 | self.assertEqual(f.seekable(), True) |
| 116 | self.write_ops(f) |
| 117 | f.close() |
| 118 | f = io.open(test_support.TESTFN, "rb", buffering=0) |
| 119 | self.assertEqual(f.readable(), True) |
| 120 | self.assertEqual(f.writable(), False) |
| 121 | self.assertEqual(f.seekable(), True) |
| 122 | self.read_ops(f) |
| 123 | f.close() |
| 124 | |
| 125 | def test_raw_bytes_io(self): |
| 126 | f = io.BytesIO() |
| 127 | self.write_ops(f) |
| 128 | data = f.getvalue() |
| 129 | self.assertEqual(data, b"hello world\n") |
| 130 | f = io.BytesIO(data) |
| 131 | self.read_ops(f) |
| 132 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 133 | def test_fileio_FileIO(self): |
| 134 | import _fileio |
| 135 | f = _fileio._FileIO(test_support.TESTFN, "w") |
| 136 | self.assertEqual(f.readable(), False) |
| 137 | self.assertEqual(f.writable(), True) |
| 138 | self.assertEqual(f.seekable(), True) |
| 139 | self.write_ops(f) |
| 140 | f.close() |
| 141 | f = _fileio._FileIO(test_support.TESTFN, "r") |
| 142 | self.assertEqual(f.readable(), True) |
| 143 | self.assertEqual(f.writable(), False) |
| 144 | self.assertEqual(f.seekable(), True) |
| 145 | self.read_ops(f) |
| 146 | f.close() |
| 147 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 149 | class MemorySeekTestMixin: |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 151 | def testInit(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 152 | buf = self.buftype("1234567890") |
| 153 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 154 | |
| 155 | def testRead(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 156 | buf = self.buftype("1234567890") |
| 157 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 158 | |
| 159 | self.assertEquals(buf[:1], bytesIo.read(1)) |
| 160 | self.assertEquals(buf[1:5], bytesIo.read(4)) |
| 161 | self.assertEquals(buf[5:], bytesIo.read(900)) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 162 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 163 | |
| 164 | def testReadNoArgs(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 165 | buf = self.buftype("1234567890") |
| 166 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 167 | |
| 168 | self.assertEquals(buf, bytesIo.read()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 169 | self.assertEquals(self.EOF, bytesIo.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 170 | |
| 171 | def testSeek(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 172 | buf = self.buftype("1234567890") |
| 173 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 174 | |
| 175 | bytesIo.read(5) |
| 176 | bytesIo.seek(0) |
| 177 | self.assertEquals(buf, bytesIo.read()) |
| 178 | |
| 179 | bytesIo.seek(3) |
| 180 | self.assertEquals(buf[3:], bytesIo.read()) |
| 181 | |
| 182 | def testTell(self): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 183 | buf = self.buftype("1234567890") |
| 184 | bytesIo = self.ioclass(buf) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 185 | |
| 186 | self.assertEquals(0, bytesIo.tell()) |
| 187 | bytesIo.seek(5) |
| 188 | self.assertEquals(5, bytesIo.tell()) |
| 189 | bytesIo.seek(10000) |
| 190 | self.assertEquals(10000, bytesIo.tell()) |
| 191 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 193 | class BytesIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 194 | buftype = bytes |
| 195 | ioclass = io.BytesIO |
| 196 | EOF = b"" |
| 197 | |
| 198 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 199 | class StringIOTest(MemorySeekTestMixin, unittest.TestCase): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 200 | buftype = str |
| 201 | ioclass = io.StringIO |
| 202 | EOF = "" |
| 203 | |
| 204 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 205 | class BufferedReaderTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 207 | def testRead(self): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 208 | rawio = MockIO((b"abc", b"d", b"efg")) |
| 209 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 211 | self.assertEquals(b"abcdef", bufio.read(6)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 213 | def testBuffering(self): |
| 214 | data = b"abcdefghi" |
| 215 | dlen = len(data) |
| 216 | |
| 217 | tests = [ |
| 218 | [ 100, [ 3, 1, 4, 8 ], [ dlen, 0 ] ], |
| 219 | [ 100, [ 3, 3, 3], [ dlen ] ], |
| 220 | [ 4, [ 1, 2, 4, 2 ], [ 4, 4, 1 ] ], |
| 221 | ] |
| 222 | |
| 223 | for bufsize, buf_read_sizes, raw_read_sizes in tests: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 224 | rawio = MockFileIO(data) |
| 225 | bufio = io.BufferedReader(rawio, buffer_size=bufsize) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 226 | pos = 0 |
| 227 | for nbytes in buf_read_sizes: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 228 | self.assertEquals(bufio.read(nbytes), data[pos:pos+nbytes]) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 229 | pos += nbytes |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 230 | self.assertEquals(rawio.read_history, raw_read_sizes) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 232 | def testReadNonBlocking(self): |
| 233 | # Inject some None's in there to simulate EWOULDBLOCK |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 234 | rawio = MockIO((b"abc", b"d", None, b"efg", None, None)) |
| 235 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 237 | self.assertEquals(b"abcd", bufio.read(6)) |
| 238 | self.assertEquals(b"e", bufio.read(1)) |
| 239 | self.assertEquals(b"fg", bufio.read()) |
| 240 | self.assert_(None is bufio.read()) |
| 241 | self.assertEquals(b"", bufio.read()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 243 | def testReadToEof(self): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 244 | rawio = MockIO((b"abc", b"d", b"efg")) |
| 245 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 246 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 247 | self.assertEquals(b"abcdefg", bufio.read(9000)) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 248 | |
| 249 | def testReadNoArgs(self): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 250 | rawio = MockIO((b"abc", b"d", b"efg")) |
| 251 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 253 | self.assertEquals(b"abcdefg", bufio.read()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 254 | |
| 255 | def testFileno(self): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 256 | rawio = MockIO((b"abc", b"d", b"efg")) |
| 257 | bufio = io.BufferedReader(rawio) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 259 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 260 | |
| 261 | def testFilenoNoFileno(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 262 | # XXX will we always have fileno() function? If so, kill |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 263 | # this test. Else, write it. |
| 264 | pass |
| 265 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 266 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 267 | class BufferedWriterTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 269 | def testWrite(self): |
| 270 | # Write to the buffered IO but don't overflow the buffer. |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 271 | writer = MockIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 272 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 273 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 274 | bufio.write(b"abc") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 276 | self.assertFalse(writer._write_stack) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 277 | |
| 278 | def testWriteOverflow(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 279 | writer = MockIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 280 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 282 | bufio.write(b"abc") |
| 283 | bufio.write(b"defghijkl") |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 285 | self.assertEquals(b"abcdefghijkl", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 287 | def testWriteNonBlocking(self): |
| 288 | raw = MockNonBlockWriterIO((9, 2, 22, -6, 10, 12, 12)) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 289 | bufio = io.BufferedWriter(raw, 8, 16) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 291 | bufio.write(b"asdf") |
| 292 | bufio.write(b"asdfa") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 293 | self.assertEquals(b"asdfasdfa", raw._write_stack[0]) |
| 294 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 295 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 296 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[1]) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 297 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 298 | self.assertEquals(b"dfasdfasdf", raw._write_stack[2]) |
| 299 | self.assertEquals(b"asdfasdfasdf", raw._write_stack[3]) |
| 300 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 301 | bufio.write(b"asdfasdfasdf") |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 303 | # XXX I don't like this test. It relies too heavily on how the |
| 304 | # algorithm actually works, which we might change. Refactor |
| 305 | # later. |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 306 | |
| 307 | def testFileno(self): |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 308 | rawio = MockIO((b"abc", b"d", b"efg")) |
| 309 | bufio = io.BufferedWriter(rawio) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 311 | self.assertEquals(42, bufio.fileno()) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 313 | def testFlush(self): |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 314 | writer = MockIO() |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 315 | bufio = io.BufferedWriter(writer, 8) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 316 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 317 | bufio.write(b"abc") |
| 318 | bufio.flush() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 320 | self.assertEquals(b"abc", writer._write_stack[0]) |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 323 | class BufferedRWPairTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 325 | def testRWPair(self): |
| 326 | r = MockIO(()) |
| 327 | w = MockIO() |
| 328 | pair = io.BufferedRWPair(r, w) |
| 329 | |
| 330 | # XXX need implementation |
| 331 | |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 332 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 333 | class BufferedRandomTest(unittest.TestCase): |
Guido van Rossum | a9e2024 | 2007-03-08 00:43:48 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 335 | def testReadAndWrite(self): |
| 336 | raw = MockIO((b"asdf", b"ghjk")) |
| 337 | rw = io.BufferedRandom(raw, 8, 12) |
| 338 | |
| 339 | self.assertEqual(b"as", rw.read(2)) |
| 340 | rw.write(b"ddd") |
| 341 | rw.write(b"eee") |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 342 | self.assertFalse(raw._write_stack) # Buffer writes |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 343 | self.assertEqual(b"ghjk", rw.read()) # This read forces write flush |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 344 | self.assertEquals(b"dddeee", raw._write_stack[0]) |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 345 | |
| 346 | def testSeekAndTell(self): |
| 347 | raw = io.BytesIO(b"asdfghjkl") |
| 348 | rw = io.BufferedRandom(raw) |
| 349 | |
| 350 | self.assertEquals(b"as", rw.read(2)) |
| 351 | self.assertEquals(2, rw.tell()) |
| 352 | rw.seek(0, 0) |
| 353 | self.assertEquals(b"asdf", rw.read(4)) |
| 354 | |
| 355 | rw.write(b"asdf") |
| 356 | rw.seek(0, 0) |
| 357 | self.assertEquals(b"asdfasdfl", rw.read()) |
| 358 | self.assertEquals(9, rw.tell()) |
| 359 | rw.seek(-4, 2) |
| 360 | self.assertEquals(5, rw.tell()) |
| 361 | rw.seek(2, 1) |
| 362 | self.assertEquals(7, rw.tell()) |
| 363 | self.assertEquals(b"fl", rw.read(11)) |
| 364 | |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 365 | |
| 366 | class TextIOWrapperTest(unittest.TestCase): |
| 367 | def testNewlines(self): |
| 368 | input_lines = [ "unix\n", "windows\r\n", "os9\r", "last\n", "nonl" ] |
| 369 | |
| 370 | tests = [ |
| 371 | [ None, [ 'unix\n', 'windows\n', 'os9\n', 'last\n', 'nonl' ] ], |
| 372 | [ '\n', input_lines ], |
| 373 | [ '\r\n', input_lines ], |
| 374 | ] |
| 375 | |
| 376 | encodings = ('utf-8', 'bz2') |
| 377 | |
| 378 | # Try a range of pad sizes to test the case where \r is the last |
| 379 | # character in TextIOWrapper._pending_line. |
| 380 | for encoding in encodings: |
| 381 | for do_reads in (False, True): |
| 382 | for padlen in chain(range(10), range(50, 60)): |
| 383 | pad = '.' * padlen |
| 384 | data_lines = [ pad + line for line in input_lines ] |
| 385 | # XXX: str.encode() should return bytes |
| 386 | data = bytes(''.join(data_lines).encode(encoding)) |
| 387 | |
| 388 | for newline, exp_line_ends in tests: |
| 389 | exp_lines = [ pad + line for line in exp_line_ends ] |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 390 | bufio = io.BufferedReader(io.BytesIO(data)) |
| 391 | textio = io.TextIOWrapper(bufio, newline=newline, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 392 | encoding=encoding) |
| 393 | if do_reads: |
| 394 | got_lines = [] |
| 395 | while True: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 396 | c2 = textio.read(2) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 397 | if c2 == '': |
| 398 | break |
| 399 | self.assertEquals(len(c2), 2) |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 400 | got_lines.append(c2 + textio.readline()) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 401 | else: |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 402 | got_lines = list(textio) |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 403 | |
| 404 | for got_line, exp_line in zip(got_lines, exp_lines): |
| 405 | self.assertEquals(got_line, exp_line) |
| 406 | self.assertEquals(len(got_lines), len(exp_lines)) |
| 407 | |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 408 | # XXX Tests for open() |
Guido van Rossum | 68bbcd2 | 2007-02-27 17:19:33 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 410 | def test_main(): |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 411 | test_support.run_unittest(IOTest, BytesIOTest, StringIOTest, |
| 412 | BufferedReaderTest, |
Guido van Rossum | 01a2752 | 2007-03-07 01:00:12 +0000 | [diff] [blame] | 413 | BufferedWriterTest, BufferedRWPairTest, |
Guido van Rossum | 78892e4 | 2007-04-06 17:31:18 +0000 | [diff] [blame] | 414 | BufferedRandomTest, TextIOWrapperTest) |
Guido van Rossum | 28524c7 | 2007-02-27 05:47:44 +0000 | [diff] [blame] | 415 | |
| 416 | if __name__ == "__main__": |
Guido van Rossum | 76c5d4d | 2007-04-06 19:10:29 +0000 | [diff] [blame] | 417 | unittest.main() |