annotate Lib/test/test_structseq.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents 95695cc45cdd
children 9620134010a5 a6eb6acfe04a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
1 import unittest
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
2 from test import test_support
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 import time
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
6 class StructSeqTest(unittest.TestCase):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
7
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
8 def test_tuple(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
9 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
10 astuple = tuple(t)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
11 self.assertEqual(len(t), len(astuple))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
12 self.assertEqual(t, astuple)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
13
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
14 # Check that slicing works the same way; at one point, slicing t[i:j] with
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
15 # 0 < i < j could produce NULLs in the result.
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
16 for i in xrange(-len(t), len(t)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
17 self.assertEqual(t[i:], astuple[i:])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
18 for j in xrange(-len(t), len(t)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
19 self.assertEqual(t[i:j], astuple[i:j])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
20
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
21 for j in xrange(-len(t), len(t)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
22 self.assertEqual(t[:j], astuple[:j])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
23
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
24 self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
25 self.assertRaises(IndexError, t.__getitem__, len(t))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
26 for i in xrange(-len(t), len(t)-1):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
27 self.assertEqual(t[i], astuple[i])
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
29 def test_repr(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
30 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
31 repr(t)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
32
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
33 def test_concat(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
34 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
35 t2 = t1 + tuple(t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
36 for i in xrange(len(t1)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
37 self.assertEqual(t2[i], t2[i+len(t1)])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
38
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
39 def test_repeat(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
40 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
41 t2 = 3 * t1
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
42 for i in xrange(len(t1)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
43 self.assertEqual(t2[i], t2[i+len(t1)])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
44 self.assertEqual(t2[i], t2[i+2*len(t1)])
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
45
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
46 def test_contains(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
47 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
48 for item in t1:
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
49 self.assert_(item in t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
50 self.assert_(-42 not in t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
51
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
52 def test_hash(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
53 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
54 self.assertEqual(hash(t1), hash(tuple(t1)))
20780
ab2e315849a7 Fix bad bug in structseq slicing (NULL pointers in result). Reported by
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
55
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
56 def test_cmp(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
57 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
58 t2 = type(t1)(t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
59 self.assertEqual(t1, t2)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
60 self.assert_(not (t1 < t2))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
61 self.assert_(t1 <= t2)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
62 self.assert_(not (t1 > t2))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
63 self.assert_(t1 >= t2)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
64 self.assert_(not (t1 != t2))
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
65
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
66 def test_fields(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
67 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
68 self.assertEqual(len(t), t.n_fields)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
69 self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
70
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
71 def test_constructor(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
72 t = time.struct_time
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
73
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
74 self.assertRaises(TypeError, t)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
75 self.assertRaises(TypeError, t, None)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
76 self.assertRaises(TypeError, t, "123")
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
77 self.assertRaises(TypeError, t, "123", dict={})
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
78 self.assertRaises(TypeError, t, "123456789", dict=None)
22163
42862169541f backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 20781
diff changeset
79
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
80 s = "123456789"
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
81 self.assertEqual("".join(t(s)), s)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
82
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
83 def test_eviltuple(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
84 class Exc(Exception):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
85 pass
22163
42862169541f backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 20781
diff changeset
86
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
87 # Devious code could crash structseqs' contructors
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
88 class C:
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
89 def __getitem__(self, i):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
90 raise Exc
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
91 def __len__(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
92 return 9
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
93
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
94 self.assertRaises(Exc, time.struct_time, C())
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
95
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
96 def test_reduce(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
97 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
98 x = t.__reduce__()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
99
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
100 def test_main():
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
101 test_support.run_unittest(StructSeqTest)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
102
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
103 if __name__ == "__main__":
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
104 test_main()