Skip to content
Snippets Groups Projects
Commit ae361f62 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

+ algo:concat (passed)

parent 2642d936
No related branches found
No related tags found
No related merge requests found
...@@ -33,6 +33,9 @@ double min_value(const Iterator& begin, const Iterator& end, const Evaluator& ev ...@@ -33,6 +33,9 @@ double min_value(const Iterator& begin, const Iterator& end, const Evaluator& ev
template <typename Evaluator, typename Iterator> template <typename Evaluator, typename Iterator>
double max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate); double max_value(const Iterator& begin, const Iterator& end, const Evaluator& evaluate);
//! Returns the concatenation of two std::vector%s.
template <class T> std::vector<T> concat(const std::vector<T>& v1, const std::vector<T>& v2);
} // namespace algo } // namespace algo
// ************************************************************************** // // ************************************************************************** //
...@@ -61,4 +64,11 @@ double algo::max_value(const Iterator& begin, const Iterator& end, const Evaluat ...@@ -61,4 +64,11 @@ double algo::max_value(const Iterator& begin, const Iterator& end, const Evaluat
return ret; return ret;
} }
template <class T> std::vector<T> algo::concat(const std::vector<T>& v1, const std::vector<T>& v2)
{
std::vector<T> v = v1;
v.insert(v.end(), v2.begin(), v2.end());
return v;
}
#endif // BORNAGAIN_CORE_BASICS_ALGORITHMS_H #endif // BORNAGAIN_CORE_BASICS_ALGORITHMS_H
#include "Core/Basics/Algorithms.h"
#include "Tests/GTestWrapper/google_test.h"
#include <string>
#include <vector>
class ConcatTest : public ::testing::Test
{
};
TEST_F(ConcatTest, SimpleType)
{
std::vector<int> A{1, 2, 3};
std::vector<int> B{4, 5};
std::vector<int> N;
EXPECT_EQ(algo::concat(A, B), (std::vector<int>{1, 2, 3, 4, 5}));
EXPECT_EQ(algo::concat(A, A), (std::vector<int>{1, 2, 3, 1, 2, 3}));
EXPECT_EQ(algo::concat(A, N), A);
EXPECT_EQ(algo::concat(N, B), B);
}
TEST_F(ConcatTest, Struct)
{
struct S { std::string nam; double val;
bool operator==(const S& o) const { return nam==o.nam && val==o.val;} };
using V = std::vector<S>;
V A{{"a", 11.}};
V B{{"b", 2.}, {"c", .3}};
V N;
EXPECT_EQ(algo::concat(A, B), (V{{"a", 11.},{"b", 2.}, {"c", .3}}));
EXPECT_EQ(algo::concat(A, A), (V{{"a", 11.},{"a", 11.}}));
EXPECT_EQ(algo::concat(A, N), A);
EXPECT_EQ(algo::concat(N, B), B);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment