Serhiy Storchaka | 76249ea | 2014-02-07 10:06:05 +0200 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | |
| 4 | # Skip this test if the _testcapi module isn't available. |
| 5 | test_support.import_module('_testcapi') |
Mark Dickinson | b05dc00 | 2010-04-03 10:27:05 +0000 | [diff] [blame] | 6 | from _testcapi import _test_structmembersType, \ |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 7 | CHAR_MAX, CHAR_MIN, UCHAR_MAX, \ |
| 8 | SHRT_MAX, SHRT_MIN, USHRT_MAX, \ |
| 9 | INT_MAX, INT_MIN, UINT_MAX, \ |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 10 | LONG_MAX, LONG_MIN, ULONG_MAX, \ |
| 11 | LLONG_MAX, LLONG_MIN, ULLONG_MAX |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 12 | |
Mark Dickinson | b05dc00 | 2010-04-03 10:27:05 +0000 | [diff] [blame] | 13 | ts=_test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8, |
Benjamin Peterson | 1c7c11e | 2010-04-03 01:28:57 +0000 | [diff] [blame] | 14 | 9.99999, 10.1010101010, "hi") |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 15 | |
| 16 | class ReadWriteTests(unittest.TestCase): |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 17 | |
| 18 | def test_bool(self): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 19 | ts.T_BOOL = True |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 20 | self.assertEqual(ts.T_BOOL, True) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 21 | ts.T_BOOL = False |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 22 | self.assertEqual(ts.T_BOOL, False) |
Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 23 | self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1) |
| 24 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 25 | def test_byte(self): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 26 | ts.T_BYTE = CHAR_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 27 | self.assertEqual(ts.T_BYTE, CHAR_MAX) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 28 | ts.T_BYTE = CHAR_MIN |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 29 | self.assertEqual(ts.T_BYTE, CHAR_MIN) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 30 | ts.T_UBYTE = UCHAR_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 31 | self.assertEqual(ts.T_UBYTE, UCHAR_MAX) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 32 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 33 | def test_short(self): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 34 | ts.T_SHORT = SHRT_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 35 | self.assertEqual(ts.T_SHORT, SHRT_MAX) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 36 | ts.T_SHORT = SHRT_MIN |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 37 | self.assertEqual(ts.T_SHORT, SHRT_MIN) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 38 | ts.T_USHORT = USHRT_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 39 | self.assertEqual(ts.T_USHORT, USHRT_MAX) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 40 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 41 | def test_int(self): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 42 | ts.T_INT = INT_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 43 | self.assertEqual(ts.T_INT, INT_MAX) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 44 | ts.T_INT = INT_MIN |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 45 | self.assertEqual(ts.T_INT, INT_MIN) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 46 | ts.T_UINT = UINT_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 47 | self.assertEqual(ts.T_UINT, UINT_MAX) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 48 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 49 | def test_long(self): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 50 | ts.T_LONG = LONG_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 51 | self.assertEqual(ts.T_LONG, LONG_MAX) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 52 | ts.T_LONG = LONG_MIN |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 53 | self.assertEqual(ts.T_LONG, LONG_MIN) |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 54 | ts.T_ULONG = ULONG_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 55 | self.assertEqual(ts.T_ULONG, ULONG_MAX) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 56 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 57 | @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present") |
| 58 | def test_longlong(self): |
| 59 | ts.T_LONGLONG = LLONG_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 60 | self.assertEqual(ts.T_LONGLONG, LLONG_MAX) |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 61 | ts.T_LONGLONG = LLONG_MIN |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 62 | self.assertEqual(ts.T_LONGLONG, LLONG_MIN) |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 63 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 64 | ts.T_ULONGLONG = ULLONG_MAX |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 65 | self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX) |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 67 | ## make sure these will accept a plain int as well as a long |
| 68 | ts.T_LONGLONG = 3 |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 69 | self.assertEqual(ts.T_LONGLONG, 3) |
Benjamin Peterson | 2b79fdf | 2010-04-03 01:08:34 +0000 | [diff] [blame] | 70 | ts.T_ULONGLONG = 4 |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 71 | self.assertEqual(ts.T_ULONGLONG, 4) |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 72 | |
Benjamin Peterson | 1c7c11e | 2010-04-03 01:28:57 +0000 | [diff] [blame] | 73 | def test_inplace_string(self): |
Ezio Melotti | 2623a37 | 2010-11-21 13:34:58 +0000 | [diff] [blame] | 74 | self.assertEqual(ts.T_STRING_INPLACE, "hi") |
Benjamin Peterson | 1c7c11e | 2010-04-03 01:28:57 +0000 | [diff] [blame] | 75 | self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s") |
| 76 | self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE") |
| 77 | |
Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 78 | |
Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 79 | class TestWarnings(unittest.TestCase): |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 80 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 81 | def test_byte_max(self): |
Florent Xicluna | 945a8ba | 2010-03-17 19:15:56 +0000 | [diff] [blame] | 82 | with test_support.check_warnings(('', RuntimeWarning)): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 83 | ts.T_BYTE = CHAR_MAX+1 |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 84 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 85 | def test_byte_min(self): |
Florent Xicluna | 945a8ba | 2010-03-17 19:15:56 +0000 | [diff] [blame] | 86 | with test_support.check_warnings(('', RuntimeWarning)): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 87 | ts.T_BYTE = CHAR_MIN-1 |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 88 | |
| 89 | def test_ubyte_max(self): |
Florent Xicluna | 945a8ba | 2010-03-17 19:15:56 +0000 | [diff] [blame] | 90 | with test_support.check_warnings(('', RuntimeWarning)): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 91 | ts.T_UBYTE = UCHAR_MAX+1 |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 92 | |
| 93 | def test_short_max(self): |
Florent Xicluna | 945a8ba | 2010-03-17 19:15:56 +0000 | [diff] [blame] | 94 | with test_support.check_warnings(('', RuntimeWarning)): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 95 | ts.T_SHORT = SHRT_MAX+1 |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 96 | |
| 97 | def test_short_min(self): |
Florent Xicluna | 945a8ba | 2010-03-17 19:15:56 +0000 | [diff] [blame] | 98 | with test_support.check_warnings(('', RuntimeWarning)): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 99 | ts.T_SHORT = SHRT_MIN-1 |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 100 | |
| 101 | def test_ushort_max(self): |
Florent Xicluna | 945a8ba | 2010-03-17 19:15:56 +0000 | [diff] [blame] | 102 | with test_support.check_warnings(('', RuntimeWarning)): |
Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 103 | ts.T_USHORT = USHRT_MAX+1 |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 104 | |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 105 | |
| 106 | def test_main(verbose=None): |
Collin Winter | 68f525f | 2007-08-24 21:09:42 +0000 | [diff] [blame] | 107 | test_support.run_unittest(__name__) |
Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 108 | |
| 109 | if __name__ == "__main__": |
| 110 | test_main(verbose=True) |