Benjamin Peterson | d02441e | 2010-07-08 22:33:03 +0000 | [diff] [blame] | 1 | import os |
| 2 | import time |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 5 | |
Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 6 | |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 7 | class StructSeqTest(unittest.TestCase): |
Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 8 | |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 9 | def test_tuple(self): |
| 10 | t = time.gmtime() |
Benjamin Peterson | 8b0ef21 | 2010-07-09 01:58:26 +0000 | [diff] [blame] | 11 | self.assertIsInstance(t, tuple) |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 12 | astuple = tuple(t) |
| 13 | self.assertEqual(len(t), len(astuple)) |
| 14 | self.assertEqual(t, astuple) |
Tim Peters | c2fe618 | 2001-10-30 23:20:46 +0000 | [diff] [blame] | 15 | |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 16 | # 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 Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 18 | for i in range(-len(t), len(t)): |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 19 | self.assertEqual(t[i:], astuple[i:]) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 20 | for j in range(-len(t), len(t)): |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 21 | self.assertEqual(t[i:j], astuple[i:j]) |
Michael W. Hudson | 02b28ec | 2002-03-06 17:18:15 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 23 | for j in range(-len(t), len(t)): |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 24 | self.assertEqual(t[:j], astuple[:j]) |
Michael W. Hudson | 02b28ec | 2002-03-06 17:18:15 +0000 | [diff] [blame] | 25 | |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 26 | self.assertRaises(IndexError, t.__getitem__, -len(t)-1) |
| 27 | self.assertRaises(IndexError, t.__getitem__, len(t)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 28 | for i in range(-len(t), len(t)-1): |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 29 | self.assertEqual(t[i], astuple[i]) |
| 30 | |
| 31 | def test_repr(self): |
| 32 | t = time.gmtime() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 33 | self.assertTrue(repr(t)) |
Christian Heimes | d32ed6f | 2008-01-14 18:49:24 +0000 | [diff] [blame] | 34 | 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 Peterson | d02441e | 2010-07-08 22:33:03 +0000 | [diff] [blame] | 38 | # os.stat() gives a complicated struct sequence. |
| 39 | st = os.stat(__file__) |
| 40 | rep = repr(st) |
Benjamin Peterson | b669221 | 2010-07-09 19:37:00 +0000 | [diff] [blame] | 41 | self.assertTrue(rep.startswith(os.name + ".stat_result")) |
Benjamin Peterson | d02441e | 2010-07-08 22:33:03 +0000 | [diff] [blame] | 42 | self.assertIn("st_mode=", rep) |
| 43 | self.assertIn("st_ino=", rep) |
| 44 | self.assertIn("st_dev=", rep) |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 45 | |
| 46 | def test_concat(self): |
| 47 | t1 = time.gmtime() |
| 48 | t2 = t1 + tuple(t1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 49 | for i in range(len(t1)): |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 50 | self.assertEqual(t2[i], t2[i+len(t1)]) |
| 51 | |
| 52 | def test_repeat(self): |
| 53 | t1 = time.gmtime() |
| 54 | t2 = 3 * t1 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 55 | for i in range(len(t1)): |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 56 | 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 Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 62 | self.assertIn(item, t1) |
| 63 | self.assertNotIn(-42, t1) |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 64 | |
| 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 73 | 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 Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 78 | |
| 79 | def test_fields(self): |
| 80 | t = time.gmtime() |
Alexander Belopolsky | c142bba | 2012-06-13 22:15:26 -0400 | [diff] [blame] | 81 | 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 Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 84 | |
| 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 Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 114 | 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 Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 126 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 127 | support.run_unittest(StructSeqTest) |
Raymond Hettinger | f9f4c69 | 2003-08-30 22:54:55 +0000 | [diff] [blame] | 128 | |
| 129 | if __name__ == "__main__": |
| 130 | test_main() |