Home | History | Annotate | Download | only in test
      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, \
      5     LONG_MAX, LONG_MIN, ULONG_MAX, \
      6     LLONG_MAX, LLONG_MIN, ULLONG_MAX
      7 
      8 import unittest
      9 from test import test_support
     10 
     11 ts=_test_structmembersType(False, 1, 2, 3, 4, 5, 6, 7, 8,
     12                           9.99999, 10.1010101010, "hi")
     13 
     14 class ReadWriteTests(unittest.TestCase):
     15 
     16     def test_bool(self):
     17         ts.T_BOOL = True
     18         self.assertEqual(ts.T_BOOL, True)
     19         ts.T_BOOL = False
     20         self.assertEqual(ts.T_BOOL, False)
     21         self.assertRaises(TypeError, setattr, ts, 'T_BOOL', 1)
     22 
     23     def test_byte(self):
     24         ts.T_BYTE = CHAR_MAX
     25         self.assertEqual(ts.T_BYTE, CHAR_MAX)
     26         ts.T_BYTE = CHAR_MIN
     27         self.assertEqual(ts.T_BYTE, CHAR_MIN)
     28         ts.T_UBYTE = UCHAR_MAX
     29         self.assertEqual(ts.T_UBYTE, UCHAR_MAX)
     30 
     31     def test_short(self):
     32         ts.T_SHORT = SHRT_MAX
     33         self.assertEqual(ts.T_SHORT, SHRT_MAX)
     34         ts.T_SHORT = SHRT_MIN
     35         self.assertEqual(ts.T_SHORT, SHRT_MIN)
     36         ts.T_USHORT = USHRT_MAX
     37         self.assertEqual(ts.T_USHORT, USHRT_MAX)
     38 
     39     def test_int(self):
     40         ts.T_INT = INT_MAX
     41         self.assertEqual(ts.T_INT, INT_MAX)
     42         ts.T_INT = INT_MIN
     43         self.assertEqual(ts.T_INT, INT_MIN)
     44         ts.T_UINT = UINT_MAX
     45         self.assertEqual(ts.T_UINT, UINT_MAX)
     46 
     47     def test_long(self):
     48         ts.T_LONG = LONG_MAX
     49         self.assertEqual(ts.T_LONG, LONG_MAX)
     50         ts.T_LONG = LONG_MIN
     51         self.assertEqual(ts.T_LONG, LONG_MIN)
     52         ts.T_ULONG = ULONG_MAX
     53         self.assertEqual(ts.T_ULONG, ULONG_MAX)
     54 
     55     @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present")
     56     def test_longlong(self):
     57         ts.T_LONGLONG = LLONG_MAX
     58         self.assertEqual(ts.T_LONGLONG, LLONG_MAX)
     59         ts.T_LONGLONG = LLONG_MIN
     60         self.assertEqual(ts.T_LONGLONG, LLONG_MIN)
     61 
     62         ts.T_ULONGLONG = ULLONG_MAX
     63         self.assertEqual(ts.T_ULONGLONG, ULLONG_MAX)
     64 
     65         ## make sure these will accept a plain int as well as a long
     66         ts.T_LONGLONG = 3
     67         self.assertEqual(ts.T_LONGLONG, 3)
     68         ts.T_ULONGLONG = 4
     69         self.assertEqual(ts.T_ULONGLONG, 4)
     70 
     71     def test_inplace_string(self):
     72         self.assertEqual(ts.T_STRING_INPLACE, "hi")
     73         self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s")
     74         self.assertRaises(TypeError, delattr, ts, "T_STRING_INPLACE")
     75 
     76 
     77 class TestWarnings(unittest.TestCase):
     78 
     79     def test_byte_max(self):
     80         with test_support.check_warnings(('', RuntimeWarning)):
     81             ts.T_BYTE = CHAR_MAX+1
     82 
     83     def test_byte_min(self):
     84         with test_support.check_warnings(('', RuntimeWarning)):
     85             ts.T_BYTE = CHAR_MIN-1
     86 
     87     def test_ubyte_max(self):
     88         with test_support.check_warnings(('', RuntimeWarning)):
     89             ts.T_UBYTE = UCHAR_MAX+1
     90 
     91     def test_short_max(self):
     92         with test_support.check_warnings(('', RuntimeWarning)):
     93             ts.T_SHORT = SHRT_MAX+1
     94 
     95     def test_short_min(self):
     96         with test_support.check_warnings(('', RuntimeWarning)):
     97             ts.T_SHORT = SHRT_MIN-1
     98 
     99     def test_ushort_max(self):
    100         with test_support.check_warnings(('', RuntimeWarning)):
    101             ts.T_USHORT = USHRT_MAX+1
    102 
    103 
    104 def test_main(verbose=None):
    105     test_support.run_unittest(__name__)
    106 
    107 if __name__ == "__main__":
    108     test_main(verbose=True)
    109