Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b93ba8

Browse files
committed
Added unit-tests from Francesc Alted
1 parent 263df75 commit 6b93ba8

5 files changed

Lines changed: 448 additions & 65 deletions

File tree

THANKS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Eric Firing for bugfixes.
1515
Arnd Baecker for 64-bit testing
1616
David Cooke for many code improvements including the auto-generated C-API
1717
Alexander Belopolsky (sasha) for Masked array bug-fixes and tests and rank-0 array improvements
18+
Francesc Altet for unicode and nested record tests and help with nested records
19+
Tim Hochberg for getting the build working on MSVC

numpy/core/include/numpy/arrayobject.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,22 +1466,22 @@ typedef struct {
14661466
*/
14671467

14681468
#define PyArray_GETPTR1(obj, i) (void *)(PyArray_BYTES(obj) + \
1469-
i*PyArray_STRIDE(obj, 0))
1470-
1471-
#define PyArray_GETPTR2(obj, i, j) (void *)(PyArray_BYTES(obj) + \
1472-
i*PyArray_STRIDE(obj, 0) + \
1473-
j*PyArray_STRIDE(obj, 1))
1474-
1475-
#define PyArray_GETPTR3(obj, i, j, k) (void *)(PyArray_BYTES(obj) + \
1476-
i*PyArray_STRIDE(obj, 0) + \
1477-
j*PyArray_STRIDE(obj, 1) + \
1478-
k*PyArray_STRIDE(obj, 2)) \
1479-
1480-
#define PyArray_GETPTR4(obj, i, j, k, l) (void *)(PyArray_BYTES(obj) + \
1481-
i*PyArray_STRIDE(obj, 0) + \
1482-
j*PyArray_STRIDE(obj, 1) + \
1483-
k*PyArray_STRIDE(obj, 2) + \
1484-
l*PyArray_STRIDE(obj, 3))
1469+
i*PyArray_STRIDE(obj, 0))
1470+
1471+
#define PyArray_GETPTR2(obj, i, j) (void *)(PyArray_BYTES(obj) + \
1472+
i*PyArray_STRIDE(obj, 0) + \
1473+
j*PyArray_STRIDE(obj, 1))
1474+
1475+
#define PyArray_GETPTR3(obj, i, j, k) (void *)(PyArray_BYTES(obj) + \
1476+
i*PyArray_STRIDE(obj, 0) + \
1477+
j*PyArray_STRIDE(obj, 1) + \
1478+
k*PyArray_STRIDE(obj, 2)) \
1479+
1480+
#define PyArray_GETPTR4(obj, i, j, k, l) (void *)(PyArray_BYTES(obj) + \
1481+
i*PyArray_STRIDE(obj, 0) + \
1482+
j*PyArray_STRIDE(obj, 1) + \
1483+
k*PyArray_STRIDE(obj, 2) + \
1484+
l*PyArray_STRIDE(obj, 3))
14851485

14861486
#define PyArray_DESCR_REPLACE(descr) do { \
14871487
PyArray_Descr *_new_; \

numpy/core/tests/test_multiarray.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ def check_test_interning(self):
191191
self.failUnless(array([True])[0] is a1)
192192
self.failUnless(array(True)[...] is a1)
193193

194+
# Import tests from unicode
195+
from test_unicode import *
194196

195197
if __name__ == "__main__":
196198
ScipyTest('numpy.core.multiarray').run()

numpy/core/tests/test_numerictypes.py

Lines changed: 123 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
22
from numpy.testing import *
3-
set_package_path()
3+
import numpy
44
from numpy import zeros, ones, array
5-
restore_path()
65

76

87
# This is the structure of the table used for plain objects:
@@ -51,7 +50,7 @@
5150
('z2', 'b1')]),
5251
('color', 'S2'),
5352
('info', [
54-
('Name', 'S2'),
53+
('Name', 'U8'), # Try out 'U8' when interpretation of Unicode strings is more clear
5554
('Value', 'c16')]),
5655
('y', 'f8', (2, 2)),
5756
('z', 'u1')]
@@ -68,7 +67,7 @@
6867
byteorder = {'little':'<', 'big':'>'}[sys.byteorder]
6968

7069
def normalize_descr(descr):
71-
"Normalize a description adding the addient byteorder."
70+
"Normalize a description adding the platform byteorder."
7271

7372
out = []
7473
for item in descr:
@@ -97,30 +96,39 @@ def normalize_descr(descr):
9796

9897

9998
############################################################
100-
# Creating tests
99+
# Creation tests
101100
############################################################
102101

103102
class create_zeros(ScipyTestCase):
104103
"""Check the creation of heterogeneous arrays zero-valued"""
105104

106-
def check_zerosScalar(self):
107-
"""Check creation of multirow objects"""
105+
def check_zeros0D(self):
106+
"""Check creation of 0-dimensional objects"""
108107
h = zeros((), dtype=self._descr)
109108
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
109+
self.assert_(h.dtype.fields['x'][0].name[:4] == 'void')
110+
self.assert_(h.dtype.fields['x'][0].char == 'V')
111+
self.assert_(h.dtype.fields['x'][0].type == numpy.void)
110112
# A small check that data is ok
111113
assert_equal(h['z'], zeros((), dtype='u1'))
112114

113115
def check_zerosSD(self):
114-
"""Check creation of multirow objects"""
116+
"""Check creation of single-dimensional objects"""
115117
h = zeros((2,), dtype=self._descr)
116118
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
119+
self.assert_(h.dtype['y'].name[:4] == 'void')
120+
self.assert_(h.dtype['y'].char == 'V')
121+
self.assert_(h.dtype['y'].type == numpy.void)
117122
# A small check that data is ok
118123
assert_equal(h['z'], zeros((2,), dtype='u1'))
119124

120125
def check_zerosMD(self):
121-
"""Check creation of multidimensional objects"""
126+
"""Check creation of multi-dimensional objects"""
122127
h = zeros((2,3), dtype=self._descr)
123128
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
129+
self.assert_(h.dtype['z'].name == 'uint8')
130+
self.assert_(h.dtype['z'].char == 'B')
131+
self.assert_(h.dtype['z'].type == numpy.uint8)
124132
# A small check that data is ok
125133
assert_equal(h['z'], zeros((2,3), dtype='u1'))
126134

@@ -140,7 +148,7 @@ class create_values(ScipyTestCase):
140148

141149
def check_tuple(self):
142150
"""Check creation from tuples"""
143-
h = array(self._bufferT, dtype=self._descr)
151+
h = array(self._buffer, dtype=self._descr)
144152
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
145153
if self.multiple_rows:
146154
self.assert_(h.shape == (2,))
@@ -149,7 +157,7 @@ def check_tuple(self):
149157

150158
def check_list_of_tuple(self):
151159
"""Check creation from list of tuples"""
152-
h = array([self._bufferT], dtype=self._descr)
160+
h = array([self._buffer], dtype=self._descr)
153161
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
154162
if self.multiple_rows:
155163
self.assert_(h.shape == (1,2))
@@ -158,7 +166,7 @@ def check_list_of_tuple(self):
158166

159167
def check_list_of_list_of_tuple(self):
160168
"""Check creation from list of list of tuples"""
161-
h = array([[self._bufferT]], dtype=self._descr)
169+
h = array([[self._buffer]], dtype=self._descr)
162170
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
163171
if self.multiple_rows:
164172
self.assert_(h.shape == (1,1,2))
@@ -170,25 +178,25 @@ class test_create_values_plain_single(create_values):
170178
"""Check the creation of heterogeneous arrays (plain, single row)"""
171179
_descr = Pdescr
172180
multiple_rows = 0
173-
_bufferT = PbufferT[0]
181+
_buffer = PbufferT[0]
174182

175183
class test_create_values_plain_multiple(create_values):
176184
"""Check the creation of heterogeneous arrays (plain, multiple rows)"""
177185
_descr = Pdescr
178186
multiple_rows = 1
179-
_bufferT = PbufferT
187+
_buffer = PbufferT
180188

181189
class test_create_values_nested_single(create_values):
182190
"""Check the creation of heterogeneous arrays (nested, single row)"""
183191
_descr = Ndescr
184192
multiple_rows = 0
185-
_bufferT = NbufferT[0]
193+
_buffer = NbufferT[0]
186194

187195
class test_create_values_nested_multiple(create_values):
188196
"""Check the creation of heterogeneous arrays (nested, multiple rows)"""
189197
_descr = Ndescr
190198
multiple_rows = 1
191-
_bufferT = NbufferT
199+
_buffer = NbufferT
192200

193201

194202
############################################################
@@ -198,65 +206,131 @@ class test_create_values_nested_multiple(create_values):
198206
class read_values_plain(ScipyTestCase):
199207
"""Check the reading of values in heterogeneous arrays (plain)"""
200208

201-
def is_correct(self):
202-
if self.multiple_rows:
203-
assert_equal(self.h['x'], array(self._buffer[0][0], dtype='i4'))
204-
assert_equal(self.h['y'], array(self._buffer[0][1], dtype='f8'))
205-
assert_equal(self.h['z'], array(self._buffer[0][2], dtype='u1'))
209+
def check_access_fields(self):
210+
h = array(self._buffer, dtype=self._descr)
211+
if not self.multiple_rows:
212+
self.assert_(h.shape == ())
213+
assert_equal(h['x'], array(self._buffer[0], dtype='i4'))
214+
assert_equal(h['y'], array(self._buffer[1], dtype='f8'))
215+
assert_equal(h['z'], array(self._buffer[2], dtype='u1'))
206216
else:
207-
assert_equal(self.h['x'], array([self._buffer[0][0],
217+
self.assert_(len(h) == 2)
218+
assert_equal(h['x'], array([self._buffer[0][0],
208219
self._buffer[1][0]], dtype='i4'))
209-
assert_equal(self.h['y'], array([self._buffer[0][1],
220+
assert_equal(h['y'], array([self._buffer[0][1],
210221
self._buffer[1][1]], dtype='f8'))
211-
assert_equal(self.h['z'], array([self._buffer[0][2],
222+
assert_equal(h['z'], array([self._buffer[0][2],
212223
self._buffer[1][2]], dtype='u1'))
213224

214225

215-
def check_read_full_tuples(self):
216-
"""Check reading from objects created from tuples"""
217-
self._buffer = self._bufferT
218-
self.h = array(self._buffer, dtype=self._descr)
219-
220-
221226
class test_read_values_plain_single(read_values_plain):
222227
"""Check the creation of heterogeneous arrays (plain, single row)"""
223228
_descr = Pdescr
224229
multiple_rows = 0
225-
_bufferT = PbufferT[0]
230+
_buffer = PbufferT[0]
226231

227232
class test_read_values_plain_multiple(read_values_plain):
228233
"""Check the values of heterogeneous arrays (plain, multiple rows)"""
229234
_descr = Pdescr
230235
multiple_rows = 1
231-
_bufferT = PbufferT
236+
_buffer = PbufferT
232237

233-
class read_values_nested(read_values_plain):
238+
class read_values_nested(ScipyTestCase):
234239
"""Check the reading of values in heterogeneous arrays (nested)"""
235240

236241

237-
# Uncomment this when numpy will eventually support lists as inputs.
238-
def _check_read_full_list(self):
239-
"""Check reading from objects created from list"""
240-
h = array(self._bufferT, dtype=self._descr)
241-
# Add here more code to check this...
242-
243-
def check_read_full_tuple(self):
244-
"""Check reading from objects created from tuples"""
245-
h = array(self._bufferT, dtype=self._descr)
246-
# Add here more code to check this...
247-
248-
# The next test classes are not finished yet...
249-
class _test_read_values_nested_single(read_values_nested):
242+
def check_access_top_fields(self):
243+
"""Check reading the top fields of a nested array"""
244+
h = array(self._buffer, dtype=self._descr)
245+
if not self.multiple_rows:
246+
self.assert_(h.shape == ())
247+
assert_equal(h['x'], array(self._buffer[0], dtype='i4'))
248+
assert_equal(h['y'], array(self._buffer[4], dtype='f8'))
249+
assert_equal(h['z'], array(self._buffer[5], dtype='u1'))
250+
else:
251+
self.assert_(len(h) == 2)
252+
assert_equal(h['x'], array([self._buffer[0][0],
253+
self._buffer[1][0]], dtype='i4'))
254+
assert_equal(h['y'], array([self._buffer[0][4],
255+
self._buffer[1][4]], dtype='f8'))
256+
assert_equal(h['z'], array([self._buffer[0][5],
257+
self._buffer[1][5]], dtype='u1'))
258+
259+
260+
def check_nested1_acessors(self):
261+
"""Check reading the nested fields of a nested array (1st level)"""
262+
h = array(self._buffer, dtype=self._descr)
263+
if not self.multiple_rows:
264+
assert_equal(h['Info']['value'],
265+
array(self._buffer[1][0], dtype='c16'))
266+
assert_equal(h['Info']['y2'],
267+
array(self._buffer[1][1], dtype='f8'))
268+
assert_equal(h['info']['Name'],
269+
array(self._buffer[3][0], dtype='U2'))
270+
assert_equal(h['info']['Value'],
271+
array(self._buffer[3][1], dtype='c16'))
272+
else:
273+
assert_equal(h['Info']['value'],
274+
array([self._buffer[0][1][0],
275+
self._buffer[1][1][0]],
276+
dtype='c16'))
277+
assert_equal(h['Info']['y2'],
278+
array([self._buffer[0][1][1],
279+
self._buffer[1][1][1]],
280+
dtype='f8'))
281+
assert_equal(h['info']['Name'],
282+
array([self._buffer[0][3][0],
283+
self._buffer[1][3][0]],
284+
dtype='U2'))
285+
assert_equal(h['info']['Value'],
286+
array([self._buffer[0][3][1],
287+
self._buffer[1][3][1]],
288+
dtype='c16'))
289+
290+
def check_nested2_acessors(self):
291+
"""Check reading the nested fields of a nested array (2nd level)"""
292+
h = array(self._buffer, dtype=self._descr)
293+
if not self.multiple_rows:
294+
assert_equal(h['Info']['Info2']['value'],
295+
array(self._buffer[1][2][1], dtype='c16'))
296+
assert_equal(h['Info']['Info2']['z3'],
297+
array(self._buffer[1][2][3], dtype='u4'))
298+
else:
299+
assert_equal(h['Info']['Info2']['value'],
300+
array([self._buffer[0][1][2][1],
301+
self._buffer[1][1][2][1]],
302+
dtype='c16'))
303+
assert_equal(h['Info']['Info2']['z3'],
304+
array([self._buffer[0][1][2][3],
305+
self._buffer[1][1][2][3]],
306+
dtype='u4'))
307+
308+
def check_nested1_descriptor(self):
309+
"""Check access nested descriptors of a nested array (1st level)"""
310+
h = array(self._buffer, dtype=self._descr)
311+
self.assert_(h.dtype['Info']['value'].name == 'complex128')
312+
self.assert_(h.dtype['Info']['y2'].name == 'float64')
313+
self.assert_(h.dtype['info']['Name'].name == 'unicode256')
314+
self.assert_(h.dtype['info']['Value'].name == 'complex128')
315+
316+
def check_nested2_descriptor(self):
317+
"""Check access nested descriptors of a nested array (2nd level)"""
318+
h = array(self._buffer, dtype=self._descr)
319+
self.assert_(h.dtype['Info']['Info2']['value'].name == 'void256')
320+
self.assert_(h.dtype['Info']['Info2']['z3'].name == 'void64')
321+
322+
323+
class test_read_values_nested_single(read_values_nested):
250324
"""Check the values of heterogeneous arrays (nested, single row)"""
251325
_descr = Ndescr
252326
multiple_rows = 0
253-
_bufferT = NbufferT[0]
327+
_buffer = NbufferT[0]
254328

255-
class _test_read_values_nested_multiple(read_values_nested):
329+
class test_read_values_nested_multiple(read_values_nested):
256330
"""Check the values of heterogeneous arrays (nested, multiple rows)"""
257331
_descr = Ndescr
258332
multiple_rows = 1
259-
_bufferT = NbufferT
333+
_buffer = NbufferT
260334

261335

262336
if __name__ == "__main__":

0 commit comments

Comments
 (0)