Page MenuHomec4science

misc.cpp
No OneTemporary

File Metadata

Created
Sun, Jan 12, 06:50

misc.cpp

#include "catch.hpp"
#include "utils/timer.hpp"
#include "utils/moving_average.hpp"
#include "utils/perfs_handler.hpp"
using namespace specmicp;
// Timer
// =======================
TEST_CASE("Timer", "[time],[CPU]") {
SECTION("Timer test") {
Timer timer;
timer.stop();
CHECK(timer.elapsed_time() >= 0.0);
timer.start();
timer.stop();
CHECK(timer.elapsed_time() >= 0.0);
CHECK(timer.get_stop() >= timer.get_start());
CHECK(timer.get_ctime_start() != nullptr);
CHECK(timer.get_ctime_stop() != nullptr);
}
}
// Moving Average
// =======================
TEST_CASE("Moving average", "[average],[timestep]") {
SECTION("Moving average test") {
utils::ExponentialMovingAverage moving_average(0.1, 1.0);
REQUIRE(moving_average.current_value() == 1.0);
CHECK(moving_average.add_point(1.0) == 1.0);
CHECK(moving_average.add_point(2.0) == 1.1);
REQUIRE(moving_average.add_point(3.0) == 0.9*1.1+0.3);
moving_average.reset(1.0);
REQUIRE(moving_average.current_value() == 1.0);
moving_average.set_alpha(0.2);
REQUIRE(moving_average.add_point(2.0) == Approx(0.8+0.4));
}
}
// Performance handler
// =======================
struct MockPerf
{
scalar_t residuals {-1};
index_t nb_iterations {-1};
};
class MockSolverPerf:
public PerformanceHandler<MockPerf>
{
public:
MockSolverPerf() {}
void do_stuff() {
get_perfs().residuals = 1e-6;
get_perfs().nb_iterations = 10;
}
void reset_solver() {
reset_perfs();
}
};
TEST_CASE("PerformanceHandler", "[performance],[base]") {
SECTION("Performance handler test") {
auto my_solver = MockSolverPerf();
const auto& perfs = my_solver.get_perfs();
CHECK(perfs.nb_iterations == -1);
CHECK(perfs.residuals == -1);
my_solver.do_stuff();
CHECK(perfs.nb_iterations == 10);
CHECK(perfs.residuals == 1e-6);
my_solver.reset_solver();
CHECK(perfs.nb_iterations == -1);
CHECK(perfs.residuals == -1);
}
}

Event Timeline