From b7e1c1d59c374f1707d7a7207211a5f8a438ee1d Mon Sep 17 00:00:00 2001 From: Casey Bodley Date: Tue, 14 Nov 2023 14:43:25 -0500 Subject: [PATCH] common: add generate_test_instances() for std::variant Signed-off-by: Casey Bodley --- src/common/versioned_variant.h | 19 +++++++++++++++++++ src/test/common/test_versioned_variant.cc | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/common/versioned_variant.h b/src/common/versioned_variant.h index 9c9c5ada9b013..f7e46b7296e7f 100644 --- a/src/common/versioned_variant.h +++ b/src/common/versioned_variant.h @@ -16,6 +16,7 @@ #include #include +#include #include #include // for mp_with_index @@ -212,4 +213,22 @@ void decode(std::variant& v, bufferlist::const_iterator& p) } // namespace converted_variant + +/// \brief Generate a list with a default-constructed variant of each type. +/// +/// This can be used in generate_test_instances() for types that contain +/// variants to ensure that an encoding of each type is present in the +/// ceph-object-corpus. This allows the ceph-dencoder tests to catch any +/// breaking changes to the variant types that are present in encodings. +template +void generate_test_instances(std::list>& instances) +{ + // use an immediately-invoked lambda to get a parameter pack of variant indices + [&instances] (std::index_sequence) { + // use a fold expression to call emplace_back() for each index in the pack + // use in_place_index to default-construct a variant of the type at index I + (instances.emplace_back(std::in_place_index), ...); + } (std::make_index_sequence{}); +} + } // namespace ceph diff --git a/src/test/common/test_versioned_variant.cc b/src/test/common/test_versioned_variant.cc index b91e24b6f13fe..81f12c23c2b4f 100644 --- a/src/test/common/test_versioned_variant.cc +++ b/src/test/common/test_versioned_variant.cc @@ -13,6 +13,7 @@ */ #include "common/versioned_variant.h" +#include #include #include @@ -320,4 +321,21 @@ TEST(ConvertedVariant, EncodeNew) } } +TEST(Variant, GenerateTestInstances) +{ + using Variant = std::variant; + + std::bitset> bits; + ASSERT_TRUE(bits.none()); + + std::list instances; + generate_test_instances(instances); + + for (const auto& v : instances) { + bits.set(v.index()); + } + + EXPECT_TRUE(bits.all()); +} + } // namespace ceph -- 2.39.5