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