From f60f5e582dd391443909bcb6ae70c726ec7aaaa4 Mon Sep 17 00:00:00 2001 From: El Hadi Moussi Date: Tue, 6 Aug 2024 10:57:11 +0200 Subject: [PATCH] Add tests for MathOps --- src/ShapeRecogn/Test/CMakeLists.txt | 23 +++---- src/ShapeRecogn/Test/MathOpsTest.cxx | 82 ++++++++++++++++++++++++ src/ShapeRecogn/Test/MathOpsTest.hxx | 49 ++++++++++++++ src/ShapeRecogn/Test/TestShapeRecogn.cxx | 2 + 4 files changed, 145 insertions(+), 11 deletions(-) create mode 100644 src/ShapeRecogn/Test/MathOpsTest.cxx create mode 100644 src/ShapeRecogn/Test/MathOpsTest.hxx diff --git a/src/ShapeRecogn/Test/CMakeLists.txt b/src/ShapeRecogn/Test/CMakeLists.txt index 83a192666..4c9987e96 100644 --- a/src/ShapeRecogn/Test/CMakeLists.txt +++ b/src/ShapeRecogn/Test/CMakeLists.txt @@ -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 index 000000000..17bef13fc --- /dev/null +++ b/src/ShapeRecogn/Test/MathOpsTest.cxx @@ -0,0 +1,82 @@ +#include "MathOpsTest.hxx" +#include "MathOps.hxx" + +using namespace MEDCoupling; + +void MathOpsTest::testLstsq() +{ + std::vector 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 b = { + 0.91167508, 0.95878824, 0.7234827, 0.51753917, 0.18603306}; + std::vector x = MathOps::lstsq(a, b); + std::array 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 a = { + 0.4564562, 0.3517006, + 0.28928215, 0.72309086, + 0.05944836, 0.56024464}; + std::vector b = {0.98902712, 0.46791812}; + std::vector x = MathOps::lstsq(a, b); + std::array 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 a(5000000 * 3, 1.0); + std::vector b(5000000, 1.0); + std::vector x = MathOps::lstsq(a, b); + std::array 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 coordinates{ + 1, 2, -3, -8, 6, -3, 9, 2, -1, -10, -11, -120}; + std::array + 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 coordinates{ + 1, 2, -3, -8, 6, -3, 9, 2, -1, -10, -11, -120}; + std::array + 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 directions{ + 1, 2, -3, -8, 6, -3, 9, 2, -1, -10, -11, -120}; + std::array axis{1, 2, 3}; + std::vector 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 index 000000000..4ff787f54 --- /dev/null +++ b/src/ShapeRecogn/Test/MathOpsTest.hxx @@ -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 +#include + +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__ diff --git a/src/ShapeRecogn/Test/TestShapeRecogn.cxx b/src/ShapeRecogn/Test/TestShapeRecogn.cxx index d474b3f1c..8bc7fa6d0 100644 --- a/src/ShapeRecogn/Test/TestShapeRecogn.cxx +++ b/src/ShapeRecogn/Test/TestShapeRecogn.cxx @@ -17,8 +17,10 @@ // 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" -- 2.39.2