Skip to content

Commit

Permalink
Merge pull request numpy#15752 from mlamarre/fix_swig_tests
Browse files Browse the repository at this point in the history
MAINT: Fix swig tests issue numpy#15743
  • Loading branch information
charris authored Mar 13, 2020
2 parents 33351f6 + 8aad05e commit 90133e1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 53 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,17 @@ numpy/random/legacy/*.c
numpy/random/_mtrand/randint_helpers.pxi
numpy/random/bounded_integers.pyx
numpy/random/bounded_integers.pxd
tools/swig/test/Array_wrap.cxx
tools/swig/test/Farray_wrap.cxx
tools/swig/test/Farray.py
tools/swig/test/Flat_wrap.cxx
tools/swig/test/Flat.py
tools/swig/test/Fortran_wrap.cxx
tools/swig/test/Fortran.py
tools/swig/test/Matrix_wrap.cxx
tools/swig/test/Matrix.py
tools/swig/test/Tensor_wrap.cxx
tools/swig/test/Tensor.py
tools/swig/test/Vector.py
tools/swig/test/Vector_wrap.cxx
tools/swig/test/Array.py
5 changes: 5 additions & 0 deletions tools/swig/test/Array2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ void Array2::resize(int nrows, int ncols, long* data)
}
}

void Array2::resize(int nrows, int ncols)
{
resize(nrows, ncols, nullptr);
}

// Set item accessor
Array1 & Array2::operator[](int i)
{
Expand Down
7 changes: 4 additions & 3 deletions tools/swig/test/Array2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ class Array2
int nrows() const;
int ncols() const;

// Resize array
void resize(int nrows, int ncols, long* data=0);

// Resize array
void resize(int nrows, int ncols, long* data);
void resize(int nrows, int ncols);

// Set item accessor
Array1 & operator[](int i);

Expand Down
92 changes: 46 additions & 46 deletions tools/swig/test/testArray.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@ def setUp(self):
def testConstructor0(self):
"Test Array1 default constructor"
a = Array.Array1()
self.failUnless(isinstance(a, Array.Array1))
self.failUnless(len(a) == 0)
self.assertTrue(isinstance(a, Array.Array1))
self.assertTrue(len(a) == 0)

def testConstructor1(self):
"Test Array1 length constructor"
self.failUnless(isinstance(self.array1, Array.Array1))
self.assertTrue(isinstance(self.array1, Array.Array1))

def testConstructor2(self):
"Test Array1 array constructor"
na = np.arange(self.length)
aa = Array.Array1(na)
self.failUnless(isinstance(aa, Array.Array1))
self.assertTrue(isinstance(aa, Array.Array1))

def testConstructor3(self):
"Test Array1 copy constructor"
for i in range(self.array1.length()): self.array1[i] = i
arrayCopy = Array.Array1(self.array1)
self.failUnless(arrayCopy == self.array1)
self.assertTrue(arrayCopy == self.array1)

def testConstructorBad(self):
"Test Array1 length constructor, negative"
self.assertRaises(ValueError, Array.Array1, -4)

def testLength(self):
"Test Array1 length method"
self.failUnless(self.array1.length() == self.length)
self.assertTrue(self.array1.length() == self.length)

def testLen(self):
"Test Array1 __len__ method"
self.failUnless(len(self.array1) == self.length)
self.assertTrue(len(self.array1) == self.length)

def testResize0(self):
"Test Array1 resize method, length"
newLen = 2 * self.length
self.array1.resize(newLen)
self.failUnless(len(self.array1) == newLen)
self.assertTrue(len(self.array1) == newLen)

def testResize1(self):
"Test Array1 resize method, array"
a = np.zeros((2*self.length,), dtype='l')
self.array1.resize(a)
self.failUnless(len(self.array1) == a.size)
self.assertTrue(len(self.array1) == a.size)

def testResizeBad(self):
"Test Array1 resize method, negative length"
Expand All @@ -77,7 +77,7 @@ def testSetGet(self):
for i in range(n):
self.array1[i] = i*i
for i in range(n):
self.failUnless(self.array1[i] == i*i)
self.assertTrue(self.array1[i] == i*i)

def testSetBad1(self):
"Test Array1 __setitem__ method, negative index"
Expand All @@ -98,20 +98,20 @@ def testGetBad2(self):
def testAsString(self):
"Test Array1 asString method"
for i in range(self.array1.length()): self.array1[i] = i+1
self.failUnless(self.array1.asString() == "[ 1, 2, 3, 4, 5 ]")
self.assertTrue(self.array1.asString() == "[ 1, 2, 3, 4, 5 ]")

def testStr(self):
"Test Array1 __str__ method"
for i in range(self.array1.length()): self.array1[i] = i-2
self.failUnless(str(self.array1) == "[ -2, -1, 0, 1, 2 ]")
self.assertTrue(str(self.array1) == "[ -2, -1, 0, 1, 2 ]")

def testView(self):
"Test Array1 view method"
for i in range(self.array1.length()): self.array1[i] = i+1
a = self.array1.view()
self.failUnless(isinstance(a, np.ndarray))
self.failUnless(len(a) == self.length)
self.failUnless((a == [1, 2, 3, 4, 5]).all())
self.assertTrue(isinstance(a, np.ndarray))
self.assertTrue(len(a) == self.length)
self.assertTrue((a == [1, 2, 3, 4, 5]).all())

######################################################################

Expand All @@ -125,26 +125,26 @@ def setUp(self):
def testConstructor0(self):
"Test Array2 default constructor"
a = Array.Array2()
self.failUnless(isinstance(a, Array.Array2))
self.failUnless(len(a) == 0)
self.assertTrue(isinstance(a, Array.Array2))
self.assertTrue(len(a) == 0)

def testConstructor1(self):
"Test Array2 nrows, ncols constructor"
self.failUnless(isinstance(self.array2, Array.Array2))
self.assertTrue(isinstance(self.array2, Array.Array2))

def testConstructor2(self):
"Test Array2 array constructor"
na = np.zeros((3, 4), dtype="l")
aa = Array.Array2(na)
self.failUnless(isinstance(aa, Array.Array2))
self.assertTrue(isinstance(aa, Array.Array2))

def testConstructor3(self):
"Test Array2 copy constructor"
for i in range(self.nrows):
for j in range(self.ncols):
self.array2[i][j] = i * j
arrayCopy = Array.Array2(self.array2)
self.failUnless(arrayCopy == self.array2)
self.assertTrue(arrayCopy == self.array2)

def testConstructorBad1(self):
"Test Array2 nrows, ncols constructor, negative nrows"
Expand All @@ -156,28 +156,28 @@ def testConstructorBad2(self):

def testNrows(self):
"Test Array2 nrows method"
self.failUnless(self.array2.nrows() == self.nrows)
self.assertTrue(self.array2.nrows() == self.nrows)

def testNcols(self):
"Test Array2 ncols method"
self.failUnless(self.array2.ncols() == self.ncols)
self.assertTrue(self.array2.ncols() == self.ncols)

def testLen(self):
"Test Array2 __len__ method"
self.failUnless(len(self.array2) == self.nrows*self.ncols)
self.assertTrue(len(self.array2) == self.nrows*self.ncols)

def testResize0(self):
"Test Array2 resize method, size"
newRows = 2 * self.nrows
newCols = 2 * self.ncols
self.array2.resize(newRows, newCols)
self.failUnless(len(self.array2) == newRows * newCols)
self.assertTrue(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) == a.size)
self.assertTrue(len(self.array2) == a.size)

def testResizeBad1(self):
"Test Array2 resize method, negative nrows"
Expand All @@ -198,7 +198,7 @@ def testSetGet1(self):
for i in range(m):
self.array2[i] = array1[i]
for i in range(m):
self.failUnless(self.array2[i] == array1[i])
self.assertTrue(self.array2[i] == array1[i])

def testSetGet2(self):
"Test Array2 chained __setitem__, __getitem__ methods"
Expand All @@ -209,7 +209,7 @@ def testSetGet2(self):
self.array2[i][j] = i*j
for i in range(m):
for j in range(n):
self.failUnless(self.array2[i][j] == i*j)
self.assertTrue(self.array2[i][j] == i*j)

def testSetBad1(self):
"Test Array2 __setitem__ method, negative index"
Expand Down Expand Up @@ -241,7 +241,7 @@ def testAsString(self):
for i in range(self.nrows):
for j in range(self.ncols):
self.array2[i][j] = i+j
self.failUnless(self.array2.asString() == result)
self.assertTrue(self.array2.asString() == result)

def testStr(self):
"Test Array2 __str__ method"
Expand All @@ -255,13 +255,13 @@ def testStr(self):
for i in range(self.nrows):
for j in range(self.ncols):
self.array2[i][j] = i-j
self.failUnless(str(self.array2) == result)
self.assertTrue(str(self.array2) == result)

def testView(self):
"Test Array2 view method"
a = self.array2.view()
self.failUnless(isinstance(a, np.ndarray))
self.failUnless(len(a) == self.nrows)
self.assertTrue(isinstance(a, np.ndarray))
self.assertTrue(len(a) == self.nrows)

######################################################################

Expand All @@ -274,48 +274,48 @@ def setUp(self):
def testConstructor0(self):
"Test ArrayZ default constructor"
a = Array.ArrayZ()
self.failUnless(isinstance(a, Array.ArrayZ))
self.failUnless(len(a) == 0)
self.assertTrue(isinstance(a, Array.ArrayZ))
self.assertTrue(len(a) == 0)

def testConstructor1(self):
"Test ArrayZ length constructor"
self.failUnless(isinstance(self.array3, Array.ArrayZ))
self.assertTrue(isinstance(self.array3, Array.ArrayZ))

def testConstructor2(self):
"Test ArrayZ array constructor"
na = np.arange(self.length, dtype=np.complex128)
aa = Array.ArrayZ(na)
self.failUnless(isinstance(aa, Array.ArrayZ))
self.assertTrue(isinstance(aa, Array.ArrayZ))

def testConstructor3(self):
"Test ArrayZ copy constructor"
for i in range(self.array3.length()): self.array3[i] = complex(i,-i)
arrayCopy = Array.ArrayZ(self.array3)
self.failUnless(arrayCopy == self.array3)
self.assertTrue(arrayCopy == self.array3)

def testConstructorBad(self):
"Test ArrayZ length constructor, negative"
self.assertRaises(ValueError, Array.ArrayZ, -4)

def testLength(self):
"Test ArrayZ length method"
self.failUnless(self.array3.length() == self.length)
self.assertTrue(self.array3.length() == self.length)

def testLen(self):
"Test ArrayZ __len__ method"
self.failUnless(len(self.array3) == self.length)
self.assertTrue(len(self.array3) == self.length)

def testResize0(self):
"Test ArrayZ resize method, length"
newLen = 2 * self.length
self.array3.resize(newLen)
self.failUnless(len(self.array3) == newLen)
self.assertTrue(len(self.array3) == newLen)

def testResize1(self):
"Test ArrayZ resize method, array"
a = np.zeros((2*self.length,), dtype=np.complex128)
self.array3.resize(a)
self.failUnless(len(self.array3) == a.size)
self.assertTrue(len(self.array3) == a.size)

def testResizeBad(self):
"Test ArrayZ resize method, negative length"
Expand All @@ -327,7 +327,7 @@ def testSetGet(self):
for i in range(n):
self.array3[i] = i*i
for i in range(n):
self.failUnless(self.array3[i] == i*i)
self.assertTrue(self.array3[i] == i*i)

def testSetBad1(self):
"Test ArrayZ __setitem__ method, negative index"
Expand All @@ -348,20 +348,20 @@ def testGetBad2(self):
def testAsString(self):
"Test ArrayZ asString method"
for i in range(self.array3.length()): self.array3[i] = complex(i+1,-i-1)
self.failUnless(self.array3.asString() == "[ (1,-1), (2,-2), (3,-3), (4,-4), (5,-5) ]")
self.assertTrue(self.array3.asString() == "[ (1,-1), (2,-2), (3,-3), (4,-4), (5,-5) ]")

def testStr(self):
"Test ArrayZ __str__ method"
for i in range(self.array3.length()): self.array3[i] = complex(i-2,(i-2)*2)
self.failUnless(str(self.array3) == "[ (-2,-4), (-1,-2), (0,0), (1,2), (2,4) ]")
self.assertTrue(str(self.array3) == "[ (-2,-4), (-1,-2), (0,0), (1,2), (2,4) ]")

def testView(self):
"Test ArrayZ view method"
for i in range(self.array3.length()): self.array3[i] = complex(i+1,i+2)
a = self.array3.view()
self.failUnless(isinstance(a, np.ndarray))
self.failUnless(len(a) == self.length)
self.failUnless((a == [1+2j, 2+3j, 3+4j, 4+5j, 5+6j]).all())
self.assertTrue(isinstance(a, np.ndarray))
self.assertTrue(len(a) == self.length)
self.assertTrue((a == [1+2j, 2+3j, 3+4j, 4+5j, 5+6j]).all())

######################################################################

Expand Down
8 changes: 4 additions & 4 deletions tools/swig/test/testFlat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def testProcess1D(self):
"Test Process function 1D array"
print(self.typeStr, "... ", end=' ', file=sys.stderr)
process = Flat.__dict__[self.typeStr + "Process"]
pack_output = ''
pack_output = b''
for i in range(10):
pack_output += struct.pack(self.typeCode,i)
x = np.frombuffer(pack_output, dtype=self.typeCode)
Expand All @@ -39,7 +39,7 @@ def testProcess3D(self):
"Test Process function 3D array"
print(self.typeStr, "... ", end=' ', file=sys.stderr)
process = Flat.__dict__[self.typeStr + "Process"]
pack_output = ''
pack_output = b''
for i in range(24):
pack_output += struct.pack(self.typeCode,i)
x = np.frombuffer(pack_output, dtype=self.typeCode)
Expand All @@ -52,7 +52,7 @@ def testProcess3DTranspose(self):
"Test Process function 3D array, FORTRAN order"
print(self.typeStr, "... ", end=' ', file=sys.stderr)
process = Flat.__dict__[self.typeStr + "Process"]
pack_output = ''
pack_output = b''
for i in range(24):
pack_output += struct.pack(self.typeCode,i)
x = np.frombuffer(pack_output, dtype=self.typeCode)
Expand All @@ -65,7 +65,7 @@ def testProcessNoncontiguous(self):
"Test Process function with non-contiguous array, which should raise an error"
print(self.typeStr, "... ", end=' ', file=sys.stderr)
process = Flat.__dict__[self.typeStr + "Process"]
pack_output = ''
pack_output = b''
for i in range(24):
pack_output += struct.pack(self.typeCode,i)
x = np.frombuffer(pack_output, dtype=self.typeCode)
Expand Down

0 comments on commit 90133e1

Please sign in to comment.