From c1a33e82239e79567a422d13d1a9a9c9f14e3ad6 Mon Sep 17 00:00:00 2001 From: ageay Date: Thu, 7 Jun 2012 14:06:50 +0000 Subject: [PATCH] Sort ascending and descending. --- .../Test/MEDCouplingBasicsTest5.cxx | 55 +++++++++++++++++++ .../Test/MEDCouplingBasicsTest5.hxx | 2 + src/MEDCoupling_Swig/MEDCouplingBasicsTest.py | 50 +++++++++++++++++ 3 files changed, 107 insertions(+) diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx index bb9412d23..2b2824c5a 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx @@ -1121,3 +1121,58 @@ void MEDCouplingBasicsTest5::testConvexEnvelop2D1() // m->decrRef(); } + +void MEDCouplingBasicsTest5::testDataArraySort1() +{ + DataArrayInt *arr=DataArrayInt::New(); + CPPUNIT_ASSERT_THROW(arr->sort(true),INTERP_KERNEL::Exception);//no allocation + CPPUNIT_ASSERT_THROW(arr->sort(false),INTERP_KERNEL::Exception);//no allocation + const int values[6]={2,1,6,5,4,7}; + arr->alloc(3,2); + CPPUNIT_ASSERT_THROW(arr->sort(true),INTERP_KERNEL::Exception);//no one component + CPPUNIT_ASSERT_THROW(arr->sort(false),INTERP_KERNEL::Exception);//no one component + arr->rearrange(1); + std::copy(values,values+6,arr->getPointer()); + DataArrayInt *arr1=arr->deepCpy(); + DataArrayInt *arr2=arr->deepCpy(); + arr1->sort(true); + const int expected1[6]={1,2,4,5,6,7}; + CPPUNIT_ASSERT_EQUAL(6,arr1->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,arr1->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected1,expected1+6,arr1->begin())); + arr2->sort(false); + const int expected2[6]={7,6,5,4,2,1}; + CPPUNIT_ASSERT_EQUAL(6,arr2->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,arr2->getNumberOfComponents()); + CPPUNIT_ASSERT(std::equal(expected2,expected2+6,arr2->begin())); + arr1->decrRef(); + arr2->decrRef(); + arr->decrRef(); + // + DataArrayDouble *ard=DataArrayDouble::New(); + CPPUNIT_ASSERT_THROW(ard->sort(true),INTERP_KERNEL::Exception);//no allocation + CPPUNIT_ASSERT_THROW(ard->sort(false),INTERP_KERNEL::Exception);//no allocation + const double valuesD[6]={2.,1.,6.,5.,4.,7.}; + ard->alloc(3,2); + CPPUNIT_ASSERT_THROW(ard->sort(true),INTERP_KERNEL::Exception);//no one component + CPPUNIT_ASSERT_THROW(ard->sort(false),INTERP_KERNEL::Exception);//no one component + ard->rearrange(1); + std::copy(valuesD,valuesD+6,ard->getPointer()); + DataArrayDouble *ard1=ard->deepCpy(); + DataArrayDouble *ard2=ard->deepCpy(); + ard1->sort(true); + const double expected3[6]={1.,2.,4.,5.,6.,7.}; + CPPUNIT_ASSERT_EQUAL(6,ard1->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,ard1->getNumberOfComponents()); + for(int i=0;i<6;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected3[i],ard1->getIJ(i,0),1e-12); + ard2->sort(false); + const double expected4[6]={7.,6.,5.,4.,2.,1.}; + CPPUNIT_ASSERT_EQUAL(6,ard2->getNumberOfTuples()); + CPPUNIT_ASSERT_EQUAL(1,ard2->getNumberOfComponents()); + for(int i=0;i<6;i++) + CPPUNIT_ASSERT_DOUBLES_EQUAL(expected4[i],ard2->getIJ(i,0),1e-12); + ard1->decrRef(); + ard2->decrRef(); + ard->decrRef(); +} diff --git a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx index 12b38e691..ad4de0c8a 100644 --- a/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx +++ b/src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx @@ -55,6 +55,7 @@ namespace ParaMEDMEM CPPUNIT_TEST( testDataArrayIntGetHashCode1 ); CPPUNIT_TEST( testZipConnectivityPol1 ); CPPUNIT_TEST( testConvexEnvelop2D1 ); + CPPUNIT_TEST( testDataArraySort1 ); CPPUNIT_TEST_SUITE_END(); public: void testUMeshTessellate2D1(); @@ -77,6 +78,7 @@ namespace ParaMEDMEM void testDataArrayIntGetHashCode1(); void testZipConnectivityPol1(); void testConvexEnvelop2D1(); + void testDataArraySort1(); }; } diff --git a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py index 9cb787855..156275851 100644 --- a/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py +++ b/src/MEDCoupling_Swig/MEDCouplingBasicsTest.py @@ -9767,6 +9767,56 @@ class MEDCouplingBasicsTest(unittest.TestCase): self.assertEqual(4,da.index([12,13])) pass + def testDataArraySort1(self): + arr=DataArrayInt.New(); + self.assertRaises(InterpKernelException,arr.sort,True) + self.assertRaises(InterpKernelException,arr.sort,False) + values=[2,1,6,5,4,7] + arr.alloc(3,2); + self.assertRaises(InterpKernelException,arr.sort,True) + self.assertRaises(InterpKernelException,arr.sort,False) + arr.rearrange(1); + arr.setValues(values,6,1) + arr1=arr.deepCpy(); + arr2=arr.deepCpy(); + arr1.sort(True); + expected1=[1,2,4,5,6,7] + self.assertEqual(6,arr1.getNumberOfTuples()); + self.assertEqual(1,arr1.getNumberOfComponents()); + self.assertEqual(expected1,arr1.getValues()); + arr2.sort(False); + expected2=[7,6,5,4,2,1] + self.assertEqual(6,arr2.getNumberOfTuples()); + self.assertEqual(1,arr2.getNumberOfComponents()); + self.assertTrue(expected2,arr2.getValues()); + # + ard=DataArrayDouble.New(); + self.assertRaises(InterpKernelException,ard.sort,True) + self.assertRaises(InterpKernelException,ard.sort,False) + valuesD=[2.,1.,6.,5.,4.,7.] + ard.alloc(3,2); + self.assertRaises(InterpKernelException,ard.sort,True) + self.assertRaises(InterpKernelException,ard.sort,False) + ard.rearrange(1); + ard.setValues(valuesD,6,1) + ard1=ard.deepCpy(); + ard2=ard.deepCpy(); + ard1.sort(True); + expected3=[1.,2.,4.,5.,6.,7.] + self.assertEqual(6,ard1.getNumberOfTuples()); + self.assertEqual(1,ard1.getNumberOfComponents()); + for i in xrange(6): + self.assertAlmostEqual(expected3[i],ard1.getIJ(i,0),12) + pass + ard2.sort(False); + expected4=[7.,6.,5.,4.,2.,1.] + self.assertEqual(6,ard2.getNumberOfTuples()); + self.assertEqual(1,ard2.getNumberOfComponents()); + for i in xrange(6): + self.assertAlmostEqual(expected4[i],ard2.getIJ(i,0),12) + pass + pass + def setUp(self): pass pass -- 2.39.2