]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Sort ascending and descending.
authorageay <ageay>
Thu, 7 Jun 2012 14:06:50 +0000 (14:06 +0000)
committerageay <ageay>
Thu, 7 Jun 2012 14:06:50 +0000 (14:06 +0000)
src/MEDCoupling/Test/MEDCouplingBasicsTest5.cxx
src/MEDCoupling/Test/MEDCouplingBasicsTest5.hxx
src/MEDCoupling_Swig/MEDCouplingBasicsTest.py

index bb9412d231476d87f46e9bf432b895646c371711..2b2824c5af8de38de2a5c97de373fb847247f90f 100644 (file)
@@ -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();
+}
index 12b38e691a8f6ab31cf44345beb90993ca349109..ad4de0c8a20b93469345e83db5f72ec8df0246cd 100644 (file)
@@ -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();
   };
 }
 
index 9cb7878551df1ac93a90eafc3e1be8edbf85738c..156275851dc3036438cb26bd92ac1f5913c1b796 100644 (file)
@@ -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