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 );
void testBuildOrthogonalField2();
void testUMInsertNextCell1();
void testFieldOperatorDivDiffComp1();
+ void testDARearrange1();
+ void testGetDifferentValues1();
//MEDCouplingBasicsTestInterp.cxx
void test2DInterpP0P0_1();
void test2DInterpP0P0PL_1();
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();
+}
+
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);
}
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);
}
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
}
}
+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);
}
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";