Skip to content

Commit

Permalink
tests: move all Catch2 code in a single library
Browse files Browse the repository at this point in the history
  • Loading branch information
tvandera committed Jun 23, 2024
1 parent 5891c4c commit aa566d6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 72 deletions.
4 changes: 2 additions & 2 deletions cpp/SmurffCpp/Sessions/CmdSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <sstream>

#ifdef ENABLE_TESTS
#include <catch2/catch_session.hpp>
#include <Tests/Tests.h>
#endif

#ifdef HAVE_BOOST
Expand Down Expand Up @@ -297,7 +297,7 @@ std::shared_ptr<ISession> create_cmd_session(int argc, char **argv)
if (argc >= 2 && std::string(argv[1]) == "--" + std::string(BIST_NAME)) {
argv[1][0] = '\0'; // remove '--bist' from arg list
// run built-in self test
exit(Catch::Session().run( argc, argv ));
exit(test::run(argc, argv));
}
#endif // ENABLE_TESTS

Expand Down
4 changes: 2 additions & 2 deletions cpp/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

FILE (GLOB HEADER_FILES "TestFixtures.h")
FILE (GLOB HEADER_FILES "TestFixtures.h" "Tests.h")
source_group ("Header Files" FILES ${HEADER_FILES})

FILE (GLOB SOURCE_FILES "tests.cpp"
Expand All @@ -20,5 +20,5 @@ include_directories("expected_${SMURFF_FLOAT_TYPE}")

source_group ("Source Files" FILES ${SOURCE_FILES} )

add_library (tests OBJECT ${HEADER_FILES} ${SOURCE_FILES})
add_library (tests ${HEADER_FILES} ${SOURCE_FILES})
target_link_libraries(tests HighFive)
71 changes: 3 additions & 68 deletions cpp/Tests/Tests.h
Original file line number Diff line number Diff line change
@@ -1,81 +1,16 @@
#pragma once

#include <vector>

#include <SmurffCpp/Types.h>

namespace smurff
{

struct ResultItem;
class DataConfig;

namespace test
{

const double rmse_epsilon = smurff::approx_epsilon<smurff::float_type>();
const double single_item_epsilon = rmse_epsilon * 10;

// noise
extern smurff::NoiseConfig fixed_ncfg;

// dense train data
extern smurff::Matrix trainDenseMatrix;
extern smurff::DenseTensor trainDenseTensor2d;
extern smurff::DenseTensor trainDenseTensor3d;

// sparse train data
extern smurff::SparseMatrix trainSparseMatrix;
extern smurff::SparseTensor trainSparseTensor2d;
extern smurff::SparseTensor trainSparseTensor3d;

// sparse test data
extern smurff::SparseMatrix testSparseMatrix;
extern smurff::SparseTensor testSparseTensor2d;
extern smurff::SparseTensor testSparseTensor3d;

// aux data
extern smurff::DataConfig rowAuxDense;
extern smurff::DataConfig colAuxDense;

// side info
extern smurff::Matrix rowSideDenseMatrix;
extern smurff::Matrix colSideDenseMatrix;
extern smurff::Matrix rowSideDenseMatrix3d;

extern smurff::SparseMatrix rowSideSparseMatrix;
extern smurff::SparseMatrix colSideSparseMatrix;

void checkResultItems(const std::vector<smurff::ResultItem> &actualResultItems,
const std::vector<smurff::ResultItem> &expectedResultItems);

template <class M>
SideInfoConfig makeSideInfoConfig(const M &data, bool direct = true)
{
smurff::NoiseConfig sampled_ncfg(NoiseTypes::sampled);
sampled_ncfg.setPrecision(10.0);
SideInfoConfig picfg(data, sampled_ncfg);
picfg.setDirect(direct);
return picfg;
}

template <class Train, class Test>
Config genConfig(const Train &train, const Test &test, std::vector<PriorTypes> priors)
{
Config config;
config.setPriorTypes(priors);
config.setBurnin(50);
config.setNSamples(50);
config.setVerbose(0);
config.setRandomSeed(1234);
config.setNumLatent(4);

config.getTrain().setData(train);
config.getTrain().setNoiseConfig(fixed_ncfg);
config.getTest().setData(test);

return config;
}

void checkValue(double actualValue, double expectedValue, double epsilon);
int run(int argc, char* argv[]);

} // namespace test
} // namespace smurff
7 changes: 7 additions & 0 deletions cpp/Tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <boost/version.hpp>

#include <catch2/catch_approx.hpp>
#include <catch2/catch_session.hpp>

#include <SmurffCpp/result.h>

Expand All @@ -35,8 +36,14 @@

#include <SmurffCpp/SideInfo/DenseSideInfo.h>

#include "Tests.h"
#include "TestFixtures.h"

namespace smurff {

int test::run(int argc, char* argv[]) {
return Catch::Session().run( argc, argv );
}

static NoiseConfig fixed_ncfg(NoiseTypes::fixed);

Expand Down

0 comments on commit aa566d6

Please sign in to comment.