Add tests for the quiet parameter and fix initialization mistake

This commit is contained in:
2024-12-13 14:32:27 +01:00
parent d1335f9f8a
commit 0406322c62
4 changed files with 46 additions and 8 deletions

View File

@@ -1,11 +1,12 @@
if(ENABLE_TESTING)
include_directories(
${Folding_SOURCE_DIR}
${CMAKE_BINARY_DIR}/configured_files/include
lib/Files
lib/mdlp/src
)
set(TEST_FOLDING "unit_tests_folding")
add_executable(${TEST_FOLDING} TestFolding.cc TestUtils.cc)
target_link_libraries(${TEST_FOLDING} PUBLIC "${TORCH_LIBRARIES}" ArffFiles mdlp Catch2::Catch2WithMain)
target_link_libraries(${TEST_FOLDING} PUBLIC "${TORCH_LIBRARIES}" ArffFiles fimdlp Catch2::Catch2WithMain)
add_test(NAME ${TEST_FOLDING} COMMAND ${TEST_FOLDING})
endif(ENABLE_TESTING)

View File

@@ -12,7 +12,7 @@
TEST_CASE("Version Test", "[Folding]")
{
std::string actual_version = { folding_project_version.begin(), folding_project_version.end() };
std::string actual_version = "1.1.1";
auto data = std::vector<int>(100);
folding::StratifiedKFold stratified_kfold(5, data, 17);
REQUIRE(stratified_kfold.version() == actual_version);
@@ -186,4 +186,38 @@ TEST_CASE("StratifiedKFold Test", "[Folding]")
}
}
}
}
TEST_CASE("Stratified KFold quiet parameter", "[Folding]")
{
auto raw = RawDatasets("glass", true);
std::string expected = "Warning! The number of samples in class 2 (9) is less than the number of folds (10).\n";
SECTION("With vectors")
{
// Redirect cerr to a stringstream
std::streambuf* originalCerrBuffer = std::cerr.rdbuf();
std::stringstream capturedOutput;
std::cerr.rdbuf(capturedOutput.rdbuf());
// StratifiedKFold with quiet parameter set to false
folding::StratifiedKFold stratified_kfold(10, raw.yv, 17, false);
// Restore the original cerr buffer
std::cerr.rdbuf(originalCerrBuffer);
// Check the captured output
REQUIRE(capturedOutput.str() == expected);
REQUIRE(stratified_kfold.isFaulty());
}
SECTION("With tensors")
{
// Redirect cerr to a stringstream
std::streambuf* originalCerrBuffer = std::cerr.rdbuf();
std::stringstream capturedOutput;
std::cerr.rdbuf(capturedOutput.rdbuf());
// StratifiedKFold with quiet parameter set to false
folding::StratifiedKFold stratified_kfold(10, raw.yt, 17, false);
// Restore the original cerr buffer
std::cerr.rdbuf(originalCerrBuffer);
// Check the captured output
REQUIRE(capturedOutput.str() == expected);
REQUIRE(stratified_kfold.isFaulty());
}
}

View File

@@ -8,7 +8,6 @@
#include <tuple>
#include "ArffFiles.h"
#include "CPPFImdlp.h"
#include "folding_config.h"
bool file_exists(const std::string& name);
std::pair<vector<mdlp::labels_t>, map<std::string, int>> discretize(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y, std::vector<string> features);
@@ -45,11 +44,11 @@ class Paths {
public:
static std::string datasets()
{
return { folding_data_path.begin(), folding_data_path.end() };
return "../../tests/data/";
}
static std::string csv()
{
return { folding_csv_path.begin(), folding_csv_path.end() };
return "../../tests/csv/";
}
};
class CSVFiles {