[Numpy-svn] r3674 - trunk/numpy/doc/swig

numpy-svn@scip... numpy-svn@scip...
Fri Apr 6 12:33:52 CDT 2007


Author: wfspotz@sandia.gov
Date: 2007-04-06 12:33:48 -0500 (Fri, 06 Apr 2007)
New Revision: 3674

Modified:
   trunk/numpy/doc/swig/numpy.i
   trunk/numpy/doc/swig/testVector.py
Log:
Added more testing of 1D typemaps, specifically error checking; found and fixed bug where INPLACE_ARRAY1 typemaps didn't check number of dimensions

Modified: trunk/numpy/doc/swig/numpy.i
===================================================================
--- trunk/numpy/doc/swig/numpy.i	2007-04-06 00:11:02 UTC (rev 3673)
+++ trunk/numpy/doc/swig/numpy.i	2007-04-06 17:33:48 UTC (rev 3674)
@@ -640,7 +640,8 @@
   (PyArrayObject* temp=NULL)
 {
   temp = obj_to_array_no_conversion($input, DATA_TYPECODE);
-  if (!temp || !require_contiguous(temp) || !require_native(temp)) SWIG_fail;
+  if (!temp || !require_dimensions(temp,1) || !require_contiguous(temp)
+      || !require_native(temp)) SWIG_fail;
   $1 = (DATA_TYPE*) temp->data;
   $2 = 1;
   for (int i=0; i<temp->nd; ++i) $2 *= array_size(temp,i);
@@ -658,7 +659,8 @@
   (PyArrayObject* temp=NULL)
 {
   temp = obj_to_array_no_conversion($input, DATA_TYPECODE);
-  if (!temp || !require_contiguous(temp) || !require_native(temp)) SWIG_fail;
+  if (!temp || !require_dimensions(temp,1) || !require_contiguous(temp)
+      || !require_native(temp)) SWIG_fail;
   $1 = 1;
   for (int i=0; i<temp->nd; ++i) $1 *= array_size(temp,i);
   $2 = (DATA_TYPE*) temp->data;

Modified: trunk/numpy/doc/swig/testVector.py
===================================================================
--- trunk/numpy/doc/swig/testVector.py	2007-04-06 00:11:02 UTC (rev 3673)
+++ trunk/numpy/doc/swig/testVector.py	2007-04-06 17:33:48 UTC (rev 3674)
@@ -27,36 +27,77 @@
         self.typeStr  = "double"
         self.typeCode = "d"
 
+    # Test the (type IN_ARRAY1[ANY]) typemap
     def testLength(self):
         "Test length function"
         print >>sys.stderr, self.typeStr, "... ",
         length = Vector.__dict__[self.typeStr + "Length"]
         self.assertEquals(length([5, 12, 0]), 13)
 
-    def testLengthBad(self):
-        "Test length function for wrong size"
+    # Test the (type IN_ARRAY1[ANY]) typemap
+    def testLengthWrongSize(self):
+        "Test length function with wrong size"
         print >>sys.stderr, self.typeStr, "... ",
         length = Vector.__dict__[self.typeStr + "Length"]
         self.assertRaises(TypeError, length, [5, 12])
 
+    # Test the (type IN_ARRAY1[ANY]) typemap
+    def testLengthWrongDim(self):
+        "Test length function with wrong dimensions"
+        print >>sys.stderr, self.typeStr, "... ",
+        length = Vector.__dict__[self.typeStr + "Length"]
+        self.assertRaises(TypeError, length, [[1,2], [3,4]])
+
+    # Test the (type IN_ARRAY1[ANY]) typemap
+    def testLengthNonContainer(self):
+        "Test length function with non-container"
+        print >>sys.stderr, self.typeStr, "... ",
+        length = Vector.__dict__[self.typeStr + "Length"]
+        self.assertRaises(TypeError, length, None)
+
+    # Test the (type* IN_ARRAY1, int DIM1) typemap
     def testProd(self):
         "Test prod function"
         print >>sys.stderr, self.typeStr, "... ",
         prod = Vector.__dict__[self.typeStr + "Prod"]
         self.assertEquals(prod([1,2,3,4]), 24)
 
+    # Test the (type* IN_ARRAY1, int DIM1) typemap
+    def testProdWrongDim(self):
+        "Test prod function with wrong dimensions"
+        print >>sys.stderr, self.typeStr, "... ",
+        prod = Vector.__dict__[self.typeStr + "Prod"]
+        self.assertRaises(TypeError, prod, [[1,2], [8,9]])
+
+    # Test the (type* IN_ARRAY1, int DIM1) typemap
     def testProdNonContainer(self):
-        "Test prod function with None"
+        "Test prod function with non-container"
         print >>sys.stderr, self.typeStr, "... ",
         prod = Vector.__dict__[self.typeStr + "Prod"]
         self.assertRaises(TypeError, prod, None)
 
+    # Test the (int DIM1, type* IN_ARRAY1) typemap
     def testSum(self):
         "Test sum function"
         print >>sys.stderr, self.typeStr, "... ",
         sum = Vector.__dict__[self.typeStr + "Sum"]
         self.assertEquals(sum([5,6,7,8]), 26)
 
+    # Test the (int DIM1, type* IN_ARRAY1) typemap
+    def testSumWrongDim(self):
+        "Test sum function with wrong dimensions"
+        print >>sys.stderr, self.typeStr, "... ",
+        sum = Vector.__dict__[self.typeStr + "Sum"]
+        self.assertRaises(TypeError, sum, [[3,4], [5,6]])
+
+    # Test the (int DIM1, type* IN_ARRAY1) typemap
+    def testSumNonContainer(self):
+        "Test sum function with non-container"
+        print >>sys.stderr, self.typeStr, "... ",
+        sum = Vector.__dict__[self.typeStr + "Sum"]
+        self.assertRaises(TypeError, sum, True)
+
+    # Test the (type INPLACE_ARRAY1[ANY]) typemap
     def testReverse(self):
         "Test reverse function"
         print >>sys.stderr, self.typeStr, "... ",
@@ -65,22 +106,102 @@
         reverse(vector)
         self.assertEquals((vector == [4,2,1]).all(), True)
 
+    # Test the (type INPLACE_ARRAY1[ANY]) typemap
+    def testReverseWrongDim(self):
+        "Test reverse function with wrong dimensions"
+        print >>sys.stderr, self.typeStr, "... ",
+        reverse = Vector.__dict__[self.typeStr + "Reverse"]
+        vector = N.array([[1,2], [3,4]],self.typeCode)
+        self.assertRaises(TypeError, reverse, vector)
+
+    # Test the (type INPLACE_ARRAY1[ANY]) typemap
+    def testReverseWrongSize(self):
+        "Test reverse function with wrong size"
+        print >>sys.stderr, self.typeStr, "... ",
+        reverse = Vector.__dict__[self.typeStr + "Reverse"]
+        vector = N.array([9,8,7,6,5,4],self.typeCode)
+        self.assertRaises(TypeError, reverse, vector)
+
+    # Test the (type INPLACE_ARRAY1[ANY]) typemap
+    def testReverseWrongType(self):
+        "Test reverse function with wrong type"
+        print >>sys.stderr, self.typeStr, "... ",
+        reverse = Vector.__dict__[self.typeStr + "Reverse"]
+        vector = N.array([1,2,4],'c')
+        self.assertRaises(TypeError, reverse, vector)
+
+    # Test the (type INPLACE_ARRAY1[ANY]) typemap
+    def testReverseNonArray(self):
+        "Test reverse function with non-array"
+        print >>sys.stderr, self.typeStr, "... ",
+        reverse = Vector.__dict__[self.typeStr + "Reverse"]
+        self.assertRaises(TypeError, reverse, [2,4,6])
+
+    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
     def testOnes(self):
         "Test ones function"
         print >>sys.stderr, self.typeStr, "... ",
         ones = Vector.__dict__[self.typeStr + "Ones"]
-        myArray = N.zeros(5,self.typeCode)
-        ones(myArray)
-        N.testing.assert_array_equal(myArray, N.array([1,1,1,1,1]))
+        vector = N.zeros(5,self.typeCode)
+        ones(vector)
+        N.testing.assert_array_equal(vector, N.array([1,1,1,1,1]))
 
+    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
+    def testOnesWrongDim(self):
+        "Test ones function with wrong dimensions"
+        print >>sys.stderr, self.typeStr, "... ",
+        ones = Vector.__dict__[self.typeStr + "Ones"]
+        vector = N.zeros((5,5),self.typeCode)
+        self.assertRaises(TypeError, ones, vector)
+
+    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
+    def testOnesWrongType(self):
+        "Test ones function with wrong type"
+        print >>sys.stderr, self.typeStr, "... ",
+        ones = Vector.__dict__[self.typeStr + "Ones"]
+        vector = N.zeros((5,5),'c')
+        self.assertRaises(TypeError, ones, vector)
+
+    # Test the (type* INPLACE_ARRAY1, int DIM1) typemap
+    def testOnesNonArray(self):
+        "Test ones function with non-array"
+        print >>sys.stderr, self.typeStr, "... ",
+        ones = Vector.__dict__[self.typeStr + "Ones"]
+        self.assertRaises(TypeError, ones, [2,4,6,8])
+
+    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
     def testZeros(self):
         "Test zeros function"
         print >>sys.stderr, self.typeStr, "... ",
         zeros = Vector.__dict__[self.typeStr + "Zeros"]
-        myArray = N.ones(5,self.typeCode)
-        zeros(myArray)
-        N.testing.assert_array_equal(myArray, N.array([0,0,0,0,0]))
+        vector = N.ones(5,self.typeCode)
+        zeros(vector)
+        N.testing.assert_array_equal(vector, N.array([0,0,0,0,0]))
 
+    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
+    def testZerosWrongDim(self):
+        "Test zeros function with wrong dimensions"
+        print >>sys.stderr, self.typeStr, "... ",
+        zeros = Vector.__dict__[self.typeStr + "Zeros"]
+        vector = N.ones((5,5),self.typeCode)
+        self.assertRaises(TypeError, zeros, vector)
+
+    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
+    def testZerosWrongType(self):
+        "Test zeros function with wrong type"
+        print >>sys.stderr, self.typeStr, "... ",
+        zeros = Vector.__dict__[self.typeStr + "Zeros"]
+        vector = N.ones(6,'c')
+        self.assertRaises(TypeError, zeros, vector)
+
+    # Test the (int DIM1, type* INPLACE_ARRAY1) typemap
+    def testZerosNonArray(self):
+        "Test zeros function with non-array"
+        print >>sys.stderr, self.typeStr, "... ",
+        zeros = Vector.__dict__[self.typeStr + "Zeros"]
+        self.assertRaises(TypeError, zeros, [1,3,5,7,9])
+
+    # Test the (type ARGOUT_ARRAY1[ANY]) typemap
     def testEOSplit(self):
         "Test eoSplit function"
         print >>sys.stderr, self.typeStr, "... ",
@@ -89,6 +210,7 @@
         self.assertEquals((even == [1,0,3]).all(), True)
         self.assertEquals((odd  == [0,2,0]).all(), True)
 
+    # Test the (type* ARGOUT_ARRAY1, int DIM1) typemap
     def testTwos(self):
         "Test twos function"
         print >>sys.stderr, self.typeStr, "... ",
@@ -96,6 +218,14 @@
         vector = twos(5)
         self.assertEquals((vector == [2,2,2,2,2]).all(), True)
 
+    # Test the (type* ARGOUT_ARRAY1, int DIM1) typemap
+    def testTwosNonInt(self):
+        "Test twos function with non-integer dimension"
+        print >>sys.stderr, self.typeStr, "... ",
+        twos = Vector.__dict__[self.typeStr + "Twos"]
+        self.assertRaises(TypeError, twos, 5.0)
+
+    # Test the (int DIM1, type* ARGOUT_ARRAY1) typemap
     def testThrees(self):
         "Test threes function"
         print >>sys.stderr, self.typeStr, "... ",
@@ -103,6 +233,13 @@
         vector = threes(6)
         self.assertEquals((vector == [3,3,3,3,3,3]).all(), True)
 
+    # Test the (type* ARGOUT_ARRAY1, int DIM1) typemap
+    def testThreesNonInt(self):
+        "Test threes function with non-integer dimension"
+        print >>sys.stderr, self.typeStr, "... ",
+        threes = Vector.__dict__[self.typeStr + "Threes"]
+        self.assertRaises(TypeError, threes, "threes")
+
 ######################################################################
 
 class scharTestCase(VectorTestCase):



More information about the Numpy-svn mailing list