From: ageay Date: Thu, 16 Dec 2010 09:19:53 +0000 (+0000) Subject: *** empty log message *** X-Git-Tag: RELIQUAT_6x_15112010~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=af4f968cb6bee2bb61fe905db30f08e3f793d11a;p=tools%2Fmedcoupling.git *** empty log message *** --- diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx index 076a59565..54bcaa886 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest.hxx @@ -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(); diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx index 1b25714ac..6bbc8b209 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest2.cxx @@ -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 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(); +} + diff --git a/src/MEDCoupling_Swig/MEDCoupling.i b/src/MEDCoupling_Swig/MEDCoupling.i index 32dd28f1a..57fd23951 100644 --- a/src/MEDCoupling_Swig/MEDCoupling.i +++ b/src/MEDCoupling_Swig/MEDCoupling.i @@ -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); } diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 6af81a202..0139b3836 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -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 diff --git a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i index 9e884c118..42dcee277 100644 --- a/src/MEDCoupling_Swig/MEDCouplingTypemaps.i +++ b/src/MEDCoupling_Swig/MEDCouplingTypemaps.i @@ -230,6 +230,63 @@ static void convertPyToNewIntArr3(PyObject *pyLi, std::vector& 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