| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 1 | from _testcapi import test_structmembersType, \ | 
 | 2 |     CHAR_MAX, CHAR_MIN, UCHAR_MAX, \ | 
 | 3 |     SHRT_MAX, SHRT_MIN, USHRT_MAX, \ | 
 | 4 |     INT_MAX, INT_MIN, UINT_MAX, \ | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 5 |     LONG_MAX, LONG_MIN, ULONG_MAX, \ | 
 | 6 |     LLONG_MAX, LLONG_MIN, ULLONG_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 7 |  | 
| Collin Winter | 68f525f | 2007-08-24 21:09:42 +0000 | [diff] [blame] | 8 | import warnings, exceptions, unittest, sys | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 9 | from test import test_support | 
 | 10 |  | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 11 | ts=test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8, | 
 | 12 |                           9.99999, 10.1010101010) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 13 |  | 
 | 14 | class ReadWriteTests(unittest.TestCase): | 
 | 15 |     def test_types(self): | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 16 |         ts.T_BOOL = True | 
| Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 17 |         self.assertEquals(ts.T_BOOL, True) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 18 |         ts.T_BOOL = False | 
| Georg Brandl | 32a3fb5 | 2008-01-21 21:23:15 +0000 | [diff] [blame] | 19 |         self.assertEquals(ts.T_BOOL, False) | 
 | 20 |         self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1) | 
 | 21 |  | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 22 |         ts.T_BYTE = CHAR_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 23 |         self.assertEquals(ts.T_BYTE, CHAR_MAX) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 24 |         ts.T_BYTE = CHAR_MIN | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 25 |         self.assertEquals(ts.T_BYTE, CHAR_MIN) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 26 |         ts.T_UBYTE = UCHAR_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 27 |         self.assertEquals(ts.T_UBYTE, UCHAR_MAX) | 
 | 28 |  | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 29 |         ts.T_SHORT = SHRT_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 30 |         self.assertEquals(ts.T_SHORT, SHRT_MAX) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 31 |         ts.T_SHORT = SHRT_MIN | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 32 |         self.assertEquals(ts.T_SHORT, SHRT_MIN) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 33 |         ts.T_USHORT = USHRT_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 34 |         self.assertEquals(ts.T_USHORT, USHRT_MAX) | 
 | 35 |  | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 36 |         ts.T_INT = INT_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 37 |         self.assertEquals(ts.T_INT, INT_MAX) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 38 |         ts.T_INT = INT_MIN | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 39 |         self.assertEquals(ts.T_INT, INT_MIN) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 40 |         ts.T_UINT = UINT_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 41 |         self.assertEquals(ts.T_UINT, UINT_MAX) | 
 | 42 |  | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 43 |         ts.T_LONG = LONG_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 44 |         self.assertEquals(ts.T_LONG, LONG_MAX) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 45 |         ts.T_LONG = LONG_MIN | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 46 |         self.assertEquals(ts.T_LONG, LONG_MIN) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 47 |         ts.T_ULONG = ULONG_MAX | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 48 |         self.assertEquals(ts.T_ULONG, ULONG_MAX) | 
 | 49 |  | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 50 |         ## T_LONGLONG and T_ULONGLONG may not be present on some platforms | 
 | 51 |         if hasattr(ts, 'T_LONGLONG'): | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 52 |             ts.T_LONGLONG = LLONG_MAX | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 53 |             self.assertEquals(ts.T_LONGLONG, LLONG_MAX) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 54 |             ts.T_LONGLONG = LLONG_MIN | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 55 |             self.assertEquals(ts.T_LONGLONG, LLONG_MIN) | 
 | 56 |  | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 57 |             ts.T_ULONGLONG = ULLONG_MAX | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 58 |             self.assertEquals(ts.T_ULONGLONG, ULLONG_MAX) | 
 | 59 |  | 
 | 60 |             ## make sure these will accept a plain int as well as a long | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 61 |             ts.T_LONGLONG = 3 | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 62 |             self.assertEquals(ts.T_LONGLONG, 3) | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 63 |             ts.T_ULONGLONG = 4 | 
| Martin v. Löwis | 6371cd8 | 2007-06-09 07:42:52 +0000 | [diff] [blame] | 64 |             self.assertEquals(ts.T_ULONGLONG, 4) | 
 | 65 |  | 
 | 66 |  | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 67 | class TestWarnings(unittest.TestCase): | 
 | 68 |     def has_warned(self, w): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 69 |         self.assertEqual(w.category, RuntimeWarning) | 
| Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 70 |  | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 71 |     def test_byte_max(self): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 72 |         with test_support.check_warnings() as w: | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 73 |             ts.T_BYTE = CHAR_MAX+1 | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 74 |             self.has_warned(w) | 
| Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 75 |  | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 76 |     def test_byte_min(self): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 77 |         with test_support.check_warnings() as w: | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 78 |             ts.T_BYTE = CHAR_MIN-1 | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 79 |             self.has_warned(w) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 80 |  | 
 | 81 |     def test_ubyte_max(self): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 82 |         with test_support.check_warnings() as w: | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 83 |             ts.T_UBYTE = UCHAR_MAX+1 | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 84 |             self.has_warned(w) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 85 |  | 
 | 86 |     def test_short_max(self): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 87 |         with test_support.check_warnings() as w: | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 88 |             ts.T_SHORT = SHRT_MAX+1 | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 89 |             self.has_warned(w) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 90 |  | 
 | 91 |     def test_short_min(self): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 92 |         with test_support.check_warnings() as w: | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 93 |             ts.T_SHORT = SHRT_MIN-1 | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 94 |             self.has_warned(w) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 95 |  | 
 | 96 |     def test_ushort_max(self): | 
| Nick Coghlan | d2e0938 | 2008-09-11 12:11:06 +0000 | [diff] [blame] | 97 |         with test_support.check_warnings() as w: | 
| Georg Brandl | c09b94e | 2008-01-21 21:28:32 +0000 | [diff] [blame] | 98 |             ts.T_USHORT = USHRT_MAX+1 | 
| Walter Dörwald | e6dae6c | 2007-04-03 18:33:29 +0000 | [diff] [blame] | 99 |             self.has_warned(w) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 100 |  | 
| Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 101 |  | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 102 |  | 
 | 103 | def test_main(verbose=None): | 
| Collin Winter | 68f525f | 2007-08-24 21:09:42 +0000 | [diff] [blame] | 104 |     test_support.run_unittest(__name__) | 
| Martin v. Löwis | b5bc537 | 2006-10-27 06:16:31 +0000 | [diff] [blame] | 105 |  | 
 | 106 | if __name__ == "__main__": | 
 | 107 |     test_main(verbose=True) |