]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Generalization on empty allocated DataArrays.
authorageay <ageay>
Thu, 13 Dec 2012 16:35:09 +0000 (16:35 +0000)
committerageay <ageay>
Thu, 13 Dec 2012 16:35:09 +0000 (16:35 +0000)
src/MEDCoupling/MEDCouplingMemArray.cxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py
src/MEDCoupling_Swig/MEDCouplingTypemaps.i

index d5420067d942701b1a3b9e102b341812e4495b68..0ea82aef18c0a8c74f4ae50bae63efd85cf74b39 100644 (file)
@@ -383,6 +383,8 @@ int DataArray::GetNumberOfItemGivenBES(int begin, int end, int step, const char
       std::ostringstream oss; oss << msg << " : end before begin !";
       throw INTERP_KERNEL::Exception(oss.str().c_str());
     }
+  if(end==begin)
+    return 0;
   if(step<=0)
     {
       std::ostringstream oss; oss << msg << " : invalid step should be > 0 !";
@@ -4207,7 +4209,7 @@ DataArrayInt *DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTu
 {
   if(!arr || !arrI)
     throw INTERP_KERNEL::Exception("DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2 : presence of NULL ref of DataArrayInt in input !");
-  DataArrayInt *ret=DataArrayInt::New();
+  MEDCouplingAutoRefCountObjectPtr<DataArrayInt> ret=DataArrayInt::New();
   ret->alloc(nbOfOldTuples,1);
   int *pt=ret->getPointer();
   std::fill(pt,pt+nbOfOldTuples,-1);
@@ -4241,6 +4243,7 @@ DataArrayInt *DataArrayInt::BuildOld2NewArrayFromSurjectiveFormat2(int nbOfOldTu
         }
     }
   newNbOfTuples=newNb;
+  ret->incrRef();
   return ret;
 }
 
index 5ec7e253e3012e44e3e5fd94b45c44b99b5785a5..68df0a3dd3142077b9cc74b3a822a1722e160f88 100644 (file)
@@ -8128,6 +8128,7 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertEqual(7,newNbTuple);
         self.assertEqual(1,ret.getNumberOfComponents());
         self.assertEqual(expected,ret.getValues());
+        self.assertRaises(InterpKernelException,DataArrayInt.BuildOld2NewArrayFromSurjectiveFormat2,9,a,b);
         pass
 
     def testDADIReverse1(self):
@@ -10434,6 +10435,42 @@ class MEDCouplingBasicsTest(unittest.TestCase):
         self.assertRaises(InterpKernelException,d.checkMonotonic,False)
         pass
 
+    def testSwigDASetItemOnEmpty1(self):
+        d=DataArrayInt(0,1)
+        isThrow=False
+        try:
+            d[0:1000:2]=4
+        except InterpKernelException as e:
+            isThrow=True
+            pass
+        self.assertTrue(isThrow)
+        d[:]=4
+        d[::2]=5
+        #
+        d=DataArrayDouble(0,1)
+        isThrow=False
+        try:
+            d[0:1000:2]=4
+        except InterpKernelException as e:
+            isThrow=True
+            pass
+        self.assertTrue(isThrow)
+        d[:]=4
+        d[::2]=5
+        d=DataArrayInt([],0,1)
+        d2=DataArrayInt(0)
+        self.assertTrue(d2.isEqual(d))
+        d=DataArrayDouble([],0,1)
+        d2=DataArrayDouble(0)
+        self.assertTrue(d2.isEqual(d,1e-12))
+        pass
+
+    def testSwigDAITransformWithIndArr1(self):
+        arr=DataArrayInt([0,4,5,1])
+        d=DataArrayInt([7,8,9,10])
+        self.assertRaises(InterpKernelException,arr.transformWithIndArr,d)
+        pass
+
     def setUp(self):
         pass
     pass
index c168124294a071549462ba225c8aa5c52205cf94..3beb08f8219fca6e30646c7e4f138fc69c9c170e 100644 (file)
@@ -453,6 +453,8 @@ static std::vector<int> fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples,
           PyObject *o=PyList_GetItem(pyLi,i);
           fillArrayWithPyListInt3(o,size2,ret);
         }
+      if(size1==0)
+        size2=1;
     }
   else if(PyTuple_Check(pyLi))
     {
@@ -462,6 +464,8 @@ static std::vector<int> fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples,
           PyObject *o=PyTuple_GetItem(pyLi,i);
           fillArrayWithPyListInt3(o,size2,ret);
         }
+      if(size1==0)
+        size2=1;
     }
   else
     throw INTERP_KERNEL::Exception("fillArrayWithPyListInt2 : Unrecognized type ! Should be a tuple or a list !");
@@ -696,6 +700,8 @@ static std::vector<double> fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTupl
           PyObject *o=PyList_GetItem(pyLi,i);
           fillArrayWithPyListDbl3(o,size2,ret);
         }
+      if(size1==0)
+        size2=1;
     }
   else if(PyTuple_Check(pyLi))
     {
@@ -705,6 +711,8 @@ static std::vector<double> fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTupl
           PyObject *o=PyTuple_GetItem(pyLi,i);
           fillArrayWithPyListDbl3(o,size2,ret);
         }
+      if(size1==0)
+        size2=1;
     }
   else
     throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl2 : Unrecognized type ! Should be a tuple or a list !");
@@ -1107,13 +1115,14 @@ static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int&
     }
   if(PySlice_Check(value))
     {
-      Py_ssize_t strt,stp,step;
+      Py_ssize_t strt=2,stp=2,step=2;
       PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
       if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
-        {
-          std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elemnts is : " << nbelem;
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
-        }
+        if(nbelem!=0 || strt!=0 || stp!=0)
+          {
+            std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
+            throw INTERP_KERNEL::Exception(oss.str().c_str());
+          }
       p.first=strt;
       p.second.first=stp;
       p.second.second=step;
@@ -1197,13 +1206,14 @@ static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int&
     }
   if(PySlice_Check(value))
     {
-      Py_ssize_t strt,stp,step;
+      Py_ssize_t strt=2,stp=2,step=2;
       PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
       if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
-        {
-          std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elemnts is : " << nbelem;
-          throw INTERP_KERNEL::Exception(oss.str().c_str());
-        }
+        if(nbelem!=0 || strt!=0 || stp!=0)
+          {
+            std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
+            throw INTERP_KERNEL::Exception(oss.str().c_str());
+          }
       p.first=strt;
       p.second.first=stp;
       p.second.second=step;