From fdbc31d89ed7ac350f6fc99e1f1acf25920678ce Mon Sep 17 00:00:00 2001
From: Rainer Kartmann <rainer.kartmann@kit.edu>
Date: Thu, 14 Jan 2021 10:39:32 +0100
Subject: [PATCH] Add tests

---
 SimoxUtility/tests/algorithm/CMakeLists.txt   |   3 +-
 .../tests/algorithm/get_map_keys_values.cpp   | 101 ++++++++++++++++++
 2 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 SimoxUtility/tests/algorithm/get_map_keys_values.cpp

diff --git a/SimoxUtility/tests/algorithm/CMakeLists.txt b/SimoxUtility/tests/algorithm/CMakeLists.txt
index edf9bc6e5..6ec915c9b 100644
--- a/SimoxUtility/tests/algorithm/CMakeLists.txt
+++ b/SimoxUtility/tests/algorithm/CMakeLists.txt
@@ -1,6 +1,7 @@
 
 ADD_SU_TEST( apply )
 ADD_SU_TEST( for_each_if )
-ADD_SU_TEST( string_tools )
 ADD_SU_TEST( fuzzy_find )
+ADD_SU_TEST( get_map_keys_values )
+ADD_SU_TEST( string_tools )
 
diff --git a/SimoxUtility/tests/algorithm/get_map_keys_values.cpp b/SimoxUtility/tests/algorithm/get_map_keys_values.cpp
new file mode 100644
index 000000000..2a7da344d
--- /dev/null
+++ b/SimoxUtility/tests/algorithm/get_map_keys_values.cpp
@@ -0,0 +1,101 @@
+/**
+* @package    SimoxUtility
+* @author     Rainer Kartmann
+* @copyright  2021 Rainer Kartmann
+*/
+
+#define BOOST_TEST_MODULE SimoxUtility/algorithm/get_map_keys_values
+
+#include <boost/test/included/unit_test.hpp>
+
+#include <SimoxUtility/algorithm/get_map_keys_values.h>
+
+
+namespace get_map_keys_values_test
+{
+
+    struct Fixture
+    {
+
+        std::map<int, std::string> map {
+            { 1, "one" },
+            { 2, "two" },
+            { 3, "three" },
+        };
+
+        const std::map<int, std::string>& const_map()
+        {
+            return map;
+        }
+    };
+
+    template <class R, class V, class K>
+    void check_equal(const std::vector<R>& values, const std::map<K, V>& map)
+    {
+        BOOST_CHECK_EQUAL(map.size(), values.size());
+
+        auto itv = values.begin();
+        for (auto itm = map.begin(); itm != map.end(); ++itm, ++itv)
+        {
+            if constexpr(std::is_same_v<R, V*> || std::is_same_v<R, const V*>)
+            {
+                BOOST_CHECK_EQUAL(&itm->second, *itv);
+            }
+            else
+            {
+                static_assert (std::is_same_v<R, V>, "Expecting types to be equal.");
+                BOOST_CHECK_EQUAL(itm->second, *itv);
+            }
+        }
+    }
+
+}
+
+
+BOOST_FIXTURE_TEST_SUITE(get_map_keys_values_test, get_map_keys_values_test::Fixture)
+
+
+BOOST_AUTO_TEST_CASE(test_get_values)
+{
+    std::vector<std::string> values = simox::alg::get_values(map);
+    check_equal(values, map);
+}
+
+BOOST_AUTO_TEST_CASE(test_get_values_unary_fn)
+{
+    std::vector<std::stringstream> values;
+    // const
+    values = simox::alg::get_values(const_map(), [](const std::string& value)
+    {
+        std::stringstream ss;
+        ss << value;
+        return ss;
+    });
+    // non-const
+    values = simox::alg::get_values(map, [](std::string& value)
+    {
+        std::stringstream ss;
+        ss << value;
+        return ss;
+    });
+}
+
+
+BOOST_AUTO_TEST_CASE(test_get_value_ptrs_const)
+{
+    std::vector<const std::string*> values = simox::alg::get_value_ptrs(const_map());
+    check_equal(values, const_map());
+}
+BOOST_AUTO_TEST_CASE(test_get_value_ptrs_non_const)
+{
+    std::vector<std::string*> values = simox::alg::get_value_ptrs(map);
+    check_equal(values, map);
+}
+BOOST_AUTO_TEST_CASE(test_get_value_cptrs_non_const)
+{
+    std::vector<const std::string*> values = simox::alg::get_value_cptrs(map);
+    check_equal(values, map);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
-- 
GitLab