diff --git a/fimdlp/CPPFImdlp.h b/fimdlp/CPPFImdlp.h index a60a93a..d36f6f0 100644 --- a/fimdlp/CPPFImdlp.h +++ b/fimdlp/CPPFImdlp.h @@ -21,6 +21,8 @@ namespace mdlp std::vector y; std::vector xDiscretized; std::vector cutPoints; + + protected: std::vector sortIndices(std::vector &); public: diff --git a/fimdlp/test/CMakeLists.txt b/fimdlp/test/CMakeLists.txt index 6177fbe..1fa0e3d 100644 --- a/fimdlp/test/CMakeLists.txt +++ b/fimdlp/test/CMakeLists.txt @@ -15,16 +15,13 @@ FetchContent_MakeAvailable(googletest) enable_testing() -add_executable( - Metrics_unittest - ../Metrics.cpp - Metrics_unittest.cc -) -target_link_libraries( - Metrics_unittest - GTest::gtest_main -) +add_executable(Metrics_unittest ../Metrics.cpp Metrics_unittest.cc) +add_executable(FImdlp_unittest ../CPPFImdlp.cpp ../Metrics.cpp FImdlp_unittest.cc) +target_link_libraries(Metrics_unittest GTest::gtest_main) +target_link_libraries(FImdlp_unittest GTest::gtest_main) + include(GoogleTest) gtest_discover_tests(Metrics_unittest) +gtest_discover_tests(FImdlp_unittest) diff --git a/fimdlp/test/FImdlp_unittest.cc b/fimdlp/test/FImdlp_unittest.cc new file mode 100644 index 0000000..a78fac3 --- /dev/null +++ b/fimdlp/test/FImdlp_unittest.cc @@ -0,0 +1,39 @@ +#include "gtest/gtest.h" +#include "../CPPFImdlp.h" +namespace +{ + float precision = 0.000001; + class TestMetrics : protected mdlp::CPPFImdlp + { + public: + std::vector testSort(std::vector &X) + { + return sortIndices(X); + } + }; + void check_sorted_vector(std::vector &X, std::vector indices) + { + TestMetrics testClass = TestMetrics(); + std::vector testSortedIndices = testClass.testSort(X); + float prev = X[testSortedIndices[0]]; + for (auto i = 0; i < X.size(); ++i) + { + EXPECT_EQ(testSortedIndices[i], indices[i]); + EXPECT_LE(prev, X[testSortedIndices[i]]); + prev = X[testSortedIndices[i]]; + } + } + TEST(FImdlpTest, SortIndices) + { + + std::vector X = {5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9}; + std::vector indices = {4, 3, 6, 8, 2, 1, 5, 0, 9, 7}; + check_sorted_vector(X, indices); + X = {5.77, 5.88, 5.99}; + indices = {0, 1, 2}; + check_sorted_vector(X, indices); + X = {5.33, 5.22, 5.11}; + indices = {2, 1, 0}; + check_sorted_vector(X, indices); + } +} \ No newline at end of file diff --git a/fimdlp/test/Metrics_unittest.cc b/fimdlp/test/Metrics_unittest.cc index 7619577..3c25fe2 100644 --- a/fimdlp/test/Metrics_unittest.cc +++ b/fimdlp/test/Metrics_unittest.cc @@ -1,7 +1,10 @@ #include "gtest/gtest.h" #include "../Metrics.h" + namespace { + + float precision = 0.000001; TEST(MetricTest, NumClasses) { std::vector y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; @@ -17,7 +20,14 @@ namespace EXPECT_EQ(1, mdlp::Metrics::entropy(y, indices, 0, 10, 2)); EXPECT_EQ(0, mdlp::Metrics::entropy(y, indices, 0, 5, 1)); std::vector yz = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; - ASSERT_NEAR(0.468996, mdlp::Metrics::entropy(yz, indices, 0, 10, 2), 0.000001); + ASSERT_NEAR(0.468996, mdlp::Metrics::entropy(yz, indices, 0, 10, 2), precision); + } + TEST(MetricTest, InformationGain) + { + std::vector y = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; + std::vector indices = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + std::vector yz = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; + ASSERT_NEAR(1, mdlp::Metrics::informationGain(y, indices, 0, 10, 5, 2), precision); + ASSERT_NEAR(0.108032, mdlp::Metrics::informationGain(yz, indices, 0, 10, 5, 2), precision); } - } \ No newline at end of file diff --git a/fimdlp/test/test.sh b/fimdlp/test/test.sh index f01c0de..7c0aa40 100755 --- a/fimdlp/test/test.sh +++ b/fimdlp/test/test.sh @@ -1,4 +1,12 @@ cmake -S . -B build +if test $? -ne 0; then + echo "Error in creating build commands." + exit 1 +fi cmake --build build +if test $? -ne 0; then + echo "Error in build command." + exit 1 +fi cd build ctest --output-on-failure