Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 1 | import unittest |
| 2 | from test import support |
| 3 | |
| 4 | # Skip this test if the _testcapi module isn't available. |
| 5 | support.import_module('_testcapi') |
Mark Dickinson | 5fc16b4 | 2010-04-03 10:49:56 +0000 | [diff] [blame] | 6 | from _testcapi import _test_structmembersType, \ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7 | CHAR_MAX, CHAR_MIN, UCHAR_MAX, \ |
| 8 | SHRT_MAX, SHRT_MIN, USHRT_MAX, \ |
| 9 | INT_MAX, INT_MIN, UINT_MAX, \ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 10 | LONG_MAX, LONG_MIN, ULONG_MAX, \ |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 11 | LLONG_MAX, LLONG_MIN, ULLONG_MAX, \ |
| 12 | PY_SSIZE_T_MAX, PY_SSIZE_T_MIN |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 13 | |
Mark Dickinson | 5fc16b4 | 2010-04-03 10:49:56 +0000 | [diff] [blame] | 14 | ts=_test_structmembersType(False, # T_BOOL |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 15 | 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 Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 25 | 10.1010101010, # T_DOUBLE |
| 26 | "hi" # T_STRING_INPLACE |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 27 | ) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 28 | |
| 29 | class ReadWriteTests(unittest.TestCase): |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 30 | |
| 31 | def test_bool(self): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 32 | ts.T_BOOL = True |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 33 | self.assertEqual(ts.T_BOOL, True) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 34 | ts.T_BOOL = False |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 35 | self.assertEqual(ts.T_BOOL, False) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 36 | self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1) |
| 37 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 38 | def test_byte(self): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 39 | ts.T_BYTE = CHAR_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 40 | self.assertEqual(ts.T_BYTE, CHAR_MAX) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 41 | ts.T_BYTE = CHAR_MIN |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 42 | self.assertEqual(ts.T_BYTE, CHAR_MIN) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 43 | ts.T_UBYTE = UCHAR_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 44 | self.assertEqual(ts.T_UBYTE, UCHAR_MAX) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 45 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 46 | def test_short(self): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 47 | ts.T_SHORT = SHRT_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 48 | self.assertEqual(ts.T_SHORT, SHRT_MAX) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 49 | ts.T_SHORT = SHRT_MIN |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 50 | self.assertEqual(ts.T_SHORT, SHRT_MIN) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 51 | ts.T_USHORT = USHRT_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 52 | self.assertEqual(ts.T_USHORT, USHRT_MAX) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 53 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 54 | def test_int(self): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 55 | ts.T_INT = INT_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 56 | self.assertEqual(ts.T_INT, INT_MAX) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 57 | ts.T_INT = INT_MIN |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 58 | self.assertEqual(ts.T_INT, INT_MIN) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 59 | ts.T_UINT = UINT_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 60 | self.assertEqual(ts.T_UINT, UINT_MAX) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 62 | def test_long(self): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 63 | ts.T_LONG = LONG_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 64 | self.assertEqual(ts.T_LONG, LONG_MAX) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 65 | ts.T_LONG = LONG_MIN |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 66 | self.assertEqual(ts.T_LONG, LONG_MIN) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 67 | ts.T_ULONG = ULONG_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 68 | self.assertEqual(ts.T_ULONG, ULONG_MAX) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 70 | def test_py_ssize_t(self): |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 71 | ts.T_PYSSIZET = PY_SSIZE_T_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 72 | self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MAX) |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 73 | ts.T_PYSSIZET = PY_SSIZE_T_MIN |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 74 | self.assertEqual(ts.T_PYSSIZET, PY_SSIZE_T_MIN) |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 76 | @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present") |
| 77 | def test_longlong(self): |
| 78 | ts.T_LONGLONG = LLONG_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 79 | self.assertEqual(ts.T_LONGLONG, LLONG_MAX) |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 80 | ts.T_LONGLONG = LLONG_MIN |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 81 | self.assertEqual(ts.T_LONGLONG, LLONG_MIN) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 82 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 83 | ts.T_ULONGLONG = ULLONG_MAX |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 84 | self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 86 | ## make sure these will accept a plain int as well as a long |
| 87 | ts.T_LONGLONG = 3 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 88 | self.assertEqual(ts.T_LONGLONG, 3) |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 89 | ts.T_ULONGLONG = 4 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 90 | self.assertEqual(ts.T_ULONGLONG, 4) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 91 | |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 92 | def test_bad_assignments(self): |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 93 | integer_attributes = [ |
| 94 | 'T_BOOL', |
| 95 | 'T_BYTE', 'T_UBYTE', |
| 96 | 'T_SHORT', 'T_USHORT', |
Mark Dickinson | 252e400 | 2010-03-13 13:23:05 +0000 | [diff] [blame] | 97 | 'T_INT', 'T_UINT', |
| 98 | 'T_LONG', 'T_ULONG', |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 99 | 'T_PYSSIZET' |
| 100 | ] |
Mark Dickinson | d59b416 | 2010-03-13 11:34:40 +0000 | [diff] [blame] | 101 | 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 Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 110 | def test_inplace_string(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 111 | self.assertEqual(ts.T_STRING_INPLACE, "hi") |
Benjamin Peterson | 80d8fd8 | 2010-04-03 01:40:24 +0000 | [diff] [blame] | 112 | self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s") |
| 113 | self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE") |
| 114 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 116 | class TestWarnings(unittest.TestCase): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 117 | |
| 118 | def test_byte_max(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 119 | with support.check_warnings(('', RuntimeWarning)): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 120 | ts.T_BYTE = CHAR_MAX+1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 121 | |
| 122 | def test_byte_min(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 123 | with support.check_warnings(('', RuntimeWarning)): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 124 | ts.T_BYTE = CHAR_MIN-1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 125 | |
| 126 | def test_ubyte_max(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 127 | with support.check_warnings(('', RuntimeWarning)): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 128 | ts.T_UBYTE = UCHAR_MAX+1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 129 | |
| 130 | def test_short_max(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 131 | with support.check_warnings(('', RuntimeWarning)): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 132 | ts.T_SHORT = SHRT_MAX+1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | |
| 134 | def test_short_min(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 135 | with support.check_warnings(('', RuntimeWarning)): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 136 | ts.T_SHORT = SHRT_MIN-1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 137 | |
| 138 | def test_ushort_max(self): |
Florent Xicluna | 8fbddf1 | 2010-03-17 20:29:51 +0000 | [diff] [blame] | 139 | with support.check_warnings(('', RuntimeWarning)): |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 140 | ts.T_USHORT = USHRT_MAX+1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 144 | support.run_unittest(__name__) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 145 | |
| 146 | if __name__ == "__main__": |
| 147 | test_main(verbose=True) |