test_structmembers.py 4.7 KB
Newer Older
1 2 3 4 5
import unittest
from test import support

# Skip this test if the _testcapi module isn't available.
support.import_module('_testcapi')
6
from _testcapi import _test_structmembersType, \
7 8 9
    CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
    SHRT_MAX, SHRT_MIN, USHRT_MAX, \
    INT_MAX, INT_MIN, UINT_MAX, \
10
    LONG_MAX, LONG_MIN, ULONG_MAX, \
11 12
    LLONG_MAX, LLONG_MIN, ULLONG_MAX, \
    PY_SSIZE_T_MAX, PY_SSIZE_T_MIN
13

14
ts=_test_structmembersType(False,  # T_BOOL
15 16 17 18 19 20 21 22 23 24
                          1,      # T_BYTE
                          2,      # T_UBYTE
                          3,      # T_SHORT
                          4,      # T_USHORT
                          5,      # T_INT
                          6,      # T_UINT
                          7,      # T_LONG
                          8,      # T_ULONG
                          23,     # T_PYSSIZET
                          9.99999,# T_FLOAT
25 26
                          10.1010101010, # T_DOUBLE
                          "hi" # T_STRING_INPLACE
27
                          )
28 29

class ReadWriteTests(unittest.TestCase):
30 31

    def test_bool(self):
32
        ts.T_BOOL = True
33
        self.assertEqual(ts.T_BOOL, True)
34
        ts.T_BOOL = False
35
        self.assertEqual(ts.T_BOOL, False)
36 37
        self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)

38
    def test_byte(self):
39
        ts.T_BYTE = CHAR_MAX
40
        self.assertEqual(ts.T_BYTE, CHAR_MAX)
41
        ts.T_BYTE = CHAR_MIN
42
        self.assertEqual(ts.T_BYTE, CHAR_MIN)
43
        ts.T_UBYTE = UCHAR_MAX
44
        self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
45

46
    def test_short(self):
47
        ts.T_SHORT = SHRT_MAX
48
        self.assertEqual(ts.T_SHORT, SHRT_MAX)
49
        ts.T_SHORT = SHRT_MIN
50
        self.assertEqual(ts.T_SHORT, SHRT_MIN)
51
        ts.T_USHORT = USHRT_MAX
52
        self.assertEqual(ts.T_USHORT, USHRT_MAX)
53

54
    def test_int(self):
55
        ts.T_INT = INT_MAX
56
        self.assertEqual(ts.T_INT, INT_MAX)
57
        ts.T_INT = INT_MIN
58
        self.assertEqual(ts.T_INT, INT_MIN)
59
        ts.T_UINT = UINT_MAX
60
        self.assertEqual(ts.T_UINT, UINT_MAX)
61

62
    def test_long(self):
63
        ts.T_LONG = LONG_MAX
64
        self.assertEqual(ts.T_LONG, LONG_MAX)
65
        ts.T_LONG = LONG_MIN
66
        self.assertEqual(ts.T_LONG, LONG_MIN)
67
        ts.T_ULONG = ULONG_MAX
68
        self.assertEqual(ts.T_ULONG, ULONG_MAX)
69

70
    def test_py_ssize_t(self):
71
        ts.T_PYSSIZET = PY_SSIZE_T_MAX
72
        self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX)
73
        ts.T_PYSSIZET = PY_SSIZE_T_MIN
74
        self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN)
75

76 77 78
    @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
    def test_longlong(self):
        ts.T_LONGLONG = LLONG_MAX
79
        self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
80
        ts.T_LONGLONG = LLONG_MIN
81
        self.assertEqual(ts.T_LONGLONG, LLONG_MIN)
82

83
        ts.T_ULONGLONG = ULLONG_MAX
84
        self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX)
85

86 87
        ## make sure these will accept a plain int as well as a long
        ts.T_LONGLONG = 3
88
        self.assertEqual(ts.T_LONGLONG, 3)
89
        ts.T_ULONGLONG = 4
90
        self.assertEqual(ts.T_ULONGLONG, 4)
91

92 93 94 95 96
    def test_bad_assignments(self):
        integer_attributes = [
            'T_BOOL',
            'T_BYTE', 'T_UBYTE',
            'T_SHORT', 'T_USHORT',
97 98
            'T_INT', 'T_UINT',
            'T_LONG', 'T_ULONG',
99 100 101 102 103 104 105 106 107 108 109
            'T_PYSSIZET'
            ]
        if hasattr(ts, 'T_LONGLONG'):
            integer_attributes.extend(['T_LONGLONG', 'T_ULONGLONG'])

        # issue8014: this produced 'bad argument to internal function'
        # internal error
        for nonint in None, 3.2j, "full of eels", {}, []:
            for attr in integer_attributes:
                self.assertRaises(TypeError, setattr, ts, attr, nonint)

110
    def test_inplace_string(self):
111
        self.assertEqual(ts.T_STRING_INPLACE, "hi")
112 113 114
        self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
        self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")

115

116
class TestWarnings(unittest.TestCase):
117 118

    def test_byte_max(self):
119
        with support.check_warnings(('', RuntimeWarning)):
120
            ts.T_BYTE = CHAR_MAX+1
121 122

    def test_byte_min(self):
123
        with support.check_warnings(('', RuntimeWarning)):
124
            ts.T_BYTE = CHAR_MIN-1
125 126

    def test_ubyte_max(self):
127
        with support.check_warnings(('', RuntimeWarning)):
128
            ts.T_UBYTE = UCHAR_MAX+1
129 130

    def test_short_max(self):
131
        with support.check_warnings(('', RuntimeWarning)):
132
            ts.T_SHORT = SHRT_MAX+1
133 134

    def test_short_min(self):
135
        with support.check_warnings(('', RuntimeWarning)):
136
            ts.T_SHORT = SHRT_MIN-1
137 138

    def test_ushort_max(self):
139
        with support.check_warnings(('', RuntimeWarning)):
140
            ts.T_USHORT = USHRT_MAX+1
141 142 143


if __name__ == "__main__":
144
    unittest.main()