PyXR

c:\python24\lib \ test \ test_structseq.py



0001 import unittest
0002 from test import test_support
0003 
0004 import time
0005 
0006 class StructSeqTest(unittest.TestCase):
0007 
0008     def test_tuple(self):
0009         t = time.gmtime()
0010         astuple = tuple(t)
0011         self.assertEqual(len(t), len(astuple))
0012         self.assertEqual(t, astuple)
0013 
0014         # Check that slicing works the same way; at one point, slicing t[i:j] with
0015         # 0 < i < j could produce NULLs in the result.
0016         for i in xrange(-len(t), len(t)):
0017             self.assertEqual(t[i:], astuple[i:])
0018             for j in xrange(-len(t), len(t)):
0019                 self.assertEqual(t[i:j], astuple[i:j])
0020 
0021         for j in xrange(-len(t), len(t)):
0022             self.assertEqual(t[:j], astuple[:j])
0023 
0024         self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
0025         self.assertRaises(IndexError, t.__getitem__, len(t))
0026         for i in xrange(-len(t), len(t)-1):
0027             self.assertEqual(t[i], astuple[i])
0028 
0029     def test_repr(self):
0030         t = time.gmtime()
0031         repr(t)
0032 
0033     def test_concat(self):
0034         t1 = time.gmtime()
0035         t2 = t1 + tuple(t1)
0036         for i in xrange(len(t1)):
0037             self.assertEqual(t2[i], t2[i+len(t1)])
0038 
0039     def test_repeat(self):
0040         t1 = time.gmtime()
0041         t2 = 3 * t1
0042         for i in xrange(len(t1)):
0043             self.assertEqual(t2[i], t2[i+len(t1)])
0044             self.assertEqual(t2[i], t2[i+2*len(t1)])
0045 
0046     def test_contains(self):
0047         t1 = time.gmtime()
0048         for item in t1:
0049             self.assert_(item in t1)
0050         self.assert_(-42 not in t1)
0051 
0052     def test_hash(self):
0053         t1 = time.gmtime()
0054         self.assertEqual(hash(t1), hash(tuple(t1)))
0055 
0056     def test_cmp(self):
0057         t1 = time.gmtime()
0058         t2 = type(t1)(t1)
0059         self.assertEqual(t1, t2)
0060         self.assert_(not (t1 < t2))
0061         self.assert_(t1 <= t2)
0062         self.assert_(not (t1 > t2))
0063         self.assert_(t1 >= t2)
0064         self.assert_(not (t1 != t2))
0065 
0066     def test_fields(self):
0067         t = time.gmtime()
0068         self.assertEqual(len(t), t.n_fields)
0069         self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
0070 
0071     def test_constructor(self):
0072         t = time.struct_time
0073 
0074         self.assertRaises(TypeError, t)
0075         self.assertRaises(TypeError, t, None)
0076         self.assertRaises(TypeError, t, "123")
0077         self.assertRaises(TypeError, t, "123", dict={})
0078         self.assertRaises(TypeError, t, "123456789", dict=None)
0079 
0080         s = "123456789"
0081         self.assertEqual("".join(t(s)), s)
0082 
0083     def test_eviltuple(self):
0084         class Exc(Exception):
0085             pass
0086 
0087         # Devious code could crash structseqs' contructors
0088         class C:
0089             def __getitem__(self, i):
0090                 raise Exc
0091             def __len__(self):
0092                 return 9
0093 
0094         self.assertRaises(Exc, time.struct_time, C())
0095 
0096     def test_reduce(self):
0097         t = time.gmtime()
0098         x = t.__reduce__()
0099 
0100 def test_main():
0101     test_support.run_unittest(StructSeqTest)
0102 
0103 if __name__ == "__main__":
0104     test_main()
0105 

Generated by PyXR 0.9.4
SourceForge.net Logo