blob: 02c2f72fdfb4d33af329ab66d64c46ac30ce4e11 [file] [log] [blame]
Raymond Hettingerf9f4c692003-08-30 22:54:55 +00001import unittest
2from test import test_support
Tim Petersc2fe6182001-10-30 23:20:46 +00003
4import time
5
Raymond Hettingerf9f4c692003-08-30 22:54:55 +00006class StructSeqTest(unittest.TestCase):
Tim Petersc2fe6182001-10-30 23:20:46 +00007
Raymond Hettingerf9f4c692003-08-30 22:54:55 +00008 def test_tuple(self):
9 t = time.gmtime()
10 astuple = tuple(t)
11 self.assertEqual(len(t), len(astuple))
12 self.assertEqual(t, astuple)
Tim Petersc2fe6182001-10-30 23:20:46 +000013
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000014 # 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])
Michael W. Hudson02b28ec2002-03-06 17:18:15 +000020
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000021 for j in xrange(-len(t), len(t)):
22 self.assertEqual(t[:j], astuple[:j])
Michael W. Hudson02b28ec2002-03-06 17:18:15 +000023
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000024 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])
28
Raymond Hettingerc216df92008-01-15 03:02:37 +000029 def test_tuple_subclass(self):
30 # Issue 1820
31 t = time.localtime()
32 s = ('%s ' * len(t)) % t # This used to fail because t was not a tuple subclass
33 self.assert_(isinstance(t, tuple))
34
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000035 def test_repr(self):
36 t = time.gmtime()
Christian Heimes9c201962008-01-14 03:33:52 +000037 self.assert_(repr(t))
38 t = time.gmtime(0)
39 self.assertEqual(repr(t),
40 "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
41 "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000042
43 def test_concat(self):
44 t1 = time.gmtime()
45 t2 = t1 + tuple(t1)
46 for i in xrange(len(t1)):
47 self.assertEqual(t2[i], t2[i+len(t1)])
48
49 def test_repeat(self):
50 t1 = time.gmtime()
51 t2 = 3 * t1
52 for i in xrange(len(t1)):
53 self.assertEqual(t2[i], t2[i+len(t1)])
54 self.assertEqual(t2[i], t2[i+2*len(t1)])
55
56 def test_contains(self):
57 t1 = time.gmtime()
58 for item in t1:
59 self.assert_(item in t1)
60 self.assert_(-42 not in t1)
61
62 def test_hash(self):
63 t1 = time.gmtime()
64 self.assertEqual(hash(t1), hash(tuple(t1)))
65
66 def test_cmp(self):
67 t1 = time.gmtime()
68 t2 = type(t1)(t1)
69 self.assertEqual(t1, t2)
70 self.assert_(not (t1 < t2))
71 self.assert_(t1 <= t2)
72 self.assert_(not (t1 > t2))
73 self.assert_(t1 >= t2)
74 self.assert_(not (t1 != t2))
75
76 def test_fields(self):
77 t = time.gmtime()
78 self.assertEqual(len(t), t.n_fields)
79 self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
80
81 def test_constructor(self):
82 t = time.struct_time
83
84 self.assertRaises(TypeError, t)
85 self.assertRaises(TypeError, t, None)
86 self.assertRaises(TypeError, t, "123")
87 self.assertRaises(TypeError, t, "123", dict={})
88 self.assertRaises(TypeError, t, "123456789", dict=None)
89
90 s = "123456789"
91 self.assertEqual("".join(t(s)), s)
92
93 def test_eviltuple(self):
94 class Exc(Exception):
95 pass
96
97 # Devious code could crash structseqs' contructors
98 class C:
99 def __getitem__(self, i):
100 raise Exc
101 def __len__(self):
102 return 9
103
104 self.assertRaises(Exc, time.struct_time, C())
105
106 def test_reduce(self):
107 t = time.gmtime()
108 x = t.__reduce__()
109
Thomas Wouters3ccec682007-08-28 15:28:19 +0000110 def test_extended_getslice(self):
111 # Test extended slicing by comparing with list slicing.
112 t = time.gmtime()
113 L = list(t)
114 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
115 for start in indices:
116 for stop in indices:
117 # Skip step 0 (invalid)
118 for step in indices[1:]:
119 self.assertEqual(list(t[start:stop:step]),
120 L[start:stop:step])
121
Raymond Hettingerf9f4c692003-08-30 22:54:55 +0000122def test_main():
123 test_support.run_unittest(StructSeqTest)
124
125if __name__ == "__main__":
126 test_main()