]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
*** empty log message ***
authorageay <ageay>
Thu, 16 Dec 2010 09:19:53 +0000 (09:19 +0000)
committerageay <ageay>
Thu, 16 Dec 2010 09:19:53 +0000 (09:19 +0000)
src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx
src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx
src/MEDCoupling_Swig/MEDCoupling.i
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingTypemaps.i

index 076a5956566859a902c85284476a13c0484085d8..54bcaa886fea061b572e98e4bb2ab4e908be8ff8 100644 (file)
@@ -174,6 +174,8 @@ namespace ParaMEDMEM
     CPPUNIT_TEST( testBuildOrthogonalField2 );
     CPPUNIT_TEST( testUMInsertNextCell1 );
     CPPUNIT_TEST( testFieldOperatorDivDiffComp1 );
+    CPPUNIT_TEST( testDARearrange1 );
+    CPPUNIT_TEST( testGetDifferentValues1 );
     //MEDCouplingBasicsTestInterp.cxx
     CPPUNIT_TEST( test2DInterpP0P0_1 );
     CPPUNIT_TEST( test2DInterpP0P0PL_1 );
@@ -376,6 +378,8 @@ namespace ParaMEDMEM
     void testBuildOrthogonalField2();
     void testUMInsertNextCell1();
     void testFieldOperatorDivDiffComp1();
+    void testDARearrange1();
+    void testGetDifferentValues1();
     //MEDCouplingBasicsTestInterp.cxx
     void test2DInterpP0P0_1();
     void test2DInterpP0P0PL_1();
index 1b25714accae8cba5a96dc018bac19b17cb482df..6bbc8b2091eb042f730fc3448a3c5a08f8eb602c 100644 (file)
@@ -4075,3 +4075,104 @@ void MEDCouplingBasicsTest::testFieldOperatorDivDiffComp1()
   d4->decrRef();
   m->decrRef();
 }
+
+void MEDCouplingBasicsTest::testDARearrange1()
+{
+  DataArrayInt *da1=DataArrayInt::New();
+  da1->alloc(12,1);
+  da1->iota(0);
+  const int *ptr=da1->getConstPointer();
+  //
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(1,da1->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNumberOfTuples());
+  da1->rearrange(4);
+  CPPUNIT_ASSERT(ptr==da1->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(4,da1->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(3,da1->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_EQUAL(i,da1->getIJ(0,i));
+  //
+  da1->rearrange(6);
+  CPPUNIT_ASSERT(ptr==da1->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(6,da1->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(2,da1->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_EQUAL(i,da1->getIJ(0,i));
+  //
+  CPPUNIT_ASSERT_THROW(da1->rearrange(7),INTERP_KERNEL::Exception);
+  //
+  da1->rearrange(12);
+  CPPUNIT_ASSERT(ptr==da1->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(1,da1->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_EQUAL(i,da1->getIJ(0,i));
+  //
+  da1->rearrange(3);
+  CPPUNIT_ASSERT(ptr==da1->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da1->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(3,da1->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(4,da1->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_EQUAL(i,da1->getIJ(0,i));
+  //double
+  DataArrayDouble *da2=da1->convertToDblArr();
+  da1->decrRef();
+  const double *ptr2=da2->getConstPointer();
+  //
+  CPPUNIT_ASSERT_EQUAL(12,da2->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(3,da2->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(4,da2->getNumberOfTuples());
+  da2->rearrange(4);
+  CPPUNIT_ASSERT(ptr2==da2->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da2->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(4,da2->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(3,da2->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL((double)i,da2->getIJ(0,i),1e-14);
+  //
+  da2->rearrange(6);
+  CPPUNIT_ASSERT(ptr2==da2->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da2->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(6,da2->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(2,da2->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL((double)i,da2->getIJ(0,i),1e-14);
+  //
+  CPPUNIT_ASSERT_THROW(da2->rearrange(7),INTERP_KERNEL::Exception);
+  //
+  da2->rearrange(1);
+  CPPUNIT_ASSERT(ptr2==da2->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da2->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(1,da2->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(12,da2->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL((double)i,da2->getIJ(0,i),1e-14);
+  //
+  da2->rearrange(3);
+  CPPUNIT_ASSERT(ptr2==da2->getConstPointer());
+  CPPUNIT_ASSERT_EQUAL(12,da2->getNbOfElems());
+  CPPUNIT_ASSERT_EQUAL(3,da2->getNumberOfComponents());
+  CPPUNIT_ASSERT_EQUAL(4,da2->getNumberOfTuples());
+  for(int i=0;i<12;i++)
+    CPPUNIT_ASSERT_DOUBLES_EQUAL((double)i,da2->getIJ(0,i),1e-14);
+  da2->decrRef();
+}
+
+void MEDCouplingBasicsTest::testGetDifferentValues1()
+{
+  DataArrayInt *da1=DataArrayInt::New();
+  const int arr[12]={1,2,3,2,2,3,5,1,5,5,2,2};
+  da1->alloc(4,3);
+  std::copy(arr,arr+12,da1->getPointer());
+  std::set<int> s=da1->getDifferentValues();
+  const int expected1[4]={1,2,3,5};
+  CPPUNIT_ASSERT_EQUAL(4,(int)s.size());
+  CPPUNIT_ASSERT(std::equal(expected1,expected1+4,s.begin()));
+  da1->decrRef();
+}
+
index 32dd28f1ae4c38399d2371810dc30bee756efdc2..57fd23951205fa8a927e9e83dbda0ed779d3a625 100644 (file)
@@ -965,8 +965,16 @@ namespace ParaMEDMEM
 
    void setValues(PyObject *li, int nbOfTuples, int nbOfElsPerTuple) throw(INTERP_KERNEL::Exception)
    {
-     int sz;
-     double *tmp=convertPyToNewDblArr2(li,&sz);
+     double *tmp=new double[nbOfTuples*nbOfElsPerTuple];
+     try
+       {
+         fillArrayWithPyListDbl(li,tmp,nbOfTuples*nbOfElsPerTuple,0.);
+       }
+     catch(INTERP_KERNEL::Exception& e)
+       {
+         delete [] tmp;
+         throw e;
+       }
      self->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfElsPerTuple);
    }
 
@@ -1168,8 +1176,16 @@ namespace ParaMEDMEM
 
    void setValues(PyObject *li, int nbOfTuples, int nbOfElsPerTuple) throw(INTERP_KERNEL::Exception)
    {
-     int size;
-     int *tmp=convertPyToNewIntArr2(li,&size);
+     int *tmp=new int[nbOfTuples*nbOfElsPerTuple];
+     try
+       {
+         fillArrayWithPyListInt(li,tmp,nbOfTuples*nbOfElsPerTuple,0.);
+       }
+     catch(INTERP_KERNEL::Exception& e)
+       {
+         delete [] tmp;
+         throw e;
+       }
      self->useArray(tmp,true,CPP_DEALLOC,nbOfTuples,nbOfElsPerTuple);
    }
 
index 6af81a202006c7186c0cf9c86ed7295df1cfbb47..0139b3836adaa8469cdd0afdecf92cd26f8d71f5 100644 (file)
@@ -5580,6 +5580,122 @@ class MEDCouplingBasicsTest(unittest.TestCase):
             self.assertAlmostEqual(expected1[i],f3.getIJ(0,i),10);
             pass
         pass
+
+    def testDARearrange1(self):
+        da1=DataArrayInt.New();
+        da1.alloc(12,1);
+        da1.iota(0);
+        #
+        self.assertEqual(12,da1.getNbOfElems());
+        self.assertEqual(1,da1.getNumberOfComponents());
+        self.assertEqual(12,da1.getNumberOfTuples());
+        da1.rearrange(4);
+        self.assertEqual(12,da1.getNbOfElems());
+        self.assertEqual(4,da1.getNumberOfComponents());
+        self.assertEqual(3,da1.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertEqual(i,da1.getIJ(0,i));
+        #
+        da1.rearrange(6);
+        self.assertEqual(12,da1.getNbOfElems());
+        self.assertEqual(6,da1.getNumberOfComponents());
+        self.assertEqual(2,da1.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertEqual(i,da1.getIJ(0,i));
+        #
+        self.assertRaises(da1.rearrange(7),Exception);
+        #
+        da1.rearrange(12);
+        self.assertEqual(12,da1.getNbOfElems());
+        self.assertEqual(12,da1.getNumberOfComponents());
+        self.assertEqual(1,da1.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertEqual(i,da1.getIJ(0,i));
+        #
+        da1.rearrange(3);
+        self.assertEqual(12,da1.getNbOfElems());
+        self.assertEqual(3,da1.getNumberOfComponents());
+        self.assertEqual(4,da1.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertEqual(i,da1.getIJ(0,i));
+        #double
+        da2=da1.convertToDblArr();
+        #
+        self.assertEqual(12,da2.getNbOfElems());
+        self.assertEqual(3,da2.getNumberOfComponents());
+        self.assertEqual(4,da2.getNumberOfTuples());
+        da2.rearrange(4);
+        self.assertEqual(12,da2.getNbOfElems());
+        self.assertEqual(4,da2.getNumberOfComponents());
+        self.assertEqual(3,da2.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertAlmostEqual(float(i),da2.getIJ(0,i),14);
+        #
+        da2.rearrange(6);
+        self.assertEqual(12,da2.getNbOfElems());
+        self.assertEqual(6,da2.getNumberOfComponents());
+        self.assertEqual(2,da2.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertAlmostEqual(float(i),da2.getIJ(0,i),14);
+        #
+        self.assertRaises(da2.rearrange(7),Exception);
+        #
+        da2.rearrange(1);
+        self.assertTrue(ptr2==da2.getConstPointer());
+        self.assertEqual(12,da2.getNbOfElems());
+        self.assertEqual(1,da2.getNumberOfComponents());
+        self.assertEqual(12,da2.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertAlmostEqual(float(i),da2.getIJ(0,i),14);
+        #
+        da2.rearrange(3);
+        self.assertEqual(12,da2.getNbOfElems());
+        self.assertEqual(3,da2.getNumberOfComponents());
+        self.assertEqual(4,da2.getNumberOfTuples());
+        for i in xrange(12):
+            self.assertAlmostEqual(float(i),da2.getIJ(0,i),14);
+        pass
+
+    def testDARearrange1(self):
+        da1=DataArrayInt.New();
+        arr=[1,2,3,2,2,3,5,1,5,5,2,2]
+        da1.setValues(arr,4,3);
+        s=da1.getDifferentValues(True);# API different from C++ because SWIG complains...
+        expected1=[1,2,3,5]
+        self.assertEqual(expected1,s);
+        pass
+
+    def testSwigErrorProtection3(self):
+        da=DataArrayInt.New()
+        da.setValues([1,2,3,4],4,3)
+        self.assertEqual([1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0],da.getValues())
+        self.assertEqual(3,da.getNumberOfComponents());
+        self.assertEqual(4,da.getNumberOfTuples());
+        da=DataArrayInt.New()
+        da.setValues((1,2,3,4,4,3),4,3)
+        self.assertEqual([1, 2, 3, 4, 4, 3, 0, 0, 0, 0, 0, 0],da.getValues())
+        self.assertEqual(3,da.getNumberOfComponents());
+        self.assertEqual(4,da.getNumberOfTuples());
+        da.setValues(10*[1]+290*[2],4,3)
+        self.assertEqual(10*[1]+[2,2],da.getValues())
+        self.assertEqual(3,da.getNumberOfComponents());
+        self.assertEqual(4,da.getNumberOfTuples());
+        #
+        da=DataArrayDouble.New()
+        da.setValues([1,2,3.,4],4,3)
+        self.assertEqual([1., 2., 3., 4., 0., 0., 0., 0., 0., 0., 0., 0.],da.getValues())
+        self.assertEqual(3,da.getNumberOfComponents());
+        self.assertEqual(4,da.getNumberOfTuples());
+        da=DataArrayDouble.New()
+        da.setValues((1,2,3,4.,4,3),4,3)
+        self.assertEqual([1., 2., 3., 4., 4., 3., 0., 0., 0., 0., 0., 0.],da.getValues())
+        self.assertEqual(3,da.getNumberOfComponents());
+        self.assertEqual(4,da.getNumberOfTuples());
+        da.setValues(10*[1]+290*[2],4,3)
+        self.assertEqual(10*[1.]+[2.,2.],da.getValues())
+        self.assertEqual(3,da.getNumberOfComponents());
+        self.assertEqual(4,da.getNumberOfTuples());
+        pass
     
     def setUp(self):
         pass
index 9e884c118a2ce2af3fb0ddb7ca53edf23d597fa1..42dcee2779550bec12b38e59391869c9b43b91e9 100644 (file)
@@ -230,6 +230,63 @@ static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(I
     }
 }
 
+static void fillArrayWithPyListInt(PyObject *pyLi, int *arrToFill, int sizeOfArray, int dftVal) throw(INTERP_KERNEL::Exception)
+{
+  if(PyList_Check(pyLi))
+    {
+      int size=PyList_Size(pyLi);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(PyInt_Check(o))
+            {
+              int val=(int)PyInt_AS_LONG(o);
+              if(i<sizeOfArray)
+                arrToFill[i]=val;
+            }
+          else
+            {
+              const char msg[]="list must contain integers only";
+              PyErr_SetString(PyExc_TypeError,msg);
+              throw INTERP_KERNEL::Exception(msg);
+            }
+        }
+      for(int i=size;i<sizeOfArray;i++)
+        arrToFill[i]=dftVal;
+      return;
+      
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      int size=PyTuple_Size(pyLi);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(PyInt_Check(o))
+            {
+              int val=(int)PyInt_AS_LONG(o);
+              if(i<sizeOfArray)
+                arrToFill[i]=val;
+            }
+          else
+            {
+              const char msg[]="tuple must contain integers only";
+              PyErr_SetString(PyExc_TypeError,msg);
+              throw INTERP_KERNEL::Exception(msg);
+            }
+        }
+      for(int i=size;i<sizeOfArray;i++)
+        arrToFill[i]=dftVal;
+      return;
+    }
+  else
+    {
+      const char msg[]="fillArrayWithPyListInt : not a list";
+      PyErr_SetString(PyExc_TypeError,msg);
+      throw INTERP_KERNEL::Exception(msg);
+    }
+}
+
 static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception)
 {
   PyObject *ret=PyList_New(size);
@@ -318,6 +375,74 @@ static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) throw(INTERP_KER
         }
       return tmp;
     }
+  else
+    {
+      const char msg[]="convertPyToNewDblArr2 : not a list";
+      PyErr_SetString(PyExc_TypeError,msg);
+      throw INTERP_KERNEL::Exception(msg);
+    }
+}
+
+static void fillArrayWithPyListDbl(PyObject *pyLi, double *arrToFill, int sizeOfArray, double dftVal) throw(INTERP_KERNEL::Exception)
+{
+  if(PyList_Check(pyLi))
+    {
+      int size=PyList_Size(pyLi);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyList_GetItem(pyLi,i);
+          if(PyFloat_Check(o))
+            {
+              double val=PyFloat_AS_DOUBLE(o);
+              if(i<sizeOfArray)
+                arrToFill[i]=val;
+            }
+          else if(PyInt_Check(o))
+            {
+              long val0=PyInt_AS_LONG(o);
+              double val=val0;
+              if(i<sizeOfArray)
+                arrToFill[i]=val;
+            }
+          else
+            {
+              const char msg[]="fillArrayWithPyListDbl : list must contain floats/integers only";
+              PyErr_SetString(PyExc_TypeError,msg);
+              throw INTERP_KERNEL::Exception(msg);
+            }
+        }
+      for(int i=size;i<sizeOfArray;i++)
+        arrToFill[i]=dftVal;
+      return;
+    }
+  else if(PyTuple_Check(pyLi))
+    {
+      int size=PyTuple_Size(pyLi);
+      for(int i=0;i<size;i++)
+        {
+          PyObject *o=PyTuple_GetItem(pyLi,i);
+          if(PyFloat_Check(o))
+            {
+              double val=PyFloat_AS_DOUBLE(o);
+              arrToFill[i]=val;
+            }
+          else if(PyInt_Check(o))
+            {
+              long val0=PyInt_AS_LONG(o);
+              double val=val0;
+              arrToFill[i]=val;
+            }
+          else
+            {
+              const char msg[]="fillArrayWithPyListDbl : tuple must contain floats/integers only";
+              PyErr_SetString(PyExc_TypeError,msg);
+              throw INTERP_KERNEL::Exception(msg);
+            }
+        }
+      for(int i=size;i<sizeOfArray;i++)
+        arrToFill[i]=dftVal;
+      return ;
+    }
   else
     {
       const char msg[]="convertPyToNewIntArr : not a list";