#pragma once #include "test_helpers.hpp" #include #include #include #include #include #include #include #include #include namespace { [[maybe_unused]] void replace_all(std::string& s, const std::string& from, const std::string& to) { if (from.empty()) return; size_t start_pos = 0; while ((start_pos = s.find(from, start_pos)) != std::string::npos) { s.replace(start_pos, from.length(), to); start_pos += to.length(); } } template void expect_error_on_command(ss::parser& p, const std::function command) { if (ss::setup::throw_on_error) { try { command(); FAIL("expected exception"); } catch (const std::exception& e) { CHECK_FALSE(std::string{e.what()}.empty()); } } else { command(); CHECK(!p.valid()); if constexpr (ss::setup::string_error) { CHECK_FALSE(p.error_msg().empty()); } } } [[maybe_unused]] void update_if_crlf(std::string& s) { #ifdef _WIN32 replace_all(s, "\r\n", "\n"); #else (void)(s); #endif } struct X { constexpr static auto delim = ","; constexpr static auto empty = "_EMPTY_"; int i; double d; std::string s; std::string to_string() const { if (s == empty) { return ""; } return std::to_string(i) .append(delim) .append(std::to_string(d)) .append(delim) .append(s); } auto tied() const { return std::tie(i, d, s); } }; template std::enable_if_t, bool> operator==(const T& lhs, const T& rhs) { return lhs.tied() == rhs.tied(); } template static void make_and_write(const std::string& file_name, const std::vector& data, const std::vector& header = {}, bool new_line_eof = true) { std::ofstream out{file_name}; #ifdef _WIN32 std::vector new_lines = {"\n"}; #else std::vector new_lines = {"\n", "\r\n"}; #endif for (const auto& i : header) { if (&i != &header.front()) { out << T::delim; } out << i; } if (!header.empty()) { out << new_lines.front(); } for (size_t i = 0; i < data.size(); ++i) { out << data[i].to_string(); if (new_line_eof || i + 1 < data.size()) { out << new_lines[i % new_lines.size()]; } } } } /* anonymous namespace */