PyXR

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



0001 #!/usr/bin/env python
0002 # -*- coding: iso-8859-1 -*-
0003 
0004 from test import test_support
0005 import marshal
0006 import sys
0007 import unittest
0008 import os
0009 
0010 class IntTestCase(unittest.TestCase):
0011     def test_ints(self):
0012         # Test the full range of Python ints.
0013         n = sys.maxint
0014         while n:
0015             for expected in (-n, n):
0016                 s = marshal.dumps(expected)
0017                 got = marshal.loads(s)
0018                 self.assertEqual(expected, got)
0019                 marshal.dump(expected, file(test_support.TESTFN, "wb"))
0020                 got = marshal.load(file(test_support.TESTFN, "rb"))
0021                 self.assertEqual(expected, got)
0022             n = n >> 1
0023         os.unlink(test_support.TESTFN)
0024 
0025     def test_int64(self):
0026         # Simulate int marshaling on a 64-bit box.  This is most interesting if
0027         # we're running the test on a 32-bit box, of course.
0028 
0029         def to_little_endian_string(value, nbytes):
0030             bytes = []
0031             for i in range(nbytes):
0032                 bytes.append(chr(value & 0xff))
0033                 value >>= 8
0034             return ''.join(bytes)
0035 
0036         maxint64 = (1L << 63) - 1
0037         minint64 = -maxint64-1
0038 
0039         for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
0040             while base:
0041                 s = 'I' + to_little_endian_string(base, 8)
0042                 got = marshal.loads(s)
0043                 self.assertEqual(base, got)
0044                 if base == -1:  # a fixed-point for shifting right 1
0045                     base = 0
0046                 else:
0047                     base >>= 1
0048 
0049     def test_bool(self):
0050         for b in (True, False):
0051             new = marshal.loads(marshal.dumps(b))
0052             self.assertEqual(b, new)
0053             self.assertEqual(type(b), type(new))
0054             marshal.dump(b, file(test_support.TESTFN, "wb"))
0055             new = marshal.load(file(test_support.TESTFN, "rb"))
0056             self.assertEqual(b, new)
0057             self.assertEqual(type(b), type(new))
0058 
0059 class FloatTestCase(unittest.TestCase):
0060     def test_floats(self):
0061         # Test a few floats
0062         small = 1e-25
0063         n = sys.maxint * 3.7e250
0064         while n > small:
0065             for expected in (-n, n):
0066                 f = float(expected)
0067                 s = marshal.dumps(f)
0068                 got = marshal.loads(s)
0069                 self.assertEqual(f, got)
0070                 marshal.dump(f, file(test_support.TESTFN, "wb"))
0071                 got = marshal.load(file(test_support.TESTFN, "rb"))
0072                 self.assertEqual(f, got)
0073             n /= 123.4567
0074 
0075         f = 0.0
0076         s = marshal.dumps(f)
0077         got = marshal.loads(s)
0078         self.assertEqual(f, got)
0079 
0080         n = sys.maxint * 3.7e-250
0081         while n < small:
0082             for expected in (-n, n):
0083                 f = float(expected)
0084                 s = marshal.dumps(f)
0085                 got = marshal.loads(s)
0086                 self.assertEqual(f, got)
0087                 marshal.dump(f, file(test_support.TESTFN, "wb"))
0088                 got = marshal.load(file(test_support.TESTFN, "rb"))
0089                 self.assertEqual(f, got)
0090             n *= 123.4567
0091         os.unlink(test_support.TESTFN)
0092 
0093 class StringTestCase(unittest.TestCase):
0094     def test_unicode(self):
0095         for s in [u"", u"Andrč Previn", u"abc", u" "*10000]:
0096             new = marshal.loads(marshal.dumps(s))
0097             self.assertEqual(s, new)
0098             self.assertEqual(type(s), type(new))
0099             marshal.dump(s, file(test_support.TESTFN, "wb"))
0100             marshal.load(file(test_support.TESTFN, "rb"))
0101             self.assertEqual(s, new)
0102             self.assertEqual(type(s), type(new))
0103         os.unlink(test_support.TESTFN)
0104 
0105     def test_string(self):
0106         for s in ["", "Andrč Previn", "abc", " "*10000]:
0107             new = marshal.loads(marshal.dumps(s))
0108             self.assertEqual(s, new)
0109             self.assertEqual(type(s), type(new))
0110             marshal.dump(s, file(test_support.TESTFN, "wb"))
0111             marshal.load(file(test_support.TESTFN, "rb"))
0112             self.assertEqual(s, new)
0113             self.assertEqual(type(s), type(new))
0114         os.unlink(test_support.TESTFN)
0115 
0116     def test_buffer(self):
0117         for s in ["", "Andrč Previn", "abc", " "*10000]:
0118             b = buffer(s)
0119             new = marshal.loads(marshal.dumps(b))
0120             self.assertEqual(s, new)
0121             marshal.dump(b, file(test_support.TESTFN, "wb"))
0122             marshal.load(file(test_support.TESTFN, "rb"))
0123             self.assertEqual(s, new)
0124         os.unlink(test_support.TESTFN)
0125 
0126 class ExceptionTestCase(unittest.TestCase):
0127     def test_exceptions(self):
0128         new = marshal.loads(marshal.dumps(StopIteration))
0129         self.assertEqual(StopIteration, new)
0130 
0131 class CodeTestCase(unittest.TestCase):
0132     def test_code(self):
0133         co = ExceptionTestCase.test_exceptions.func_code
0134         new = marshal.loads(marshal.dumps(co))
0135         self.assertEqual(co, new)
0136 
0137 class ContainerTestCase(unittest.TestCase):
0138     d = {'astring': 'foo@bar.baz.spam',
0139          'afloat': 7283.43,
0140          'anint': 2**20,
0141          'ashortlong': 2L,
0142          'alist': ['.zyx.41'],
0143          'atuple': ('.zyx.41',)*10,
0144          'aboolean': False,
0145          'aunicode': u"Andrč Previn"
0146          }
0147     def test_dict(self):
0148         new = marshal.loads(marshal.dumps(self.d))
0149         self.assertEqual(self.d, new)
0150         marshal.dump(self.d, file(test_support.TESTFN, "wb"))
0151         marshal.load(file(test_support.TESTFN, "rb"))
0152         self.assertEqual(self.d, new)
0153         os.unlink(test_support.TESTFN)
0154 
0155     def test_list(self):
0156         lst = self.d.items()
0157         new = marshal.loads(marshal.dumps(lst))
0158         self.assertEqual(lst, new)
0159         marshal.dump(lst, file(test_support.TESTFN, "wb"))
0160         marshal.load(file(test_support.TESTFN, "rb"))
0161         self.assertEqual(lst, new)
0162         os.unlink(test_support.TESTFN)
0163 
0164     def test_tuple(self):
0165         t = tuple(self.d.keys())
0166         new = marshal.loads(marshal.dumps(t))
0167         self.assertEqual(t, new)
0168         marshal.dump(t, file(test_support.TESTFN, "wb"))
0169         marshal.load(file(test_support.TESTFN, "rb"))
0170         self.assertEqual(t, new)
0171         os.unlink(test_support.TESTFN)
0172 
0173 class BugsTestCase(unittest.TestCase):
0174     def test_bug_5888452(self):
0175         # Simple-minded check for SF 588452: Debug build crashes
0176         marshal.dumps([128] * 1000)
0177 
0178     def test_patch_873224(self):
0179         self.assertRaises(Exception, marshal.loads, '0')
0180         self.assertRaises(Exception, marshal.loads, 'f')
0181         self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1])
0182 
0183 def test_main():
0184     test_support.run_unittest(IntTestCase,
0185                               FloatTestCase,
0186                               StringTestCase,
0187                               CodeTestCase,
0188                               ContainerTestCase,
0189                               ExceptionTestCase,
0190                               BugsTestCase)
0191 
0192 if __name__ == "__main__":
0193     test_main()
0194 

Generated by PyXR 0.9.4
SourceForge.net Logo