1#include <Core/Containers/DynamicVisitor.hpp>
2#include <Core/Containers/VariableSet.hpp>
4#include <Core/Utils/TypesUtils.hpp>
5#include <catch2/catch_test_macros.hpp>
9#include "../unittestUtils.hpp"
14 :
public PrintAllHelper<Ra::Core::Utils::TypeList<int, size_t, float, double, std::string>> {};
26 void operator()(
const std::string& name, T& _in, std::any&& ) {
27 std::cout <<
"\t(MyParameterVisitor : ( " << Utils::simplifiedDemangledType<T>() <<
" ) "
28 << name <<
" --> " << _in <<
" // ";
35 std::cout <<
"\t(MyParameterVisitor : ( std::string ) " << name <<
" --> " << _in <<
"\n";
39 size_t getCount() {
return m_counter; }
40 void resetCount() { m_counter = 0; }
43 size_t m_counter { 0 };
49 void operator()(
const std::string&,
int& value, F&& f ) {
55 ps.visit( PrintThemAll {} );
59TEST_CASE(
"Core/Container/VariableSet",
"[unittests][Core][Container][VariableSet]" ) {
60 REQUIRE( PrintThemAll::types::Size == 5 );
75 print_container(
"Initial set", params );
77 SECTION(
"Construction, access and removal to and from a variable set" ) {
86 REQUIRE( added.second ==
false );
87 REQUIRE( added.first->second == x );
92 REQUIRE( fooHandle->first ==
"foo" );
93 REQUIRE( fooHandle->second ==
"bar" );
98 REQUIRE( params.
size() == 5 );
99 REQUIRE( params.
numberOf<
int>() == 2 );
101 REQUIRE( params.
numberOf<
float>() == 1 );
107 REQUIRE( xHandle->second == x );
114 REQUIRE( inserted.second ==
false );
120 REQUIRE( inserted.second ==
true );
138 SECTION(
"Visiting and modifying variable set using static visitor" ) {
139 auto modifyFunction = [](
int x_ ) {
return 2 * x_ + 1; };
145 params.
visit( modifyInts {}, modifyFunction );
146 REQUIRE( params.
getVariable<
int>(
"i" ) == modifyFunction( initial_i ) );
147 REQUIRE( i == modifyFunction( initial_i ) );
149 modifyFunction( initial_i ) );
150 REQUIRE( params.
getVariable<
int>(
"x" ) == modifyFunction( 2 ) );
154 SECTION(
"Visiting and modifying variable set using dynamic visitor" ) {
162 std::cout <<
"\tDoubling the int " << name <<
" (equal to " << value <<
")\n";
166 print_container(
"Doubled set", params );
167 REQUIRE( params.
getVariable<
int>(
"i" ) == ( 2 * initial_i ) );
170 REQUIRE( params.
getVariable<
int>(
"x" ) == ( 2 * 2 ) );
175 std::cout <<
"\tHalving the int " << name <<
" (equal to " << value <<
")\n";
179 REQUIRE( params.
getVariable<
int>(
"i" ) == ( i ) );
180 REQUIRE( params.
getVariable<
int>(
"x" ) == ( 2 ) );
185 REQUIRE( params.
getVariable<
int>(
"i" ) == ( i ) );
186 REQUIRE( params.
getVariable<
int>(
"x" ) == ( 2 ) );
190 SECTION(
"Visiting and modifying variable set using standard range for" ) {
197 for (
auto& p : floatParams ) {
198 std::cout << p.first <<
" = " << p.second;
199 p.second = p.second * 2;
200 std::cout <<
" ==> " << p.second <<
"\n";
202 REQUIRE( params.
getVariable<
float>(
"x" ) == 2 * x );
207 std::cout << p.first <<
" = " << p.second;
209 std::cout <<
" ==> " << p.second <<
"\n";
211 print_container(
"Final set", params );
218 std::cout <<
"Looping on all variables with the same type of a given handle\n";
219 for (
const auto& p : intVariables ) {
220 std::cout << p.first <<
" = " << p.second <<
" ";
224 SECTION(
"General visit using a custom visitor" ) {
227 MyParameterVisitor mp;
228 REQUIRE( mp.getCount() == 0 );
230 REQUIRE( mp.getCount() == 5 );
236 REQUIRE( deletedFloats );
239 REQUIRE( !deletedFloats );
245 REQUIRE( mp.getCount() == 4 );
246 print_container(
"Final set", params );
249 print_container(
"Final set", params );
251TEST_CASE(
"Core/Container/VariableSet/Merging, copying, moving",
252 "[Core][Container][VariableSet]" ) {
259 print_container(
"initial params ", params );
265 print_container(
"initial params2 ", params2 );
271 print_container(
"initial params3 ", params3 );
273 print_container(
"initial params ", params );
279 print_container(
"params after merge of params2 (replace)", params );
285 print_container(
"params after merge of params3 (keep)", params );
289 REQUIRE( newparams->size() == params.
size() );
291 auto numInt = params.
numberOf<
int>();
292 auto numIntN = newparams->numberOf<
int>();
293 REQUIRE( numInt == numIntN );
295 auto numFloat = params.
numberOf<
float>();
296 auto numFloatN = newparams->numberOf<
float>();
297 REQUIRE( numFloat == numFloatN );
299 auto numDouble = params.
numberOf<
double>();
300 auto numDoubleN = newparams->numberOf<
double>();
301 REQUIRE( numDouble == numDoubleN );
303 REQUIRE( ( numIntN + numFloatN + numDoubleN ) == newparams->size() );
305 auto numString = newparams->numberOf<
std::string>();
306 REQUIRE( numString == 0 );
307 auto removed = newparams->deleteAllVariables<
std::string>();
308 REQUIRE( removed ==
false );
310 print_container(
"Copied params into newparams", *newparams );
312 auto sp = params.
size();
313 auto paramsMoved {
std::move( params ) };
314 REQUIRE( params.
size() == 0 );
315 REQUIRE( paramsMoved.size() == sp );
316 print_container(
"Moved params into paramsMoved", paramsMoved );
317 print_container(
"params is empty", params );
322TEST_CASE(
"Core/Container/VariableSe/Iterating on stored types",
323 "[unittests][Core][Container][VariableSet]" ) {
327 std::function<Scalar( Scalar )> multBy2 = []( Scalar x ) {
return x * 2_ra; };
331 for (
const auto& t : typeVector ) {
332 std::cout <<
"\t" << Ra::Core::Utils::simplifiedDemangledType( t ) <<
"\n";
351TEST_CASE(
"Core/Container/VariableSet/Clear",
"[unittests][Core][Container][VariableSet]" ) {
362 REQUIRE( params.
size() == 3 );
364 REQUIRE( params.
size() == 0 );
Base class for visitors with configurable per-type callbacks. Visiting will be prepared at running ti...
bool addOperator(F &&f)
Add a visiting operator.
void addOrReplaceOperator(F &&f)
Add or replace a visiting operator.
bool removeOperator()
Remove a visiting operator.
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
auto getStoredTypes() const -> const std::vector< std::type_index > &
Gets the stored data type.
bool isHandleValid(const H &handle) const
Test the validity of a handle.
bool deleteAllVariables()
Removes all variables of the given type.
auto existsVariable(const std::string &name) const -> Utils::optional< VariableHandle< T > >
test the existence of the given variable
bool deleteVariable(const std::string &name)
Remove a variable, i.e. a name->value association.
auto getAllVariables() const -> VariableContainer< T > &
Get the whole container for variables of a given type.
size_t numberOf() const
Get the number of variables of the given type.
void mergeKeepVariables(const VariableSet &from)
Merge the VariableSet from into this.
auto getVariableHandle(const std::string &name) const -> const VariableHandle< T >
get the handle on the variable with the given name
auto getVariable(const std::string &name) const -> const T &
get the value of the given variable
auto setVariable(const std::string &name, const T &value) -> std::pair< VariableHandle< T >, bool >
reset (or set if the variable does not exist yet) the value of the variable.
auto existsVariableType() const -> Utils::optional< VariableContainer< T > * >
Test if the storage supports a given variable type.
auto getAllVariablesFromHandle(const H &handle) -> VariableContainer< VariableTypeFromHandle< H > > &
Get the whole container for variables of the same type than the given handled variable.
auto insertVariable(const std::string &name, const T &value) -> std::pair< VariableHandle< T >, bool >
Add a variable, i.e. an association name->value, into the container.
void clear()
remove all elements from the container
void mergeReplaceVariables(const VariableSet &from)
Merge the VariableSet from into this.
size_t size() const
Gets the total number of variables (of any type)
void visit(F &&visitor) const
Visit the container using a user defined visitor.
This namespace contains everything "low level", related to data, datastuctures, and computation.
Types, list of types as Ra::Core::Utils::TypeList< ... >
Base class for visitors with static supported types. Visiting will be prepared at compile time by unf...