##// END OF EJS Templates
update serialize tests
MinRK -
Show More
@@ -20,6 +20,10 b' from IPython.zmq.serialize import serialize_object, unserialize_object'
20 20 from IPython.testing import decorators as dec
21 21 from IPython.utils.pickleutil import CannedArray
22 22
23 #-------------------------------------------------------------------------------
24 # Globals and Utilities
25 #-------------------------------------------------------------------------------
26
23 27 def roundtrip(obj):
24 28 """roundtrip an object through serialization"""
25 29 bufs = serialize_object(obj)
@@ -34,6 +38,12 b' class C(object):'
34 38 for key,value in kwargs.iteritems():
35 39 setattr(self, key, value)
36 40
41 SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,))
42 DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10')
43 #-------------------------------------------------------------------------------
44 # Tests
45 #-------------------------------------------------------------------------------
46
37 47 @dec.parametric
38 48 def test_roundtrip_simple():
39 49 for obj in [
@@ -67,17 +77,54 b' def test_roundtrip_buffered():'
67 77 yield nt.assert_equals(remainder, [])
68 78 yield nt.assert_equals(obj, obj2)
69 79
80 def _scrub_nan(A):
81 """scrub nans out of empty arrays
82
83 since nan != nan
84 """
85 import numpy
86 if A.dtype.fields and A.shape:
87 for field in A.dtype.fields.keys():
88 try:
89 A[field][numpy.isnan(A[field])] = 0
90 except TypeError:
91 # e.g. str dtype
92 pass
93
70 94 @dec.parametric
71 95 @dec.skip_without('numpy')
72 96 def test_numpy():
73 97 import numpy
74 98 from numpy.testing.utils import assert_array_equal
75 for shape in ((), (0,), (100,), (1024,10), (10,8,6,5)):
76 for dtype in ('uint8', 'float64', 'int32', [('int16', 'float32')]):
99 for shape in SHAPES:
100 for dtype in DTYPES:
101 A = numpy.empty(shape, dtype=dtype)
102 _scrub_nan(A)
103 bufs = serialize_object(A)
104 B, r = unserialize_object(bufs)
105 yield nt.assert_equals(r, [])
106 yield nt.assert_equals(A.shape, B.shape)
107 yield nt.assert_equals(A.dtype, B.dtype)
108 yield assert_array_equal(A,B)
109
110 @dec.parametric
111 @dec.skip_without('numpy')
112 def test_recarray():
113 import numpy
114 from numpy.testing.utils import assert_array_equal
115 for shape in SHAPES:
116 for dtype in [
117 [('f', float), ('s', '|S10')],
118 [('n', int), ('s', '|S1'), ('u', 'uint32')],
119 ]:
77 120 A = numpy.empty(shape, dtype=dtype)
121 _scrub_nan(A)
122
78 123 bufs = serialize_object(A)
79 124 B, r = unserialize_object(bufs)
80 125 yield nt.assert_equals(r, [])
126 yield nt.assert_equals(A.shape, B.shape)
127 yield nt.assert_equals(A.dtype, B.dtype)
81 128 yield assert_array_equal(A,B)
82 129
83 130 @dec.parametric
@@ -85,15 +132,18 b' def test_numpy():'
85 132 def test_numpy_in_seq():
86 133 import numpy
87 134 from numpy.testing.utils import assert_array_equal
88 for shape in ((), (0,), (100,), (1024,10), (10,8,6,5)):
89 for dtype in ('uint8', 'float64', 'int32', [('int16', 'float32')]):
135 for shape in SHAPES:
136 for dtype in DTYPES:
90 137 A = numpy.empty(shape, dtype=dtype)
138 _scrub_nan(A)
91 139 bufs = serialize_object((A,1,2,b'hello'))
92 140 canned = pickle.loads(bufs[0])
93 141 yield nt.assert_true(canned[0], CannedArray)
94 142 tup, r = unserialize_object(bufs)
95 143 B = tup[0]
96 144 yield nt.assert_equals(r, [])
145 yield nt.assert_equals(A.shape, B.shape)
146 yield nt.assert_equals(A.dtype, B.dtype)
97 147 yield assert_array_equal(A,B)
98 148
99 149 @dec.parametric
@@ -101,15 +151,18 b' def test_numpy_in_seq():'
101 151 def test_numpy_in_dict():
102 152 import numpy
103 153 from numpy.testing.utils import assert_array_equal
104 for shape in ((), (0,), (100,), (1024,10), (10,8,6,5)):
105 for dtype in ('uint8', 'float64', 'int32', [('int16', 'float32')]):
154 for shape in SHAPES:
155 for dtype in DTYPES:
106 156 A = numpy.empty(shape, dtype=dtype)
157 _scrub_nan(A)
107 158 bufs = serialize_object(dict(a=A,b=1,c=range(20)))
108 159 canned = pickle.loads(bufs[0])
109 160 yield nt.assert_true(canned['a'], CannedArray)
110 161 d, r = unserialize_object(bufs)
111 162 B = d['a']
112 163 yield nt.assert_equals(r, [])
164 yield nt.assert_equals(A.shape, B.shape)
165 yield nt.assert_equals(A.dtype, B.dtype)
113 166 yield assert_array_equal(A,B)
114 167
115 168
General Comments 0
You need to be logged in to leave comments. Login now