40#include <visp3/core/vpConfig.h>
42#if defined(VISP_HAVE_NLOHMANN_JSON) && defined(VISP_HAVE_CATCH2) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
45#include <visp3/core/vpIoTools.h>
46#include <visp3/me/vpMe.h>
48#include <nlohmann/json.hpp>
49using json = nlohmann::json;
51#define CATCH_CONFIG_RUNNER
54template <
typename T,
typename C>
void checkProperties(
const T &t1,
const T &t2, C fn,
const std::string &message)
56 THEN(message) { REQUIRE((t1.*fn)() == (t2.*fn)()); }
59template <
typename T,
typename C,
typename... Fns>
60void checkProperties(
const T &t1,
const T &t2, C fn,
const std::string &message, Fns... fns)
62 checkProperties(t1, t2, fn, message);
63 checkProperties(t1, t2, fns...);
67void testOptionalProperty(json &j,
const std::vector<std::string> &keys,
vpMe &me,
68 std::function<
void(
vpMe *, C)> setter, std::function<C(
vpMe *)> getter,
69 std::function<C(C)> valueFn)
71 THEN(
"Removing keys does not modify the value")
73 const C v = valueFn(getter(&me));
75 for (
const std::string &k : keys) {
82 REQUIRE(getter(&me) == v);
88class RandomMeGenerator :
public Catch::Generators::IGenerator<vpMe>
91 std::minstd_rand m_rand;
92 std::uniform_real_distribution<> m_dist;
93 std::uniform_int_distribution<> m_int_dist;
98 RandomMeGenerator() : m_rand(std::random_device{}()), m_dist(0.0, 1.0), m_int_dist(1, 10)
100 static_cast<void>(next());
103 vpMe const &get()
const override {
return current; }
108 current.
setMaskSign(m_int_dist(m_rand) > 5 ? 1 : 0);
109 current.
setMu1(m_dist(m_rand));
113 current.
setRange(m_int_dist(m_rand));
114 current.
setStrip(m_int_dist(m_rand));
118Catch::Generators::GeneratorWrapper<vpMe> randomMe()
120 return Catch::Generators::GeneratorWrapper<vpMe>(
121 std::unique_ptr<Catch::Generators::IGenerator<vpMe> >(
new RandomMeGenerator()));
125SCENARIO(
"Serializing and deserializing a single vpMe",
"[json]")
127 GIVEN(
"Some random vpMe object")
129 vpMe me = GENERATE(take(10, randomMe()));
130 WHEN(
"Serializing and deserializing an object")
133 const vpMe otherMe = j;
134 THEN(
"The object's properties are the same")
142 "Number of points to track should be equal", &
vpMe::getRange,
"Range should be equal",
146 WHEN(
"Removing optional properties in JSON object")
150 const auto testInt = [&j, &me](
const std::string &key, std::function<void(
vpMe *,
int)> setter,
151 std::function<int(
vpMe *)> getter) ->
void {
152 testOptionalProperty<int>(j, { key }, me, setter, getter, [](
int v) ->
int {
return v - 1; });
154 const auto testDouble = [&j, &me](
const std::string &key, std::function<void(
vpMe *,
double)> setter,
155 std::function<double(
vpMe *)> getter) ->
void {
156 testOptionalProperty<double>(j, { key }, me, setter, getter, [](
double v) ->
double {
return v + 1.0; });
160 WHEN(
"Removing mu1 and mu2")
178int main(
int argc,
char *argv [])
180 Catch::Session session;
181 session.applyCommandLine(argc, argv);
183 int numFailed = session.run();
189int main() {
return EXIT_SUCCESS; }
void setMu1(const double &mu_1)
void setSampleStep(const double &s)
void setMaskSign(const int &a)
void setRange(const unsigned int &r)
void setStrip(const int &a)
double getMinSampleStep() const
void setMinSampleStep(const double &min)
void setMaskSize(const unsigned int &a)
int getNbTotalSample() const
unsigned int getAngleStep() const
unsigned int getMaskNumber() const
int getPointsToTrack() const
void setNbTotalSample(const int &nb)
double getThreshold() const
void setPointsToTrack(const int &n)
unsigned int getMaskSize() const
void setMu2(const double &mu_2)
double getSampleStep() const
unsigned int getRange() const
void setMaskNumber(const unsigned int &a)
void setThreshold(const double &t)