blob: 1c931ae77815fab6e69bb846aaf0ca382334a5a2 [file] [log] [blame]
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02001import unittest
2from test import support
3
4# Skip this test if the _testcapi module isn't available.
5support.import_module('_testcapi')
Mark Dickinson5fc16b42010-04-03 10:49:56 +00006from _testcapi import _test_structmembersType, \
Thomas Wouters89f507f2006-12-13 04:49:30 +00007 CHAR_MAX, CHAR_MIN, UCHAR_MAX, \
8 SHRT_MAX, SHRT_MIN, USHRT_MAX, \
9 INT_MAX, INT_MIN, UINT_MAX, \
Guido van Rossumcd16bf62007-06-13 18:07:49 +000010 LONG_MAX, LONG_MIN, ULONG_MAX, \
Mark Dickinsond59b4162010-03-13 11:34:40 +000011 LLONG_MAX, LLONG_MIN, ULLONG_MAX, \
12 PY_SSIZE_T_MAX, PY_SSIZE_T_MIN
Thomas Wouters89f507f2006-12-13 04:49:30 +000013
Mark Dickinson5fc16b42010-04-03 10:49:56 +000014ts=_test_structmembersType(False, # T_BOOL
Mark Dickinsond59b4162010-03-13 11:34:40 +000015 1, # T_BYTE
16 2, # T_UBYTE
17 3, # T_SHORT
18 4, # T_USHORT
19 5, # T_INT
20 6, # T_UINT
21 7, # T_LONG
22 8, # T_ULONG
23 23, # T_PYSSIZET
24 9.99999,# T_FLOAT
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000025 10.1010101010, # T_DOUBLE
26 "hi" # T_STRING_INPLACE
Mark Dickinsond59b4162010-03-13 11:34:40 +000027 )
Thomas Wouters89f507f2006-12-13 04:49:30 +000028
29class ReadWriteTests(unittest.TestCase):
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000030
31 def test_bool(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000032 ts.T_BOOL = True
Ezio Melottib3aedd42010-11-20 19:04:17 +000033 self.assertEqual(ts.T_BOOL, True)
Christian Heimes1af737c2008-01-23 08:24:23 +000034 ts.T_BOOL = False
Ezio Melottib3aedd42010-11-20 19:04:17 +000035 self.assertEqual(ts.T_BOOL, False)
Christian Heimes1af737c2008-01-23 08:24:23 +000036 self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
37
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000038 def test_byte(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000039 ts.T_BYTE = CHAR_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000040 self.assertEqual(ts.T_BYTE, CHAR_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000041 ts.T_BYTE = CHAR_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000042 self.assertEqual(ts.T_BYTE, CHAR_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000043 ts.T_UBYTE = UCHAR_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000044 self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000045
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000046 def test_short(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000047 ts.T_SHORT = SHRT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000048 self.assertEqual(ts.T_SHORT, SHRT_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000049 ts.T_SHORT = SHRT_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000050 self.assertEqual(ts.T_SHORT, SHRT_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000051 ts.T_USHORT = USHRT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000052 self.assertEqual(ts.T_USHORT, USHRT_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000053
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000054 def test_int(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000055 ts.T_INT = INT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000056 self.assertEqual(ts.T_INT, INT_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000057 ts.T_INT = INT_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000058 self.assertEqual(ts.T_INT, INT_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000059 ts.T_UINT = UINT_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000060 self.assertEqual(ts.T_UINT, UINT_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000061
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000062 def test_long(self):
Christian Heimes1af737c2008-01-23 08:24:23 +000063 ts.T_LONG = LONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000064 self.assertEqual(ts.T_LONG, LONG_MAX)
Christian Heimes1af737c2008-01-23 08:24:23 +000065 ts.T_LONG = LONG_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000066 self.assertEqual(ts.T_LONG, LONG_MIN)
Christian Heimes1af737c2008-01-23 08:24:23 +000067 ts.T_ULONG = ULONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000068 self.assertEqual(ts.T_ULONG, ULONG_MAX)
Thomas Wouters89f507f2006-12-13 04:49:30 +000069
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000070 def test_py_ssize_t(self):
Mark Dickinsond59b4162010-03-13 11:34:40 +000071 ts.T_PYSSIZET = PY_SSIZE_T_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000072 self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX)
Mark Dickinsond59b4162010-03-13 11:34:40 +000073 ts.T_PYSSIZET = PY_SSIZE_T_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000074 self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN)
Mark Dickinsond59b4162010-03-13 11:34:40 +000075
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000076 @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
77 def test_longlong(self):
78 ts.T_LONGLONG = LLONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000079 self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000080 ts.T_LONGLONG = LLONG_MIN
Ezio Melottib3aedd42010-11-20 19:04:17 +000081 self.assertEqual(ts.T_LONGLONG, LLONG_MIN)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000082
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000083 ts.T_ULONGLONG = ULLONG_MAX
Ezio Melottib3aedd42010-11-20 19:04:17 +000084 self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000085
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000086 ## make sure these will accept a plain int as well as a long
87 ts.T_LONGLONG = 3
Ezio Melottib3aedd42010-11-20 19:04:17 +000088 self.assertEqual(ts.T_LONGLONG, 3)
Benjamin Peterson80d8fd82010-04-03 01:40:24 +000089 ts.T_ULONGLONG = 4
Ezio Melottib3aedd42010-11-20 19:04:17 +000090 self.assertEqual(ts.T_ULONGLONG, 4)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000091
Mark Dickinsond59b4162010-03-13 11:34:40 +000092 def test_bad_assignments(self):
Mark Dickinsond59b4162010-03-13 11:34:40 +000093 integer_attributes = [
94 'T_BOOL',
95 'T_BYTE', 'T_UBYTE',
96 'T_SHORT', 'T_USHORT',
Mark Dickinson252e4002010-03-13 13:23:05 +000097 'T_INT', 'T_UINT',
98 'T_LONG', 'T_ULONG',
Mark Dickinsond59b4162010-03-13 11:34:40 +000099 'T_PYSSIZET'
100 ]
Mark Dickinsond59b4162010-03-13 11:34:40 +0000101 if hasattr(ts, 'T_LONGLONG'):
102 integer_attributes.extend(['T_LONGLONG', 'T_ULONGLONG'])
103
104 # issue8014: this produced 'bad argument to internal function'
105 # internal error
106 for nonint in None, 3.2j, "full of eels", {}, []:
107 for attr in integer_attributes:
108 self.assertRaises(TypeError, setattr, ts, attr, nonint)
109
Benjamin Peterson80d8fd82010-04-03 01:40:24 +0000110 def test_inplace_string(self):
Ezio Melottib3aedd42010-11-20 19:04:17 +0000111 self.assertEqual(ts.T_STRING_INPLACE, "hi")
Benjamin Peterson80d8fd82010-04-03 01:40:24 +0000112 self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
113 self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
114
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000115
Guido van Rossumd8faa362007-04-27 19:54:29 +0000116class TestWarnings(unittest.TestCase):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000117
118 def test_byte_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000119 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000120 ts.T_BYTE = CHAR_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121
122 def test_byte_min(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000123 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000124 ts.T_BYTE = CHAR_MIN-1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000125
126 def test_ubyte_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000127 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000128 ts.T_UBYTE = UCHAR_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000129
130 def test_short_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000131 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000132 ts.T_SHORT = SHRT_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000133
134 def test_short_min(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000135 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000136 ts.T_SHORT = SHRT_MIN-1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000137
138 def test_ushort_max(self):
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000139 with support.check_warnings(('', RuntimeWarning)):
Christian Heimes1af737c2008-01-23 08:24:23 +0000140 ts.T_USHORT = USHRT_MAX+1
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141
142
143def test_main(verbose=None):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000144 support.run_unittest(__name__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000145
146if __name__ == "__main__":
147 test_main(verbose=True)