Skip to content

Commit

Permalink
Added unit-tests from Francesc Alted
Browse files Browse the repository at this point in the history
  • Loading branch information
teoliphant committed Feb 14, 2006
1 parent 263df75 commit 6b93ba8
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 65 deletions.
2 changes: 2 additions & 0 deletions THANKS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Eric Firing for bugfixes.
Arnd Baecker for 64-bit testing
David Cooke for many code improvements including the auto-generated C-API
Alexander Belopolsky (sasha) for Masked array bug-fixes and tests and rank-0 array improvements
Francesc Altet for unicode and nested record tests and help with nested records
Tim Hochberg for getting the build working on MSVC
32 changes: 16 additions & 16 deletions numpy/core/include/numpy/arrayobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1466,22 +1466,22 @@ typedef struct {
*/

#define PyArray_GETPTR1(obj, i) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0))

#define PyArray_GETPTR2(obj, i, j) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0) + \
j*PyArray_STRIDE(obj, 1))

#define PyArray_GETPTR3(obj, i, j, k) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0) + \
j*PyArray_STRIDE(obj, 1) + \
k*PyArray_STRIDE(obj, 2)) \

#define PyArray_GETPTR4(obj, i, j, k, l) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0) + \
j*PyArray_STRIDE(obj, 1) + \
k*PyArray_STRIDE(obj, 2) + \
l*PyArray_STRIDE(obj, 3))
i*PyArray_STRIDE(obj, 0))

#define PyArray_GETPTR2(obj, i, j) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0) + \
j*PyArray_STRIDE(obj, 1))

#define PyArray_GETPTR3(obj, i, j, k) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0) + \
j*PyArray_STRIDE(obj, 1) + \
k*PyArray_STRIDE(obj, 2)) \

#define PyArray_GETPTR4(obj, i, j, k, l) (void *)(PyArray_BYTES(obj) + \
i*PyArray_STRIDE(obj, 0) + \
j*PyArray_STRIDE(obj, 1) + \
k*PyArray_STRIDE(obj, 2) + \
l*PyArray_STRIDE(obj, 3))

#define PyArray_DESCR_REPLACE(descr) do { \
PyArray_Descr *_new_; \
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ def check_test_interning(self):
self.failUnless(array([True])[0] is a1)
self.failUnless(array(True)[...] is a1)

# Import tests from unicode
from test_unicode import *

if __name__ == "__main__":
ScipyTest('numpy.core.multiarray').run()
172 changes: 123 additions & 49 deletions numpy/core/tests/test_numerictypes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
from numpy.testing import *
set_package_path()
import numpy
from numpy import zeros, ones, array
restore_path()


# This is the structure of the table used for plain objects:
Expand Down Expand Up @@ -51,7 +50,7 @@
('z2', 'b1')]),
('color', 'S2'),
('info', [
('Name', 'S2'),
('Name', 'U8'), # Try out 'U8' when interpretation of Unicode strings is more clear
('Value', 'c16')]),
('y', 'f8', (2, 2)),
('z', 'u1')]
Expand All @@ -68,7 +67,7 @@
byteorder = {'little':'<', 'big':'>'}[sys.byteorder]

def normalize_descr(descr):
"Normalize a description adding the addient byteorder."
"Normalize a description adding the platform byteorder."

out = []
for item in descr:
Expand Down Expand Up @@ -97,30 +96,39 @@ def normalize_descr(descr):


############################################################
# Creating tests
# Creation tests
############################################################

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

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

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

def check_zerosMD(self):
"""Check creation of multidimensional objects"""
"""Check creation of multi-dimensional objects"""
h = zeros((2,3), dtype=self._descr)
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
self.assert_(h.dtype['z'].name == 'uint8')
self.assert_(h.dtype['z'].char == 'B')
self.assert_(h.dtype['z'].type == numpy.uint8)
# A small check that data is ok
assert_equal(h['z'], zeros((2,3), dtype='u1'))

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

def check_tuple(self):
"""Check creation from tuples"""
h = array(self._bufferT, dtype=self._descr)
h = array(self._buffer, dtype=self._descr)
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
if self.multiple_rows:
self.assert_(h.shape == (2,))
Expand All @@ -149,7 +157,7 @@ def check_tuple(self):

def check_list_of_tuple(self):
"""Check creation from list of tuples"""
h = array([self._bufferT], dtype=self._descr)
h = array([self._buffer], dtype=self._descr)
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
if self.multiple_rows:
self.assert_(h.shape == (1,2))
Expand All @@ -158,7 +166,7 @@ def check_list_of_tuple(self):

def check_list_of_list_of_tuple(self):
"""Check creation from list of list of tuples"""
h = array([[self._bufferT]], dtype=self._descr)
h = array([[self._buffer]], dtype=self._descr)
self.assert_(normalize_descr(self._descr) == h.dtype.descr)
if self.multiple_rows:
self.assert_(h.shape == (1,1,2))
Expand All @@ -170,25 +178,25 @@ class test_create_values_plain_single(create_values):
"""Check the creation of heterogeneous arrays (plain, single row)"""
_descr = Pdescr
multiple_rows = 0
_bufferT = PbufferT[0]
_buffer = PbufferT[0]

class test_create_values_plain_multiple(create_values):
"""Check the creation of heterogeneous arrays (plain, multiple rows)"""
_descr = Pdescr
multiple_rows = 1
_bufferT = PbufferT
_buffer = PbufferT

class test_create_values_nested_single(create_values):
"""Check the creation of heterogeneous arrays (nested, single row)"""
_descr = Ndescr
multiple_rows = 0
_bufferT = NbufferT[0]
_buffer = NbufferT[0]

class test_create_values_nested_multiple(create_values):
"""Check the creation of heterogeneous arrays (nested, multiple rows)"""
_descr = Ndescr
multiple_rows = 1
_bufferT = NbufferT
_buffer = NbufferT


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

def is_correct(self):
if self.multiple_rows:
assert_equal(self.h['x'], array(self._buffer[0][0], dtype='i4'))
assert_equal(self.h['y'], array(self._buffer[0][1], dtype='f8'))
assert_equal(self.h['z'], array(self._buffer[0][2], dtype='u1'))
def check_access_fields(self):
h = array(self._buffer, dtype=self._descr)
if not self.multiple_rows:
self.assert_(h.shape == ())
assert_equal(h['x'], array(self._buffer[0], dtype='i4'))
assert_equal(h['y'], array(self._buffer[1], dtype='f8'))
assert_equal(h['z'], array(self._buffer[2], dtype='u1'))
else:
assert_equal(self.h['x'], array([self._buffer[0][0],
self.assert_(len(h) == 2)
assert_equal(h['x'], array([self._buffer[0][0],
self._buffer[1][0]], dtype='i4'))
assert_equal(self.h['y'], array([self._buffer[0][1],
assert_equal(h['y'], array([self._buffer[0][1],
self._buffer[1][1]], dtype='f8'))
assert_equal(self.h['z'], array([self._buffer[0][2],
assert_equal(h['z'], array([self._buffer[0][2],
self._buffer[1][2]], dtype='u1'))


def check_read_full_tuples(self):
"""Check reading from objects created from tuples"""
self._buffer = self._bufferT
self.h = array(self._buffer, dtype=self._descr)


class test_read_values_plain_single(read_values_plain):
"""Check the creation of heterogeneous arrays (plain, single row)"""
_descr = Pdescr
multiple_rows = 0
_bufferT = PbufferT[0]
_buffer = PbufferT[0]

class test_read_values_plain_multiple(read_values_plain):
"""Check the values of heterogeneous arrays (plain, multiple rows)"""
_descr = Pdescr
multiple_rows = 1
_bufferT = PbufferT
_buffer = PbufferT

class read_values_nested(read_values_plain):
class read_values_nested(ScipyTestCase):
"""Check the reading of values in heterogeneous arrays (nested)"""


# Uncomment this when numpy will eventually support lists as inputs.
def _check_read_full_list(self):
"""Check reading from objects created from list"""
h = array(self._bufferT, dtype=self._descr)
# Add here more code to check this...

def check_read_full_tuple(self):
"""Check reading from objects created from tuples"""
h = array(self._bufferT, dtype=self._descr)
# Add here more code to check this...

# The next test classes are not finished yet...
class _test_read_values_nested_single(read_values_nested):
def check_access_top_fields(self):
"""Check reading the top fields of a nested array"""
h = array(self._buffer, dtype=self._descr)
if not self.multiple_rows:
self.assert_(h.shape == ())
assert_equal(h['x'], array(self._buffer[0], dtype='i4'))
assert_equal(h['y'], array(self._buffer[4], dtype='f8'))
assert_equal(h['z'], array(self._buffer[5], dtype='u1'))
else:
self.assert_(len(h) == 2)
assert_equal(h['x'], array([self._buffer[0][0],
self._buffer[1][0]], dtype='i4'))
assert_equal(h['y'], array([self._buffer[0][4],
self._buffer[1][4]], dtype='f8'))
assert_equal(h['z'], array([self._buffer[0][5],
self._buffer[1][5]], dtype='u1'))


def check_nested1_acessors(self):
"""Check reading the nested fields of a nested array (1st level)"""
h = array(self._buffer, dtype=self._descr)
if not self.multiple_rows:
assert_equal(h['Info']['value'],
array(self._buffer[1][0], dtype='c16'))
assert_equal(h['Info']['y2'],
array(self._buffer[1][1], dtype='f8'))
assert_equal(h['info']['Name'],
array(self._buffer[3][0], dtype='U2'))
assert_equal(h['info']['Value'],
array(self._buffer[3][1], dtype='c16'))
else:
assert_equal(h['Info']['value'],
array([self._buffer[0][1][0],
self._buffer[1][1][0]],
dtype='c16'))
assert_equal(h['Info']['y2'],
array([self._buffer[0][1][1],
self._buffer[1][1][1]],
dtype='f8'))
assert_equal(h['info']['Name'],
array([self._buffer[0][3][0],
self._buffer[1][3][0]],
dtype='U2'))
assert_equal(h['info']['Value'],
array([self._buffer[0][3][1],
self._buffer[1][3][1]],
dtype='c16'))

def check_nested2_acessors(self):
"""Check reading the nested fields of a nested array (2nd level)"""
h = array(self._buffer, dtype=self._descr)
if not self.multiple_rows:
assert_equal(h['Info']['Info2']['value'],
array(self._buffer[1][2][1], dtype='c16'))
assert_equal(h['Info']['Info2']['z3'],
array(self._buffer[1][2][3], dtype='u4'))
else:
assert_equal(h['Info']['Info2']['value'],
array([self._buffer[0][1][2][1],
self._buffer[1][1][2][1]],
dtype='c16'))
assert_equal(h['Info']['Info2']['z3'],
array([self._buffer[0][1][2][3],
self._buffer[1][1][2][3]],
dtype='u4'))

def check_nested1_descriptor(self):
"""Check access nested descriptors of a nested array (1st level)"""
h = array(self._buffer, dtype=self._descr)
self.assert_(h.dtype['Info']['value'].name == 'complex128')
self.assert_(h.dtype['Info']['y2'].name == 'float64')
self.assert_(h.dtype['info']['Name'].name == 'unicode256')
self.assert_(h.dtype['info']['Value'].name == 'complex128')

def check_nested2_descriptor(self):
"""Check access nested descriptors of a nested array (2nd level)"""
h = array(self._buffer, dtype=self._descr)
self.assert_(h.dtype['Info']['Info2']['value'].name == 'void256')
self.assert_(h.dtype['Info']['Info2']['z3'].name == 'void64')


class test_read_values_nested_single(read_values_nested):
"""Check the values of heterogeneous arrays (nested, single row)"""
_descr = Ndescr
multiple_rows = 0
_bufferT = NbufferT[0]
_buffer = NbufferT[0]

class _test_read_values_nested_multiple(read_values_nested):
class test_read_values_nested_multiple(read_values_nested):
"""Check the values of heterogeneous arrays (nested, multiple rows)"""
_descr = Ndescr
multiple_rows = 1
_bufferT = NbufferT
_buffer = NbufferT


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 6b93ba8

Please sign in to comment.