From 05a08a7b6a2e6c9694377e69b0cb811faa1b4818 Mon Sep 17 00:00:00 2001 From: Tuomo Kriikkula Date: Wed, 26 Jun 2024 10:37:38 +0300 Subject: [PATCH] Attempts to fix MSVC meta builds (WIP) --- templates/cpp_meta.jinja | 4 +-- tests/test_randomized.cpp | 74 +++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/templates/cpp_meta.jinja b/templates/cpp_meta.jinja index 194d0a5..6beef51 100644 --- a/templates/cpp_meta.jinja +++ b/templates/cpp_meta.jinja @@ -148,7 +148,7 @@ template } template -[[nodiscard]] constexpr auto to_string() +[[nodiscard]] constexpr auto to_string() noexcept { if constexpr (MT == ::{{ cpp_namespace }}::MessageType::None) { @@ -240,7 +240,7 @@ public: } } - [[nodiscard]] static constexpr auto field_count() + [[nodiscard]] static constexpr size_t field_count() noexcept { if constexpr (MT == ::{{ cpp_namespace }}::MessageType::None) { diff --git a/tests/test_randomized.cpp b/tests/test_randomized.cpp index dd4bdbe..eb00d4d 100644 --- a/tests/test_randomized.cpp +++ b/tests/test_randomized.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -52,6 +53,43 @@ namespace { +/* Generate a compile-time sequence of Integers from Start to End + * + * typename IntegralType : the type of the sequence + * End: end of the sequence + * Start(0): beginning of the sequence + * Step(1): optionally define an amount to increment by + * + * The same parameters for the range<> variable template also + * perform the same function + */ +template +struct integer_range: std::conditional< + std::integral_constant= End>::value, // Check if we're at the end + std::integer_sequence, // End of the sequence + integer_range // Add the next element +>::type +{ +}; // Result type is an std::integer_sequence<...> + +// Variable template for constructing a range +template +constexpr auto integer_range_v = integer_range(); + +// Create an std::array from some sequence +template +constexpr auto create_array(std::integer_sequence) +{ + return std::array{Seq...}; +} + +// Create an std::array for a range +template +constexpr auto range = create_array( + integer_range_v); + template constexpr uint64_t get_rand() { @@ -67,7 +105,7 @@ constexpr std::u16string get_rand_str(std::integer_sequence sl { std::u16string str_in; str_in.reserve(slen_seq.size()); - boost::hana::for_each(slen_seq, [&](const auto i) + boost::hana::for_each(slen_seq, [&str_in](const auto i) { constexpr auto i64 = static_cast(i); constexpr auto rnd = get_rand(); @@ -135,22 +173,29 @@ TEST_CASE("test TestMessages messages with randomized data") constexpr auto rounds = std::make_integer_sequence(); constexpr auto mts = meta::message_types(); - constexpr auto seq = std::make_integer_sequence(); + constexpr auto seq = std::make_integer_sequence(); + // constexpr auto seq = range; + constexpr auto seq_size = seq.size(); // "Fire up" the RNG. TODO: does this actually help with anything? constexpr uint64_t r_initial = get_rand<0, 0>(); std::cout << std::format("*** begin RNG tests, r_initial={}, kiss_seed={} ***\n\n", r_initial, ::umb::meta::rng::kiss_seed); - boost::hana::for_each(rounds, [&](const auto round) + boost::hana::for_each(rounds, [ + &seq = std::as_const(seq), + seq_size + ](const auto round) { - std::cout << std::format("\n####### round: {}\n", static_cast(round)); + std::cout << std::format("\n####### round: {}\n", static_cast(round)); - boost::hana::for_each(seq, [&](const auto index) + boost::hana::for_each(seq, [round, seq_size](const auto index) { - constexpr auto i = static_cast(index); - constexpr auto mt = static_cast<::testmessages::umb::MessageType>(i); + constexpr auto idx = decltype(index)::value; + constexpr auto mt = static_cast<::testmessages::umb::MessageType>(idx); constexpr auto field_count = meta::Message::field_count(); + constexpr auto field_seq = std::make_integer_sequence(); + constexpr auto field_seq_size = field_seq.size(); // Skip MessageType::None. // TODO: is there a constexpr way to specify start index for @@ -158,17 +203,18 @@ TEST_CASE("test TestMessages messages with randomized data") if constexpr (index > 0) { std::cout << std::format("index={}, mt={}, field_count={}\n", - std::to_string(index), + std::to_string(idx), meta::to_string(), field_count); std::shared_ptr<::umb::Message> message = meta::make_shared_message(); - constexpr auto field_seq = std::make_integer_sequence(); - boost::hana::for_each(field_seq, [&](const auto findex) + boost::hana::for_each(field_seq, [ + round, idx, seq_size, message, field_seq_size, mt + ](const auto findex) { constexpr auto field = meta::Message::template field(); - constexpr auto rng_iter = - findex + field_seq.size() * (index + seq.size() * round); + constexpr uint64_t rng_iter = + findex + field_seq_size * (idx + seq_size * round); std::cout << std::format("-- field.name={}\n", field.name); constexpr uint64_t r = get_rand(); @@ -214,7 +260,7 @@ TEST_CASE("test TestMessages messages with randomized data") } else { - REQUIRE(float_in == doctest::Approx(float_val)); + REQUIRE((float_in == doctest::Approx(float_val))); } } else if constexpr (field.type == ::umb::meta::FieldType::String) @@ -257,7 +303,7 @@ TEST_CASE("test TestMessages messages with randomized data") bool ok = m2->from_bytes(bytes); CHECK(ok); const auto bytes2 = m2->to_bytes(); - CHECK_MESSAGE(bytes == bytes2, byte_cmp_msg(bytes, bytes2)); + CHECK_MESSAGE((bytes == bytes2), byte_cmp_msg(bytes, bytes2)); CHECK_EQ(*message, *m2); // TODO: meta comparison functions?