Skip to content

Commit

Permalink
tests for 3D arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
wlav committed Jul 11, 2021
1 parent 915ab3e commit 77c282f
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 49 deletions.
76 changes: 76 additions & 0 deletions test/datatypes.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,28 @@ static inline void free_2d(void** arr, size_t N) {
}
}

template<typename T>
static inline T*** allocate_3d(size_t N, size_t M, size_t K) {
T*** arr = (T***)malloc(sizeof(void*)*N);
for (size_t i = 0; i < N; ++i) {
arr[i] = (T**)malloc(sizeof(void*)*M);
for (size_t j = 0; j < M; ++j)
arr[i][j] = (T*)malloc(sizeof(T)*K);
}
return arr;
}

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

} // namespace MultiDimArrays

MultiDimArrays::DataHolder::DataHolder() {
Expand Down Expand Up @@ -889,6 +911,49 @@ MultiDimArrays::DataHolder::DataHolder() {
m_unsigned_long_long2c[i][j] = (unsigned long long)val;
m_float2c[i][j] = (float)val;
m_double2c[i][j] = (double)val;

for (size_t k = 0; k < 7; ++k) {
val = 3*i+2*j+k;
m_short3c[i][j][k] = (short)val;
m_unsigned_short3c[i][j][k] = (unsigned short)val;
m_int3c[i][j][k] = (int)val;
m_unsigned_int3c[i][j][k] = (unsigned int)val;
m_long3c[i][j][k] = (long)val;
m_unsigned_long3c[i][j][k] = (unsigned long)val;
m_long_long3c[i][j][k] = (long long)val;
m_unsigned_long_long3c[i][j][k] = (unsigned long long)val;
m_float3c[i][j][k] = (float)val;
m_double3c[i][j][k] = (double)val;
}
}
}

m_short3a = allocate_3d<short>(5, 7, 11);
m_unsigned_short3a = allocate_3d<unsigned short>(5, 7, 11);
m_int3a = allocate_3d<int>(5, 7, 11);
m_unsigned_int3a = allocate_3d<unsigned int>(5, 7, 11);
m_long3a = allocate_3d<long>(5, 7, 11);
m_unsigned_long3a = allocate_3d<unsigned long>(5, 7, 11);
m_long_long3a = allocate_3d<long long>(5, 7, 11);
m_unsigned_long_long3a = allocate_3d<unsigned long long>(5, 7, 11);
m_float3a = allocate_3d<float>(5, 7, 11);
m_double3a = allocate_3d<double>(5, 7, 11);

for (size_t i = 0; i < 5; ++i) {
for (size_t j = 0; j < 7; ++j) {
for (size_t k = 0; k < 11; ++k) {
size_t val = 7*i+3*j+k;
m_short3a[i][j][k] = (short)val;
m_unsigned_short3a[i][j][k] = (unsigned short)val;
m_int3a[i][j][k] = (int)val;
m_unsigned_int3a[i][j][k] = (unsigned int)val;
m_long3a[i][j][k] = (long)val;
m_unsigned_long3a[i][j][k] = (unsigned long)val;
m_long_long3a[i][j][k] = (long long)val;
m_unsigned_long_long3a[i][j][k] = (unsigned long long)val;
m_float3a[i][j][k] = (float)val;
m_double3a[i][j][k] = (double)val;
}
}
}
}
Expand All @@ -915,6 +980,17 @@ MultiDimArrays::DataHolder::~DataHolder() {
free_2d((void**)m_unsigned_long_long2b, 5);
free_2d((void**)m_float2b, 5);
free_2d((void**)m_double2b, 5);

free_3d((void***)m_short3a, 5, 7);
free_3d((void***)m_unsigned_short3a, 5, 7);
free_3d((void***)m_int3a, 5, 7);
free_3d((void***)m_unsigned_int3a, 5, 7);
free_3d((void***)m_long3a, 5, 7);
free_3d((void***)m_unsigned_long3a, 5, 7);
free_3d((void***)m_long_long3a, 5, 7);
free_3d((void***)m_unsigned_long_long3a, 5, 7);
free_3d((void***)m_float3a, 5, 7);
free_3d((void***)m_double3a, 5, 7);
}

#define MULTIDIM_ARRAYS_NEW2D(type, name) \
Expand Down
106 changes: 64 additions & 42 deletions test/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -775,48 +775,70 @@ struct DataHolder {
DataHolder();
~DataHolder();

short** m_short2a;
unsigned short** m_unsigned_short2a;
int** m_int2a;
unsigned int** m_unsigned_int2a;
long** m_long2a;
unsigned long** m_unsigned_long2a;
long long** m_long_long2a;
unsigned long long** m_unsigned_long_long2a;
float** m_float2a;
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];
int m_int2c[3][5];
unsigned int m_unsigned_int2c[3][5];
long m_long2c[3][5];
unsigned long m_unsigned_long2c[3][5];
long long m_long_long2c[3][5];
unsigned long long m_unsigned_long_long2c[3][5];
float m_float2c[3][5];
double m_double2c[3][5];
short** m_short2a;
unsigned short** m_unsigned_short2a;
int** m_int2a;
unsigned int** m_unsigned_int2a;
long** m_long2a;
unsigned long** m_unsigned_long2a;
long long** m_long_long2a;
unsigned long long** m_unsigned_long_long2a;
float** m_float2a;
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];
int m_int2c[3][5];
unsigned int m_unsigned_int2c[3][5];
long m_long2c[3][5];
unsigned long m_unsigned_long2c[3][5];
long long m_long_long2c[3][5];
unsigned long long m_unsigned_long_long2c[3][5];
float m_float2c[3][5];
double m_double2c[3][5];

short*** m_short3a;
unsigned short*** m_unsigned_short3a;
int*** m_int3a;
unsigned int*** m_unsigned_int3a;
long*** m_long3a;
unsigned long*** m_unsigned_long3a;
long long*** m_long_long3a;
unsigned long long*** m_unsigned_long_long3a;
float*** m_float3a;
double*** m_double3a;

short m_short3c[3][5][7];
unsigned short m_unsigned_short3c[3][5][7];
int m_int3c[3][5][7];
unsigned int m_unsigned_int3c[3][5][7];
long m_long3c[3][5][7];
unsigned long m_unsigned_long3c[3][5][7];
long long m_long_long3c[3][5][7];
unsigned long long m_unsigned_long_long3c[3][5][7];
float m_float3c[3][5][7];
double m_double3c[3][5][7];
};

} // namespace MultiDimArrays
Expand Down
79 changes: 72 additions & 7 deletions test/test_lowlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,36 @@ def test01_2D_arrays(self):
data2a = self._data_m('2a')
for m, tp in data2a:
getattr(h, m).reshape((5, 7))
assert getattr(h, m).shape == (5, 7)

arr = getattr(h, m)
assert arr.shape == (5, 7)
elem_tp = getattr(cppyy.gbl, tp)
for i in range(5):
for j in range(7):
assert arr[i][j] == elem_tp(5*i+j)
val = elem_tp(5*i+j)
assert arr[i][j] == val
assert arr[i, j] == val

for i in range(5):
for j in range(7):
arr[i][j] = elem_tp(4+5*i+j)

for i in range(5):
for j in range(7):
assert arr[i][j] == elem_tp(4+5*i+j)
val = elem_tp(4+5*i+j)
assert arr[i][j] == val
assert arr[i, j] == val

data2c = self._data_m('2c')
for m, tp in data2c:
arr = getattr(h, m)
assert arr.shape == (3, 5)
elem_tp = getattr(cppyy.gbl, tp)
for i in range(3):
for j in range(5):
val = elem_tp(3*i+j)
assert arr[i][j] == val
assert arr[i, j] == val

def test02_assign_2D_arrays(self):
"""Direct assignment of 2D arrays"""
Expand All @@ -546,10 +561,12 @@ def test02_assign_2D_arrays(self):
setattr(h, m, np.ones((3, 5), dtype=self.numpy_builtin_types[itp]))

arr = getattr(h, m)
assert arr.shape == (3, 5)
val = getattr(cppyy.gbl, tp)(1)
for i in range(3):
for j in range(5):
assert arr[i][j] == 1
assert arr[i, j] == 1
assert arr[i][j] == val
assert arr[i, j] == val

# size checking for copy assignment
for itp, (m, tp) in enumerate(data2c):
Expand All @@ -566,11 +583,59 @@ def test02_assign_2D_arrays(self):
setattr(h, m, getattr(h, 'new_'+self.nbt_short_names[itp]+'2d')(N, M))

arr = getattr(h, m)
elem_tp = getattr(cppyy.gbl, tp)
for i in range(N):
for j in range(M):
assert arr[i][j] == 7*i+j
assert arr[i, j] == 7*i+j
val = elem_tp(7*i+j)
assert arr[i][j] == val
assert arr[i, j] == val

assert arr[2][3] != 10
arr[2][3] = 10
assert arr[2][3] == 10

def test03_3D_arrays(self):
"""Access and use of 3D data members"""

import cppyy

ns = cppyy.gbl.MultiDimArrays
h = ns.DataHolder()

data3a = self._data_m('3a')
for m, tp in data3a:
getattr(h, m).reshape((5, 7, 11))

arr = getattr(h, m)
assert arr.shape == (5, 7, 11)
elem_tp = getattr(cppyy.gbl, tp)
for i in range(5):
for j in range(7):
for k in range(11):
val = elem_tp(7*i+3*j+k)
assert arr[i][j][k] == val
assert arr[i, j, k] == val

for i in range(5):
for j in range(7):
for k in range(11):
arr[i][j][k] = elem_tp(4+7*i+3*j+k)

for i in range(5):
for j in range(7):
for k in range(11):
val = elem_tp(4+7*i+3*j+k)
assert arr[i][j][k] == val
assert arr[i, j, k] == val

data3c = self._data_m('3c')
for m, tp in data3c:
arr = getattr(h, m)
assert arr.shape == (3, 5, 7)
elem_tp = getattr(cppyy.gbl, tp)
for i in range(3):
for j in range(5):
for k in range(7):
val = elem_tp(3*i+2*j+k)
assert arr[i][j][k] == val
assert arr[i, j, k] == val

0 comments on commit 77c282f

Please sign in to comment.