diff options
Diffstat (limited to 'doc/swig/test')
-rw-r--r-- | doc/swig/test/Array.i | 5 | ||||
-rw-r--r-- | doc/swig/test/Array2.h | 2 | ||||
-rw-r--r-- | doc/swig/test/SuperTensor.cxx | 144 | ||||
-rw-r--r-- | doc/swig/test/SuperTensor.h | 53 | ||||
-rw-r--r-- | doc/swig/test/SuperTensor.i | 50 | ||||
-rw-r--r-- | doc/swig/test/Tensor.cxx | 80 | ||||
-rw-r--r-- | doc/swig/test/Tensor.h | 16 | ||||
-rw-r--r-- | doc/swig/test/Tensor.i | 8 | ||||
-rwxr-xr-x | doc/swig/test/testArray.py | 12 | ||||
-rw-r--r-- | doc/swig/test/testFortran.py | 21 | ||||
-rw-r--r-- | doc/swig/test/testSuperTensor.py | 388 |
11 files changed, 706 insertions, 73 deletions
diff --git a/doc/swig/test/Array.i b/doc/swig/test/Array.i index d56dd2d1c..6a8605eb6 100644 --- a/doc/swig/test/Array.i +++ b/doc/swig/test/Array.i @@ -50,11 +50,6 @@ {(int nrows, int ncols, long* data )}; %apply (int* DIM1 , int* DIM2 , long** ARGOUTVIEW_ARRAY2) {(int* nrows, int* ncols, long** data )}; -// Note: the %apply for INPLACE_ARRAY2 above gets successfully applied -// to the constructor Array2(int nrows, int ncols, long* data), but -// does not get applied to the method Array2::resize(int nrows, int -// ncols, long* data). I have no idea why. For this reason the test -// for Apply2.resize(numpy.ndarray) in testArray.py is commented out. // Array1 support %include "Array1.h" diff --git a/doc/swig/test/Array2.h b/doc/swig/test/Array2.h index a6e5bfc30..7f8d4ca65 100644 --- a/doc/swig/test/Array2.h +++ b/doc/swig/test/Array2.h @@ -32,7 +32,7 @@ public: int ncols() const; // Resize array - void resize(int ncols, int nrows, long* data=0); + void resize(int nrows, int ncols, long* data=0); // Set item accessor Array1 & operator[](int i); diff --git a/doc/swig/test/SuperTensor.cxx b/doc/swig/test/SuperTensor.cxx new file mode 100644 index 000000000..82e8a4bd5 --- /dev/null +++ b/doc/swig/test/SuperTensor.cxx @@ -0,0 +1,144 @@ +#include <stdlib.h> +#include <math.h> +#include <iostream> +#include "SuperTensor.h" + +// The following macro defines a family of functions that work with 3D +// arrays with the forms +// +// TYPE SNAMENorm( TYPE supertensor[2][2][2][2]); +// TYPE SNAMEMax( TYPE * supertensor, int cubes, int slices, int rows, int cols); +// TYPE SNAMEMin( int cubes, int slices, int rows, int cols, TYPE * supertensor); +// void SNAMEScale( TYPE supertensor[3][3][3][3]); +// void SNAMEFloor( TYPE * array, int cubes, int slices, int rows, int cols, TYPE floor); +// void SNAMECeil( int slices, int cubes, int slices, int rows, int cols, TYPE * array, TYPE ceil); +// void SNAMELUSplit(TYPE in[2][2][2][2], TYPE lower[2][2][2][2], TYPE upper[2][2][2][2]); +// +// for any specified type TYPE (for example: short, unsigned int, long +// long, etc.) with given short name SNAME (for example: short, uint, +// longLong, etc.). The macro is then expanded for the given +// TYPE/SNAME pairs. The resulting functions are for testing numpy +// interfaces, respectively, for: +// +// * 4D input arrays, hard-coded length +// * 4D input arrays +// * 4D input arrays, data last +// * 4D in-place arrays, hard-coded lengths +// * 4D in-place arrays +// * 4D in-place arrays, data last +// * 4D argout arrays, hard-coded length +// +#define TEST_FUNCS(TYPE, SNAME) \ +\ +TYPE SNAME ## Norm(TYPE supertensor[2][2][2][2]) { \ + double result = 0; \ + for (int l=0; l<2; ++l) \ + for (int k=0; k<2; ++k) \ + for (int j=0; j<2; ++j) \ + for (int i=0; i<2; ++i) \ + result += supertensor[l][k][j][i] * supertensor[l][k][j][i]; \ + return (TYPE)sqrt(result/16); \ +} \ +\ +TYPE SNAME ## Max(TYPE * supertensor, int cubes, int slices, int rows, int cols) { \ + int i, j, k, l, index; \ + TYPE result = supertensor[0]; \ + for (l=0; l<cubes; ++l) { \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ + if (supertensor[index] > result) result = supertensor[index]; \ + } \ + } \ + } \ + } \ + return result; \ +} \ +\ +TYPE SNAME ## Min(int cubes, int slices, int rows, int cols, TYPE * supertensor) { \ + int i, j, k, l, index; \ + TYPE result = supertensor[0]; \ + for (l=0; l<cubes; ++l) { \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ + if (supertensor[index] < result) result = supertensor[index]; \ + } \ + } \ + } \ + } \ + return result; \ +} \ +\ +void SNAME ## Scale(TYPE array[3][3][3][3], TYPE val) { \ + for (int l=0; l<3; ++l) \ + for (int k=0; k<3; ++k) \ + for (int j=0; j<3; ++j) \ + for (int i=0; i<3; ++i) \ + array[l][k][j][i] *= val; \ +} \ +\ +void SNAME ## Floor(TYPE * array, int cubes, int slices, int rows, int cols, TYPE floor) { \ + int i, j, k, l, index; \ + for (l=0; l<cubes; ++l) { \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ + if (array[index] < floor) array[index] = floor; \ + } \ + } \ + } \ + } \ +} \ +\ +void SNAME ## Ceil(int cubes, int slices, int rows, int cols, TYPE * array, TYPE ceil) { \ + int i, j, k, l, index; \ + for (l=0; l<cubes; ++l) { \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = l*slices*rows*cols + k*rows*cols + j*cols + i; \ + if (array[index] > ceil) array[index] = ceil; \ + } \ + } \ + } \ + } \ +} \ +\ +void SNAME ## LUSplit(TYPE supertensor[2][2][2][2], TYPE lower[2][2][2][2], \ + TYPE upper[2][2][2][2]) { \ + int sum; \ + for (int l=0; l<2; ++l) { \ + for (int k=0; k<2; ++k) { \ + for (int j=0; j<2; ++j) { \ + for (int i=0; i<2; ++i) { \ + sum = i + j + k + l; \ + if (sum < 2) { \ + lower[l][k][j][i] = supertensor[l][k][j][i]; \ + upper[l][k][j][i] = 0; \ + } else { \ + upper[l][k][j][i] = supertensor[l][k][j][i]; \ + lower[l][k][j][i] = 0; \ + } \ + } \ + } \ + } \ + } \ +} + +TEST_FUNCS(signed char , schar ) +TEST_FUNCS(unsigned char , uchar ) +TEST_FUNCS(short , short ) +TEST_FUNCS(unsigned short , ushort ) +TEST_FUNCS(int , int ) +TEST_FUNCS(unsigned int , uint ) +TEST_FUNCS(long , long ) +TEST_FUNCS(unsigned long , ulong ) +TEST_FUNCS(long long , longLong ) +TEST_FUNCS(unsigned long long, ulongLong) +TEST_FUNCS(float , float ) +TEST_FUNCS(double , double ) + diff --git a/doc/swig/test/SuperTensor.h b/doc/swig/test/SuperTensor.h new file mode 100644 index 000000000..29cc3bbbb --- /dev/null +++ b/doc/swig/test/SuperTensor.h @@ -0,0 +1,53 @@ +#ifndef SUPERTENSOR_H +#define SUPERTENSOR_H + +// The following macro defines the prototypes for a family of +// functions that work with 4D arrays with the forms +// +// TYPE SNAMENorm( TYPE supertensor[2][2][2][2]); +// TYPE SNAMEMax( TYPE * supertensor, int cubes, int slices, int rows, int cols); +// TYPE SNAMEMin( int cubes, int slices, int rows, int cols, TYPE * supertensor); +// void SNAMEScale( TYPE array[3][3][3][3]); +// void SNAMEFloor( TYPE * array, int cubes, int slices, int rows, int cols, TYPE floor); +// void SNAMECeil( int cubes, int slices, int rows, int cols, TYPE * array, TYPE ceil ); +// void SNAMELUSplit(TYPE in[3][3][3][3], TYPE lower[3][3][3][3], TYPE upper[3][3][3][3]); +// +// for any specified type TYPE (for example: short, unsigned int, long +// long, etc.) with given short name SNAME (for example: short, uint, +// longLong, etc.). The macro is then expanded for the given +// TYPE/SNAME pairs. The resulting functions are for testing numpy +// interfaces, respectively, for: +// +// * 4D input arrays, hard-coded lengths +// * 4D input arrays +// * 4D input arrays, data last +// * 4D in-place arrays, hard-coded lengths +// * 4D in-place arrays +// * 4D in-place arrays, data last +// * 4D argout arrays, hard-coded length +// +#define TEST_FUNC_PROTOS(TYPE, SNAME) \ +\ +TYPE SNAME ## Norm( TYPE supertensor[2][2][2][2]); \ +TYPE SNAME ## Max( TYPE * supertensor, int cubes, int slices, int rows, int cols); \ +TYPE SNAME ## Min( int cubes, int slices, int rows, int cols, TYPE * supertensor); \ +void SNAME ## Scale( TYPE array[3][3][3][3], TYPE val); \ +void SNAME ## Floor( TYPE * array, int cubes, int slices, int rows, int cols, TYPE floor); \ +void SNAME ## Ceil( int cubes, int slices, int rows, int cols, TYPE * array, TYPE ceil ); \ +void SNAME ## LUSplit(TYPE supertensor[2][2][2][2], TYPE lower[2][2][2][2], TYPE upper[2][2][2][2]); + +TEST_FUNC_PROTOS(signed char , schar ) +TEST_FUNC_PROTOS(unsigned char , uchar ) +TEST_FUNC_PROTOS(short , short ) +TEST_FUNC_PROTOS(unsigned short , ushort ) +TEST_FUNC_PROTOS(int , int ) +TEST_FUNC_PROTOS(unsigned int , uint ) +TEST_FUNC_PROTOS(long , long ) +TEST_FUNC_PROTOS(unsigned long , ulong ) +TEST_FUNC_PROTOS(long long , longLong ) +TEST_FUNC_PROTOS(unsigned long long, ulongLong) +TEST_FUNC_PROTOS(float , float ) +TEST_FUNC_PROTOS(double , double ) + +#endif + diff --git a/doc/swig/test/SuperTensor.i b/doc/swig/test/SuperTensor.i new file mode 100644 index 000000000..7521b8ec4 --- /dev/null +++ b/doc/swig/test/SuperTensor.i @@ -0,0 +1,50 @@ +// -*- c++ -*- +%module SuperTensor + +%{ +#define SWIG_FILE_WITH_INIT +#include "SuperTensor.h" +%} + +// Get the NumPy typemaps +%include "../numpy.i" + +%init %{ + import_array(); +%} + +%define %apply_numpy_typemaps(TYPE) + +%apply (TYPE IN_ARRAY4[ANY][ANY][ANY][ANY]) {(TYPE supertensor[ANY][ANY][ANY][ANY])}; +%apply (TYPE* IN_ARRAY4, int DIM1, int DIM2, int DIM3, int DIM4) + {(TYPE* supertensor, int cubes, int slices, int rows, int cols)}; +%apply (int DIM1, int DIM2, int DIM3, int DIM4, TYPE* IN_ARRAY4) + {(int cubes, int slices, int rows, int cols, TYPE* supertensor)}; + +%apply (TYPE INPLACE_ARRAY4[ANY][ANY][ANY][ANY]) {(TYPE array[3][3][3][3])}; +%apply (TYPE* INPLACE_ARRAY4, int DIM1, int DIM2, int DIM3, int DIM4) + {(TYPE* array, int cubes, int slices, int rows, int cols)}; +%apply (int DIM1, int DIM2, int DIM3, int DIM4, TYPE* INPLACE_ARRAY4) + {(int cubes, int slices, int rows, int cols, TYPE* array)}; + +%apply (TYPE ARGOUT_ARRAY4[ANY][ANY][ANY][ANY]) {(TYPE lower[2][2][2][2])}; +%apply (TYPE ARGOUT_ARRAY4[ANY][ANY][ANY][ANY]) {(TYPE upper[2][2][2][2])}; + +%enddef /* %apply_numpy_typemaps() macro */ + +%apply_numpy_typemaps(signed char ) +%apply_numpy_typemaps(unsigned char ) +%apply_numpy_typemaps(short ) +%apply_numpy_typemaps(unsigned short ) +%apply_numpy_typemaps(int ) +%apply_numpy_typemaps(unsigned int ) +%apply_numpy_typemaps(long ) +%apply_numpy_typemaps(unsigned long ) +%apply_numpy_typemaps(long long ) +%apply_numpy_typemaps(unsigned long long) +%apply_numpy_typemaps(float ) +%apply_numpy_typemaps(double ) + +// Include the header file to be wrapped +%include "SuperTensor.h" + diff --git a/doc/swig/test/Tensor.cxx b/doc/swig/test/Tensor.cxx index dce595291..4ccefa144 100644 --- a/doc/swig/test/Tensor.cxx +++ b/doc/swig/test/Tensor.cxx @@ -7,11 +7,11 @@ // arrays with the forms // // TYPE SNAMENorm( TYPE tensor[2][2][2]); -// TYPE SNAMEMax( TYPE * tensor, int rows, int cols, int num); -// TYPE SNAMEMin( int rows, int cols, int num, TYPE * tensor); +// TYPE SNAMEMax( TYPE * tensor, int slices, int rows, int cols); +// TYPE SNAMEMin( int slices, int rows, int cols TYPE * tensor); // void SNAMEScale( TYPE tensor[3][3][3]); -// void SNAMEFloor( TYPE * array, int rows, int cols, int num, TYPE floor); -// void SNAMECeil( int rows, int cols, int num, TYPE * array, TYPE ceil); +// void SNAMEFloor( TYPE * array, int slices, int rows, int cols, TYPE floor); +// void SNAMECeil( int slices, int rows, int cols, TYPE * array, TYPE ceil); // void SNAMELUSplit(TYPE in[2][2][2], TYPE lower[2][2][2], TYPE upper[2][2][2]); // // for any specified type TYPE (for example: short, unsigned int, long @@ -35,32 +35,32 @@ TYPE SNAME ## Norm(TYPE tensor[2][2][2]) { \ for (int k=0; k<2; ++k) \ for (int j=0; j<2; ++j) \ for (int i=0; i<2; ++i) \ - result += tensor[i][j][k] * tensor[i][j][k]; \ + result += tensor[k][j][i] * tensor[k][j][i]; \ return (TYPE)sqrt(result/8); \ } \ \ -TYPE SNAME ## Max(TYPE * tensor, int rows, int cols, int num) { \ +TYPE SNAME ## Max(TYPE * tensor, int slices, int rows, int cols) { \ int i, j, k, index; \ TYPE result = tensor[0]; \ - for (k=0; k<num; ++k) { \ - for (j=0; j<cols; ++j) { \ - for (i=0; i<rows; ++i) { \ - index = k*rows*cols + j*rows + i; \ - if (tensor[index] > result) result = tensor[index]; \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = k*rows*cols + j*cols + i; \ + if (tensor[index] > result) result = tensor[index]; \ } \ } \ } \ return result; \ } \ \ -TYPE SNAME ## Min(int rows, int cols, int num, TYPE * tensor) { \ +TYPE SNAME ## Min(int slices, int rows, int cols, TYPE * tensor) { \ int i, j, k, index; \ TYPE result = tensor[0]; \ - for (k=0; k<num; ++k) { \ - for (j=0; j<cols; ++j) { \ - for (i=0; i<rows; ++i) { \ - index = k*rows*cols + j*rows + i; \ - if (tensor[index] < result) result = tensor[index]; \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = k*rows*cols + j*cols + i; \ + if (tensor[index] < result) result = tensor[index]; \ } \ } \ } \ @@ -68,31 +68,31 @@ TYPE SNAME ## Min(int rows, int cols, int num, TYPE * tensor) { \ } \ \ void SNAME ## Scale(TYPE array[3][3][3], TYPE val) { \ - for (int i=0; i<3; ++i) \ + for (int k=0; k<3; ++k) \ for (int j=0; j<3; ++j) \ - for (int k=0; k<3; ++k) \ - array[i][j][k] *= val; \ + for (int i=0; i<3; ++i) \ + array[k][j][i] *= val; \ } \ \ -void SNAME ## Floor(TYPE * array, int rows, int cols, int num, TYPE floor) { \ +void SNAME ## Floor(TYPE * array, int slices, int rows, int cols, TYPE floor) { \ int i, j, k, index; \ - for (k=0; k<num; ++k) { \ - for (j=0; j<cols; ++j) { \ - for (i=0; i<rows; ++i) { \ - index = k*cols*rows + j*rows + i; \ - if (array[index] < floor) array[index] = floor; \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = k*rows*cols + j*cols + i; \ + if (array[index] < floor) array[index] = floor; \ } \ } \ } \ } \ \ -void SNAME ## Ceil(int rows, int cols, int num, TYPE * array, TYPE ceil) { \ +void SNAME ## Ceil(int slices, int rows, int cols, TYPE * array, TYPE ceil) { \ int i, j, k, index; \ - for (k=0; k<num; ++k) { \ - for (j=0; j<cols; ++j) { \ - for (i=0; i<rows; ++i) { \ - index = j*rows + i; \ - if (array[index] > ceil) array[index] = ceil; \ + for (k=0; k<slices; ++k) { \ + for (j=0; j<rows; ++j) { \ + for (i=0; i<cols; ++i) { \ + index = k*rows*cols + j*cols + i; \ + if (array[index] > ceil) array[index] = ceil; \ } \ } \ } \ @@ -104,14 +104,14 @@ void SNAME ## LUSplit(TYPE tensor[2][2][2], TYPE lower[2][2][2], \ for (int k=0; k<2; ++k) { \ for (int j=0; j<2; ++j) { \ for (int i=0; i<2; ++i) { \ - sum = i + j + k; \ - if (sum < 2) { \ - lower[i][j][k] = tensor[i][j][k]; \ - upper[i][j][k] = 0; \ - } else { \ - upper[i][j][k] = tensor[i][j][k]; \ - lower[i][j][k] = 0; \ - } \ + sum = i + j + k; \ + if (sum < 2) { \ + lower[k][j][i] = tensor[k][j][i]; \ + upper[k][j][i] = 0; \ + } else { \ + upper[k][j][i] = tensor[k][j][i]; \ + lower[k][j][i] = 0; \ + } \ } \ } \ } \ diff --git a/doc/swig/test/Tensor.h b/doc/swig/test/Tensor.h index d60eb2d2e..1f483b328 100644 --- a/doc/swig/test/Tensor.h +++ b/doc/swig/test/Tensor.h @@ -5,11 +5,11 @@ // functions that work with 3D arrays with the forms // // TYPE SNAMENorm( TYPE tensor[2][2][2]); -// TYPE SNAMEMax( TYPE * tensor, int rows, int cols, int num); -// TYPE SNAMEMin( int rows, int cols, int num, TYPE * tensor); +// TYPE SNAMEMax( TYPE * tensor, int slices, int rows, int cols); +// TYPE SNAMEMin( int slices, int rows, int cols, TYPE * tensor); // void SNAMEScale( TYPE array[3][3][3]); -// void SNAMEFloor( TYPE * array, int rows, int cols, int num, TYPE floor); -// void SNAMECeil( int rows, int cols, int num, TYPE * array, TYPE ceil ); +// void SNAMEFloor( TYPE * array, int slices, int rows, int cols, TYPE floor); +// void SNAMECeil( int slices, int rows, int cols, TYPE * array, TYPE ceil ); // void SNAMELUSplit(TYPE in[3][3][3], TYPE lower[3][3][3], TYPE upper[3][3][3]); // // for any specified type TYPE (for example: short, unsigned int, long @@ -29,11 +29,11 @@ #define TEST_FUNC_PROTOS(TYPE, SNAME) \ \ TYPE SNAME ## Norm( TYPE tensor[2][2][2]); \ -TYPE SNAME ## Max( TYPE * tensor, int rows, int cols, int num); \ -TYPE SNAME ## Min( int rows, int cols, int num, TYPE * tensor); \ +TYPE SNAME ## Max( TYPE * tensor, int slices, int rows, int cols); \ +TYPE SNAME ## Min( int slices, int rows, int cols, TYPE * tensor); \ void SNAME ## Scale( TYPE array[3][3][3], TYPE val); \ -void SNAME ## Floor( TYPE * array, int rows, int cols, int num, TYPE floor); \ -void SNAME ## Ceil( int rows, int cols, int num, TYPE * array, TYPE ceil ); \ +void SNAME ## Floor( TYPE * array, int slices, int rows, int cols, TYPE floor); \ +void SNAME ## Ceil( int slices, int rows, int cols, TYPE * array, TYPE ceil ); \ void SNAME ## LUSplit(TYPE tensor[2][2][2], TYPE lower[2][2][2], TYPE upper[2][2][2]); TEST_FUNC_PROTOS(signed char , schar ) diff --git a/doc/swig/test/Tensor.i b/doc/swig/test/Tensor.i index a1198dc9e..5bf9da7e2 100644 --- a/doc/swig/test/Tensor.i +++ b/doc/swig/test/Tensor.i @@ -17,15 +17,15 @@ %apply (TYPE IN_ARRAY3[ANY][ANY][ANY]) {(TYPE tensor[ANY][ANY][ANY])}; %apply (TYPE* IN_ARRAY3, int DIM1, int DIM2, int DIM3) - {(TYPE* tensor, int rows, int cols, int num)}; + {(TYPE* tensor, int slices, int rows, int cols)}; %apply (int DIM1, int DIM2, int DIM3, TYPE* IN_ARRAY3) - {(int rows, int cols, int num, TYPE* tensor)}; + {(int slices, int rows, int cols, TYPE* tensor)}; %apply (TYPE INPLACE_ARRAY3[ANY][ANY][ANY]) {(TYPE array[3][3][3])}; %apply (TYPE* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) - {(TYPE* array, int rows, int cols, int num)}; + {(TYPE* array, int slices, int rows, int cols)}; %apply (int DIM1, int DIM2, int DIM3, TYPE* INPLACE_ARRAY3) - {(int rows, int cols, int num, TYPE* array)}; + {(int slices, int rows, int cols, TYPE* array)}; %apply (TYPE ARGOUT_ARRAY3[ANY][ANY][ANY]) {(TYPE lower[2][2][2])}; %apply (TYPE ARGOUT_ARRAY3[ANY][ANY][ANY]) {(TYPE upper[2][2][2])}; diff --git a/doc/swig/test/testArray.py b/doc/swig/test/testArray.py index de744ce37..278a75f7a 100755 --- a/doc/swig/test/testArray.py +++ b/doc/swig/test/testArray.py @@ -69,7 +69,7 @@ class Array1TestCase(unittest.TestCase): "Test Array1 resize method, array" a = np.zeros((2*self.length,), dtype='l') self.array1.resize(a) - self.failUnless(len(self.array1) == len(a)) + self.failUnless(len(self.array1) == a.size) def testResizeBad(self): "Test Array1 resize method, negative length" @@ -177,11 +177,11 @@ class Array2TestCase(unittest.TestCase): self.array2.resize(newRows, newCols) self.failUnless(len(self.array2) == newRows * newCols) - #def testResize1(self): - # "Test Array2 resize method, array" - # a = np.zeros((2*self.nrows, 2*self.ncols), dtype='l') - # self.array2.resize(a) - # self.failUnless(len(self.array2) == len(a)) + def testResize1(self): + "Test Array2 resize method, array" + a = np.zeros((2*self.nrows, 2*self.ncols), dtype='l') + self.array2.resize(a) + self.failUnless(len(self.array2) == a.size) def testResizeBad1(self): "Test Array2 resize method, negative nrows" diff --git a/doc/swig/test/testFortran.py b/doc/swig/test/testFortran.py index ae7415d50..2175ad1bf 100644 --- a/doc/swig/test/testFortran.py +++ b/doc/swig/test/testFortran.py @@ -24,16 +24,19 @@ class FortranTestCase(unittest.TestCase): self.typeStr = "double" self.typeCode = "d" - # Test (type* IN_FARRAY2, int DIM1, int DIM2) typemap - def testSecondElementContiguous(self): - "Test luSplit function with a Fortran-array" - print(self.typeStr, "... ", end=' ', file=sys.stderr) - second = Fortran.__dict__[self.typeStr + "SecondElement"] - matrix = np.arange(9).reshape(3, 3).astype(self.typeCode) - self.assertEquals(second(matrix), 3) + # This test used to work before the update to avoid deprecated code. Now it + # doesn't work. As best I can tell, it never should have worked, so I am + # commenting it out. --WFS + # def testSecondElementContiguous(self): + # "Test Fortran matrix initialized from reshaped default array" + # print >>sys.stderr, self.typeStr, "... ", + # second = Fortran.__dict__[self.typeStr + "SecondElement"] + # matrix = np.arange(9).reshape(3, 3).astype(self.typeCode) + # self.assertEquals(second(matrix), 3) + # Test (type* IN_FARRAY2, int DIM1, int DIM2) typemap def testSecondElementFortran(self): - "Test luSplit function with a Fortran-array" + "Test Fortran matrix initialized from reshaped NumPy fortranarray" print(self.typeStr, "... ", end=' ', file=sys.stderr) second = Fortran.__dict__[self.typeStr + "SecondElement"] matrix = np.asfortranarray(np.arange(9).reshape(3, 3), @@ -41,7 +44,7 @@ class FortranTestCase(unittest.TestCase): self.assertEquals(second(matrix), 3) def testSecondElementObject(self): - "Test luSplit function with a Fortran-array" + "Test Fortran matrix initialized from nested list fortranarray" print(self.typeStr, "... ", end=' ', file=sys.stderr) second = Fortran.__dict__[self.typeStr + "SecondElement"] matrix = np.asfortranarray([[0,1,2],[3,4,5],[6,7,8]], self.typeCode) diff --git a/doc/swig/test/testSuperTensor.py b/doc/swig/test/testSuperTensor.py new file mode 100644 index 000000000..f4ae09e76 --- /dev/null +++ b/doc/swig/test/testSuperTensor.py @@ -0,0 +1,388 @@ +#! /usr/bin/env python +from __future__ import division + +# System imports +from distutils.util import get_platform +from math import sqrt +import os +import sys +import unittest + +# Import NumPy +import numpy as np +major, minor = [ int(d) for d in np.__version__.split(".")[:2] ] +if major == 0: BadListError = TypeError +else: BadListError = ValueError + +import SuperTensor + +###################################################################### + +class SuperTensorTestCase(unittest.TestCase): + + def __init__(self, methodName="runTests"): + unittest.TestCase.__init__(self, methodName) + self.typeStr = "double" + self.typeCode = "d" + + # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap + def testNorm(self): + "Test norm function" + print >>sys.stderr, self.typeStr, "... ", + norm = SuperTensor.__dict__[self.typeStr + "Norm"] + supertensor = np.arange(2*2*2*2,dtype=self.typeCode).reshape((2,2,2,2)) + #Note: cludge to get an answer of the same type as supertensor. + #Answer is simply sqrt(sum(supertensor*supertensor)/16) + answer = np.array([np.sqrt(np.sum(supertensor.astype('d')*supertensor)/16.)],dtype=self.typeCode)[0] + self.assertAlmostEqual(norm(supertensor), answer, 6) + + # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap + def testNormBadList(self): + "Test norm function with bad list" + print >>sys.stderr, self.typeStr, "... ", + norm = SuperTensor.__dict__[self.typeStr + "Norm"] + supertensor = [[[[0,"one"],[2,3]], [[3,"two"],[1,0]]],[[[0,"one"],[2,3]], [[3,"two"],[1,0]]]] + self.assertRaises(BadListError, norm, supertensor) + + # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap + def testNormWrongDim(self): + "Test norm function with wrong dimensions" + print >>sys.stderr, self.typeStr, "... ", + norm = SuperTensor.__dict__[self.typeStr + "Norm"] + supertensor = np.arange(2*2*2,dtype=self.typeCode).reshape((2,2,2)) + self.assertRaises(TypeError, norm, supertensor) + + # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap + def testNormWrongSize(self): + "Test norm function with wrong size" + print >>sys.stderr, self.typeStr, "... ", + norm = SuperTensor.__dict__[self.typeStr + "Norm"] + supertensor = np.arange(3*2*2,dtype=self.typeCode).reshape((3,2,2)) + self.assertRaises(TypeError, norm, supertensor) + + # Test (type IN_ARRAY3[ANY][ANY][ANY]) typemap + def testNormNonContainer(self): + "Test norm function with non-container" + print >>sys.stderr, self.typeStr, "... ", + norm = SuperTensor.__dict__[self.typeStr + "Norm"] + self.assertRaises(TypeError, norm, None) + + # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testMax(self): + "Test max function" + print >>sys.stderr, self.typeStr, "... ", + max = SuperTensor.__dict__[self.typeStr + "Max"] + supertensor = [[[[1,2], [3,4]], [[5,6], [7,8]]],[[[1,2], [3,4]], [[5,6], [7,8]]]] + self.assertEquals(max(supertensor), 8) + + # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testMaxBadList(self): + "Test max function with bad list" + print >>sys.stderr, self.typeStr, "... ", + max = SuperTensor.__dict__[self.typeStr + "Max"] + supertensor = [[[[1,"two"], [3,4]], [[5,"six"], [7,8]]],[[[1,"two"], [3,4]], [[5,"six"], [7,8]]]] + self.assertRaises(BadListError, max, supertensor) + + # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testMaxNonContainer(self): + "Test max function with non-container" + print >>sys.stderr, self.typeStr, "... ", + max = SuperTensor.__dict__[self.typeStr + "Max"] + self.assertRaises(TypeError, max, None) + + # Test (type* IN_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testMaxWrongDim(self): + "Test max function with wrong dimensions" + print >>sys.stderr, self.typeStr, "... ", + max = SuperTensor.__dict__[self.typeStr + "Max"] + self.assertRaises(TypeError, max, [0, -1, 2, -3]) + + # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap + def testMin(self): + "Test min function" + print >>sys.stderr, self.typeStr, "... ", + min = SuperTensor.__dict__[self.typeStr + "Min"] + supertensor = [[[[9,8], [7,6]], [[5,4], [3,2]]],[[[9,8], [7,6]], [[5,4], [3,2]]]] + self.assertEquals(min(supertensor), 2) + + # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap + def testMinBadList(self): + "Test min function with bad list" + print >>sys.stderr, self.typeStr, "... ", + min = SuperTensor.__dict__[self.typeStr + "Min"] + supertensor = [[[["nine",8], [7,6]], [["five",4], [3,2]]],[[["nine",8], [7,6]], [["five",4], [3,2]]]] + self.assertRaises(BadListError, min, supertensor) + + # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap + def testMinNonContainer(self): + "Test min function with non-container" + print >>sys.stderr, self.typeStr, "... ", + min = SuperTensor.__dict__[self.typeStr + "Min"] + self.assertRaises(TypeError, min, True) + + # Test (int DIM1, int DIM2, int DIM3, type* IN_ARRAY3) typemap + def testMinWrongDim(self): + "Test min function with wrong dimensions" + print >>sys.stderr, self.typeStr, "... ", + min = SuperTensor.__dict__[self.typeStr + "Min"] + self.assertRaises(TypeError, min, [[1,3],[5,7]]) + + # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap + def testScale(self): + "Test scale function" + print >>sys.stderr, self.typeStr, "... ", + scale = SuperTensor.__dict__[self.typeStr + "Scale"] + supertensor = np.arange(3*3*3*3,dtype=self.typeCode).reshape((3,3,3,3)) + answer = supertensor.copy()*4 + scale(supertensor,4) + self.assertEquals((supertensor == answer).all(), True) + + # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap + def testScaleWrongType(self): + "Test scale function with wrong type" + print >>sys.stderr, self.typeStr, "... ", + scale = SuperTensor.__dict__[self.typeStr + "Scale"] + supertensor = np.array([[[1,0,1], [0,1,0], [1,0,1]], + [[0,1,0], [1,0,1], [0,1,0]], + [[1,0,1], [0,1,0], [1,0,1]]],'c') + self.assertRaises(TypeError, scale, supertensor) + + # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap + def testScaleWrongDim(self): + "Test scale function with wrong dimensions" + print >>sys.stderr, self.typeStr, "... ", + scale = SuperTensor.__dict__[self.typeStr + "Scale"] + supertensor = np.array([[1,0,1], [0,1,0], [1,0,1], + [0,1,0], [1,0,1], [0,1,0]],self.typeCode) + self.assertRaises(TypeError, scale, supertensor) + + # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap + def testScaleWrongSize(self): + "Test scale function with wrong size" + print >>sys.stderr, self.typeStr, "... ", + scale = SuperTensor.__dict__[self.typeStr + "Scale"] + supertensor = np.array([[[1,0], [0,1], [1,0]], + [[0,1], [1,0], [0,1]], + [[1,0], [0,1], [1,0]]],self.typeCode) + self.assertRaises(TypeError, scale, supertensor) + + # Test (type INPLACE_ARRAY3[ANY][ANY][ANY]) typemap + def testScaleNonArray(self): + "Test scale function with non-array" + print >>sys.stderr, self.typeStr, "... ", + scale = SuperTensor.__dict__[self.typeStr + "Scale"] + self.assertRaises(TypeError, scale, True) + + # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testFloor(self): + "Test floor function" + print >>sys.stderr, self.typeStr, "... ", + supertensor = np.arange(2*2*2*2,dtype=self.typeCode).reshape((2,2,2,2)) + answer = supertensor.copy() + answer[answer < 4] = 4 + + floor = SuperTensor.__dict__[self.typeStr + "Floor"] + floor(supertensor,4) + np.testing.assert_array_equal(supertensor, answer) + + # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testFloorWrongType(self): + "Test floor function with wrong type" + print >>sys.stderr, self.typeStr, "... ", + floor = SuperTensor.__dict__[self.typeStr + "Floor"] + supertensor = np.ones(2*2*2*2,dtype='c').reshape((2,2,2,2)) + self.assertRaises(TypeError, floor, supertensor) + + # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testFloorWrongDim(self): + "Test floor function with wrong type" + print >>sys.stderr, self.typeStr, "... ", + floor = SuperTensor.__dict__[self.typeStr + "Floor"] + supertensor = np.arange(2*2*2,dtype=self.typeCode).reshape((2,2,2)) + self.assertRaises(TypeError, floor, supertensor) + + # Test (type* INPLACE_ARRAY3, int DIM1, int DIM2, int DIM3) typemap + def testFloorNonArray(self): + "Test floor function with non-array" + print >>sys.stderr, self.typeStr, "... ", + floor = SuperTensor.__dict__[self.typeStr + "Floor"] + self.assertRaises(TypeError, floor, object) + + # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap + def testCeil(self): + "Test ceil function" + print >>sys.stderr, self.typeStr, "... ", + supertensor = np.arange(2*2*2*2,dtype=self.typeCode).reshape((2,2,2,2)) + answer = supertensor.copy() + answer[answer > 5] = 5 + ceil = SuperTensor.__dict__[self.typeStr + "Ceil"] + ceil(supertensor,5) + np.testing.assert_array_equal(supertensor, answer) + + # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap + def testCeilWrongType(self): + "Test ceil function with wrong type" + print >>sys.stderr, self.typeStr, "... ", + ceil = SuperTensor.__dict__[self.typeStr + "Ceil"] + supertensor = np.ones(2*2*2*2,'c').reshape((2,2,2,2)) + self.assertRaises(TypeError, ceil, supertensor) + + # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap + def testCeilWrongDim(self): + "Test ceil function with wrong dimensions" + print >>sys.stderr, self.typeStr, "... ", + ceil = SuperTensor.__dict__[self.typeStr + "Ceil"] + supertensor = np.arange(2*2*2,dtype=self.typeCode).reshape((2,2,2)) + self.assertRaises(TypeError, ceil, supertensor) + + # Test (int DIM1, int DIM2, int DIM3, type* INPLACE_ARRAY3) typemap + def testCeilNonArray(self): + "Test ceil function with non-array" + print >>sys.stderr, self.typeStr, "... ", + ceil = SuperTensor.__dict__[self.typeStr + "Ceil"] + supertensor = np.arange(2*2*2*2,dtype=self.typeCode).reshape((2,2,2,2)).tolist() + self.assertRaises(TypeError, ceil, supertensor) + + # Test (type ARGOUT_ARRAY3[ANY][ANY][ANY]) typemap + def testLUSplit(self): + "Test luSplit function" + print >>sys.stderr, self.typeStr, "... ", + luSplit = SuperTensor.__dict__[self.typeStr + "LUSplit"] + supertensor = np.ones(2*2*2*2,dtype=self.typeCode).reshape((2,2,2,2)) + answer_upper = [[[[0, 0], [0, 1]], [[0, 1], [1, 1]]], [[[0, 1], [1, 1]], [[1, 1], [1, 1]]]] + answer_lower = [[[[1, 1], [1, 0]], [[1, 0], [0, 0]]], [[[1, 0], [0, 0]], [[0, 0], [0, 0]]]] + lower, upper = luSplit(supertensor) + self.assertEquals((lower == answer_lower).all(), True) + self.assertEquals((upper == answer_upper).all(), True) + +###################################################################### + +class scharTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "schar" + self.typeCode = "b" + #self.result = int(self.result) + +###################################################################### + +class ucharTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "uchar" + self.typeCode = "B" + #self.result = int(self.result) + +###################################################################### + +class shortTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "short" + self.typeCode = "h" + #self.result = int(self.result) + +###################################################################### + +class ushortTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "ushort" + self.typeCode = "H" + #self.result = int(self.result) + +###################################################################### + +class intTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "int" + self.typeCode = "i" + #self.result = int(self.result) + +###################################################################### + +class uintTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "uint" + self.typeCode = "I" + #self.result = int(self.result) + +###################################################################### + +class longTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "long" + self.typeCode = "l" + #self.result = int(self.result) + +###################################################################### + +class ulongTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "ulong" + self.typeCode = "L" + #self.result = int(self.result) + +###################################################################### + +class longLongTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "longLong" + self.typeCode = "q" + #self.result = int(self.result) + +###################################################################### + +class ulongLongTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "ulongLong" + self.typeCode = "Q" + #self.result = int(self.result) + +###################################################################### + +class floatTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "float" + self.typeCode = "f" + +###################################################################### + +class doubleTestCase(SuperTensorTestCase): + def __init__(self, methodName="runTest"): + SuperTensorTestCase.__init__(self, methodName) + self.typeStr = "double" + self.typeCode = "d" + +###################################################################### + +if __name__ == "__main__": + + # Build the test suite + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite( scharTestCase)) + suite.addTest(unittest.makeSuite( ucharTestCase)) + suite.addTest(unittest.makeSuite( shortTestCase)) + suite.addTest(unittest.makeSuite( ushortTestCase)) + suite.addTest(unittest.makeSuite( intTestCase)) + suite.addTest(unittest.makeSuite( uintTestCase)) + suite.addTest(unittest.makeSuite( longTestCase)) + suite.addTest(unittest.makeSuite( ulongTestCase)) + suite.addTest(unittest.makeSuite( longLongTestCase)) + suite.addTest(unittest.makeSuite(ulongLongTestCase)) + suite.addTest(unittest.makeSuite( floatTestCase)) + suite.addTest(unittest.makeSuite( doubleTestCase)) + + # Execute the test suite + print "Testing 4D Functions of Module SuperTensor" + print "NumPy version", np.__version__ + print + result = unittest.TextTestRunner(verbosity=2).run(suite) + sys.exit(len(result.errors) + len(result.failures)) |