diff --git a/fairmq/Properties.cxx b/fairmq/Properties.cxx index 086ef7d5..4a3cebb1 100644 --- a/fairmq/Properties.cxx +++ b/fairmq/Properties.cxx @@ -32,6 +32,28 @@ ostream& operator<<(ostream& os, const vector& v) return os; } +ostream& operator<<(ostream& os, const vector& v) +{ + for (unsigned int i = 0; i < v.size(); ++i) { + os << to_string(v[i]); + if (i != v.size() - 1) { + os << ", "; + } + } + return os; +} + +ostream& operator<<(ostream& os, const vector& v) +{ + for (unsigned int i = 0; i < v.size(); ++i) { + os << to_string(v[i]); + if (i != v.size() - 1) { + os << ", "; + } + } + return os; +} + template pair getString(const boost::any& v, const string& label) { @@ -49,31 +71,27 @@ pair getStringPair(const boost::any& v, const string& label) unordered_map(const Property&)>> PropertyHelper::fTypeInfos = { { type_index(typeid(char)), [](const Property& p) { return pair{ string(1, any_cast(p)), "char" }; } }, - { type_index(typeid(unsigned char)), [](const Property& p) { return pair{ string(1, any_cast(p)), "unsigned char" }; } }, + { type_index(typeid(signed char)), [](const Property& p) { return getString(p, "signed char"); } }, + { type_index(typeid(unsigned char)), [](const Property& p) { return getString(p, "unsigned char"); } }, { type_index(typeid(const char*)), [](const Property& p) { return pair{ string(any_cast(p)), "string" }; } }, { type_index(typeid(string)), [](const Property& p) { return pair{ any_cast(p), "string" }; } }, { type_index(typeid(int)), [](const Property& p) { return getString(p, "int"); } }, - { type_index(typeid(size_t)), [](const Property& p) { return getString(p, "size_t"); } }, - { type_index(typeid(uint32_t)), [](const Property& p) { return getString(p, "uint32_t"); } }, - { type_index(typeid(uint64_t)), [](const Property& p) { return getString(p, "uint64_t"); } }, { type_index(typeid(long)), [](const Property& p) { return getString(p, "long"); } }, { type_index(typeid(long long)), [](const Property& p) { return getString(p, "long long"); } }, { type_index(typeid(unsigned)), [](const Property& p) { return getString(p, "unsigned"); } }, { type_index(typeid(unsigned long)), [](const Property& p) { return getString(p, "unsigned long"); } }, { type_index(typeid(unsigned long long)), [](const Property& p) { return getString(p, "unsigned long long"); } }, - { type_index(typeid(float)), [](const Property& p) { return getString(p, "float"); } }, - { type_index(typeid(double)), [](const Property& p) { return getString(p, "double"); } }, - { type_index(typeid(long double)), [](const Property& p) { return getString(p, "long double"); } }, + { type_index(typeid(float)), [](const Property& p) { return getStringPair(p, "float"); } }, + { type_index(typeid(double)), [](const Property& p) { return getStringPair(p, "double"); } }, + { type_index(typeid(long double)), [](const Property& p) { return getStringPair(p, "long double"); } }, { type_index(typeid(bool)), [](const Property& p) { stringstream ss; ss << boolalpha << any_cast(p); return pair{ ss.str(), "bool" }; } }, { type_index(typeid(vector)), [](const Property& p) { stringstream ss; ss << boolalpha << any_cast>(p); return pair{ ss.str(), "vector>" }; } }, { type_index(typeid(boost::filesystem::path)), [](const Property& p) { return getStringPair(p, "boost::filesystem::path"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, + { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, - { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, - { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, - { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, { type_index(typeid(vector)), [](const Property& p) { return getStringPair>(p, "vector"); } }, @@ -87,13 +105,11 @@ unordered_map(const Property&)>> Prope unordered_map PropertyHelper::fEventEmitters = { { type_index(typeid(char)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, + { type_index(typeid(signed char)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(unsigned char)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(const char*)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, string(any_cast(p))); } }, { type_index(typeid(string)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(int)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, - { type_index(typeid(size_t)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, - { type_index(typeid(uint32_t)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, - { type_index(typeid(uint64_t)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(long)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(long long)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(unsigned)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, @@ -106,12 +122,10 @@ unordered_map)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(boost::filesystem::path)), [](const EventManager& em, const string& k, const Property& p) { em.Emit(k, any_cast(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, + { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, - { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, - { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, - { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, { type_index(typeid(vector)), [](const EventManager& em, const string& k, const Property& p) { em.Emit>(k, any_cast>(p)); } }, diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f49f88dc..202b1483 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -197,6 +197,17 @@ add_testsuite(EventManager TIMEOUT 5 ) +add_testsuite(Properties + SOURCES + ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx + properties/_properties.cxx + + LINKS FairMQ + INCLUDES ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + TIMEOUT 5 +) + # add_testsuite(StateMachine # SOURCES # ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx diff --git a/test/properties/_properties.cxx b/test/properties/_properties.cxx new file mode 100644 index 00000000..0f5ad5af --- /dev/null +++ b/test/properties/_properties.cxx @@ -0,0 +1,174 @@ +/******************************************************************************** + * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#include +#include + +#include + +#include + +#include + +namespace +{ + +using namespace std; + +struct TestEvent : fair::mq::Event {}; + +TEST(Properties, ConversionToString) +{ + fair::mq::ProgOptions o; + o.SetProperty("_char", 'a'); + o.SetProperty("_signed char", -2); + o.SetProperty("_unsigned char", 2); + o.SetProperty("_const char*", "testcstring"); + o.SetProperty("_string", "teststring"); + o.SetProperty("_int", 1); + o.SetProperty("_size_t", 11); + o.SetProperty("_uint32_t", 12); + o.SetProperty("_uint64_t", 123); + o.SetProperty("_long", 1234); + o.SetProperty("_long long", 12345); + o.SetProperty("_unsigned", 3); + o.SetProperty("_unsigned long", 32); + o.SetProperty("_unsigned long long", 321); + o.SetProperty("_float", 3.2); + o.SetProperty("_double", 33.22); + o.SetProperty("_long double", 333.222); + o.SetProperty("_bool", true); + o.SetProperty>("_vector", { true, true }); + o.SetProperty("_boost::filesystem::path", boost::filesystem::path("C:\\Windows")); + o.SetProperty>("_vector", { 'a', 'b', 'c' }); + o.SetProperty>("_vector", { -1, -2, -3 }); + o.SetProperty>("_vector", { 1, 2, 3 }); + o.SetProperty>("_vector", { "aa", "bb", "cc" }); + o.SetProperty>("_vector", { 1, 2, 3 }); + o.SetProperty>("_vector", { 1, 2, 3 }); + o.SetProperty>("_vector", { 12, 13, 14 }); + o.SetProperty>("_vector", { 123, 124, 125 }); + o.SetProperty>("_vector", { 1234, 1235, 1236 }); + o.SetProperty>("_vector", { 12345, 12346, 12347 }); + o.SetProperty>("_vector", { 3, 4, 5 }); + o.SetProperty>("_vector", { 32, 33, 34 }); + o.SetProperty>("_vector", { 321, 322, 323 }); + o.SetProperty>("_vector", { 3.2, 3.3, 3.4 }); + o.SetProperty>("_vector", { 33.22, 33.23, 33.24 }); + o.SetProperty>("_vector", { 333.222, 333.223, 333.224 }); + o.SetProperty>("_vector", { boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") }); + + ASSERT_EQ(o.GetPropertyAsString("_char"), "a"); + ASSERT_EQ(o.GetPropertyAsString("_signed char"), "-2"); + ASSERT_EQ(o.GetPropertyAsString("_unsigned char"), "2"); + ASSERT_EQ(o.GetPropertyAsString("_const char*"), "testcstring"); + ASSERT_EQ(o.GetPropertyAsString("_string"), "teststring"); + ASSERT_EQ(o.GetPropertyAsString("_int"), "1"); + ASSERT_EQ(o.GetPropertyAsString("_size_t"), "11"); + ASSERT_EQ(o.GetPropertyAsString("_uint32_t"), "12"); + ASSERT_EQ(o.GetPropertyAsString("_uint64_t"), "123"); + ASSERT_EQ(o.GetPropertyAsString("_long"), "1234"); + ASSERT_EQ(o.GetPropertyAsString("_long long"), "12345"); + ASSERT_EQ(o.GetPropertyAsString("_unsigned"), "3"); + ASSERT_EQ(o.GetPropertyAsString("_unsigned long"), "32"); + ASSERT_EQ(o.GetPropertyAsString("_unsigned long long"), "321"); + ASSERT_EQ(o.GetPropertyAsString("_float"), "3.2"); + ASSERT_EQ(o.GetPropertyAsString("_double"), "33.22"); + ASSERT_EQ(o.GetPropertyAsString("_long double"), "333.222"); + ASSERT_EQ(o.GetPropertyAsString("_bool"), "true"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "true, true"); + ASSERT_EQ(o.GetPropertyAsString("_boost::filesystem::path"), "\"C:\\Windows\""); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "a, b, c"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "-1, -2, -3"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "1, 2, 3"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "aa, bb, cc"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "1, 2, 3"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "1, 2, 3"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "12, 13, 14"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "123, 124, 125"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "1234, 1235, 1236"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "12345, 12346, 12347"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "3, 4, 5"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "32, 33, 34"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "321, 322, 323"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "3.2, 3.3, 3.4"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "33.22, 33.23, 33.24"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "333.222, 333.223, 333.224"); + ASSERT_EQ(o.GetPropertyAsString("_vector"), "\"C:\\Windows\", \"C:\\Windows\\System32\""); +} + +TEST(Properties, EventEmissionAndStringConversion) +{ + fair::mq::ProgOptions o; + + o.Subscribe("test", [](const string& /* key */, char e) { ASSERT_EQ(e, 'a'); }); + o.Subscribe("test", [](const string& /* key */, signed char e) { ASSERT_EQ(e, -2); }); + o.Subscribe("test", [](const string& /* key */, unsigned char e) { ASSERT_EQ(e, 2); }); + o.Subscribe("test", [](const string& /* key */, const char* e) { ASSERT_EQ(e, "testcstring"); }); + o.Subscribe("test", [](const string& /* key */, string e) { ASSERT_EQ(e, "teststring"); }); + o.Subscribe("test", [](const string& /* key */, int e) { ASSERT_EQ(e, 1); }); + o.Subscribe("test", [](const string& /* key */, long e) { ASSERT_EQ(e, 1234); }); + o.Subscribe("test", [](const string& /* key */, long long e) { ASSERT_EQ(e, 12345); }); + o.Subscribe("test", [](const string& /* key */, unsigned e) { ASSERT_EQ(e, 3); }); + o.Subscribe("test", [](const string& /* key */, unsigned long e) { ASSERT_EQ(e, 32); }); + o.Subscribe("test", [](const string& /* key */, unsigned long long e) { ASSERT_EQ(e, 321); }); + o.Subscribe("test", [](const string& /* key */, float e) { ASSERT_EQ(e, 3.2f); }); + o.Subscribe("test", [](const string& /* key */, double e) { ASSERT_EQ(e, 33.22); }); + o.Subscribe("test", [](const string& /* key */, long double e) { ASSERT_EQ(e, 333.222); }); + o.Subscribe("test", [](const string& /* key */, bool e) { ASSERT_EQ(e, true); }); + o.Subscribe("test", [](const string& /* key */, boost::filesystem::path e) { ASSERT_EQ(e, boost::filesystem::path("C:\\Windows")); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ true, true })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 'a', 'b', 'c' })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ -1, -2, -3 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 1, 2, 3 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ "aa", "bb", "cc" })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 1, 2, 3 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 1234, 1235, 1236 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 12345, 12346, 12347 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 3, 4, 5 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 32, 33, 34 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 321, 322, 323 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 3.2, 3.3, 3.4 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 33.22, 33.23, 33.24 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ 333.222, 333.223, 333.224 })); }); + o.Subscribe>("test", [](const string& /* key */, vector e) { ASSERT_EQ(e, vector({ boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") })); }); + + o.SetProperty("_char", 'a'); + o.SetProperty("_signed char", -2); + o.SetProperty("_unsigned char", 2); + o.SetProperty("_const char*", "testcstring"); + o.SetProperty("_string", "teststring"); + o.SetProperty("_int", 1); + o.SetProperty("_long", 1234); + o.SetProperty("_long long", 12345); + o.SetProperty("_unsigned", 3); + o.SetProperty("_unsigned long", 32); + o.SetProperty("_unsigned long long", 321); + o.SetProperty("_float", 3.2); + o.SetProperty("_double", 33.22); + o.SetProperty("_long double", 333.222); + o.SetProperty("_bool", true); + o.SetProperty>("_vector", { true, true }); + o.SetProperty("_boost::filesystem::path", boost::filesystem::path("C:\\Windows")); + o.SetProperty>("_vector", { 'a', 'b', 'c' }); + o.SetProperty>("_vector", { -1, -2, -3 }); + o.SetProperty>("_vector", { 1, 2, 3 }); + o.SetProperty>("_vector", { "aa", "bb", "cc" }); + o.SetProperty>("_vector", { 1, 2, 3 }); + o.SetProperty>("_vector", { 1234, 1235, 1236 }); + o.SetProperty>("_vector", { 12345, 12346, 12347 }); + o.SetProperty>("_vector", { 3, 4, 5 }); + o.SetProperty>("_vector", { 32, 33, 34 }); + o.SetProperty>("_vector", { 321, 322, 323 }); + o.SetProperty>("_vector", { 3.2, 3.3, 3.4 }); + o.SetProperty>("_vector", { 33.22, 33.23, 33.24 }); + o.SetProperty>("_vector", { 333.222, 333.223, 333.224 }); + o.SetProperty>("_vector", { boost::filesystem::path("C:\\Windows"), boost::filesystem::path("C:\\Windows\\System32") }); +} + +} // namespace