Skip to content

Commit

Permalink
Fix function order and more unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmertama committed Dec 11, 2024
1 parent bdf5b3b commit 19a9521
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
40 changes: 20 additions & 20 deletions gempyrelib/include/gempyre_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class LogWriter {
/// @cond INTERNAL
class UTILS_EX FileLogWriter : public LogWriter {
public:
FileLogWriter(const std::string& path);
FileLogWriter(std::string_view path);
protected:
bool do_write(const char* buffer, size_t count) override;
protected:
Expand Down Expand Up @@ -586,7 +586,7 @@ template <class T, typename In, typename Out>
template <class IT, typename In, typename Out, typename = std::enable_if_t<std::is_pointer<IT>::value>>
[[deprecated("See join with Callable")]] std::string join(const IT begin,
const IT end,
const std::string joinChar = "",
std::string_view joinChar = "",
const std::function<Out (const In&)>& f = [](const In& k)->Out{return k;}) {
std::string s;
std::ostringstream iss(s);
Expand Down Expand Up @@ -630,20 +630,6 @@ std::string join(const IT& begin,
return iss.str();
}

/// @brief Join container values, try 1st if compiler can deduct types
/// @tparam IT container
/// @tparam Callable conversion
/// @param t container
/// @param joinChar optional glue string
/// @param f optional transform function
/// @return string
template <class T, typename Callable = DefaultJoiner<typename T::value_type>>
std::string join(const T& t,
std::string_view joinChar = "",
const Callable& f = Callable{}) {
return join(t.begin(), t.end(), joinChar, f);
}

/// @brief Join container values, try 1st if compiler can deduct types
/// @tparam IT container
/// @tparam Callable conversion
Expand All @@ -652,11 +638,11 @@ std::string join(const T& t,
/// @param joinChar optional glue string
/// @param f optional transform function
/// @return string
template <class IT, typename Callable = DefaultJoiner<typename std::remove_pointer<IT>::type>,
template <typename IT, typename Callable = DefaultJoiner<typename std::remove_pointer<IT>::type>,
typename = std::enable_if_t<std::is_pointer<IT>::value>>
std::string join(const IT begin,
const IT end,
const std::string joinChar = "",
std::string_view joinChar = "",
const Callable& f = Callable{}) {
std::string s;
std::ostringstream iss(s);
Expand All @@ -671,8 +657,22 @@ std::string join(const IT begin,
return iss.str();
}

/// @brief Join container values, try 1st if compiler can deduct types
/// @tparam IT container
/// @tparam Callable conversion
/// @param t container
/// @param joinChar optional glue string
/// @param f optional transform function
/// @return string
template <typename T, typename Callable = DefaultJoiner<typename T::value_type>>
std::string join(const T& t,
std::string_view joinChar = "",
const Callable& f = Callable{}) {
return join(std::begin(t), std::end(t), joinChar, f);
}

/// @cond INTERNAL
template <class T>
template <typename T>
T merge(const T& b1, const T& b2) {
T bytes(b1.size() + b2.size());
auto begin = bytes.begin();
Expand All @@ -683,7 +683,7 @@ T merge(const T& b1, const T& b2) {
}

// I just wonder why copy instead of move hence not public
template <class T, typename ...Arg>
template <typename T, typename ...Arg>
T merge(const T& b1, const T& b2, Arg ...args) {
T bytes(b1.size() + b2.size());
auto begin = bytes.begin();
Expand Down
2 changes: 1 addition & 1 deletion gempyrelib/src/common/utils/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class DevNull : public LogWriter {
};


FileLogWriter::FileLogWriter(const std::string& path) : m_file(path, std::ios::out | std::ios::app ) {}
FileLogWriter::FileLogWriter(std::string_view path) : m_file(path, std::ios::out | std::ios::app ) {}
bool FileLogWriter::do_write(const char* bytes, size_t count) {
(void) count;
if(!m_file.is_open())
Expand Down
34 changes: 34 additions & 0 deletions test/unittests/unittests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,40 @@ TEST(Unittests, join5) {
EXPECT_EQ(joined, "AA_BEE_CEE_DEE");
}

TEST(Unittests, join6) {
const std::array<std::string_view, 4> strs = {"aa", "bee", "cee", "dee"};
const auto joined = GempyreUtils::join(strs, "_", [](const auto& s) {return GempyreUtils::to_upper(std::string_view{s});});
EXPECT_EQ(joined, "AA_BEE_CEE_DEE");
}

TEST(Unittests, join7) {
const std::array<int, 4> strs = {1, 2, 3, 4};
const auto joined = GempyreUtils::join(strs, ",");
EXPECT_EQ(joined, "1,2,3,4");
}

TEST(Unittests, join8) {
const std::array<std::pair<int, int>, 4> strs {{{1, 2}, {3,4}, {5,6}, {7,8}}};
const auto joined = GempyreUtils::join(strs, ",", [](const auto& a){return a.first + a.second;});
EXPECT_EQ(joined, "3,7,11,15");
}

TEST(Unittests, join9) {
const std::array<std::array<int, 2>, 4> strs {{{1, 2}, {3,4}, {5,6}, {7,8}}};
const auto joined = GempyreUtils::join(std::begin(strs), std::end(strs), ",", [](const auto& a) {
return std::to_string(a[0]) + 'x' + std::to_string(a[1]);});
EXPECT_EQ(joined, "1x2,3x4,5x6,7x8");
}

TEST(Unittests, join10) {
const std::array<std::array<int, 2>, 4> strs {{{1, 2}, {3,4}, {5,6}, {7,8}}};
const auto joined = GempyreUtils::join(strs, ",", [](const auto& a) {
return std::to_string(a[0]) + 'x' + std::to_string(a[1]);});
EXPECT_EQ(joined, "1x2,3x4,5x6,7x8");
}



int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
for(int i = 1 ; i < argc; ++i)
Expand Down

0 comments on commit 19a9521

Please sign in to comment.