]> SALOME platform Git repositories - tools/medcoupling.git/commitdiff
Salome HOME
Add tests for MathOps
authorEl Hadi Moussi <moussi@phimeca.com>
Tue, 6 Aug 2024 08:57:11 +0000 (10:57 +0200)
committerEl Hadi Moussi <moussi@phimeca.com>
Tue, 6 Aug 2024 09:01:05 +0000 (11:01 +0200)
src/ShapeRecogn/Test/CMakeLists.txt
src/ShapeRecogn/Test/MathOpsTest.cxx [new file with mode: 0644]
src/ShapeRecogn/Test/MathOpsTest.hxx [new file with mode: 0644]
src/ShapeRecogn/Test/TestShapeRecogn.cxx

index 83a192666635c81e0e669cf4661cac560953c4e7..4c9987e96019c20fe9bebc2f962b5740fd826be1 100644 (file)
@@ -31,6 +31,7 @@ INCLUDE_DIRECTORIES(
 
 SET(TestShapeRecogn_SOURCES
   TestShapeRecogn.cxx
+  MathOpsTest.cxx
   PlaneTest.cxx
 )
 
@@ -44,17 +45,17 @@ INSTALL(TARGETS TestShapeRecogn DESTINATION ${MEDCOUPLING_INSTALL_BINS})
 
 SET(BASE_TESTS TestShapeRecogn)
 
-FOREACH(test ${BASE_TESTS})
-  ADD_TEST(NAME ${test}
-           COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MCTestLauncher.py ${CMAKE_CURRENT_BINARY_DIR}/${test})
-  SET_TESTS_PROPERTIES(${test} PROPERTIES ENVIRONMENT "${tests_env}")
-ENDFOREACH()
+FOREACH(test ${BASE_TESTS})
+  ADD_TEST(NAME ${test}
+           COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/MCTestLauncher.py ${CMAKE_CURRENT_BINARY_DIR}/${test})
+  SET_TESTS_PROPERTIES(${test} PROPERTIES ENVIRONMENT "${tests_env}")
+ENDFOREACH()
 
-# Application tests
+# Application tests
 
-SET(TEST_INSTALL_DIRECTORY ${MEDCOUPLING_INSTALL_TESTS}/ShapeRecogn)
-INSTALL(TARGETS TestShapeRecogn DESTINATION ${TEST_INSTALL_DIRECTORY})
+SET(TEST_INSTALL_DIRECTORY ${MEDCOUPLING_INSTALL_TESTS}/ShapeRecogn)
+INSTALL(TARGETS TestShapeRecogn DESTINATION ${TEST_INSTALL_DIRECTORY})
 
-INSTALL(FILES CTestTestfileInstall.cmake
-        DESTINATION ${TEST_INSTALL_DIRECTORY}
-        RENAME CTestTestfile.cmake)
+INSTALL(FILES CTestTestfileInstall.cmake
+        DESTINATION ${TEST_INSTALL_DIRECTORY}
+        RENAME CTestTestfile.cmake)
diff --git a/src/ShapeRecogn/Test/MathOpsTest.cxx b/src/ShapeRecogn/Test/MathOpsTest.cxx
new file mode 100644 (file)
index 0000000..17bef13
--- /dev/null
@@ -0,0 +1,82 @@
+#include "MathOpsTest.hxx"
+#include "MathOps.hxx"
+
+using namespace MEDCoupling;
+
+void MathOpsTest::testLstsq()
+{
+    std::vector<double> a = {
+        0.69473263, 0.83318004, 0.60822673, 0.59243878, 0.82872553,
+        0.84048546, 0.95698819, 0.02171218, 0.27683381, 0.20628928,
+        0.80895323, 0.4207767, 0.37468575, 0.86258204, 0.42571846};
+    std::vector<double> b = {
+        0.91167508, 0.95878824, 0.7234827, 0.51753917, 0.18603306};
+    std::vector<double> x = MathOps::lstsq(a, b);
+    std::array<double, 3> xRef = {0.35719095, 0.5134345, 0.26039343};
+    for (size_t i = 0; i < 3; ++i)
+        CPPUNIT_ASSERT_DOUBLES_EQUAL(xRef[i], x[i], 1E-6);
+}
+
+void MathOpsTest::testLstsq2()
+{
+    std::vector<double> a = {
+        0.4564562, 0.3517006,
+        0.28928215, 0.72309086,
+        0.05944836, 0.56024464};
+    std::vector<double> b = {0.98902712, 0.46791812};
+    std::vector<double> x = MathOps::lstsq(a, b);
+    std::array<double, 3> xRef = {2.10752524, 0.2636243, -0.82807416};
+    for (size_t i = 0; i < 3; ++i)
+        CPPUNIT_ASSERT_DOUBLES_EQUAL(xRef[i], x[i], 1E-6);
+}
+
+void MathOpsTest::testLstsqBig()
+{
+    std::vector<double> a(5000000 * 3, 1.0);
+    std::vector<double> b(5000000, 1.0);
+    std::vector<double> x = MathOps::lstsq(a, b);
+    std::array<double, 3> xRef = {1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0};
+    for (size_t i = 2; i < 3; ++i)
+        CPPUNIT_ASSERT_DOUBLES_EQUAL(xRef[i], x[i], 1E-6);
+}
+
+void MathOpsTest::testComputeCov()
+{
+    std::vector<double> coordinates{
+        1, 2, -3, -8, 6, -3, 9, 2, -1, -10, -11, -120};
+    std::array<double, 9>
+        covMatrix = MathOps::computeCov(coordinates);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(76.666666, covMatrix[0], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(26.666666, covMatrix[1], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(319.333333, covMatrix[2], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(26.666666, covMatrix[3], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(54.916666, covMatrix[4], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(420.75, covMatrix[5], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(319.333333, covMatrix[6], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(420.75, covMatrix[7], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(3462.25, covMatrix[8], 1E-6);
+}
+
+void MathOpsTest::testComputePCAFirstAxis()
+{
+    std::vector<double> coordinates{
+        1, 2, -3, -8, 6, -3, 9, 2, -1, -10, -11, -120};
+    std::array<double, 3>
+        axis = MathOps::computePCAFirstAxis(coordinates);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.09198798, axis[0], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.11994164, axis[1], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(0.9885101, axis[2], 1E-6);
+}
+
+void MathOpsTest::testComputeAngles()
+{
+    std::vector<double> directions{
+        1, 2, -3, -8, 6, -3, 9, 2, -1, -10, -11, -120};
+    std::array<double, 3> axis{1, 2, 3};
+    std::vector<double> angles = MathOps::computeAngles(
+        directions, axis);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.86054803, angles[0], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.69914333, angles[1], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(1.27845478, angles[2], 1E-6);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(2.61880376, angles[3], 1E-6);
+}
diff --git a/src/ShapeRecogn/Test/MathOpsTest.hxx b/src/ShapeRecogn/Test/MathOpsTest.hxx
new file mode 100644 (file)
index 0000000..4ff787f
--- /dev/null
@@ -0,0 +1,49 @@
+// Copyright (C) 2007-2024  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#ifndef __MATHOPSTEST_HXX__
+#define __MATHOPSTEST_HXX__
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace MEDCoupling
+{
+    class MathOpsTest : public CppUnit::TestFixture
+    {
+        CPPUNIT_TEST_SUITE(MathOpsTest);
+        CPPUNIT_TEST(testLstsq);
+        CPPUNIT_TEST(testLstsq2);
+        CPPUNIT_TEST(testLstsqBig);
+        CPPUNIT_TEST(testComputeCov);
+        CPPUNIT_TEST(testComputePCAFirstAxis);
+        CPPUNIT_TEST(testComputeAngles);
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+        static void testLstsq();
+        static void testLstsq2();
+        static void testLstsqBig();
+        static void testComputeCov();
+        static void testComputePCAFirstAxis();
+        static void testComputeAngles();
+    };
+};
+
+#endif // __MATHOPSTEST_HXX__
index d474b3f1c6b925eac8f72341b03c18129a9e33cb..8bc7fa6d05e2c643b31b796e71caf6f1ae2bd765 100644 (file)
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
+#include "MathOpsTest.hxx"
 #include "PlaneTest.hxx"
 
 CPPUNIT_TEST_SUITE_REGISTRATION(MEDCoupling::PlaneTest);
+CPPUNIT_TEST_SUITE_REGISTRATION(MEDCoupling::MathOpsTest);
 
 #include "BasicMainTest.hxx"