OLD | NEW |
1 """Unit tests for the bytes and bytearray types. | 1 """Unit tests for the bytes and bytearray types. |
2 | 2 |
3 XXX This is a mess. Common tests should be moved to buffer_tests.py, | 3 XXX This is a mess. Common tests should be moved to buffer_tests.py, |
4 which itself ought to be unified with string_tests.py (and the latter | 4 which itself ought to be unified with string_tests.py (and the latter |
5 should be modernized). | 5 should be modernized). |
6 """ | 6 """ |
7 | 7 |
8 import os | 8 import os |
9 import re | 9 import re |
10 import sys | 10 import sys |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 self.assertIn(f(b"bc"), b) | 259 self.assertIn(f(b"bc"), b) |
260 self.assertIn(f(b"abc"), b) | 260 self.assertIn(f(b"abc"), b) |
261 self.assertNotIn(f(b"ac"), b) | 261 self.assertNotIn(f(b"ac"), b) |
262 self.assertNotIn(f(b"d"), b) | 262 self.assertNotIn(f(b"d"), b) |
263 self.assertNotIn(f(b"dab"), b) | 263 self.assertNotIn(f(b"dab"), b) |
264 self.assertNotIn(f(b"abd"), b) | 264 self.assertNotIn(f(b"abd"), b) |
265 | 265 |
266 def test_fromhex(self): | 266 def test_fromhex(self): |
267 self.assertRaises(TypeError, self.type2test.fromhex) | 267 self.assertRaises(TypeError, self.type2test.fromhex) |
268 self.assertRaises(TypeError, self.type2test.fromhex, 1) | 268 self.assertRaises(TypeError, self.type2test.fromhex, 1) |
269 self.assertEquals(self.type2test.fromhex(''), self.type2test()) | 269 self.assertEqual(self.type2test.fromhex(''), self.type2test()) |
270 b = bytearray([0x1a, 0x2b, 0x30]) | 270 b = bytearray([0x1a, 0x2b, 0x30]) |
271 self.assertEquals(self.type2test.fromhex('1a2B30'), b) | 271 self.assertEqual(self.type2test.fromhex('1a2B30'), b) |
272 self.assertEquals(self.type2test.fromhex(' 1A 2B 30 '), b) | 272 self.assertEqual(self.type2test.fromhex(' 1A 2B 30 '), b) |
273 self.assertEquals(self.type2test.fromhex('0000'), b'\0\0') | 273 self.assertEqual(self.type2test.fromhex('0000'), b'\0\0') |
274 self.assertRaises(TypeError, self.type2test.fromhex, b'1B') | 274 self.assertRaises(TypeError, self.type2test.fromhex, b'1B') |
275 self.assertRaises(ValueError, self.type2test.fromhex, 'a') | 275 self.assertRaises(ValueError, self.type2test.fromhex, 'a') |
276 self.assertRaises(ValueError, self.type2test.fromhex, 'rt') | 276 self.assertRaises(ValueError, self.type2test.fromhex, 'rt') |
277 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd') | 277 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd') |
278 self.assertRaises(ValueError, self.type2test.fromhex, '\x00') | 278 self.assertRaises(ValueError, self.type2test.fromhex, '\x00') |
279 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') | 279 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') |
280 | 280 |
281 def test_join(self): | 281 def test_join(self): |
282 self.assertEqual(self.type2test(b"").join([]), b"") | 282 self.assertEqual(self.type2test(b"").join([]), b"") |
283 self.assertEqual(self.type2test(b"").join([b""]), b"") | 283 self.assertEqual(self.type2test(b"").join([b""]), b"") |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
619 # Skip invalid step 0 | 619 # Skip invalid step 0 |
620 for step in indices[1:]: | 620 for step in indices[1:]: |
621 L = list(range(255)) | 621 L = list(range(255)) |
622 b = bytearray(L) | 622 b = bytearray(L) |
623 # Make sure we have a slice of exactly the right length, | 623 # Make sure we have a slice of exactly the right length, |
624 # but with different data. | 624 # but with different data. |
625 data = L[start:stop:step] | 625 data = L[start:stop:step] |
626 data.reverse() | 626 data.reverse() |
627 L[start:stop:step] = data | 627 L[start:stop:step] = data |
628 b[start:stop:step] = data | 628 b[start:stop:step] = data |
629 self.assertEquals(b, bytearray(L)) | 629 self.assertEqual(b, bytearray(L)) |
630 | 630 |
631 del L[start:stop:step] | 631 del L[start:stop:step] |
632 del b[start:stop:step] | 632 del b[start:stop:step] |
633 self.assertEquals(b, bytearray(L)) | 633 self.assertEqual(b, bytearray(L)) |
634 | 634 |
635 def test_setslice_trap(self): | 635 def test_setslice_trap(self): |
636 # This test verifies that we correctly handle assigning self | 636 # This test verifies that we correctly handle assigning self |
637 # to a slice of self (the old Lambert Meertens trap). | 637 # to a slice of self (the old Lambert Meertens trap). |
638 b = bytearray(range(256)) | 638 b = bytearray(range(256)) |
639 b[8:] = b | 639 b[8:] = b |
640 self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) | 640 self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) |
641 | 641 |
642 def test_iconcat(self): | 642 def test_iconcat(self): |
643 b = bytearray(b"abc") | 643 b = bytearray(b"abc") |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 # if it wouldn't reallocate the underlying buffer. | 802 # if it wouldn't reallocate the underlying buffer. |
803 # Furthermore, no destructive changes to the buffer may be applied | 803 # Furthermore, no destructive changes to the buffer may be applied |
804 # before raising the error. | 804 # before raising the error. |
805 b = bytearray(range(10)) | 805 b = bytearray(range(10)) |
806 v = memoryview(b) | 806 v = memoryview(b) |
807 def resize(n): | 807 def resize(n): |
808 b[1:-1] = range(n + 1, 2*n - 1) | 808 b[1:-1] = range(n + 1, 2*n - 1) |
809 resize(10) | 809 resize(10) |
810 orig = b[:] | 810 orig = b[:] |
811 self.assertRaises(BufferError, resize, 11) | 811 self.assertRaises(BufferError, resize, 11) |
812 self.assertEquals(b, orig) | 812 self.assertEqual(b, orig) |
813 self.assertRaises(BufferError, resize, 9) | 813 self.assertRaises(BufferError, resize, 9) |
814 self.assertEquals(b, orig) | 814 self.assertEqual(b, orig) |
815 self.assertRaises(BufferError, resize, 0) | 815 self.assertRaises(BufferError, resize, 0) |
816 self.assertEquals(b, orig) | 816 self.assertEqual(b, orig) |
817 # Other operations implying resize | 817 # Other operations implying resize |
818 self.assertRaises(BufferError, b.pop, 0) | 818 self.assertRaises(BufferError, b.pop, 0) |
819 self.assertEquals(b, orig) | 819 self.assertEqual(b, orig) |
820 self.assertRaises(BufferError, b.remove, b[1]) | 820 self.assertRaises(BufferError, b.remove, b[1]) |
821 self.assertEquals(b, orig) | 821 self.assertEqual(b, orig) |
822 def delitem(): | 822 def delitem(): |
823 del b[1] | 823 del b[1] |
824 self.assertRaises(BufferError, delitem) | 824 self.assertRaises(BufferError, delitem) |
825 self.assertEquals(b, orig) | 825 self.assertEqual(b, orig) |
826 # deleting a non-contiguous slice | 826 # deleting a non-contiguous slice |
827 def delslice(): | 827 def delslice(): |
828 b[1:-1:2] = b"" | 828 b[1:-1:2] = b"" |
829 self.assertRaises(BufferError, delslice) | 829 self.assertRaises(BufferError, delslice) |
830 self.assertEquals(b, orig) | 830 self.assertEqual(b, orig) |
831 | 831 |
832 | 832 |
833 class AssortedBytesTest(unittest.TestCase): | 833 class AssortedBytesTest(unittest.TestCase): |
834 # | 834 # |
835 # Test various combinations of bytes and bytearray | 835 # Test various combinations of bytes and bytearray |
836 # | 836 # |
837 | 837 |
838 @check_bytes_warnings | 838 @check_bytes_warnings |
839 def test_repr_str(self): | 839 def test_repr_str(self): |
840 for f in str, repr: | 840 for f in str, repr: |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 | 1110 |
1111 | 1111 |
1112 def test_main(): | 1112 def test_main(): |
1113 test.support.run_unittest( | 1113 test.support.run_unittest( |
1114 BytesTest, AssortedBytesTest, BytesAsStringTest, | 1114 BytesTest, AssortedBytesTest, BytesAsStringTest, |
1115 ByteArrayTest, ByteArrayAsStringTest, BytesSubclassTest, | 1115 ByteArrayTest, ByteArrayAsStringTest, BytesSubclassTest, |
1116 ByteArraySubclassTest, BytearrayPEP3137Test) | 1116 ByteArraySubclassTest, BytearrayPEP3137Test) |
1117 | 1117 |
1118 if __name__ == "__main__": | 1118 if __name__ == "__main__": |
1119 test_main() | 1119 test_main() |
OLD | NEW |