blob: 18fecc382901e8e4770741e7ce9cd21e0060160f [file] [log] [blame]
Mark Dickinson5fc16b42010-04-03 10:49:56 +00001from _testcapi import _test_structmembersType, \
Thomas Wouters89f507f2006-12-13 04:49:30 +00002 CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
3 SHRT_MAX, SHRT_MIN, USHRT_MAX, \
4 INT_MAX, INT_MIN, UINT_MAX, \
Guido van Rossumcd16bf62007-06-13 18:07:49 +00005 LONG_MAX, LONG_MIN, ULONG_MAX, \
Mark Dickinsond59b4162010-03-13 11:34:40 +00006 LLONG_MAX, LLONG_MIN, ULLONG_MAX, \
7 PY_SSIZE_T_MAX, PY_SSIZE_T_MIN
Thomas Wouters89f507f2006-12-13 04:49:30 +00008
Georg Brandl1b37e872010-03-14 10:45:50 +00009import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
Thomas Wouters89f507f2006-12-13 04:49:30 +000011
Mark Dickinson5fc16b42010-04-03 10:49:56 +000012ts=_test_structmembersType(False, # T_BOOL
Mark Dickinsond59b4162010-03-13 11:34:40 +000013 1, # T_BYTE
14 2, # T_UBYTE
15 3, # T_SHORT
16 4, # T_USHORT
17 5, # T_INT
18 6, # T_UINT
19 7, # T_LONG
20 8, # T_ULONG
21 23, # T_PYSSIZET
22 9.99999,# T_FLOAT
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000023 10.1010101010, # T_DOUBLE
24 "hi" # T_STRING_INPLACE
Mark Dickinsond59b4162010-03-13 11:34:40 +000025 )
Thomas Wouters89f507f2006-12-13 04:49:30 +000026
27class ReadWriteTests(unittest.TestCase):
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000028
29 def test_bool(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000030 ts.T_BOOL = True
Ezio Melottib3aedd42010-11-20 19:04:17 +000031 self.assertEqual(ts.T_BOOL, True)
Christian Heimes1af737c2008-01-23 08:24:23 +000032 ts.T_BOOL = False
Ezio Melottib3aedd42010-11-20 19:04:17 +000033 self.assertEqual(ts.T_BOOL, False)
Christian Heimes1af737c2008-01-23 08:24:23 +000034 self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
35
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000036 def test_byte(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000037 ts.T_BYTE = CHAR_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000038 self.assertEqual(ts.T_BYTE, CHAR_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000039 ts.T_BYTE = CHAR_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000040 self.assertEqual(ts.T_BYTE, CHAR_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000041 ts.T_UBYTE = UCHAR_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000042 self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000043
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000044 def test_short(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000045 ts.T_SHORT = SHRT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000046 self.assertEqual(ts.T_SHORT, SHRT_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000047 ts.T_SHORT = SHRT_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000048 self.assertEqual(ts.T_SHORT, SHRT_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000049 ts.T_USHORT = USHRT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000050 self.assertEqual(ts.T_USHORT, USHRT_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000051
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000052 def test_int(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000053 ts.T_INT = INT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000054 self.assertEqual(ts.T_INT, INT_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000055 ts.T_INT = INT_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000056 self.assertEqual(ts.T_INT, INT_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000057 ts.T_UINT = UINT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000058 self.assertEqual(ts.T_UINT, UINT_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000059
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000060 def test_long(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000061 ts.T_LONG = LONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000062 self.assertEqual(ts.T_LONG, LONG_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000063 ts.T_LONG = LONG_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000064 self.assertEqual(ts.T_LONG, LONG_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000065 ts.T_ULONG = ULONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000066 self.assertEqual(ts.T_ULONG, ULONG_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000067
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000068 def test_py_ssize_t(self):
Mark Dickinsond59b4162010-03-13 11:34:40 +000069 ts.T_PYSSIZET = PY_SSIZE_T_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000070 self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX)
Mark Dickinsond59b4162010-03-13 11:34:40 +000071 ts.T_PYSSIZET = PY_SSIZE_T_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000072 self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN)
Mark Dickinsond59b4162010-03-13 11:34:40 +000073
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000074 @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
75 def test_longlong(self):
76 ts.T_LONGLONG = LLONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000077 self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000078 ts.T_LONGLONG = LLONG_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000079 self.assertEqual(ts.T_LONGLONG, LLONG_MIN)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000080
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000081 ts.T_ULONGLONG = ULLONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000082 self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000083
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000084 ## make sure these will accept a plain int as well as a long
85 ts.T_LONGLONG = 3
Ezio Melottib3aedd42010-11-20 19:04:17 +000086 self.assertEqual(ts.T_LONGLONG, 3)
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000087 ts.T_ULONGLONG = 4
Ezio Melottib3aedd42010-11-20 19:04:17 +000088 self.assertEqual(ts.T_ULONGLONG, 4)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000089
Mark Dickinsond59b4162010-03-13 11:34:40 +000090 def test_bad_assignments(self):
Mark Dickinsond59b4162010-03-13 11:34:40 +000091 integer_attributes = [
92 'T_BOOL',
93 'T_BYTE', 'T_UBYTE',
94 'T_SHORT', 'T_USHORT',
Mark Dickinson252e4002010-03-13 13:23:05 +000095 'T_INT', 'T_UINT',
96 'T_LONG', 'T_ULONG',
Mark Dickinsond59b4162010-03-13 11:34:40 +000097 'T_PYSSIZET'
98 ]
Mark Dickinsond59b4162010-03-13 11:34:40 +000099 if hasattr(ts, 'T_LONGLONG'):
100 integer_attributes.extend(['T_LONGLONG', 'T_ULONGLONG'])
101
102 # issue8014: this produced 'bad argument to internal function'
103 # internal error
104 for nonint in None, 3.2j, "full of eels", {}, []:
105 for attr in integer_attributes:
106 self.assertRaises(TypeError, setattr, ts, attr, nonint)
107
Benjamin Peterson80d8fd82010-04-03 01:40:24 +0000108 def test_inplace_string(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000109 self.assertEqual(ts.T_STRING_INPLACE, "hi")
Benjamin Peterson80d8fd82010-04-03 01:40:24 +0000110 self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
111 self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
112
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000113
Guido van Rossumd8faa362007-04-27 19:54:29 +0000114class TestWarnings(unittest.TestCase):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115
116 def test_byte_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000117 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000118 ts.T_BYTE = CHAR_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119
120 def test_byte_min(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000121 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000122 ts.T_BYTE = CHAR_MIN-1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000123
124 def test_ubyte_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000125 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000126 ts.T_UBYTE = UCHAR_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000127
128 def test_short_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000129 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000130 ts.T_SHORT = SHRT_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000131
132 def test_short_min(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000133 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000134 ts.T_SHORT = SHRT_MIN-1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000135
136 def test_ushort_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000137 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000138 ts.T_USHORT = USHRT_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139
140
141def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000142 support.run_unittest(__name__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143
144if __name__ == "__main__":
145 test_main(verbose=True)