diff --git a/CMakeLists.txt b/CMakeLists.txt index e47deaa..eedf605 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,9 +86,7 @@ if(MSVC) else() add_compile_options(-Wall -Wextra -Werror -pedantic $<$:-O3>) endif() -if($) - message(STATUS "Enable Release mode") -endif() + enable_testing() # include(CTest) set(CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/contrib ${CMAKE_MODULE_PATH}) diff --git a/src/include/cafea/base/base.hpp b/src/include/cafea/base/base.hpp index 4b70619..75edf14 100644 --- a/src/include/cafea/base/base.hpp +++ b/src/include/cafea/base/base.hpp @@ -92,6 +92,26 @@ class ObjectBase { assert(0 < n && n <= 10); for (int i = 0; i < n; i++) group_[i] = y[i]; } + //! Set object's tags. + template + void set_tags(init_list_ vals) { + assert(0 < n && n <= 10); + std::transform(vals.begin(), vals.end(), tags_.begin(), + [] (T a)->std::string { return fmt::format("{}", a);}); + } + //! Set object's tag by index. + template + void set_tag_by_index(T val, int indx=0) { + assert(0 <= indx && indx <=9); + tags_[indx] = fmt::format("{}", val); + } + //! Get object's tags. + std::array get_tags() const { return tags_;} + //! Get object's tag by index. + std::string get_tag_by_index(int indx=0) { + assert(0 <= indx && indx <= 9); + return tags_[indx]; + } //! Get object's name. std::string get_name() const { return name_;} //! Get object's id. @@ -106,6 +126,7 @@ class ObjectBase { protected: int id_{-1};//!< Object's id. std::array group_;//!< Object's group array. + std::array tags_;//!< Object's tags array. std::string name_{"Empty"};//!< Object's name. }; } // namespace cafea diff --git a/test/basic/a01.cc b/test/basic/a01.cc index f6191e0..43ba653 100644 --- a/test/basic/a01.cc +++ b/test/basic/a01.cc @@ -3,6 +3,7 @@ #include "cafea/base/base.hpp" +using namespace std::string_literals; using cafea::ObjectBase; TEST_CASE("init", "[ObjectBase]") { @@ -53,3 +54,19 @@ TEST_CASE("group", "[ObjectBase]") { for (int i = 0; i < 9; i++) REQUIRE(gp[i] == a[i]); } } + +TEST_CASE("tags", "[ObjectBase]") { + ObjectBase obj; + obj.set_tags({2, 4, 6, 8, 10}); + auto tags = obj.get_tags(); + int i{0}; + for(auto x: {2, 4, 6, 8, 10}) REQUIRE(std::to_string(x) == tags[i++]); + obj.set_tag_by_index(10, 1); + REQUIRE(std::to_string(10) == obj.get_tag_by_index(1)); + obj.set_tags({"apple", "pear", "banana", "peach", "grape"}); + auto newTags = obj.get_tags(); + i = 0; + for(auto x: {"apple"s, "pear"s, "banana"s, "peach"s, "grape"s}) REQUIRE(x == newTags[i++]); + obj.set_tag_by_index("Pineapple", 3); + REQUIRE("Pineapple"s == obj.get_tag_by_index(3)); +}