Add Tests to sortIndices

This commit is contained in:
2022-12-01 11:41:25 +01:00
parent 267e0073e4
commit c7613ef237
5 changed files with 67 additions and 11 deletions

View File

@@ -21,6 +21,8 @@ namespace mdlp
std::vector<int> y; std::vector<int> y;
std::vector<float> xDiscretized; std::vector<float> xDiscretized;
std::vector<CutPointBody> cutPoints; std::vector<CutPointBody> cutPoints;
protected:
std::vector<size_t> sortIndices(std::vector<float> &); std::vector<size_t> sortIndices(std::vector<float> &);
public: public:

View File

@@ -15,16 +15,13 @@ FetchContent_MakeAvailable(googletest)
enable_testing() enable_testing()
add_executable( add_executable(Metrics_unittest ../Metrics.cpp Metrics_unittest.cc)
Metrics_unittest add_executable(FImdlp_unittest ../CPPFImdlp.cpp ../Metrics.cpp FImdlp_unittest.cc)
../Metrics.cpp target_link_libraries(Metrics_unittest GTest::gtest_main)
Metrics_unittest.cc target_link_libraries(FImdlp_unittest GTest::gtest_main)
)
target_link_libraries(
Metrics_unittest
GTest::gtest_main
)
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(Metrics_unittest) gtest_discover_tests(Metrics_unittest)
gtest_discover_tests(FImdlp_unittest)

View File

@@ -0,0 +1,39 @@
#include "gtest/gtest.h"
#include "../CPPFImdlp.h"
namespace
{
float precision = 0.000001;
class TestMetrics : protected mdlp::CPPFImdlp
{
public:
std::vector<size_t> testSort(std::vector<float> &X)
{
return sortIndices(X);
}
};
void check_sorted_vector(std::vector<float> &X, std::vector<size_t> indices)
{
TestMetrics testClass = TestMetrics();
std::vector<size_t> 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<float> X = {5.7, 5.3, 5.2, 5.1, 5.0, 5.6, 5.1, 6.0, 5.1, 5.9};
std::vector<size_t> 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);
}
}

View File

@@ -1,7 +1,10 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "../Metrics.h" #include "../Metrics.h"
namespace namespace
{ {
float precision = 0.000001;
TEST(MetricTest, NumClasses) TEST(MetricTest, NumClasses)
{ {
std::vector<int> y = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; std::vector<int> 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(1, mdlp::Metrics::entropy(y, indices, 0, 10, 2));
EXPECT_EQ(0, mdlp::Metrics::entropy(y, indices, 0, 5, 1)); EXPECT_EQ(0, mdlp::Metrics::entropy(y, indices, 0, 5, 1));
std::vector<int> yz = {1, 1, 1, 1, 1, 1, 1, 1, 2, 1}; std::vector<int> 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<int> y = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
std::vector<size_t> indices = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<int> 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);
} }
} }

View File

@@ -1,4 +1,12 @@
cmake -S . -B build cmake -S . -B build
if test $? -ne 0; then
echo "Error in creating build commands."
exit 1
fi
cmake --build build cmake --build build
if test $? -ne 0; then
echo "Error in build command."
exit 1
fi
cd build cd build
ctest --output-on-failure ctest --output-on-failure