Issue #5788: `datetime.timedelta` objects get a new `total_seconds()` method returning
[python.git] / Lib / test / test_structseq.py
blob5783ccb1a4077d0363d5fbf139150744e0e66425
1 import unittest
2 from test import test_support
4 import time
6 class StructSeqTest(unittest.TestCase):
8 def test_tuple(self):
9 t = time.gmtime()
10 astuple = tuple(t)
11 self.assertEqual(len(t), len(astuple))
12 self.assertEqual(t, astuple)
14 # Check that slicing works the same way; at one point, slicing t[i:j] with
15 # 0 < i < j could produce NULLs in the result.
16 for i in xrange(-len(t), len(t)):
17 self.assertEqual(t[i:], astuple[i:])
18 for j in xrange(-len(t), len(t)):
19 self.assertEqual(t[i:j], astuple[i:j])
21 for j in xrange(-len(t), len(t)):
22 self.assertEqual(t[:j], astuple[:j])
24 self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
25 self.assertRaises(IndexError, t.__getitem__, len(t))
26 for i in xrange(-len(t), len(t)-1):
27 self.assertEqual(t[i], astuple[i])
29 def test_repr(self):
30 t = time.gmtime()
31 self.assertTrue(repr(t))
32 t = time.gmtime(0)
33 self.assertEqual(repr(t),
34 "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
35 "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
37 def test_concat(self):
38 t1 = time.gmtime()
39 t2 = t1 + tuple(t1)
40 for i in xrange(len(t1)):
41 self.assertEqual(t2[i], t2[i+len(t1)])
43 def test_repeat(self):
44 t1 = time.gmtime()
45 t2 = 3 * t1
46 for i in xrange(len(t1)):
47 self.assertEqual(t2[i], t2[i+len(t1)])
48 self.assertEqual(t2[i], t2[i+2*len(t1)])
50 def test_contains(self):
51 t1 = time.gmtime()
52 for item in t1:
53 self.assertTrue(item in t1)
54 self.assertTrue(-42 not in t1)
56 def test_hash(self):
57 t1 = time.gmtime()
58 self.assertEqual(hash(t1), hash(tuple(t1)))
60 def test_cmp(self):
61 t1 = time.gmtime()
62 t2 = type(t1)(t1)
63 self.assertEqual(t1, t2)
64 self.assertTrue(not (t1 < t2))
65 self.assertTrue(t1 <= t2)
66 self.assertTrue(not (t1 > t2))
67 self.assertTrue(t1 >= t2)
68 self.assertTrue(not (t1 != t2))
70 def test_fields(self):
71 t = time.gmtime()
72 self.assertEqual(len(t), t.n_fields)
73 self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
75 def test_constructor(self):
76 t = time.struct_time
78 self.assertRaises(TypeError, t)
79 self.assertRaises(TypeError, t, None)
80 self.assertRaises(TypeError, t, "123")
81 self.assertRaises(TypeError, t, "123", dict={})
82 self.assertRaises(TypeError, t, "123456789", dict=None)
84 s = "123456789"
85 self.assertEqual("".join(t(s)), s)
87 def test_eviltuple(self):
88 class Exc(Exception):
89 pass
91 # Devious code could crash structseqs' contructors
92 class C:
93 def __getitem__(self, i):
94 raise Exc
95 def __len__(self):
96 return 9
98 self.assertRaises(Exc, time.struct_time, C())
100 def test_reduce(self):
101 t = time.gmtime()
102 x = t.__reduce__()
104 def test_extended_getslice(self):
105 # Test extended slicing by comparing with list slicing.
106 t = time.gmtime()
107 L = list(t)
108 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
109 for start in indices:
110 for stop in indices:
111 # Skip step 0 (invalid)
112 for step in indices[1:]:
113 self.assertEqual(list(t[start:stop:step]),
114 L[start:stop:step])
116 def test_main():
117 test_support.run_unittest(StructSeqTest)
119 if __name__ == "__main__":
120 test_main()