summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_bytes.py
blob: 1ba5e11ab772803e0f82f3b00dca9980cfeaa43c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""Unit tests for the bytes type."""

import sys
import unittest
import test.test_support


class BytesTest(unittest.TestCase):

    def test_basics(self):
        b = bytes()
        self.assertEqual(type(b), bytes)
        self.assertEqual(b.__class__, bytes)

    def test_empty_sequence(self):
        b = bytes()
        self.assertEqual(len(b), 0)
        self.assertRaises(IndexError, lambda: b[0])
        self.assertRaises(IndexError, lambda: b[1])
        self.assertRaises(IndexError, lambda: b[sys.maxint])
        self.assertRaises(IndexError, lambda: b[sys.maxint+1])
        self.assertRaises(IndexError, lambda: b[10**100])
        self.assertRaises(IndexError, lambda: b[-1])
        self.assertRaises(IndexError, lambda: b[-2])
        self.assertRaises(IndexError, lambda: b[-sys.maxint])
        self.assertRaises(IndexError, lambda: b[-sys.maxint-1])
        self.assertRaises(IndexError, lambda: b[-sys.maxint-2])
        self.assertRaises(IndexError, lambda: b[-10**100])

    def test_from_list(self):
        ints = list(range(256))
        b = bytes(i for i in ints)
        self.assertEqual(len(b), 256)
        self.assertEqual(list(b), ints)

    def test_from_index(self):
        class C:
            def __init__(self, i=0):
                self.i = i
            def __index__(self):
                return self.i
        b = bytes([C(), C(1), C(254), C(255)])
        self.assertEqual(list(b), [0, 1, 254, 255])
        self.assertRaises(ValueError, bytes, [C(-1)])
        self.assertRaises(ValueError, bytes, [C(256)])

    def test_constructor_type_errors(self):
        self.assertRaises(TypeError, bytes, 0)
        class C:
            pass
        self.assertRaises(TypeError, bytes, ["0"])
        self.assertRaises(TypeError, bytes, [0.0])
        self.assertRaises(TypeError, bytes, [None])
        self.assertRaises(TypeError, bytes, [C()])

    def test_constructor_value_errors(self):
        self.assertRaises(ValueError, bytes, [-1])
        self.assertRaises(ValueError, bytes, [-sys.maxint])
        self.assertRaises(ValueError, bytes, [-sys.maxint-1])
        self.assertRaises(ValueError, bytes, [-sys.maxint-2])
        self.assertRaises(ValueError, bytes, [-10**100])
        self.assertRaises(ValueError, bytes, [256])
        self.assertRaises(ValueError, bytes, [257])
        self.assertRaises(ValueError, bytes, [sys.maxint])
        self.assertRaises(ValueError, bytes, [sys.maxint+1])
        self.assertRaises(ValueError, bytes, [10**100])

    def test_repr(self):
        self.assertEqual(repr(bytes()), "bytes()")
        self.assertEqual(repr(bytes([0])), "bytes([0x00])")
        self.assertEqual(repr(bytes([0, 1, 254, 255])), "bytes([0x00, 0x01, 0xfe, 0xff])")

    def test_compare(self):
        b1 = bytes([1, 2, 3])
        b2 = bytes([1, 2, 3])
        b3 = bytes([1, 3])

        self.failUnless(b1 == b2)
        self.failUnless(b2 != b3)
        self.failUnless(b1 <= b2)
        self.failUnless(b1 <= b3)
        self.failUnless(b1 <  b3)
        self.failUnless(b1 >= b2)
        self.failUnless(b3 >= b2)
        self.failUnless(b3 >  b2)

        self.failIf(b1 != b2)
        self.failIf(b2 == b3)
        self.failIf(b1 >  b2)
        self.failIf(b1 >  b3)
        self.failIf(b1 >= b3)
        self.failIf(b1 <  b2)
        self.failIf(b3 <  b2)
        self.failIf(b3 <= b2)

    def test_nohash(self):
        self.assertRaises(TypeError, hash, bytes())

    def test_doc(self):
        self.failUnless(bytes.__doc__ != None)
        self.failUnless(bytes.__doc__.startswith("bytes("))

    # XXX More stuff to test and build (TDD):
    # constructor from str: bytes(<str>) == bytes(map(ord, <str>))?
    # encoding constructor: bytes(<unicode>[, <encoding>[, <errors>]])
    # default encoding Latin-1? (Matching ord)
    # slicing
    # extended slicing?
    # item assignment
    # slice assignment
    # extended slice assignment?
    # __contains__ with simple int arg
    # __contains__ with another bytes arg?
    # find/index? (int or bytes arg?)
    # count? (int arg)
    # concatenation (+)
    # repeat?
    # extend?
    # append?
    # insert?
    # pop?
    # __reversed__?
    # reverse? (inplace)
    # NOT sort!
    # __iter__? (optimization)
    # __str__? (could return "".join(map(chr, self))
    # decode
    # buffer API
    # check that regexp searches work
    # (I suppose re.sub() returns a string)
    # file.readinto
    # file.write


def test_main():
    test.test_support.run_unittest(BytesTest)


if __name__ == "__main__":
    ##test_main()
    unittest.main()