2021-01-23 21:39:18 +01:00
|
|
|
#pragma once
|
2023-07-29 20:41:31 +02:00
|
|
|
#include <ctime>
|
2023-08-08 14:11:51 +02:00
|
|
|
#include <filesystem>
|
2023-07-29 20:41:31 +02:00
|
|
|
#include <iomanip>
|
2023-08-03 21:28:41 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-01-23 21:39:18 +01:00
|
|
|
|
2023-08-08 02:08:31 +02:00
|
|
|
#ifdef CMAKE_GITHUB_CI
|
2021-01-23 21:39:18 +01:00
|
|
|
#include <doctest/doctest.h>
|
|
|
|
#else
|
|
|
|
#include <doctest.h>
|
|
|
|
#endif
|
|
|
|
|
2023-07-29 20:41:31 +02:00
|
|
|
namespace {
|
2021-02-20 15:53:18 +01:00
|
|
|
struct buffer {
|
2023-07-29 20:41:31 +02:00
|
|
|
std::string data_;
|
|
|
|
|
|
|
|
char* operator()(const std::string& data) {
|
|
|
|
data_ = data;
|
|
|
|
return data_.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
char* append(const std::string& data) {
|
|
|
|
data_ += data;
|
|
|
|
return data_.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
char* append_overwrite_last(const std::string& data, size_t size) {
|
|
|
|
data_.resize(data_.size() - size);
|
|
|
|
return append(data);
|
|
|
|
}
|
2021-01-23 21:39:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
[[maybe_unused]] inline buffer buff;
|
2022-03-30 19:54:50 +02:00
|
|
|
|
2023-07-29 20:41:31 +02:00
|
|
|
std::string time_now_rand() {
|
|
|
|
std::stringstream ss;
|
|
|
|
auto t = std::time(nullptr);
|
|
|
|
auto tm = *std::localtime(&t);
|
|
|
|
ss << std::put_time(&tm, "%d%m%Y%H%M%S");
|
|
|
|
srand(time(nullptr));
|
|
|
|
return ss.str() + std::to_string(rand());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct unique_file_name {
|
|
|
|
static inline int i = 0;
|
|
|
|
|
|
|
|
const std::string name;
|
|
|
|
|
|
|
|
unique_file_name(const std::string& test)
|
2023-08-03 21:28:41 +02:00
|
|
|
: name{"random_" + test + "_" + std::to_string(i++) + "_" +
|
|
|
|
time_now_rand() + "_file.csv"} {
|
2023-07-29 20:41:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~unique_file_name() {
|
2023-08-03 21:28:41 +02:00
|
|
|
std::filesystem::remove(name);
|
2023-07-29 20:41:31 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-30 19:54:50 +02:00
|
|
|
#define CHECK_FLOATING_CONVERSION(input, type) \
|
2023-07-29 20:41:31 +02:00
|
|
|
{ \
|
|
|
|
auto eps = std::numeric_limits<type>::min(); \
|
|
|
|
std::string s = #input; \
|
|
|
|
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
|
|
|
REQUIRE(t.has_value()); \
|
|
|
|
CHECK_LT(std::abs(t.value() - type(input)), eps); \
|
|
|
|
} \
|
|
|
|
{ \
|
|
|
|
/* check negative too */ \
|
|
|
|
auto eps = std::numeric_limits<type>::min(); \
|
|
|
|
auto s = std::string("-") + #input; \
|
|
|
|
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
|
|
|
REQUIRE(t.has_value()); \
|
|
|
|
CHECK_LT(std::abs(t.value() - type(-input)), eps); \
|
|
|
|
}
|
2022-03-30 19:54:50 +02:00
|
|
|
|
2023-08-08 14:11:51 +02:00
|
|
|
#define CHECK_FLOATING_CONVERSION_LONG_NUMBER(STRING_NUMBER, TYPE, CONVERTER) \
|
|
|
|
{ \
|
|
|
|
auto begin = STRING_NUMBER.c_str(); \
|
|
|
|
auto end = begin + STRING_NUMBER.size(); \
|
|
|
|
\
|
|
|
|
auto number = ss::to_num<TYPE>(begin, end); \
|
|
|
|
REQUIRE(number.has_value()); \
|
|
|
|
\
|
|
|
|
auto expected_number = CONVERTER(STRING_NUMBER); \
|
|
|
|
CHECK_EQ(number.value(), expected_number); \
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:54:50 +02:00
|
|
|
#define CHECK_INVALID_CONVERSION(input, type) \
|
2023-07-29 20:41:31 +02:00
|
|
|
{ \
|
|
|
|
std::string s = input; \
|
|
|
|
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
|
|
|
|
CHECK_FALSE(t.has_value()); \
|
|
|
|
}
|
2022-03-30 19:54:50 +02:00
|
|
|
|
|
|
|
#define REQUIRE_VARIANT(var, el, type) \
|
2023-07-29 20:41:31 +02:00
|
|
|
{ \
|
|
|
|
auto ptr = std::get_if<type>(&var); \
|
|
|
|
REQUIRE(ptr); \
|
|
|
|
REQUIRE_EQ(el, *ptr); \
|
|
|
|
}
|
2022-03-30 19:54:50 +02:00
|
|
|
|
|
|
|
#define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative<type>(var));
|
2023-07-09 17:11:52 +02:00
|
|
|
|
|
|
|
#define REQUIRE_EXCEPTION(...) \
|
|
|
|
try { \
|
|
|
|
__VA_ARGS__; \
|
|
|
|
FAIL("Expected exception"); \
|
|
|
|
} catch (ss::exception & e) { \
|
|
|
|
CHECK_FALSE(std::string{e.what()}.empty()); \
|
|
|
|
}
|
2023-07-28 00:17:22 +02:00
|
|
|
|
|
|
|
template <typename T>
|
2023-07-29 20:41:31 +02:00
|
|
|
std::vector<std::vector<T>> vector_combinations(const std::vector<T>& v,
|
|
|
|
size_t n) {
|
2023-07-28 00:17:22 +02:00
|
|
|
std::vector<std::vector<T>> ret;
|
|
|
|
if (n <= 1) {
|
|
|
|
for (const auto& i : v) {
|
|
|
|
ret.push_back({i});
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto inner_combinations = vector_combinations(v, n - 1);
|
|
|
|
for (const auto& i : v) {
|
|
|
|
for (auto j : inner_combinations) {
|
|
|
|
j.insert(j.begin(), i);
|
|
|
|
ret.push_back(move(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2023-07-29 20:41:31 +02:00
|
|
|
} /* namespace */
|