annotate Lib/test/test_structseq.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 17891566a478
children
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()
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44687
diff changeset
31 self.assertTrue(repr(t))
44667
b409941c7a51 Added new an better structseq representation. E.g. repr(time.gmtime(0)) now returns 'time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)' instead of '(1970, 1, 1, 0, 0, 0, 3, 1, 0)'. The feature is part of #1816: sys.flags
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
32 t = time.gmtime(0)
b409941c7a51 Added new an better structseq representation. E.g. repr(time.gmtime(0)) now returns 'time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)' instead of '(1970, 1, 1, 0, 0, 0, 3, 1, 0)'. The feature is part of #1816: sys.flags
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
33 self.assertEqual(repr(t),
b409941c7a51 Added new an better structseq representation. E.g. repr(time.gmtime(0)) now returns 'time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)' instead of '(1970, 1, 1, 0, 0, 0, 3, 1, 0)'. The feature is part of #1816: sys.flags
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
34 "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
b409941c7a51 Added new an better structseq representation. E.g. repr(time.gmtime(0)) now returns 'time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)' instead of '(1970, 1, 1, 0, 0, 0, 3, 1, 0)'. The feature is part of #1816: sys.flags
Christian Heimes <christian@cheimes.de>
parents: 42951
diff changeset
35 "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
36
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
37 def test_concat(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
38 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
39 t2 = t1 + tuple(t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
40 for i in xrange(len(t1)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
41 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
42
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
43 def test_repeat(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
44 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
45 t2 = 3 * t1
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
46 for i in xrange(len(t1)):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
47 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
48 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
49
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
50 def test_contains(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
51 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
52 for item in t1:
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
53 self.assertIn(item, t1)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55002
diff changeset
54 self.assertNotIn(-42, t1)
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
55
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
56 def test_hash(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 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
59
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
60 def test_cmp(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
61 t1 = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
62 t2 = type(t1)(t1)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
63 self.assertEqual(t1, t2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44687
diff changeset
64 self.assertTrue(not (t1 < t2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44687
diff changeset
65 self.assertTrue(t1 <= t2)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44687
diff changeset
66 self.assertTrue(not (t1 > t2))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44687
diff changeset
67 self.assertTrue(t1 >= t2)
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44687
diff changeset
68 self.assertTrue(not (t1 != t2))
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
69
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
70 def test_fields(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
71 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
72 self.assertEqual(len(t), t.n_fields)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
73 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
74
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
75 def test_constructor(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
76 t = time.struct_time
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
77
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
78 self.assertRaises(TypeError, t)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
79 self.assertRaises(TypeError, t, None)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
80 self.assertRaises(TypeError, t, "123")
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
81 self.assertRaises(TypeError, t, "123", dict={})
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
82 self.assertRaises(TypeError, t, "123456789", dict=None)
22163
42862169541f backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 20781
diff changeset
83
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
84 s = "123456789"
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
85 self.assertEqual("".join(t(s)), s)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
86
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
87 def test_eviltuple(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
88 class Exc(Exception):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
89 pass
22163
42862169541f backport my checkin of
Michael W. Hudson <mwh@python.net>
parents: 20781
diff changeset
90
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
91 # Devious code could crash structseqs' contructors
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
92 class C:
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
93 def __getitem__(self, i):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
94 raise Exc
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
95 def __len__(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
96 return 9
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
97
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
98 self.assertRaises(Exc, time.struct_time, C())
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_reduce(self):
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
101 t = time.gmtime()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
102 x = t.__reduce__()
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
103
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
104 def test_extended_getslice(self):
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
105 # Test extended slicing by comparing with list slicing.
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
106 t = time.gmtime()
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
107 L = list(t)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
108 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
109 for start in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
110 for stop in indices:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
111 # Skip step 0 (invalid)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
112 for step in indices[1:]:
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
113 self.assertEqual(list(t[start:stop:step]),
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
114 L[start:stop:step])
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 30247
diff changeset
115
30247
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
116 def test_main():
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
117 test_support.run_unittest(StructSeqTest)
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
118
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
119 if __name__ == "__main__":
95695cc45cdd SF patch #736962: Port tests to unittest
Raymond Hettinger <python@rcn.com>
parents: 24364
diff changeset
120 test_main()