test_structseq.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import unittest
  2. from test import test_support
  3. import time
  4. class StructSeqTest(unittest.TestCase):
  5. def test_tuple(self):
  6. t = time.gmtime()
  7. astuple = tuple(t)
  8. self.assertEqual(len(t), len(astuple))
  9. self.assertEqual(t, astuple)
  10. # Check that slicing works the same way; at one point, slicing t[i:j] with
  11. # 0 < i < j could produce NULLs in the result.
  12. for i in xrange(-len(t), len(t)):
  13. self.assertEqual(t[i:], astuple[i:])
  14. for j in xrange(-len(t), len(t)):
  15. self.assertEqual(t[i:j], astuple[i:j])
  16. for j in xrange(-len(t), len(t)):
  17. self.assertEqual(t[:j], astuple[:j])
  18. self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
  19. self.assertRaises(IndexError, t.__getitem__, len(t))
  20. for i in xrange(-len(t), len(t)-1):
  21. self.assertEqual(t[i], astuple[i])
  22. def test_repr(self):
  23. t = time.gmtime()
  24. self.assertTrue(repr(t))
  25. t = time.gmtime(0)
  26. self.assertEqual(repr(t),
  27. "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
  28. "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
  29. def test_concat(self):
  30. t1 = time.gmtime()
  31. t2 = t1 + tuple(t1)
  32. for i in xrange(len(t1)):
  33. self.assertEqual(t2[i], t2[i+len(t1)])
  34. def test_repeat(self):
  35. t1 = time.gmtime()
  36. t2 = 3 * t1
  37. for i in xrange(len(t1)):
  38. self.assertEqual(t2[i], t2[i+len(t1)])
  39. self.assertEqual(t2[i], t2[i+2*len(t1)])
  40. def test_contains(self):
  41. t1 = time.gmtime()
  42. for item in t1:
  43. self.assertIn(item, t1)
  44. self.assertNotIn(-42, t1)
  45. def test_hash(self):
  46. t1 = time.gmtime()
  47. self.assertEqual(hash(t1), hash(tuple(t1)))
  48. def test_cmp(self):
  49. t1 = time.gmtime()
  50. t2 = type(t1)(t1)
  51. self.assertEqual(t1, t2)
  52. self.assertTrue(not (t1 < t2))
  53. self.assertTrue(t1 <= t2)
  54. self.assertTrue(not (t1 > t2))
  55. self.assertTrue(t1 >= t2)
  56. self.assertTrue(not (t1 != t2))
  57. def test_fields(self):
  58. t = time.gmtime()
  59. self.assertEqual(len(t), t.n_fields)
  60. self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
  61. def test_constructor(self):
  62. t = time.struct_time
  63. self.assertRaises(TypeError, t)
  64. self.assertRaises(TypeError, t, None)
  65. self.assertRaises(TypeError, t, "123")
  66. self.assertRaises(TypeError, t, "123", dict={})
  67. self.assertRaises(TypeError, t, "123456789", dict=None)
  68. s = "123456789"
  69. self.assertEqual("".join(t(s)), s)
  70. def test_eviltuple(self):
  71. class Exc(Exception):
  72. pass
  73. # Devious code could crash structseqs' contructors
  74. class C:
  75. def __getitem__(self, i):
  76. raise Exc
  77. def __len__(self):
  78. return 9
  79. self.assertRaises(Exc, time.struct_time, C())
  80. def test_reduce(self):
  81. t = time.gmtime()
  82. x = t.__reduce__()
  83. def test_extended_getslice(self):
  84. # Test extended slicing by comparing with list slicing.
  85. t = time.gmtime()
  86. L = list(t)
  87. indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
  88. for start in indices:
  89. for stop in indices:
  90. # Skip step 0 (invalid)
  91. for step in indices[1:]:
  92. self.assertEqual(list(t[start:stop:step]),
  93. L[start:stop:step])
  94. def test_main():
  95. test_support.run_unittest(StructSeqTest)
  96. if __name__ == "__main__":
  97. test_main()