1import unittest
2from test import test_support
3
4import time
5
6class StructSeqTest(unittest.TestCase):
7
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)
13
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])
20
21        for j in xrange(-len(t), len(t)):
22            self.assertEqual(t[:j], astuple[:j])
23
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])
28
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)")
36
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)])
42
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)])
49
50    def test_contains(self):
51        t1 = time.gmtime()
52        for item in t1:
53            self.assertIn(item, t1)
54        self.assertNotIn(-42, t1)
55
56    def test_hash(self):
57        t1 = time.gmtime()
58        self.assertEqual(hash(t1), hash(tuple(t1)))
59
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))
69
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)
74
75    def test_constructor(self):
76        t = time.struct_time
77
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)
83
84        s = "123456789"
85        self.assertEqual("".join(t(s)), s)
86
87    def test_eviltuple(self):
88        class Exc(Exception):
89            pass
90
91        # Devious code could crash structseqs' constructors
92        class C:
93            def __getitem__(self, i):
94                raise Exc
95            def __len__(self):
96                return 9
97
98        self.assertRaises(Exc, time.struct_time, C())
99
100    def test_reduce(self):
101        t = time.gmtime()
102        x = t.__reduce__()
103
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])
115
116def test_main():
117    test_support.run_unittest(StructSeqTest)
118
119if __name__ == "__main__":
120    test_main()
121