c++catch2

C++ Catch2 : Generate a array with random values


I try to implement a generator in catch2 of an std::array of 1024 random values. Below my code

#include "catch2/catch_all.hpp"
#include <array>
#include <vector>

    TEST_CASE("Foo") {
        auto s = GENERATE(as<std::array<char, 1024> >{}, 'a');
        REQUIRE(!s.empty());
    }

The following error is generated

 In file included from /opt/compiler-explorer/libs/catch2/v3.0.0-preview3/src/catch2/generators/catch_generators_all.hpp:25,
                 from /opt/compiler-explorer/libs/catch2/v3.0.0-preview3/src/catch2/catch_all.hpp:46,
                 from <source>:1:
/opt/compiler-explorer/libs/catch2/v3.0.0-preview3/src/catch2/generators/catch_generators.hpp: In instantiation of 'Catch::Generators::Generators<T> Catch::Generators::makeGenerators(as<T>, U&&, Gs&& ...) [with T = std::array<char, 1024>; U = char; Gs = {}]':
<source>:7:14:   required from here
/opt/compiler-explorer/libs/catch2/v3.0.0-preview3/src/catch2/generators/catch_generators.hpp:181:39: error: array must be initialized with a brace-enclosed initializer
  181 |         return makeGenerators( value( T( std::forward<U>( val ) ) ), std::forward<Gs>( moreGenerators )... );
      |   

Any idea how to overcome this issue?

Example


Solution

  • The problem is that the macro GENERATE is using the wrong kind of initialization for array types, you can create a simple type to translate it like this (full example)

    template<typename T, std::size_t N>
    struct array_wrapper : std::array<T, N>
    {
        template<typename... ArgTs>
        constexpr array_wrapper(ArgTs&&... args)
            : std::array<T, N>{std::forward<ArgTs>(args)...}
        {
        }
    };
    

    Note that you would either assign it to a variable of type std::array<T, N> (as opposed to auto) or cast it to the base type if you need a "pure" std::array. Talue is implicitly convertible to std::array due to public inheritance anyway, so it can be used as one.