Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_bytes.py @ 48715:48dddfd2fbf3
RPC_WSTR is not available Visual Studio 2003 or earlier.
(Maybe I should have defined RPC_WSTR on old compiler,
but uuidcreate() is only place using it, so I simply replaced
with unsigned short *)
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Sun, 17 Aug 2008 07:26:26 +0000 |
| parents | e0c65922b878 |
| children | 83b2d07b475b |
| rev | line source |
|---|---|
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
1 """Unit tests for the bytes and bytearray types. |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
2 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
3 XXX This is a mess. Common tests should be moved to buffer_tests.py, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
4 which itself ought to be unified with string_tests.py (and the latter |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
5 should be modernized). |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
6 """ |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
8 import os |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
9 import re |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 import sys |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
11 import copy |
|
41425
0486deaaade3
An example of action-at-a-distance: fix the problems I had in test_io.py
Guido van Rossum <guido@python.org>
parents:
41412
diff
changeset
|
12 import pickle |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
13 import tempfile |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 import unittest |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
15 import warnings |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
16 import test.support |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
17 import test.string_tests |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
18 import test.buffer_tests |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
20 class Indexable: |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
21 def __init__(self, value=0): |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
22 self.value = value |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
23 def __index__(self): |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
24 return self.value |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
25 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
27 class BaseBytesTest(unittest.TestCase): |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
29 def setUp(self): |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
30 self.warning_filters = warnings.filters[:] |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
31 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
32 def tearDown(self): |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
33 warnings.filters = self.warning_filters |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
34 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 def test_basics(self): |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
36 b = self.type2test() |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
37 self.assertEqual(type(b), self.type2test) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
38 self.assertEqual(b.__class__, self.type2test) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 def test_empty_sequence(self): |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
41 b = self.type2test() |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 self.assertEqual(len(b), 0) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 self.assertRaises(IndexError, lambda: b[0]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 self.assertRaises(IndexError, lambda: b[1]) |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44137
diff
changeset
|
45 self.assertRaises(IndexError, lambda: b[sys.maxsize]) |
|
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44137
diff
changeset
|
46 self.assertRaises(IndexError, lambda: b[sys.maxsize+1]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 self.assertRaises(IndexError, lambda: b[10**100]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 self.assertRaises(IndexError, lambda: b[-1]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 self.assertRaises(IndexError, lambda: b[-2]) |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44137
diff
changeset
|
50 self.assertRaises(IndexError, lambda: b[-sys.maxsize]) |
|
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44137
diff
changeset
|
51 self.assertRaises(IndexError, lambda: b[-sys.maxsize-1]) |
|
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44137
diff
changeset
|
52 self.assertRaises(IndexError, lambda: b[-sys.maxsize-2]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 self.assertRaises(IndexError, lambda: b[-10**100]) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 def test_from_list(self): |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 ints = list(range(256)) |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
57 b = self.type2test(i for i in ints) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 self.assertEqual(len(b), 256) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 self.assertEqual(list(b), ints) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 def test_from_index(self): |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
62 b = self.type2test([Indexable(), Indexable(1), Indexable(254), |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
63 Indexable(255)]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 self.assertEqual(list(b), [0, 1, 254, 255]) |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
65 self.assertRaises(ValueError, bytearray, [Indexable(-1)]) |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
66 self.assertRaises(ValueError, bytearray, [Indexable(256)]) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
67 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
68 def test_from_ssize(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
69 self.assertEqual(bytearray(0), b'') |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
70 self.assertEqual(bytearray(1), b'\x00') |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
71 self.assertEqual(bytearray(5), b'\x00\x00\x00\x00\x00') |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
72 self.assertRaises(ValueError, bytearray, -1) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
73 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
74 self.assertEqual(bytearray('0', 'ascii'), b'0') |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
75 self.assertEqual(bytearray(b'0'), b'0') |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 def test_constructor_type_errors(self): |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
78 self.assertRaises(TypeError, self.type2test, 0.0) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 class C: |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 pass |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
81 self.assertRaises(TypeError, self.type2test, ["0"]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
82 self.assertRaises(TypeError, self.type2test, [0.0]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
83 self.assertRaises(TypeError, self.type2test, [None]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
84 self.assertRaises(TypeError, self.type2test, [C()]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 def test_constructor_value_errors(self): |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
87 self.assertRaises(ValueError, self.type2test, [-1]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
88 self.assertRaises(ValueError, self.type2test, [-sys.maxsize]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
89 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-1]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
90 self.assertRaises(ValueError, self.type2test, [-sys.maxsize-2]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
91 self.assertRaises(ValueError, self.type2test, [-10**100]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
92 self.assertRaises(ValueError, self.type2test, [256]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
93 self.assertRaises(ValueError, self.type2test, [257]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
94 self.assertRaises(ValueError, self.type2test, [sys.maxsize]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
95 self.assertRaises(ValueError, self.type2test, [sys.maxsize+1]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
96 self.assertRaises(ValueError, self.type2test, [10**100]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 def test_compare(self): |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
99 b1 = self.type2test([1, 2, 3]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
100 b2 = self.type2test([1, 2, 3]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
101 b3 = self.type2test([1, 3]) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
42188
81930eaae715
Patch by Christian Heimes to change self.assert_(x == y) into
Guido van Rossum <guido@python.org>
parents:
42142
diff
changeset
|
103 self.assertEqual(b1, b2) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 self.failUnless(b2 != b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 self.failUnless(b1 <= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 self.failUnless(b1 <= b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 self.failUnless(b1 < b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 self.failUnless(b1 >= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 self.failUnless(b3 >= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 self.failUnless(b3 > b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 self.failIf(b1 != b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 self.failIf(b2 == b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 self.failIf(b1 > b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 self.failIf(b1 > b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 self.failIf(b1 >= b3) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 self.failIf(b1 < b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 self.failIf(b3 < b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 self.failIf(b3 <= b2) |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
121 def test_compare_to_str(self): |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
122 warnings.simplefilter('ignore', BytesWarning) |
|
43508
47c02791ec6b
Patch #1049 by Thomas Lee.
Guido van Rossum <guido@python.org>
parents:
43487
diff
changeset
|
123 # Byte comparisons with unicode should always fail! |
|
41412
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
124 # Test this for all expected byte orders and Unicode character sizes |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
125 self.assertEqual(self.type2test(b"\0a\0b\0c") == "abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
126 self.assertEqual(self.type2test(b"\0\0\0a\0\0\0b\0\0\0c") == "abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
127 self.assertEqual(self.type2test(b"a\0b\0c\0") == "abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
128 self.assertEqual(self.type2test(b"a\0\0\0b\0\0\0c\0\0\0") == "abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
129 self.assertEqual(self.type2test() == str(), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
130 self.assertEqual(self.type2test() != str(), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
131 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
132 def test_reversed(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
133 input = list(map(ord, "Hello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
134 b = self.type2test(input) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
135 output = list(reversed(b)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
136 input.reverse() |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
137 self.assertEqual(output, input) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
138 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
139 def test_getslice(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
140 def by(s): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
141 return self.type2test(map(ord, s)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
142 b = by("Hello, world") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
143 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
144 self.assertEqual(b[:5], by("Hello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
145 self.assertEqual(b[1:5], by("ello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
146 self.assertEqual(b[5:7], by(", ")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
147 self.assertEqual(b[7:], by("world")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
148 self.assertEqual(b[7:12], by("world")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
149 self.assertEqual(b[7:100], by("world")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
150 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
151 self.assertEqual(b[:-7], by("Hello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
152 self.assertEqual(b[-11:-7], by("ello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
153 self.assertEqual(b[-7:-5], by(", ")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
154 self.assertEqual(b[-5:], by("world")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
155 self.assertEqual(b[-5:12], by("world")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
156 self.assertEqual(b[-5:100], by("world")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
157 self.assertEqual(b[-100:5], by("Hello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
158 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
159 def test_extended_getslice(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
160 # Test extended slicing by comparing with list slicing. |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
161 L = list(range(255)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
162 b = self.type2test(L) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
163 indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
164 for start in indices: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
165 for stop in indices: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
166 # Skip step 0 (invalid) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
167 for step in indices[1:]: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
168 self.assertEqual(b[start:stop:step], self.type2test(L[start:stop:step])) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
169 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
170 def test_encoding(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
171 sample = "Hello world\n\u1234\u5678\u9abc\udef0" |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
172 for enc in ("utf8", "utf16"): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
173 b = self.type2test(sample, enc) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
174 self.assertEqual(b, self.type2test(sample.encode(enc))) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
175 self.assertRaises(UnicodeEncodeError, self.type2test, sample, "latin1") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
176 b = self.type2test(sample, "latin1", "ignore") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
177 self.assertEqual(b, self.type2test(sample[:-4], "utf-8")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
178 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
179 def test_decode(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
180 sample = "Hello world\n\u1234\u5678\u9abc\def0\def0" |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
181 for enc in ("utf8", "utf16"): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
182 b = self.type2test(sample, enc) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
183 self.assertEqual(b.decode(enc), sample) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
184 sample = "Hello world\n\x80\x81\xfe\xff" |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
185 b = self.type2test(sample, "latin1") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
186 self.assertRaises(UnicodeDecodeError, b.decode, "utf8") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
187 self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
188 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
189 def test_from_int(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
190 b = self.type2test(0) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
191 self.assertEqual(b, self.type2test()) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
192 b = self.type2test(10) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
193 self.assertEqual(b, self.type2test([0]*10)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
194 b = self.type2test(10000) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
195 self.assertEqual(b, self.type2test([0]*10000)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
196 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
197 def test_concat(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
198 b1 = self.type2test(b"abc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
199 b2 = self.type2test(b"def") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
200 self.assertEqual(b1 + b2, b"abcdef") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
201 self.assertEqual(b1 + bytes(b"def"), b"abcdef") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
202 self.assertEqual(bytes(b"def") + b1, b"defabc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
203 self.assertRaises(TypeError, lambda: b1 + "def") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
204 self.assertRaises(TypeError, lambda: "abc" + b2) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
205 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
206 def test_repeat(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
207 for b in b"abc", self.type2test(b"abc"): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
208 self.assertEqual(b * 3, b"abcabcabc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
209 self.assertEqual(b * 0, b"") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
210 self.assertEqual(b * -1, b"") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
211 self.assertRaises(TypeError, lambda: b * 3.14) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
212 self.assertRaises(TypeError, lambda: 3.14 * b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
213 # XXX Shouldn't bytes and bytearray agree on what to raise? |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
214 self.assertRaises((OverflowError, MemoryError), |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
215 lambda: b * sys.maxsize) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
216 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
217 def test_repeat_1char(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
218 self.assertEqual(self.type2test(b'x')*100, self.type2test([ord('x')]*100)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
219 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
220 def test_contains(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
221 b = self.type2test(b"abc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
222 self.failUnless(ord('a') in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
223 self.failUnless(int(ord('a')) in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
224 self.failIf(200 in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
225 self.failIf(200 in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
226 self.assertRaises(ValueError, lambda: 300 in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
227 self.assertRaises(ValueError, lambda: -1 in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
228 self.assertRaises(TypeError, lambda: None in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
229 self.assertRaises(TypeError, lambda: float(ord('a')) in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
230 self.assertRaises(TypeError, lambda: "a" in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
231 for f in bytes, bytearray: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
232 self.failUnless(f(b"") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
233 self.failUnless(f(b"a") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
234 self.failUnless(f(b"b") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
235 self.failUnless(f(b"c") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
236 self.failUnless(f(b"ab") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
237 self.failUnless(f(b"bc") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
238 self.failUnless(f(b"abc") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
239 self.failIf(f(b"ac") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
240 self.failIf(f(b"d") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
241 self.failIf(f(b"dab") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
242 self.failIf(f(b"abd") in b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
243 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
244 def test_fromhex(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
245 self.assertRaises(TypeError, self.type2test.fromhex) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
246 self.assertRaises(TypeError, self.type2test.fromhex, 1) |
|
45058
afd0cb25c73e
Enabled fromhex('') test for bytes
Christian Heimes <christian@cheimes.de>
parents:
45057
diff
changeset
|
247 self.assertEquals(self.type2test.fromhex(''), self.type2test()) |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
248 b = bytearray([0x1a, 0x2b, 0x30]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
249 self.assertEquals(self.type2test.fromhex('1a2B30'), b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
250 self.assertEquals(self.type2test.fromhex(' 1A 2B 30 '), b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
251 self.assertEquals(self.type2test.fromhex('0000'), b'\0\0') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
252 self.assertRaises(TypeError, self.type2test.fromhex, b'1B') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
253 self.assertRaises(ValueError, self.type2test.fromhex, 'a') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
254 self.assertRaises(ValueError, self.type2test.fromhex, 'rt') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
255 self.assertRaises(ValueError, self.type2test.fromhex, '1a b cd') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
256 self.assertRaises(ValueError, self.type2test.fromhex, '\x00') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
257 self.assertRaises(ValueError, self.type2test.fromhex, '12 \x00 34') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
258 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
259 def test_join(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
260 self.assertEqual(self.type2test(b"").join([]), b"") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
261 self.assertEqual(self.type2test(b"").join([b""]), b"") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
262 for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
263 lst = list(map(self.type2test, lst)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
264 self.assertEqual(self.type2test(b"").join(lst), b"abc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
265 self.assertEqual(self.type2test(b"").join(tuple(lst)), b"abc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
266 self.assertEqual(self.type2test(b"").join(iter(lst)), b"abc") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
267 self.assertEqual(self.type2test(b".").join([b"ab", b"cd"]), b"ab.cd") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
268 # XXX more... |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
269 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
270 def test_index(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
271 b = self.type2test(b'parrot') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
272 self.assertEqual(b.index('p'), 0) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
273 self.assertEqual(b.index('rr'), 2) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
274 self.assertEqual(b.index('t'), 5) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
275 self.assertRaises(ValueError, lambda: b.index('w')) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
276 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
277 def test_count(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
278 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
279 self.assertEqual(b.count(b'i'), 4) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
280 self.assertEqual(b.count(b'ss'), 2) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
281 self.assertEqual(b.count(b'w'), 0) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
282 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
283 def test_startswith(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
284 b = self.type2test(b'hello') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
285 self.assertFalse(self.type2test().startswith(b"anything")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
286 self.assertTrue(b.startswith(b"hello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
287 self.assertTrue(b.startswith(b"hel")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
288 self.assertTrue(b.startswith(b"h")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
289 self.assertFalse(b.startswith(b"hellow")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
290 self.assertFalse(b.startswith(b"ha")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
291 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
292 def test_endswith(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
293 b = self.type2test(b'hello') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
294 self.assertFalse(bytearray().endswith(b"anything")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
295 self.assertTrue(b.endswith(b"hello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
296 self.assertTrue(b.endswith(b"llo")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
297 self.assertTrue(b.endswith(b"o")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
298 self.assertFalse(b.endswith(b"whello")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
299 self.assertFalse(b.endswith(b"no")) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
300 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
301 def test_find(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
302 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
303 self.assertEqual(b.find(b'ss'), 2) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
304 self.assertEqual(b.find(b'ss', 3), 5) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
305 self.assertEqual(b.find(b'ss', 1, 7), 2) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
306 self.assertEqual(b.find(b'ss', 1, 3), -1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
307 self.assertEqual(b.find(b'w'), -1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
308 self.assertEqual(b.find(b'mississippian'), -1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
309 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
310 def test_rfind(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
311 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
312 self.assertEqual(b.rfind(b'ss'), 5) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
313 self.assertEqual(b.rfind(b'ss', 3), 5) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
314 self.assertEqual(b.rfind(b'ss', 0, 6), 2) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
315 self.assertEqual(b.rfind(b'w'), -1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
316 self.assertEqual(b.rfind(b'mississippian'), -1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
317 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
318 def test_index(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
319 b = self.type2test(b'world') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
320 self.assertEqual(b.index(b'w'), 0) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
321 self.assertEqual(b.index(b'orl'), 1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
322 self.assertRaises(ValueError, b.index, b'worm') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
323 self.assertRaises(ValueError, b.index, b'ldo') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
324 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
325 def test_rindex(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
326 # XXX could be more rigorous |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
327 b = self.type2test(b'world') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
328 self.assertEqual(b.rindex(b'w'), 0) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
329 self.assertEqual(b.rindex(b'orl'), 1) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
330 self.assertRaises(ValueError, b.rindex, b'worm') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
331 self.assertRaises(ValueError, b.rindex, b'ldo') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
332 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
333 def test_replace(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
334 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
335 self.assertEqual(b.replace(b'i', b'a'), b'massassappa') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
336 self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
337 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
338 def test_split(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
339 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
340 self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
341 self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
342 self.assertEqual(b.split(b'w'), [b]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
343 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
344 def test_split_whitespace(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
345 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
346 b'arf\fbarf', b'arf\vbarf'): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
347 b = self.type2test(b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
348 self.assertEqual(b.split(), [b'arf', b'barf']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
349 self.assertEqual(b.split(None), [b'arf', b'barf']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
350 self.assertEqual(b.split(None, 2), [b'arf', b'barf']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
351 for b in (b'a\x1Cb', b'a\x1Db', b'a\x1Eb', b'a\x1Fb'): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
352 b = self.type2test(b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
353 self.assertEqual(b.split(), [b]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
354 self.assertEqual(self.type2test(b' a bb c ').split(None, 0), [b'a bb c ']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
355 self.assertEqual(self.type2test(b' a bb c ').split(None, 1), [b'a', b'bb c ']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
356 self.assertEqual(self.type2test(b' a bb c ').split(None, 2), [b'a', b'bb', b'c ']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
357 self.assertEqual(self.type2test(b' a bb c ').split(None, 3), [b'a', b'bb', b'c']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
358 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
359 def test_split_string_error(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
360 self.assertRaises(TypeError, self.type2test(b'a b').split, ' ') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
361 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
362 def test_rsplit(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
363 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
364 self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
365 self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
366 self.assertEqual(b.rsplit(b'w'), [b]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
367 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
368 def test_rsplit_whitespace(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
369 for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
370 b'arf\fbarf', b'arf\vbarf'): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
371 b = self.type2test(b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
372 self.assertEqual(b.rsplit(), [b'arf', b'barf']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
373 self.assertEqual(b.rsplit(None), [b'arf', b'barf']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
374 self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
375 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 0), [b' a bb c']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
376 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 1), [b' a bb', b'c']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
377 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 2), [b' a', b'bb', b'c']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
378 self.assertEqual(self.type2test(b' a bb c ').rsplit(None, 3), [b'a', b'bb', b'c']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
379 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
380 def test_rsplit_string_error(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
381 self.assertRaises(TypeError, self.type2test(b'a b').rsplit, ' ') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
382 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
383 def test_rsplit_unicodewhitespace(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
384 b = self.type2test(b"\x09\x0A\x0B\x0C\x0D\x1C\x1D\x1E\x1F") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
385 self.assertEqual(b.split(), [b'\x1c\x1d\x1e\x1f']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
386 self.assertEqual(b.rsplit(), [b'\x1c\x1d\x1e\x1f']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
387 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
388 def test_partition(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
389 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
390 self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
391 self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
392 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
393 def test_rpartition(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
394 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
395 self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
396 self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
397 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
398 def test_pickling(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
399 for proto in range(pickle.HIGHEST_PROTOCOL): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
400 for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
401 b = self.type2test(b) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
402 ps = pickle.dumps(b, proto) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
403 q = pickle.loads(ps) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
404 self.assertEqual(b, q) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
405 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
406 def test_strip(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
407 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
408 self.assertEqual(b.strip(b'i'), b'mississipp') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
409 self.assertEqual(b.strip(b'm'), b'ississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
410 self.assertEqual(b.strip(b'pi'), b'mississ') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
411 self.assertEqual(b.strip(b'im'), b'ssissipp') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
412 self.assertEqual(b.strip(b'pim'), b'ssiss') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
413 self.assertEqual(b.strip(b), b'') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
414 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
415 def test_lstrip(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
416 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
417 self.assertEqual(b.lstrip(b'i'), b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
418 self.assertEqual(b.lstrip(b'm'), b'ississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
419 self.assertEqual(b.lstrip(b'pi'), b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
420 self.assertEqual(b.lstrip(b'im'), b'ssissippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
421 self.assertEqual(b.lstrip(b'pim'), b'ssissippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
422 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
423 def test_rstrip(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
424 b = self.type2test(b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
425 self.assertEqual(b.rstrip(b'i'), b'mississipp') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
426 self.assertEqual(b.rstrip(b'm'), b'mississippi') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
427 self.assertEqual(b.rstrip(b'pi'), b'mississ') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
428 self.assertEqual(b.rstrip(b'im'), b'mississipp') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
429 self.assertEqual(b.rstrip(b'pim'), b'mississ') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
430 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
431 def test_strip_whitespace(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
432 b = self.type2test(b' \t\n\r\f\vabc \t\n\r\f\v') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
433 self.assertEqual(b.strip(), b'abc') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
434 self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
435 self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
436 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
437 def test_strip_bytearray(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
438 self.assertEqual(self.type2test(b'abc').strip(memoryview(b'ac')), b'b') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
439 self.assertEqual(self.type2test(b'abc').lstrip(memoryview(b'ac')), b'bc') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
440 self.assertEqual(self.type2test(b'abc').rstrip(memoryview(b'ac')), b'ab') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
441 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
442 def test_strip_string_error(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
443 self.assertRaises(TypeError, self.type2test(b'abc').strip, 'b') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
444 self.assertRaises(TypeError, self.type2test(b'abc').lstrip, 'b') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
445 self.assertRaises(TypeError, self.type2test(b'abc').rstrip, 'b') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
446 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
447 def test_ord(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
448 b = self.type2test(b'\0A\x7f\x80\xff') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
449 self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
450 [0, 65, 127, 128, 255]) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
451 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
452 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
453 class BytesTest(BaseBytesTest): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
454 type2test = bytes |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
455 |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48508
diff
changeset
|
456 def test_buffer_is_readonly(self): |
|
48710
e0c65922b878
Issue #3571: test_bytes mistakingly closed stdin
Antoine Pitrou <solipsis@pitrou.net>
parents:
48623
diff
changeset
|
457 fd = os.dup(sys.stdin.fileno()) |
|
e0c65922b878
Issue #3571: test_bytes mistakingly closed stdin
Antoine Pitrou <solipsis@pitrou.net>
parents:
48623
diff
changeset
|
458 with open(fd, "rb", buffering=0) as f: |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48508
diff
changeset
|
459 self.assertRaises(TypeError, f.readinto, b"") |
|
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48508
diff
changeset
|
460 |
|
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
48508
diff
changeset
|
461 |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
462 class ByteArrayTest(BaseBytesTest): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
463 type2test = bytearray |
|
41412
522b3f35d0eb
Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents:
41411
diff
changeset
|
464 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
465 def test_nohash(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
466 self.assertRaises(TypeError, hash, bytearray()) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
467 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
468 def test_bytearray_api(self): |
| 41855 | 469 short_sample = b"Hello world\n" |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
470 sample = short_sample + b"\0"*(20 - len(short_sample)) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
471 tfn = tempfile.mktemp() |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
472 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
473 # Prepare |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
474 with open(tfn, "wb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
475 f.write(short_sample) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
476 # Test readinto |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
477 with open(tfn, "rb") as f: |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
478 b = bytearray(20) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
479 n = f.readinto(b) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
480 self.assertEqual(n, len(short_sample)) |
|
42142
a53e48be6df9
Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42084
diff
changeset
|
481 self.assertEqual(list(b), list(sample)) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
482 # Test writing in binary mode |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
483 with open(tfn, "wb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
484 f.write(b) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
485 with open(tfn, "rb") as f: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
486 self.assertEqual(f.read(), sample) |
|
41455
5dfd2f4bc834
Support name and mode attributes on all file types.
Guido van Rossum <guido@python.org>
parents:
41451
diff
changeset
|
487 # Text mode is ambiguous; don't test |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
488 finally: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
489 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
490 os.remove(tfn) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
491 except os.error: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
492 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
493 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
494 def test_reverse(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
495 b = bytearray(b'hello') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
496 self.assertEqual(b.reverse(), None) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
497 self.assertEqual(b, b'olleh') |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
498 b = bytearray(b'hello1') # test even number of items |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
499 b.reverse() |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
500 self.assertEqual(b, b'1olleh') |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
501 b = bytearray() |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
502 b.reverse() |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
503 self.assertFalse(b) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
504 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
505 def test_regexps(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
506 def by(s): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
507 return bytearray(map(ord, s)) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
508 b = by("Hello, world") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
509 self.assertEqual(re.findall(r"\w+", b), [by("Hello"), by("world")]) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
510 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
511 def test_setitem(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
512 b = bytearray([1, 2, 3]) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
513 b[1] = 100 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
514 self.assertEqual(b, bytearray([1, 100, 3])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
515 b[-1] = 200 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
516 self.assertEqual(b, bytearray([1, 100, 200])) |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
517 b[0] = Indexable(10) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
518 self.assertEqual(b, bytearray([10, 100, 200])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
519 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
520 b[3] = 0 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
521 self.fail("Didn't raise IndexError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
522 except IndexError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
523 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
524 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
525 b[-10] = 0 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
526 self.fail("Didn't raise IndexError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
527 except IndexError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
528 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
529 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
530 b[0] = 256 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
531 self.fail("Didn't raise ValueError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
532 except ValueError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
533 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
534 try: |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
535 b[0] = Indexable(-1) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
536 self.fail("Didn't raise ValueError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
537 except ValueError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
538 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
539 try: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
540 b[0] = None |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
541 self.fail("Didn't raise TypeError") |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
542 except TypeError: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
543 pass |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
544 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
545 def test_delitem(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
546 b = bytearray(range(10)) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
547 del b[0] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
548 self.assertEqual(b, bytearray(range(1, 10))) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
549 del b[-1] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
550 self.assertEqual(b, bytearray(range(1, 9))) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
551 del b[4] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
552 self.assertEqual(b, bytearray([1, 2, 3, 4, 6, 7, 8])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
553 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
554 def test_setslice(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
555 b = bytearray(range(10)) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
556 self.assertEqual(list(b), list(range(10))) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
557 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
558 b[0:5] = bytearray([1, 1, 1, 1, 1]) |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
559 self.assertEqual(b, bytearray([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
560 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
561 del b[0:-5] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
562 self.assertEqual(b, bytearray([5, 6, 7, 8, 9])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
563 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
564 b[0:0] = bytearray([0, 1, 2, 3, 4]) |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
565 self.assertEqual(b, bytearray(range(10))) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
566 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
567 b[-7:-3] = bytearray([100, 101]) |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
568 self.assertEqual(b, bytearray([0, 1, 2, 100, 101, 7, 8, 9])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
569 |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
570 b[3:5] = [3, 4, 5, 6] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
571 self.assertEqual(b, bytearray(range(10))) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
572 |
|
39643
3dd61ffd049e
Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents:
39631
diff
changeset
|
573 b[3:0] = [42, 42, 42] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
574 self.assertEqual(b, bytearray([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
575 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
576 def test_extended_set_del_slice(self): |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
577 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
578 for start in indices: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
579 for stop in indices: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
580 # Skip invalid step 0 |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
581 for step in indices[1:]: |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
582 L = list(range(255)) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
583 b = bytearray(L) |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
584 # Make sure we have a slice of exactly the right length, |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
585 # but with different data. |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
586 data = L[start:stop:step] |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
587 data.reverse() |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
588 L[start:stop:step] = data |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
589 b[start:stop:step] = data |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
590 self.assertEquals(b, bytearray(L)) |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
591 |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
592 del L[start:stop:step] |
|
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
593 del b[start:stop:step] |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
594 self.assertEquals(b, bytearray(L)) |
|
40472
9548ca0e6560
Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents:
39643
diff
changeset
|
595 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
596 def test_setslice_trap(self): |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
597 # This test verifies that we correctly handle assigning self |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
598 # to a slice of self (the old Lambert Meertens trap). |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
599 b = bytearray(range(256)) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
600 b[8:] = b |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
601 self.assertEqual(b, bytearray(list(range(8)) + list(range(256)))) |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
602 |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
603 def test_iconcat(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
604 b = bytearray(b"abc") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
605 b1 = b |
|
42907
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
606 b += b"def" |
|
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
607 self.assertEqual(b, b"abcdef") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
608 self.assertEqual(b, b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
609 self.failUnless(b is b1) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
610 b += b"xyz" |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
611 self.assertEqual(b, b"abcdefxyz") |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
612 try: |
|
41575
beacf40b5960
Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents:
41455
diff
changeset
|
613 b += "" |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
614 except TypeError: |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
615 pass |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
616 else: |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
617 self.fail("bytes += unicode didn't raise TypeError") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
618 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
619 def test_irepeat(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
620 b = bytearray(b"abc") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
621 b1 = b |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
622 b *= 3 |
|
42907
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
623 self.assertEqual(b, b"abcabcabc") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
624 self.assertEqual(b, b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
625 self.failUnless(b is b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
626 |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
627 def test_irepeat_1char(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
628 b = bytearray(b"x") |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
629 b1 = b |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
630 b *= 100 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
631 self.assertEqual(b, b"x"*100) |
|
38017
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
632 self.assertEqual(b, b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
633 self.failUnless(b is b1) |
|
0b8e782b8484
Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents:
37960
diff
changeset
|
634 |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
635 def test_alloc(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
636 b = bytearray() |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
637 alloc = b.__alloc__() |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
638 self.assert_(alloc >= 0) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
639 seq = [alloc] |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
640 for i in range(100): |
|
42907
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
641 b += b"x" |
|
38117
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
642 alloc = b.__alloc__() |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
643 self.assert_(alloc >= len(b)) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
644 if alloc not in seq: |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
645 seq.append(alloc) |
|
57bd7c7d91e3
Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents:
38017
diff
changeset
|
646 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
647 def test_extend(self): |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
648 orig = b'hello' |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
649 a = bytearray(orig) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
650 a.extend(a) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
651 self.assertEqual(a, orig + orig) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
652 self.assertEqual(a[5:], orig) |
|
44137
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
653 a = bytearray(b'') |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
654 # Test iterators that don't have a __length_hint__ |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
655 a.extend(map(int, orig * 25)) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
656 a.extend(int(x) for x in orig * 25) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
657 self.assertEqual(a, orig * 50) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
658 self.assertEqual(a[-5:], orig) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
659 a = bytearray(b'') |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
660 a.extend(iter(map(int, orig * 50))) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
661 self.assertEqual(a, orig * 50) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
662 self.assertEqual(a[-5:], orig) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
663 a = bytearray(b'') |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
664 a.extend(list(map(int, orig * 50))) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
665 self.assertEqual(a, orig * 50) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
666 self.assertEqual(a[-5:], orig) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
667 a = bytearray(b'') |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
668 self.assertRaises(ValueError, a.extend, [0, 1, 2, 256]) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
669 self.assertRaises(ValueError, a.extend, [0, 1, 2, -1]) |
|
825cffd6c0cd
Issue #1283: Allow any iterable of integers to be passed to
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
43963
diff
changeset
|
670 self.assertEqual(len(a), 0) |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
671 a = bytearray(b'') |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
672 a.extend([Indexable(ord('a'))]) |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
673 self.assertEqual(a, b'a') |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
674 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
675 def test_remove(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
676 b = bytearray(b'hello') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
677 b.remove(ord('l')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
678 self.assertEqual(b, b'helo') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
679 b.remove(ord('l')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
680 self.assertEqual(b, b'heo') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
681 self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
682 self.assertRaises(ValueError, lambda: b.remove(400)) |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
683 self.assertRaises(TypeError, lambda: b.remove('e')) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
684 # remove first and last |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
685 b.remove(ord('o')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
686 b.remove(ord('h')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
687 self.assertEqual(b, b'e') |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
688 self.assertRaises(TypeError, lambda: b.remove(b'e')) |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
689 b.remove(Indexable(ord('e'))) |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
690 self.assertEqual(b, b'') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
691 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
692 def test_pop(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
693 b = bytearray(b'world') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
694 self.assertEqual(b.pop(), ord('d')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
695 self.assertEqual(b.pop(0), ord('w')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
696 self.assertEqual(b.pop(-2), ord('r')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
697 self.assertRaises(IndexError, lambda: b.pop(10)) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
698 self.assertRaises(OverflowError, lambda: bytearray().pop()) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
699 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
700 def test_nosort(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
701 self.assertRaises(AttributeError, lambda: bytearray().sort()) |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
702 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
703 def test_append(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
704 b = bytearray(b'hell') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
705 b.append(ord('o')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
706 self.assertEqual(b, b'hello') |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
707 self.assertEqual(b.append(100), None) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
708 b = bytearray() |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
709 b.append(ord('A')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
710 self.assertEqual(len(b), 1) |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
711 self.assertRaises(TypeError, lambda: b.append(b'o')) |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
712 b = bytearray() |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
713 b.append(Indexable(ord('A'))) |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
714 self.assertEqual(b, b'A') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
715 |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
716 def test_insert(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
717 b = bytearray(b'msssspp') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
718 b.insert(1, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
719 b.insert(4, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
720 b.insert(-2, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
721 b.insert(1000, ord('i')) |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
722 self.assertEqual(b, b'mississippi') |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
723 self.assertRaises(TypeError, lambda: b.insert(0, b'1')) |
|
48508
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
724 b = bytearray() |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
725 b.insert(0, Indexable(ord('A'))) |
|
b9828b2a51c0
Merged revisions 65041 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47391
diff
changeset
|
726 self.assertEqual(b, b'A') |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
727 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
728 def test_partition_bytearray_doesnt_share_nullstring(self): |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
729 a, b, c = bytearray(b"x").partition(b"y") |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
730 self.assertEqual(b, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
731 self.assertEqual(c, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
732 self.assert_(b is not c) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
733 b += b"!" |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
734 self.assertEqual(c, b"") |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
735 a, b, c = bytearray(b"x").partition(b"y") |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
736 self.assertEqual(b, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
737 self.assertEqual(c, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
738 # Same for rpartition |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
739 b, c, a = bytearray(b"x").rpartition(b"y") |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
740 self.assertEqual(b, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
741 self.assertEqual(c, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
742 self.assert_(b is not c) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
743 b += b"!" |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
744 self.assertEqual(c, b"") |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
745 c, b, a = bytearray(b"x").rpartition(b"y") |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
746 self.assertEqual(b, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
747 self.assertEqual(c, b"") |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
748 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43757
diff
changeset
|
749 |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
750 class AssortedBytesTest(unittest.TestCase): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
751 # |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
752 # Test various combinations of bytes and bytearray |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
753 # |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
754 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
755 def setUp(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
756 self.warning_filters = warnings.filters[:] |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
757 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
758 def tearDown(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
759 warnings.filters = self.warning_filters |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
760 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
761 def test_repr_str(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
762 warnings.simplefilter('ignore', BytesWarning) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
763 for f in str, repr: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
764 self.assertEqual(f(bytearray()), "bytearray(b'')") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
765 self.assertEqual(f(bytearray([0])), "bytearray(b'\\x00')") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
766 self.assertEqual(f(bytearray([0, 1, 254, 255])), |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
767 "bytearray(b'\\x00\\x01\\xfe\\xff')") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
768 self.assertEqual(f(b"abc"), "b'abc'") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
769 self.assertEqual(f(b"'"), '''b"'"''') # ''' |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
770 self.assertEqual(f(b"'\""), r"""b'\'"'""") # ' |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
771 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
772 def test_compare_bytes_to_bytearray(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
773 self.assertEqual(b"abc" == bytes(b"abc"), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
774 self.assertEqual(b"ab" != bytes(b"abc"), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
775 self.assertEqual(b"ab" <= bytes(b"abc"), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
776 self.assertEqual(b"ab" < bytes(b"abc"), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
777 self.assertEqual(b"abc" >= bytes(b"ab"), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
778 self.assertEqual(b"abc" > bytes(b"ab"), True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
779 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
780 self.assertEqual(b"abc" != bytes(b"abc"), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
781 self.assertEqual(b"ab" == bytes(b"abc"), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
782 self.assertEqual(b"ab" > bytes(b"abc"), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
783 self.assertEqual(b"ab" >= bytes(b"abc"), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
784 self.assertEqual(b"abc" < bytes(b"ab"), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
785 self.assertEqual(b"abc" <= bytes(b"ab"), False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
786 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
787 self.assertEqual(bytes(b"abc") == b"abc", True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
788 self.assertEqual(bytes(b"ab") != b"abc", True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
789 self.assertEqual(bytes(b"ab") <= b"abc", True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
790 self.assertEqual(bytes(b"ab") < b"abc", True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
791 self.assertEqual(bytes(b"abc") >= b"ab", True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
792 self.assertEqual(bytes(b"abc") > b"ab", True) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
793 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
794 self.assertEqual(bytes(b"abc") != b"abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
795 self.assertEqual(bytes(b"ab") == b"abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
796 self.assertEqual(bytes(b"ab") > b"abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
797 self.assertEqual(bytes(b"ab") >= b"abc", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
798 self.assertEqual(bytes(b"abc") < b"ab", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
799 self.assertEqual(bytes(b"abc") <= b"ab", False) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
800 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
801 def test_doc(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
802 self.failUnless(bytearray.__doc__ != None) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
803 self.failUnless(bytearray.__doc__.startswith("bytearray("), bytearray.__doc__) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
804 self.failUnless(bytes.__doc__ != None) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
805 self.failUnless(bytes.__doc__.startswith("bytes("), bytes.__doc__) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
806 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
807 def test_from_bytearray(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
808 sample = bytes(b"Hello world\n\x80\x81\xfe\xff") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
809 buf = memoryview(sample) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
810 b = bytearray(buf) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
811 self.assertEqual(b, bytearray(sample)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
812 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
813 def test_to_str(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
814 warnings.simplefilter('ignore', BytesWarning) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
815 self.assertEqual(str(b''), "b''") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
816 self.assertEqual(str(b'x'), "b'x'") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
817 self.assertEqual(str(b'\x80'), "b'\\x80'") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
818 self.assertEqual(str(bytearray(b'')), "bytearray(b'')") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
819 self.assertEqual(str(bytearray(b'x')), "bytearray(b'x')") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
820 self.assertEqual(str(bytearray(b'\x80')), "bytearray(b'\\x80')") |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
821 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
822 def test_literal(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
823 tests = [ |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
824 (b"Wonderful spam", "Wonderful spam"), |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
825 (br"Wonderful spam too", "Wonderful spam too"), |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
826 (b"\xaa\x00\000\200", "\xaa\x00\000\200"), |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
827 (br"\xaa\x00\000\200", r"\xaa\x00\000\200"), |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
828 ] |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
829 for b, s in tests: |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
830 self.assertEqual(b, bytearray(s, 'latin-1')) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
831 for c in range(128, 256): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
832 self.assertRaises(SyntaxError, eval, |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
833 'b"%s"' % chr(c)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
834 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
835 def test_translate(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
836 b = b'hello' |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
837 rosetta = bytearray(range(0, 256)) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
838 rosetta[ord('o')] = ord('e') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
839 c = b.translate(rosetta, b'l') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
840 self.assertEqual(b, b'hello') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
841 self.assertEqual(c, b'hee') |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
842 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
843 def test_split_bytearray(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
844 self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
845 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
846 def test_rsplit_bytearray(self): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
847 self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b']) |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
848 |
|
47391
eca882731194
Add test case for r63810.
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
849 def test_return_self(self): |
|
eca882731194
Add test case for r63810.
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
850 # bytearray.replace must always return a new bytearray |
|
eca882731194
Add test case for r63810.
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
851 b = bytearray() |
|
eca882731194
Add test case for r63810.
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
852 self.failIf(b.replace(b'', b'') is b) |
|
eca882731194
Add test case for r63810.
Georg Brandl <georg@python.org>
parents:
47201
diff
changeset
|
853 |
|
37960
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
854 # Optimizations: |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
855 # __iter__? (optimization) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
856 # __reversed__? (optimization) |
|
e0b7e34b5971
Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents:
37930
diff
changeset
|
857 |
|
41450
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
858 # XXX More string methods? (Those that don't use character properties) |
|
d1c3dd184e38
Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents:
41425
diff
changeset
|
859 |
|
40978
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
860 # There are tests in string_tests.py that are more |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
861 # comprehensive for things like split, partition, etc. |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
862 # Unfortunately they are all bundled with tests that |
|
61fee1853ca8
SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40967
diff
changeset
|
863 # are not appropriate for bytes |
|
37930
e1252cea439c
Fix a leak and a buglet discovered by Thomas.
Guido van Rossum <guido@python.org>
parents:
37928
diff
changeset
|
864 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
865 # I've started porting some of those into bytearray_tests.py, we should port |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
866 # the rest that make sense (the code can be cleaned up to use modern |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
867 # unittest methods at the same time). |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
868 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
869 class BytearrayPEP3137Test(unittest.TestCase, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
870 test.buffer_tests.MixinBytesBufferCommonTests): |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
871 def marshal(self, x): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
872 return bytearray(x) |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
873 |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
874 def test_returns_new_copy(self): |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
875 val = self.marshal(b'1234') |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
876 # On immutable types these MAY return a reference to themselves |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
877 # but on mutable types like bytearray they MUST return a new copy. |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
878 for methname in ('zfill', 'rjust', 'ljust', 'center'): |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
879 method = getattr(val, methname) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
880 newval = method(3) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
881 self.assertEqual(val, newval) |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
882 self.assertTrue(val is not newval, |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
883 methname+' returned self on a mutable object') |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43508
diff
changeset
|
884 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
885 |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
886 class FixedStringTest(test.string_tests.BaseTest): |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
887 |
|
42907
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
888 def fixtype(self, obj): |
|
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
889 if isinstance(obj, str): |
|
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
890 return obj.encode("utf-8") |
|
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
891 return super().fixtype(obj) |
|
0170418dadea
Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents:
42499
diff
changeset
|
892 |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
893 # Currently the bytes containment testing uses a single integer |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
894 # value. This may not be the final design, but until then the |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
895 # bytes section with in a bytes containment not valid |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
896 def test_contains(self): |
|
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
897 pass |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41855
diff
changeset
|
898 def test_expandtabs(self): |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41855
diff
changeset
|
899 pass |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41855
diff
changeset
|
900 def test_upper(self): |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41855
diff
changeset
|
901 pass |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41855
diff
changeset
|
902 def test_lower(self): |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41855
diff
changeset
|
903 pass |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
904 |
|
45055
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
905 class ByteArrayAsStringTest(FixedStringTest): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
906 type2test = bytearray |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
907 |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
908 class BytesAsStringTest(FixedStringTest): |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
909 type2test = bytes |
|
510a48289d1d
Patch #1972 by Antoine Pitrou: improve bytes and bytearray tests
Christian Heimes <christian@cheimes.de>
parents:
45050
diff
changeset
|
910 |
|
41038
efa0a761918c
Addendum of patch #1669633: additional tests for bytes methods.
Georg Brandl <georg@python.org>
parents:
40978
diff
changeset
|
911 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
912 class ByteArraySubclass(bytearray): |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
913 pass |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
914 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
915 class ByteArraySubclassTest(unittest.TestCase): |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
916 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
917 def test_basic(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
918 self.assert_(issubclass(ByteArraySubclass, bytearray)) |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
919 self.assert_(isinstance(ByteArraySubclass(), bytearray)) |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
920 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
921 a, b = b"abcd", b"efgh" |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
922 _a, _b = ByteArraySubclass(a), ByteArraySubclass(b) |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
923 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
924 # test comparison operators with subclass instances |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
925 self.assert_(_a == _a) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
926 self.assert_(_a != _b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
927 self.assert_(_a < _b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
928 self.assert_(_a <= _b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
929 self.assert_(_b >= _a) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
930 self.assert_(_b > _a) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
931 self.assert_(_a is not a) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
932 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
933 # test concat of subclass instances |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
934 self.assertEqual(a + b, _a + _b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
935 self.assertEqual(a + b, a + _b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
936 self.assertEqual(a + b, _a + b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
937 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
938 # test repeat |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
939 self.assert_(a*5 == _a*5) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
940 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
941 def test_join(self): |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
942 # Make sure join returns a NEW object for single item sequences |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
943 # involving a subclass. |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
944 # Make sure that it is of the appropriate type. |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
945 s1 = ByteArraySubclass(b"abcd") |
|
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
946 s2 = bytearray().join([s1]) |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
947 self.assert_(s1 is not s2) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
948 self.assert_(type(s2) is bytearray, type(s2)) |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
949 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
950 # Test reverse, calling join on subclass |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
951 s3 = s1.join([b"abcd"]) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
952 self.assert_(type(s3) is bytearray) |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
953 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
954 def test_pickle(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
955 a = ByteArraySubclass(b"abcd") |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
956 a.x = 10 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
957 a.y = ByteArraySubclass(b"efgh") |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
958 for proto in range(pickle.HIGHEST_PROTOCOL): |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
959 b = pickle.loads(pickle.dumps(a, proto)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
960 self.assertNotEqual(id(a), id(b)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
961 self.assertEqual(a, b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
962 self.assertEqual(a.x, b.x) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
963 self.assertEqual(a.y, b.y) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
964 self.assertEqual(type(a), type(b)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
965 self.assertEqual(type(a.y), type(b.y)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
966 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
967 def test_copy(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
968 a = ByteArraySubclass(b"abcd") |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
969 a.x = 10 |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
970 a.y = ByteArraySubclass(b"efgh") |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
971 for copy_method in (copy.copy, copy.deepcopy): |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
972 b = copy_method(a) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
973 self.assertNotEqual(id(a), id(b)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
974 self.assertEqual(a, b) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
975 self.assertEqual(a.x, b.x) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
976 self.assertEqual(a.y, b.y) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
977 self.assertEqual(type(a), type(b)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
978 self.assertEqual(type(a.y), type(b.y)) |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
979 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
980 def test_init_override(self): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
981 class subclass(bytearray): |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
982 def __init__(self, newarg=1, *args, **kwargs): |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
983 bytearray.__init__(self, *args, **kwargs) |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
984 x = subclass(4, source=b"abcd") |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
985 self.assertEqual(x, b"abcd") |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
986 x = subclass(newarg=4, source=b"abcd") |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
987 self.assertEqual(x, b"abcd") |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
988 |
|
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
989 |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
990 def test_main(): |
|
47201
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
991 test.support.run_unittest(BytesTest) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
992 test.support.run_unittest(ByteArrayTest) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
993 test.support.run_unittest(AssortedBytesTest) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
994 test.support.run_unittest(BytesAsStringTest) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
995 test.support.run_unittest(ByteArrayAsStringTest) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
996 test.support.run_unittest(ByteArraySubclassTest) |
|
9c930a32cebb
#2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents:
45058
diff
changeset
|
997 test.support.run_unittest(BytearrayPEP3137Test) |
|
37927
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
998 |
|
a7f3765ea464
Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
999 if __name__ == "__main__": |
|
43757
2ff0e81e68fc
Patch 1171 by mfenniak -- allow subclassing of bytes.
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
1000 test_main() |
