From 3cd0652d1e30b003e127aed3a5a8cc60cf0ddb29 Mon Sep 17 00:00:00 2001 From: Mark Deric Date: Wed, 28 Jul 2021 10:48:17 -0700 Subject: [PATCH] Add the getJoinedString() function Useful in unit tests for which the input vector should be properly sorted by the code under test. This function is very similar to getSortedJoinedString(), but it avoids sorting. Because of the similarity, the getSortedJoinedString() function is refactored to use the new getJoinedString() function. Signed-off-by: Mark Deric --- test/unittests/test_helper.hpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/unittests/test_helper.hpp b/test/unittests/test_helper.hpp index 755015bd..6013ccba 100644 --- a/test/unittests/test_helper.hpp +++ b/test/unittests/test_helper.hpp @@ -188,6 +188,20 @@ inline std::string getTempDirPath(const std::string& fn) #endif +/** + * Returns a string joined with the delimiter + * @param r the array to join + * @param delim the delimiter to use + * @return A string joined by delim from the vector r + */ +template +inline std::string getJoinedString(const std::vector& r, const std::string& delim = "|") +{ + std::stringstream s; + std::copy(r.begin(), r.end(), std::ostream_iterator(s, delim.c_str())); + return s.str(); +} + /** * Returns a sorted string join with the delimiter * This function modifes the input @@ -199,9 +213,7 @@ template inline std::string getSortedJoinedString(std::vector& r, const std::string& delim = "|") { std::sort(r.begin(), r.end()); - std::stringstream s; - std::copy(r.begin(), r.end(), std::ostream_iterator(s, delim.c_str())); - return s.str(); + return getJoinedString(r, delim); } namespace detail {