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