Files
scylladb/test/boost/enum_set_test.cc
Kefu Chai 168ade72f8 treewide: replace formatter<std::string_view> with formatter<string_view>
in in {fmt} before v10, it provides the specialization of `fmt::formatter<..>`
for `std::string_view` as well as the specialization of `fmt::formatter<..>`
for `fmt::string_view` which is an implementation builtin in {fmt} for
compatibility of pre-C++17. and this type is used even if the code is
compiled with C++ stadandard greater or equal to C++17. also, before v10,
the `fmt::formatter<std::string_view>::format()` is defined so it accepts
`std::string_view`. after v10, `fmt::formatter<std::string_view>` still
exists, but it is now defined using `format_as()` machinery, so it's
`format()` method does not actually accept `std::string_view`, it
accepts `fmt::string_view`, as the former can be converted to
`fmt::string_view`.

this is why we can inherit from `fmt::formatter<std::string_view>` and
use `formatter<std::string_view>::format(foo, ctx);` to implement the
`format()` method with {fmt} v9, but we cannot do this with {fmt} v10,
and we would have following compilation failure:

```
FAILED: service/CMakeFiles/service.dir/RelWithDebInfo/topology_state_machine.cc.o
/home/kefu/.local/bin/clang++ -DFMT_DEPRECATED_OSTREAM -DFMT_SHARED -DSCYLLA_BUILD_MODE=release -DSEASTAR_API_LEVEL=7 -DSEASTAR_LOGGER_COMPILE_TIME_FMT -DSEASTAR_LOGGER_TYPE_STDOUT -DSEASTAR_SCHEDULING_GROUPS_COUNT=16 -DSEASTAR_SSTRING -DXXH_PRIVATE_API -DCMAKE_INTDIR=\"RelWithDebInfo\" -I/home/kefu/dev/scylladb -I/home/kefu/dev/scylladb/build/gen -I/home/kefu/dev/scylladb/seastar/include -I/home/kefu/dev/scylladb/build/seastar/gen/include -I/home/kefu/dev/scylladb/build/seastar/gen/src -ffunction-sections -fdata-sections -O3 -g -gz -std=gnu++20 -fvisibility=hidden -Wall -Werror -Wextra -Wno-error=deprecated-declarations -Wimplicit-fallthrough -Wno-c++11-narrowing -Wno-deprecated-copy -Wno-mismatched-tags -Wno-missing-field-initializers -Wno-overloaded-virtual -Wno-unsupported-friend -Wno-enum-constexpr-conversion -Wno-unused-parameter -ffile-prefix-map=/home/kefu/dev/scylladb=. -march=westmere -mllvm -inline-threshold=2500 -fno-slp-vectorize -U_FORTIFY_SOURCE -Werror=unused-result -MD -MT service/CMakeFiles/service.dir/RelWithDebInfo/topology_state_machine.cc.o -MF service/CMakeFiles/service.dir/RelWithDebInfo/topology_state_machine.cc.o.d -o service/CMakeFiles/service.dir/RelWithDebInfo/topology_state_machine.cc.o -c /home/kefu/dev/scylladb/service/topology_state_machine.cc
/home/kefu/dev/scylladb/service/topology_state_machine.cc:254:41: error: no matching member function for call to 'format'
  254 |     return formatter<std::string_view>::format(it->second, ctx);
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
/usr/include/fmt/core.h:2759:22: note: candidate function template not viable: no known conversion from 'seastar::basic_sstring<char, unsigned int, 15>' to 'const fmt::basic_string_view<char>' for 1st argument
 2759 |   FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const
      |                      ^      ~~~~~~~~~~~~
```

because the inherited `format()` method actually comes from
`fmt::formatter<fmt::string_view>`. to reduce the confusion, in this
change, we just inherit from `fmt::format<string_view>`, where
`string_view` is actually `fmt::string_view`. this follows
the document at
https://fmt.dev/latest/api.html#formatting-user-defined-types,
and since there is less indirection under the hood -- we do not
use the specialization created by `FMT_FORMAT_AS` which inherit
from `formatter<fmt::string_view>`, hopefully this can improve
the compilation speed a little bit. also, this change addresses
the build failure with {fmt} v10.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes scylladb/scylladb#18299
2024-04-19 07:44:07 +03:00

207 lines
5.8 KiB
C++

/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#define BOOST_TEST_MODULE core
#include "enum_set.hh"
#include <iterator>
#include <unordered_set>
#include <boost/test/unit_test.hpp>
#include <fmt/ostream.h>
#include <fmt/ranges.h>
enum class fruit { apple = 3, pear = 7, banana = 8 };
template <> struct fmt::formatter<fruit> : fmt::formatter<string_view> {
auto format(fruit f, fmt::format_context& ctx) const {
std::string_view name;
using enum fruit;
switch (f) {
case apple: name = "apple"; break;
case pear: name = "pear"; break;
case banana: name = "banana"; break;
}
return formatter<string_view>::format(name, ctx);
}
};
namespace std {
std::ostream& boost_test_print_type(std::ostream& os, const std::unordered_set<fruit>& fruits) {
fmt::print(os, "{}", fruits);
return os;
}
}
using fruit_enum = super_enum<fruit, fruit::apple, fruit::pear, fruit::banana>;
//
// `super_enum`
//
BOOST_AUTO_TEST_CASE(enum_max_sequence) {
BOOST_REQUIRE_EQUAL(fruit_enum::max_sequence, 8);
}
BOOST_AUTO_TEST_CASE(enum_min_sequence) {
BOOST_REQUIRE_EQUAL(fruit_enum::min_sequence, 3);
}
BOOST_AUTO_TEST_CASE(enum_valid_sequence) {
BOOST_REQUIRE(fruit_enum::is_valid_sequence(3));
BOOST_REQUIRE(fruit_enum::is_valid_sequence(7));
BOOST_REQUIRE(fruit_enum::is_valid_sequence(8));
BOOST_REQUIRE(!fruit_enum::is_valid_sequence(0));
BOOST_REQUIRE(!fruit_enum::is_valid_sequence(4));
BOOST_REQUIRE(!fruit_enum::is_valid_sequence(9));
}
//
// `enum_set`
//
using fruit_set = enum_set<fruit_enum>;
BOOST_AUTO_TEST_CASE(set_contains) {
const auto fs = fruit_set::of<fruit::apple, fruit::banana>();
BOOST_REQUIRE(fs.contains(fruit::apple));
BOOST_REQUIRE(!fs.contains(fruit::pear));
}
BOOST_AUTO_TEST_CASE(full_set) {
const auto fs = fruit_set::full();
BOOST_REQUIRE(fs.contains(fruit::apple));
BOOST_REQUIRE(fs.contains(fruit::pear));
BOOST_REQUIRE(fs.contains(fruit::banana));
}
BOOST_AUTO_TEST_CASE(set_from_mask) {
const auto fs = fruit_set::of<fruit::apple, fruit::banana>();
BOOST_REQUIRE_EQUAL(fs.mask(), fruit_set::from_mask(fs.mask()).mask());
BOOST_REQUIRE_THROW(fruit_set::from_mask(0xdead), bad_enum_set_mask);
}
BOOST_AUTO_TEST_CASE(set_enable) {
auto fs = fruit_set();
fs.set(fruit::apple);
BOOST_REQUIRE(fs.contains(fruit::apple));
}
BOOST_AUTO_TEST_CASE(set_enable_if) {
auto fs = fruit_set();
fs.set_if<fruit::apple>(false);
BOOST_REQUIRE(!fs.contains(fruit::apple));
fs.set_if<fruit::apple>(true);
BOOST_REQUIRE(fs.contains(fruit::apple));
}
BOOST_AUTO_TEST_CASE(set_remove) {
auto fs = fruit_set::of<fruit::pear>();
fs.remove(fruit::pear);
BOOST_REQUIRE(!fs.contains(fruit::pear));
}
BOOST_AUTO_TEST_CASE(set_iterator) {
const auto fs1 = fruit_set::of<fruit::pear, fruit::banana>();
std::unordered_set<fruit> us1;
std::copy(fs1.begin(), fs1.end(), std::inserter(us1, us1.begin()));
BOOST_REQUIRE_EQUAL(us1, (std::unordered_set<fruit>{fruit::pear, fruit::banana}));
//
// Empty set.
//
const auto fs2 = fruit_set();
std::unordered_set<fruit> us2;
std::copy(fs2.begin(), fs2.end(), std::inserter(us2, us2.begin()));
BOOST_REQUIRE(us2.empty());
}
BOOST_AUTO_TEST_CASE(set_add) {
auto fs0 = fruit_set();
auto fs1 = fruit_set::of<fruit::pear>();
auto fs2 = fruit_set::of<fruit::apple, fruit::banana>();
auto fs3 = fruit_set::of<fruit::apple>();
fs1.add(fs2);
fs3.add(fs2);
BOOST_REQUIRE(fs1.contains(fruit::pear) && fs1.contains(fruit::apple) && fs1.contains(fruit::banana));
BOOST_REQUIRE(!fs3.contains(fruit::pear) && fs3.contains(fruit::apple) && fs3.contains(fruit::banana));
fs1.add(fs0);
fs3.add(fs0);
BOOST_REQUIRE(fs1.contains(fruit::pear) && fs1.contains(fruit::apple) && fs1.contains(fruit::banana));
BOOST_REQUIRE(!fs3.contains(fruit::pear) && fs3.contains(fruit::apple) && fs3.contains(fruit::banana));
fs0.add(fruit_set::of<fruit::apple>());
BOOST_REQUIRE(!fs0.contains(fruit::pear) && fs0.contains(fruit::apple) && !fs0.contains(fruit::banana));
}
BOOST_AUTO_TEST_CASE(set_toggle) {
auto fs = fruit_set();
fs.set<fruit::pear>();
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
fs.toggle<fruit::pear>();
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(!fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
fs.toggle<fruit::pear>();
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
fs.toggle(fruit::pear);
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(!fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
fs.toggle(fruit::pear);
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
fs.toggle<fruit::banana>();
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(fs.contains<fruit::banana>());
fs.toggle<fruit::apple>();
BOOST_REQUIRE(fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(fs.contains<fruit::banana>());
fs.toggle(fruit::banana);
BOOST_REQUIRE(fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
fs.toggle(fruit::apple);
BOOST_REQUIRE(!fs.contains<fruit::apple>());
BOOST_REQUIRE(fs.contains<fruit::pear>());
BOOST_REQUIRE(!fs.contains<fruit::banana>());
}