Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: iso-8859-1 -*- |
| 3 | |
| 4 | from test import test_support |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 5 | import marshal |
| 6 | import sys |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 7 | import unittest |
| 8 | import os |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 9 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 10 | class IntTestCase(unittest.TestCase): |
| 11 | def test_ints(self): |
| 12 | # Test the full range of Python ints. |
| 13 | n = sys.maxint |
| 14 | while n: |
| 15 | for expected in (-n, n): |
| 16 | s = marshal.dumps(expected) |
| 17 | got = marshal.loads(s) |
| 18 | self.assertEqual(expected, got) |
| 19 | marshal.dump(expected, file(test_support.TESTFN, "wb")) |
| 20 | got = marshal.load(file(test_support.TESTFN, "rb")) |
| 21 | self.assertEqual(expected, got) |
| 22 | n = n >> 1 |
| 23 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 24 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 25 | def test_int64(self): |
| 26 | # Simulate int marshaling on a 64-bit box. This is most interesting if |
| 27 | # we're running the test on a 32-bit box, of course. |
| 28 | |
| 29 | def to_little_endian_string(value, nbytes): |
| 30 | bytes = [] |
| 31 | for i in range(nbytes): |
| 32 | bytes.append(chr(value & 0xff)) |
| 33 | value >>= 8 |
| 34 | return ''.join(bytes) |
| 35 | |
| 36 | maxint64 = (1L << 63) - 1 |
| 37 | minint64 = -maxint64-1 |
| 38 | |
| 39 | for base in maxint64, minint64, -maxint64, -(minint64 >> 1): |
| 40 | while base: |
| 41 | s = 'I' + to_little_endian_string(base, 8) |
| 42 | got = marshal.loads(s) |
| 43 | self.assertEqual(base, got) |
| 44 | if base == -1: # a fixed-point for shifting right 1 |
| 45 | base = 0 |
| 46 | else: |
| 47 | base >>= 1 |
| 48 | |
| 49 | def test_bool(self): |
| 50 | for b in (True, False): |
| 51 | new = marshal.loads(marshal.dumps(b)) |
| 52 | self.assertEqual(b, new) |
| 53 | self.assertEqual(type(b), type(new)) |
| 54 | marshal.dump(b, file(test_support.TESTFN, "wb")) |
| 55 | new = marshal.load(file(test_support.TESTFN, "rb")) |
| 56 | self.assertEqual(b, new) |
| 57 | self.assertEqual(type(b), type(new)) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 58 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 59 | class FloatTestCase(unittest.TestCase): |
| 60 | def test_floats(self): |
| 61 | # Test a few floats |
| 62 | small = 1e-25 |
| 63 | n = sys.maxint * 3.7e250 |
| 64 | while n > small: |
| 65 | for expected in (-n, n): |
| 66 | f = float(expected) |
| 67 | s = marshal.dumps(f) |
| 68 | got = marshal.loads(s) |
| 69 | self.assertEqual(f, got) |
| 70 | marshal.dump(f, file(test_support.TESTFN, "wb")) |
| 71 | got = marshal.load(file(test_support.TESTFN, "rb")) |
| 72 | self.assertEqual(f, got) |
| 73 | n /= 123.4567 |
| 74 | |
| 75 | f = 0.0 |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame^] | 76 | s = marshal.dumps(f, 2) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 77 | got = marshal.loads(s) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 78 | self.assertEqual(f, got) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame^] | 79 | # and with version <= 1 (floats marshalled differently then) |
| 80 | s = marshal.dumps(f, 1) |
| 81 | got = marshal.loads(s) |
| 82 | self.assertEqual(f, got) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 83 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 84 | n = sys.maxint * 3.7e-250 |
| 85 | while n < small: |
| 86 | for expected in (-n, n): |
| 87 | f = float(expected) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame^] | 88 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 89 | s = marshal.dumps(f) |
| 90 | got = marshal.loads(s) |
| 91 | self.assertEqual(f, got) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame^] | 92 | |
| 93 | s = marshal.dumps(f, 1) |
| 94 | got = marshal.loads(s) |
| 95 | self.assertEqual(f, got) |
| 96 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 97 | marshal.dump(f, file(test_support.TESTFN, "wb")) |
| 98 | got = marshal.load(file(test_support.TESTFN, "rb")) |
| 99 | self.assertEqual(f, got) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame^] | 100 | |
| 101 | marshal.dump(f, file(test_support.TESTFN, "wb"), 1) |
| 102 | got = marshal.load(file(test_support.TESTFN, "rb")) |
| 103 | self.assertEqual(f, got) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 104 | n *= 123.4567 |
| 105 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 106 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 107 | class StringTestCase(unittest.TestCase): |
| 108 | def test_unicode(self): |
| 109 | for s in [u"", u"Andrè Previn", u"abc", u" "*10000]: |
| 110 | new = marshal.loads(marshal.dumps(s)) |
| 111 | self.assertEqual(s, new) |
| 112 | self.assertEqual(type(s), type(new)) |
| 113 | marshal.dump(s, file(test_support.TESTFN, "wb")) |
| 114 | marshal.load(file(test_support.TESTFN, "rb")) |
| 115 | self.assertEqual(s, new) |
| 116 | self.assertEqual(type(s), type(new)) |
| 117 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 118 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 119 | def test_string(self): |
| 120 | for s in ["", "Andrè Previn", "abc", " "*10000]: |
| 121 | new = marshal.loads(marshal.dumps(s)) |
| 122 | self.assertEqual(s, new) |
| 123 | self.assertEqual(type(s), type(new)) |
| 124 | marshal.dump(s, file(test_support.TESTFN, "wb")) |
| 125 | marshal.load(file(test_support.TESTFN, "rb")) |
| 126 | self.assertEqual(s, new) |
| 127 | self.assertEqual(type(s), type(new)) |
| 128 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 129 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 130 | def test_buffer(self): |
| 131 | for s in ["", "Andrè Previn", "abc", " "*10000]: |
| 132 | b = buffer(s) |
| 133 | new = marshal.loads(marshal.dumps(b)) |
| 134 | self.assertEqual(s, new) |
| 135 | marshal.dump(b, file(test_support.TESTFN, "wb")) |
| 136 | marshal.load(file(test_support.TESTFN, "rb")) |
| 137 | self.assertEqual(s, new) |
| 138 | os.unlink(test_support.TESTFN) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 139 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 140 | class ExceptionTestCase(unittest.TestCase): |
| 141 | def test_exceptions(self): |
| 142 | new = marshal.loads(marshal.dumps(StopIteration)) |
| 143 | self.assertEqual(StopIteration, new) |
Thomas Heller | 3e1c18a | 2002-07-30 11:40:57 +0000 | [diff] [blame] | 144 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 145 | class CodeTestCase(unittest.TestCase): |
| 146 | def test_code(self): |
| 147 | co = ExceptionTestCase.test_exceptions.func_code |
| 148 | new = marshal.loads(marshal.dumps(co)) |
| 149 | self.assertEqual(co, new) |
| 150 | |
| 151 | class ContainerTestCase(unittest.TestCase): |
| 152 | d = {'astring': 'foo@bar.baz.spam', |
| 153 | 'afloat': 7283.43, |
| 154 | 'anint': 2**20, |
| 155 | 'ashortlong': 2L, |
| 156 | 'alist': ['.zyx.41'], |
| 157 | 'atuple': ('.zyx.41',)*10, |
| 158 | 'aboolean': False, |
| 159 | 'aunicode': u"Andrè Previn" |
| 160 | } |
| 161 | def test_dict(self): |
| 162 | new = marshal.loads(marshal.dumps(self.d)) |
| 163 | self.assertEqual(self.d, new) |
| 164 | marshal.dump(self.d, file(test_support.TESTFN, "wb")) |
| 165 | marshal.load(file(test_support.TESTFN, "rb")) |
| 166 | self.assertEqual(self.d, new) |
| 167 | os.unlink(test_support.TESTFN) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 168 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 169 | def test_list(self): |
| 170 | lst = self.d.items() |
| 171 | new = marshal.loads(marshal.dumps(lst)) |
| 172 | self.assertEqual(lst, new) |
| 173 | marshal.dump(lst, file(test_support.TESTFN, "wb")) |
| 174 | marshal.load(file(test_support.TESTFN, "rb")) |
| 175 | self.assertEqual(lst, new) |
| 176 | os.unlink(test_support.TESTFN) |
| 177 | |
| 178 | def test_tuple(self): |
| 179 | t = tuple(self.d.keys()) |
| 180 | new = marshal.loads(marshal.dumps(t)) |
| 181 | self.assertEqual(t, new) |
| 182 | marshal.dump(t, file(test_support.TESTFN, "wb")) |
| 183 | marshal.load(file(test_support.TESTFN, "rb")) |
| 184 | self.assertEqual(t, new) |
| 185 | os.unlink(test_support.TESTFN) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 186 | |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 187 | def test_sets(self): |
| 188 | for constructor in (set, frozenset): |
| 189 | t = constructor(self.d.keys()) |
| 190 | new = marshal.loads(marshal.dumps(t)) |
| 191 | self.assertEqual(t, new) |
| 192 | self.assert_(isinstance(new, constructor)) |
| 193 | self.assertNotEqual(id(t), id(new)) |
| 194 | marshal.dump(t, file(test_support.TESTFN, "wb")) |
| 195 | marshal.load(file(test_support.TESTFN, "rb")) |
| 196 | self.assertEqual(t, new) |
| 197 | os.unlink(test_support.TESTFN) |
| 198 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 199 | class BugsTestCase(unittest.TestCase): |
| 200 | def test_bug_5888452(self): |
| 201 | # Simple-minded check for SF 588452: Debug build crashes |
| 202 | marshal.dumps([128] * 1000) |
| 203 | |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 204 | def test_patch_873224(self): |
| 205 | self.assertRaises(Exception, marshal.loads, '0') |
| 206 | self.assertRaises(Exception, marshal.loads, 'f') |
| 207 | self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1]) |
| 208 | |
Armin Rigo | 2ccea17 | 2004-12-20 12:25:57 +0000 | [diff] [blame] | 209 | def test_version_argument(self): |
| 210 | # Python 2.4.0 crashes for any call to marshal.dumps(x, y) |
| 211 | self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5) |
| 212 | self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5) |
| 213 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 214 | def test_main(): |
| 215 | test_support.run_unittest(IntTestCase, |
| 216 | FloatTestCase, |
| 217 | StringTestCase, |
| 218 | CodeTestCase, |
| 219 | ContainerTestCase, |
| 220 | ExceptionTestCase, |
| 221 | BugsTestCase) |
| 222 | |
| 223 | if __name__ == "__main__": |
| 224 | test_main() |