test_structmembers.py 3.5 KB

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