Files
scylladb/test/lib/test_utils.cc
Kefu Chai 372a4d1b79 treewide: do not define FMT_DEPRECATED_OSTREAM
since we do not rely on FMT_DEPRECATED_OSTREAM to define the
fmt::formatter for us anymore, let's stop defining `FMT_DEPRECATED_OSTREAM`.

in this change,

* utils: drop the range formatters in to_string.hh and to_string.c, as
  we don't use them anymore. and the tests for them in
  test/boost/string_format_test.cc are removed accordingly.
* utils: use fmt to print chunk_vector and small_vector. as
  we are not able to print the elements using operator<< anymore
  after switching to {fmt} formatters.
* test/boost: specialize fmt::details::is_std_string_like<bytes>
  due to a bug in {fmt} v9, {fmt} fails to format a range whose
  element type is `basic_sstring<uint8_t>`, as it considers it
  as a string-like type, but `basic_sstring<uint8_t>`'s char type
  is signed char, not char. this issue does not exist in {fmt} v10,
  so, in this change, we add a workaround to explicitly specialize
  the type trait to assure that {fmt} format this type using its
  `fmt::formatter` specialization instead of trying to format it
  as a string. also, {fmt}'s generic ranges formatter calls the
  pair formatter's `set_brackets()` and `set_separator()` methods
  when printing the range, but operator<< based formatter does not
  provide these method, we have to include this change in the change
  switching to {fmt}, otherwise the change specializing
  `fmt::details::is_std_string_like<bytes>` won't compile.
* test/boost: in tests, we use `BOOST_REQUIRE_EQUAL()` and its friends
  for comparing values. but without the operator<< based formatters,
  Boost.Test would not be able to print them. after removing
  the homebrew formatters, we need to use the generic
  `boost_test_print_type()` helper to do this job. so we are
  including `test_utils.hh` in tests so that we can print
  the formattable types.
* treewide: add "#include "utils/to_string.hh" where
  `fmt::formatter<optional<>>` is used.
* configure.py: do not define FMT_DEPRECATED_OSTREAM
* cmake: do not define FMT_DEPRECATED_OSTREAM

Refs #13245

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
2024-04-19 22:57:36 +08:00

124 lines
3.4 KiB
C++

/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "test/lib/test_utils.hh"
#include <seastar/util/file.hh>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <seastar/core/print.hh>
#include <seastar/util/backtrace.hh>
#include "test/lib/log.hh"
#include "test/lib/simple_schema.hh"
#include "utils/to_string.hh"
#include "seastarx.hh"
#include <random>
namespace tests {
namespace {
std::string format_msg(std::string_view test_function_name, bool ok, seastar::compat::source_location sl, std::string_view msg) {
return fmt::format("{}(): {} @ {}() {}:{:d}{}{}", test_function_name, ok ? "OK" : "FAIL", sl.function_name(), sl.file_name(), sl.line(), msg.empty() ? "" : ": ", msg);
}
}
bool do_check(bool condition, seastar::compat::source_location sl, std::string_view msg) {
if (condition) {
testlog.trace("{}", format_msg(__FUNCTION__, condition, sl, msg));
} else {
testlog.error("{}", format_msg(__FUNCTION__, condition, sl, msg));
}
return condition;
}
void do_require(bool condition, seastar::compat::source_location sl, std::string_view msg) {
if (condition) {
testlog.trace("{}", format_msg(__FUNCTION__, condition, sl, msg));
} else {
auto formatted_msg = format_msg(__FUNCTION__, condition, sl, msg);
testlog.error("{}", formatted_msg);
throw_with_backtrace<std::runtime_error>(std::move(formatted_msg));
}
}
void fail(std::string_view msg, seastar::compat::source_location sl) {
throw_with_backtrace<std::runtime_error>(format_msg(__FUNCTION__, false, sl, msg));
}
extern boost::test_tools::assertion_result has_scylla_test_env(boost::unit_test::test_unit_id) {
if (::getenv("SCYLLA_TEST_ENV")) {
return true;
}
testlog.info("Test environment is not configured. "
"Check test/pylib/minio_server.py for an example of how to configure the environment for it to run.");
return false;
}
}
sstring make_random_string(size_t size) {
static thread_local std::default_random_engine rng;
std::uniform_int_distribution<char> dist;
sstring str = uninitialized_string(size);
for (auto&& b : str) {
b = dist(rng);
}
return str;
}
sstring make_random_numeric_string(size_t size) {
static thread_local std::default_random_engine rng;
std::uniform_int_distribution<char> dist('0', '9');
sstring str = uninitialized_string(size);
for (auto&& b : str) {
b = dist(rng);
}
return str;
}
namespace tests {
future<bool> compare_files(std::string fa, std::string fb) {
auto cont_a = co_await util::read_entire_file_contiguous(fa);
auto cont_b = co_await util::read_entire_file_contiguous(fb);
co_return cont_a == cont_b;
}
future<> touch_file(std::string name) {
auto f = co_await open_file_dma(name, open_flags::create);
co_await f.close();
}
std::mutex boost_logger_mutex;
}
namespace std {
std::ostream& boost_test_print_type(std::ostream& os, const std::strong_ordering& order) {
fmt::print(os, "{}", order);
return os;
}
std::ostream& boost_test_print_type(std::ostream& os, const std::weak_ordering& order) {
fmt::print(os, "{}", order);
return os;
}
std::ostream& boost_test_print_type(std::ostream& os, const std::partial_ordering& order) {
fmt::print(os, "{}", order);
return os;
}
} // namespace std