40#include <visp3/core/vpConfig.h>
42#if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2)
45#include <visp3/core/vpIoTools.h>
46#include <visp3/me/vpMe.h>
48#include VISP_NLOHMANN_JSON(json.hpp)
49using json = nlohmann::json;
51#include <catch_amalgamated.hpp>
53#ifdef ENABLE_VISP_NAMESPACE
57template <
typename T,
typename C>
void checkProperties(
const T &t1,
const T &t2, C fn,
const std::string &message)
59 THEN(message) { REQUIRE((
t1.*fn)() == (t2.*fn)()); }
62template <
typename T,
typename C,
typename... Fns>
63void checkProperties(
const T &t1,
const T &t2, C fn,
const std::string &message, Fns... fns)
65 checkProperties(t1, t2, fn, message);
66 checkProperties(t1, t2, fns...);
70void testOptionalProperty(json &j,
const std::vector<std::string> &keys,
vpMe &me,
71 std::function<
void(
vpMe *, C)> setter, std::function<C(
vpMe *)> getter,
72 std::function<C(C)> valueFn)
74 THEN(
"Removing keys does not modify the value")
76 const C
v = valueFn(getter(&me));
78 for (
const std::string &k : keys) {
85 REQUIRE(getter(&me) == v);
91class RandomMeGenerator :
public Catch::Generators::IGenerator<vpMe>
94 std::minstd_rand m_rand;
95 std::uniform_real_distribution<> m_dist;
96 std::uniform_int_distribution<> m_int_dist;
101 RandomMeGenerator() : m_rand(std::random_device {}()), m_dist(0.0, 1.0), m_int_dist(1, 10)
103 static_cast<void>(next());
106 vpMe
const &get() const VP_OVERRIDE {
return current; }
107 bool next() VP_OVERRIDE
111 current.
setMaskSign(m_int_dist(m_rand) > 5 ? 1 : 0);
112 current.
setMu1(m_dist(m_rand));
116 current.
setRange(m_int_dist(m_rand));
117 current.
setStrip(m_int_dist(m_rand));
122Catch::Generators::GeneratorWrapper<vpMe> randomMe()
124 return Catch::Generators::GeneratorWrapper<vpMe>(Catch::Detail::make_unique<RandomMeGenerator>());
128SCENARIO(
"Serializing and deserializing a single vpMe",
"[json]")
130 GIVEN(
"Some random vpMe object")
132 vpMe me = GENERATE(take(10, randomMe()));
133 WHEN(
"Serializing and deserializing an object")
136 const vpMe otherMe =
j;
137 THEN(
"The object's properties are the same")
145 "Number of points to track should be equal", &
vpMe::getRange,
"Range should be equal",
149 WHEN(
"Removing optional properties in JSON object")
153 const auto testInt = [&
j, &me](
const std::string &key, std::function<void(
vpMe *,
int)> setter,
154 std::function<
int(
vpMe *)> getter) ->
void {
155 testOptionalProperty<int>(j, { key }, me, setter, getter, [](
int v) ->
int {
return v - 1; });
157 const auto testDouble = [&
j, &me](
const std::string &key, std::function<void(
vpMe *,
double)> setter,
158 std::function<double(
vpMe *)> getter) ->
void {
159 testOptionalProperty<double>(j, { key }, me, setter, getter, [](
double v) ->
double {
return v + 1.0; });
163 WHEN(
"Removing mu1 and mu2")
181int main(
int argc,
char *argv[])
183 Catch::Session session;
184 session.applyCommandLine(argc, argv);
186 int numFailed = session.run();
192int main() {
return EXIT_SUCCESS; }
void setMu1(const double &mu_1)
void setPointsToTrack(const int &points_to_track)
double getMinSampleStep() const
void setMaskSign(const int &mask_sign)
void setRange(const unsigned int &range)
void setNbTotalSample(const int &ntotal_sample)
void setMaskNumber(const unsigned int &mask_number)
int getNbTotalSample() const
void setThreshold(const double &threshold)
void setStrip(const int &strip)
unsigned int getAngleStep() const
void setMinSampleStep(const double &min_samplestep)
unsigned int getMaskNumber() const
int getPointsToTrack() const
void setSampleStep(const double &sample_step)
double getThreshold() const
unsigned int getMaskSize() const
void setMaskSize(const unsigned int &mask_size)
void setMu2(const double &mu_2)
double getSampleStep() const
unsigned int getRange() const