Skip to content

Commit

Permalink
more 2D array testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Jul 10, 2021
1 parent 948979e commit 27c8971
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 13 deletions.
44 changes: 40 additions & 4 deletions test/datatypes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,11 @@ static inline T** allocate_2d(size_t N, size_t M) {
}

static inline void free_2d(void** arr, size_t N) {
for (size_t i = 0; i < N; ++i)
free(arr[i]);
free(arr);
if (arr) {
for (size_t i = 0; i < N; ++i)
free(arr[i]);
free(arr);
}
}

} // namespace MultiDimArrays
Expand Down Expand Up @@ -902,4 +904,38 @@ MultiDimArrays::DataHolder::~DataHolder() {
free_2d((void**)m_unsigned_long_long2a, 5);
free_2d((void**)m_float2a, 5);
free_2d((void**)m_double2a, 5);
}

free_2d((void**)m_short2b, 5);
free_2d((void**)m_unsigned_short2b, 5);
free_2d((void**)m_int2b, 5);
free_2d((void**)m_unsigned_int2b, 5);
free_2d((void**)m_long2b, 5);
free_2d((void**)m_unsigned_long2b, 5);
free_2d((void**)m_long_long2b, 5);
free_2d((void**)m_unsigned_long_long2b, 5);
free_2d((void**)m_float2b, 5);
free_2d((void**)m_double2b, 5);
}

#define MULTIDIM_ARRAYS_NEW2D(type, name) \
type** MultiDimArrays::DataHolder::new_##name##2d(int N, int M) { \
type** arr = allocate_2d<type>(N, M); \
for (size_t i = 0; i < N; ++i) { \
for (size_t j = 0; j < M; ++j) { \
size_t val = 7*i+j; \
arr [i][j] = (type)val; \
} \
} \
return arr; \
}

MULTIDIM_ARRAYS_NEW2D(short, short)
MULTIDIM_ARRAYS_NEW2D(unsigned short, ushort)
MULTIDIM_ARRAYS_NEW2D(int, int)
MULTIDIM_ARRAYS_NEW2D(unsigned int, uint)
MULTIDIM_ARRAYS_NEW2D(long, long)
MULTIDIM_ARRAYS_NEW2D(unsigned long, ulong)
MULTIDIM_ARRAYS_NEW2D(long long, llong)
MULTIDIM_ARRAYS_NEW2D(unsigned long long, ullong)
MULTIDIM_ARRAYS_NEW2D(float, float)
MULTIDIM_ARRAYS_NEW2D(double, double)
10 changes: 10 additions & 0 deletions test/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -787,15 +787,25 @@ struct DataHolder {
double** m_double2a;

short** m_short2b;
short** new_short2d(int N, int M);
unsigned short** m_unsigned_short2b;
unsigned short** new_ushort2d(int N, int M);
int** m_int2b;
int** new_int2d(int N, int M);
unsigned int** m_unsigned_int2b;
unsigned int** new_uint2d(int N, int M);
long** m_long2b;
long** new_long2d(int N, int M);
unsigned long** m_unsigned_long2b;
unsigned long** new_ulong2d(int N, int M);
long long** m_long_long2b;
long long** new_llong2d(int N, int M);
unsigned long long** m_unsigned_long_long2b;
unsigned long long** new_ullong2d(int N, int M);
float** m_float2b;
float** new_float2d(int N, int M);
double** m_double2b;
double** new_double2d(int N, int M);

short m_short2c[3][5];
unsigned short m_unsigned_short2c[3][5];
Expand Down
51 changes: 42 additions & 9 deletions test/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,21 @@ def setup_class(cls):
'short', 'unsigned short', 'int', 'unsigned int', 'long', 'unsigned long',
'long long', 'unsigned long long', 'float', 'double'
]
cls.nbt_short_names = [
'short', 'ushort', 'int', 'uint', 'long', 'ulong', 'llong', 'ullong', 'float', 'double'
]
try:
import numpy as np
if IS_WINDOWS == 32:
np_long, np_ulong = np.int32, np.uint32
else:
np_long, np_ulong = np.int64, np.uint64
cls.numpy_builtin_types = [
np.short, np.ushort, np.int32, np.uint32, np_long, np_ulong,
np.longlong, np.ulonglong, np.float32, np.double
]
except ImportError:
pass

def _data_m(self, lbl):
return [('m_'+tp.replace(' ', '_')+lbl, tp) for tp in self.numeric_builtin_types]
Expand Down Expand Up @@ -525,13 +540,31 @@ def test02_assign_2D_arrays(self):
ns = cppyy.gbl.MultiDimArrays
h = ns.DataHolder()

data2a = self._data_m('2a')
for m, tp in data2a:
getattr(h, m).reshape((5, 7))
assert getattr(h, m).shape == (5, 7)

# copy assignment
h.m_int2c = np.ones((3, 5), dtype=np.int32)
for i in range(3):
for j in range(5):
assert h.m_int2c[i][j] == 1
data2c = self._data_m('2c')
for itp, (m, tp) in enumerate(data2c):
setattr(h, m, np.ones((3, 5), dtype=self.numpy_builtin_types[itp]))

arr = getattr(h, m)
for i in range(3):
for j in range(5):
assert arr[i][j] == 1

# size checking for copy assignment
for itp, (m, tp) in enumerate(data2c):
with raises(ValueError):
setattr(h, m, np.ones((5, 5), dtype=self.numpy_builtin_types[itp]))

with raises(ValueError):
setattr(h, m, np.ones((3, 7), dtype=self.numpy_builtin_types[itp]))

# pointer assignment
N, M = 11, 7
data2b = self._data_m('2b')
for itp, (m, tp) in enumerate(data2b):
setattr(h, m, getattr(h, 'new_'+self.nbt_short_names[itp]+'2d')(N, M))

arr = getattr(h, m)
for i in range(N):
for j in range(M):
assert arr[i][j] == 7*i+j

0 comments on commit 27c8971

Please sign in to comment.