blob: a89e9556c14e930742c84327513fbcd320070376 [file] [log] [blame]
Benjamin Petersond02441e2010-07-08 22:33:03 +00001import os
2import time
Raymond Hettingerf9f4c692003-08-30 22:54:55 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Tim Petersc2fe6182001-10-30 23:20:46 +00005
Tim Petersc2fe6182001-10-30 23:20:46 +00006
Raymond Hettingerf9f4c692003-08-30 22:54:55 +00007class StructSeqTest(unittest.TestCase):
Tim Petersc2fe6182001-10-30 23:20:46 +00008
Raymond Hettingerf9f4c692003-08-30 22:54:55 +00009 def test_tuple(self):
10 t = time.gmtime()
Benjamin Peterson8b0ef212010-07-09 01:58:26 +000011 self.assertIsInstance(t, tuple)
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000012 astuple = tuple(t)
13 self.assertEqual(len(t), len(astuple))
14 self.assertEqual(t, astuple)
Tim Petersc2fe6182001-10-30 23:20:46 +000015
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000016 # Check that slicing works the same way; at one point, slicing t[i:j] with
17 # 0 < i < j could produce NULLs in the result.
Guido van Rossum805365e2007-05-07 22:24:25 +000018 for i in range(-len(t), len(t)):
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000019 self.assertEqual(t[i:], astuple[i:])
Guido van Rossum805365e2007-05-07 22:24:25 +000020 for j in range(-len(t), len(t)):
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000021 self.assertEqual(t[i:j], astuple[i:j])
Michael W. Hudson02b28ec2002-03-06 17:18:15 +000022
Guido van Rossum805365e2007-05-07 22:24:25 +000023 for j in range(-len(t), len(t)):
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000024 self.assertEqual(t[:j], astuple[:j])
Michael W. Hudson02b28ec2002-03-06 17:18:15 +000025
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000026 self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
27 self.assertRaises(IndexError, t.__getitem__, len(t))
Guido van Rossum805365e2007-05-07 22:24:25 +000028 for i in range(-len(t), len(t)-1):
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000029 self.assertEqual(t[i], astuple[i])
30
31 def test_repr(self):
32 t = time.gmtime()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000033 self.assertTrue(repr(t))
Christian Heimesd32ed6f2008-01-14 18:49:24 +000034 t = time.gmtime(0)
35 self.assertEqual(repr(t),
36 "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
37 "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
Benjamin Petersond02441e2010-07-08 22:33:03 +000038 # os.stat() gives a complicated struct sequence.
39 st = os.stat(__file__)
40 rep = repr(st)
Benjamin Petersonb6692212010-07-09 19:37:00 +000041 self.assertTrue(rep.startswith(os.name + ".stat_result"))
Benjamin Petersond02441e2010-07-08 22:33:03 +000042 self.assertIn("st_mode=", rep)
43 self.assertIn("st_ino=", rep)
44 self.assertIn("st_dev=", rep)
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000045
46 def test_concat(self):
47 t1 = time.gmtime()
48 t2 = t1 + tuple(t1)
Guido van Rossum805365e2007-05-07 22:24:25 +000049 for i in range(len(t1)):
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000050 self.assertEqual(t2[i], t2[i+len(t1)])
51
52 def test_repeat(self):
53 t1 = time.gmtime()
54 t2 = 3 * t1
Guido van Rossum805365e2007-05-07 22:24:25 +000055 for i in range(len(t1)):
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000056 self.assertEqual(t2[i], t2[i+len(t1)])
57 self.assertEqual(t2[i], t2[i+2*len(t1)])
58
59 def test_contains(self):
60 t1 = time.gmtime()
61 for item in t1:
Benjamin Peterson577473f2010-01-19 00:09:57 +000062 self.assertIn(item, t1)
63 self.assertNotIn(-42, t1)
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000064
65 def test_hash(self):
66 t1 = time.gmtime()
67 self.assertEqual(hash(t1), hash(tuple(t1)))
68
69 def test_cmp(self):
70 t1 = time.gmtime()
71 t2 = type(t1)(t1)
72 self.assertEqual(t1, t2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000073 self.assertTrue(not (t1 < t2))
74 self.assertTrue(t1 <= t2)
75 self.assertTrue(not (t1 > t2))
76 self.assertTrue(t1 >= t2)
77 self.assertTrue(not (t1 != t2))
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000078
79 def test_fields(self):
80 t = time.gmtime()
Alexander Belopolskyc142bba2012-06-13 22:15:26 -040081 self.assertEqual(len(t), t.n_sequence_fields)
82 self.assertEqual(t.n_unnamed_fields, 0)
83 self.assertEqual(t.n_fields, time._STRUCT_TM_ITEMS)
Raymond Hettingerf9f4c692003-08-30 22:54:55 +000084
85 def test_constructor(self):
86 t = time.struct_time
87
88 self.assertRaises(TypeError, t)
89 self.assertRaises(TypeError, t, None)
90 self.assertRaises(TypeError, t, "123")
91 self.assertRaises(TypeError, t, "123", dict={})
92 self.assertRaises(TypeError, t, "123456789", dict=None)
93
94 s = "123456789"
95 self.assertEqual("".join(t(s)), s)
96
97 def test_eviltuple(self):
98 class Exc(Exception):
99 pass
100
101 # Devious code could crash structseqs' contructors
102 class C:
103 def __getitem__(self, i):
104 raise Exc
105 def __len__(self):
106 return 9
107
108 self.assertRaises(Exc, time.struct_time, C())
109
110 def test_reduce(self):
111 t = time.gmtime()
112 x = t.__reduce__()
113
Thomas Woutersed03b412007-08-28 21:37:11 +0000114 def test_extended_getslice(self):
115 # Test extended slicing by comparing with list slicing.
116 t = time.gmtime()
117 L = list(t)
118 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
119 for start in indices:
120 for stop in indices:
121 # Skip step 0 (invalid)
122 for step in indices[1:]:
123 self.assertEqual(list(t[start:stop:step]),
124 L[start:stop:step])
125
Raymond Hettingerf9f4c692003-08-30 22:54:55 +0000126def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000127 support.run_unittest(StructSeqTest)
Raymond Hettingerf9f4c692003-08-30 22:54:55 +0000128
129if __name__ == "__main__":
130 test_main()