1import unittest
2from test import test_support
3
4# Skip this test if the _testcapi module isn't available.
5test_support.import_module('_testcapi')
6from _testcapi import _test_structmembersType, \
7    CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
8    SHRT_MAX, SHRT_MIN, USHRT_MAX, \
9    INT_MAX, INT_MIN, UINT_MAX, \
10    LONG_MAX, LONG_MIN, ULONG_MAX, \
11    LLONG_MAX, LLONG_MIN, ULLONG_MAX
12
13ts=_test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8,
14                          9.99999, 10.1010101010, "hi")
15
16class ReadWriteTests(unittest.TestCase):
17
18    def test_bool(self):
19        ts.T_BOOL = True
20        self.assertEqual(ts.T_BOOL, True)
21        ts.T_BOOL = False
22        self.assertEqual(ts.T_BOOL, False)
23        self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
24
25    def test_byte(self):
26        ts.T_BYTE = CHAR_MAX
27        self.assertEqual(ts.T_BYTE, CHAR_MAX)
28        ts.T_BYTE = CHAR_MIN
29        self.assertEqual(ts.T_BYTE, CHAR_MIN)
30        ts.T_UBYTE = UCHAR_MAX
31        self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
32
33    def test_short(self):
34        ts.T_SHORT = SHRT_MAX
35        self.assertEqual(ts.T_SHORT, SHRT_MAX)
36        ts.T_SHORT = SHRT_MIN
37        self.assertEqual(ts.T_SHORT, SHRT_MIN)
38        ts.T_USHORT = USHRT_MAX
39        self.assertEqual(ts.T_USHORT, USHRT_MAX)
40
41    def test_int(self):
42        ts.T_INT = INT_MAX
43        self.assertEqual(ts.T_INT, INT_MAX)
44        ts.T_INT = INT_MIN
45        self.assertEqual(ts.T_INT, INT_MIN)
46        ts.T_UINT = UINT_MAX
47        self.assertEqual(ts.T_UINT, UINT_MAX)
48
49    def test_long(self):
50        ts.T_LONG = LONG_MAX
51        self.assertEqual(ts.T_LONG, LONG_MAX)
52        ts.T_LONG = LONG_MIN
53        self.assertEqual(ts.T_LONG, LONG_MIN)
54        ts.T_ULONG = ULONG_MAX
55        self.assertEqual(ts.T_ULONG, ULONG_MAX)
56
57    @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
58    def test_longlong(self):
59        ts.T_LONGLONG = LLONG_MAX
60        self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
61        ts.T_LONGLONG = LLONG_MIN
62        self.assertEqual(ts.T_LONGLONG, LLONG_MIN)
63
64        ts.T_ULONGLONG = ULLONG_MAX
65        self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX)
66
67        ## make sure these will accept a plain int as well as a long
68        ts.T_LONGLONG = 3
69        self.assertEqual(ts.T_LONGLONG, 3)
70        ts.T_ULONGLONG = 4
71        self.assertEqual(ts.T_ULONGLONG, 4)
72
73    def test_inplace_string(self):
74        self.assertEqual(ts.T_STRING_INPLACE, "hi")
75        self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
76        self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
77
78
79class TestWarnings(unittest.TestCase):
80
81    def test_byte_max(self):
82        with test_support.check_warnings(('', RuntimeWarning)):
83            ts.T_BYTE = CHAR_MAX+1
84
85    def test_byte_min(self):
86        with test_support.check_warnings(('', RuntimeWarning)):
87            ts.T_BYTE = CHAR_MIN-1
88
89    def test_ubyte_max(self):
90        with test_support.check_warnings(('', RuntimeWarning)):
91            ts.T_UBYTE = UCHAR_MAX+1
92
93    def test_short_max(self):
94        with test_support.check_warnings(('', RuntimeWarning)):
95            ts.T_SHORT = SHRT_MAX+1
96
97    def test_short_min(self):
98        with test_support.check_warnings(('', RuntimeWarning)):
99            ts.T_SHORT = SHRT_MIN-1
100
101    def test_ushort_max(self):
102        with test_support.check_warnings(('', RuntimeWarning)):
103            ts.T_USHORT = USHRT_MAX+1
104
105
106def test_main(verbose=None):
107    test_support.run_unittest(__name__)
108
109if __name__ == "__main__":
110    test_main(verbose=True)
111