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 |
| 76 | s = marshal.dumps(f) |
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) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 79 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 80 | n = sys.maxint * 3.7e-250 |
| 81 | while n < small: |
| 82 | for expected in (-n, n): |
| 83 | f = float(expected) |
| 84 | s = marshal.dumps(f) |
| 85 | got = marshal.loads(s) |
| 86 | self.assertEqual(f, got) |
| 87 | marshal.dump(f, file(test_support.TESTFN, "wb")) |
| 88 | got = marshal.load(file(test_support.TESTFN, "rb")) |
| 89 | self.assertEqual(f, got) |
| 90 | n *= 123.4567 |
| 91 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 92 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 93 | class StringTestCase(unittest.TestCase): |
| 94 | def test_unicode(self): |
| 95 | for s in [u"", u"Andrè Previn", u"abc", u" "*10000]: |
| 96 | new = marshal.loads(marshal.dumps(s)) |
| 97 | self.assertEqual(s, new) |
| 98 | self.assertEqual(type(s), type(new)) |
| 99 | marshal.dump(s, file(test_support.TESTFN, "wb")) |
| 100 | marshal.load(file(test_support.TESTFN, "rb")) |
| 101 | self.assertEqual(s, new) |
| 102 | self.assertEqual(type(s), type(new)) |
| 103 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 104 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 105 | def test_string(self): |
| 106 | for s in ["", "Andrè Previn", "abc", " "*10000]: |
| 107 | new = marshal.loads(marshal.dumps(s)) |
| 108 | self.assertEqual(s, new) |
| 109 | self.assertEqual(type(s), type(new)) |
| 110 | marshal.dump(s, file(test_support.TESTFN, "wb")) |
| 111 | marshal.load(file(test_support.TESTFN, "rb")) |
| 112 | self.assertEqual(s, new) |
| 113 | self.assertEqual(type(s), type(new)) |
| 114 | os.unlink(test_support.TESTFN) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 115 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 116 | def test_buffer(self): |
| 117 | for s in ["", "Andrè Previn", "abc", " "*10000]: |
| 118 | b = buffer(s) |
| 119 | new = marshal.loads(marshal.dumps(b)) |
| 120 | self.assertEqual(s, new) |
| 121 | marshal.dump(b, file(test_support.TESTFN, "wb")) |
| 122 | marshal.load(file(test_support.TESTFN, "rb")) |
| 123 | self.assertEqual(s, new) |
| 124 | os.unlink(test_support.TESTFN) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 125 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 126 | class ExceptionTestCase(unittest.TestCase): |
| 127 | def test_exceptions(self): |
| 128 | new = marshal.loads(marshal.dumps(StopIteration)) |
| 129 | self.assertEqual(StopIteration, new) |
Thomas Heller | 3e1c18a | 2002-07-30 11:40:57 +0000 | [diff] [blame] | 130 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 131 | class CodeTestCase(unittest.TestCase): |
| 132 | def test_code(self): |
| 133 | co = ExceptionTestCase.test_exceptions.func_code |
| 134 | new = marshal.loads(marshal.dumps(co)) |
| 135 | self.assertEqual(co, new) |
| 136 | |
| 137 | class ContainerTestCase(unittest.TestCase): |
| 138 | d = {'astring': 'foo@bar.baz.spam', |
| 139 | 'afloat': 7283.43, |
| 140 | 'anint': 2**20, |
| 141 | 'ashortlong': 2L, |
| 142 | 'alist': ['.zyx.41'], |
| 143 | 'atuple': ('.zyx.41',)*10, |
| 144 | 'aboolean': False, |
| 145 | 'aunicode': u"Andrè Previn" |
| 146 | } |
| 147 | def test_dict(self): |
| 148 | new = marshal.loads(marshal.dumps(self.d)) |
| 149 | self.assertEqual(self.d, new) |
| 150 | marshal.dump(self.d, file(test_support.TESTFN, "wb")) |
| 151 | marshal.load(file(test_support.TESTFN, "rb")) |
| 152 | self.assertEqual(self.d, new) |
| 153 | os.unlink(test_support.TESTFN) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 154 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 155 | def test_list(self): |
| 156 | lst = self.d.items() |
| 157 | new = marshal.loads(marshal.dumps(lst)) |
| 158 | self.assertEqual(lst, new) |
| 159 | marshal.dump(lst, file(test_support.TESTFN, "wb")) |
| 160 | marshal.load(file(test_support.TESTFN, "rb")) |
| 161 | self.assertEqual(lst, new) |
| 162 | os.unlink(test_support.TESTFN) |
| 163 | |
| 164 | def test_tuple(self): |
| 165 | t = tuple(self.d.keys()) |
| 166 | new = marshal.loads(marshal.dumps(t)) |
| 167 | self.assertEqual(t, new) |
| 168 | marshal.dump(t, file(test_support.TESTFN, "wb")) |
| 169 | marshal.load(file(test_support.TESTFN, "rb")) |
| 170 | self.assertEqual(t, new) |
| 171 | os.unlink(test_support.TESTFN) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 172 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 173 | class BugsTestCase(unittest.TestCase): |
| 174 | def test_bug_5888452(self): |
| 175 | # Simple-minded check for SF 588452: Debug build crashes |
| 176 | marshal.dumps([128] * 1000) |
| 177 | |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame^] | 178 | def test_patch_873224(self): |
| 179 | self.assertRaises(Exception, marshal.loads, '0') |
| 180 | self.assertRaises(Exception, marshal.loads, 'f') |
| 181 | self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1]) |
| 182 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 183 | def test_main(): |
| 184 | test_support.run_unittest(IntTestCase, |
| 185 | FloatTestCase, |
| 186 | StringTestCase, |
| 187 | CodeTestCase, |
| 188 | ContainerTestCase, |
| 189 | ExceptionTestCase, |
| 190 | BugsTestCase) |
| 191 | |
| 192 | if __name__ == "__main__": |
| 193 | test_main() |