Audacity 3.2.0
Classes | Namespaces | Typedefs | Functions | Variables
CompositeTest.cpp File Reference
#include <catch2/catch.hpp>
#include "Composite.h"
#include "Callable.h"
#include <algorithm>
#include <array>
#include <functional>
#include <iterator>
#include <numeric>
Include dependency graph for CompositeTest.cpp:

Go to the source code of this file.

Classes

struct  anonymous_namespace{CompositeTest.cpp}::Ignore
 
struct  anonymous_namespace{CompositeTest.cpp}::MyComponent
 
struct  anonymous_namespace{CompositeTest.cpp}::MyComponentEx
 
struct  Composite::Traits< MyCompositeBase, MyBuilder >
 
struct  Composite::Traits< MyCompositeBase, MyBuilder >::ItemBuilderType
 
struct  anonymous_namespace{CompositeTest.cpp}::MyBuilder
 

Namespaces

namespace  anonymous_namespace{CompositeTest.cpp}
 

Typedefs

using anonymous_namespace{CompositeTest.cpp}::MyCompositeBase = Base< MyComponent, unique_ptr< MyComponent >, int >
 
using anonymous_namespace{CompositeTest.cpp}::MyCompositeBase2 = Base< MyComponent, unique_ptr< MyComponent >, int, Ignore >
 

Functions

bool anonymous_namespace{CompositeTest.cpp}::operator== (int n, const unique_ptr< MyComponent > &p)
 
template<bool members = true, typename Container1 , typename Container2 >
bool anonymous_namespace{CompositeTest.cpp}::compareSequences (const Container1 &c1, const Container2 &c2)
 
template<typename Container , auto Maker = nullptr, typename... Args>
void DoTest (Args ...args)
 
 TEST_CASE ("Composite::Base")
 
bool anonymous_namespace{CompositeTest.cpp}::operator== (int n, const std::unique_ptr< MyComponentEx > &p)
 
static auto anonymous_namespace{CompositeTest.cpp}::Maker (int value)
 
 TEST_CASE ("Composite::Builder")
 
 TEST_CASE ("Composite::Extension")
 
 TEST_CASE ("Composite::Extension specialized for void")
 

Variables

constexpr auto anonymous_namespace{CompositeTest.cpp}::Component = Callable::UniqueMaker<MyComponent, int>()
 

Function Documentation

◆ DoTest()

template<typename Container , auto Maker = nullptr, typename... Args>
void DoTest ( Args ...  args)

Definition at line 75 of file CompositeTest.cpp.

76{
77 auto Make = [](int value){
78 if constexpr (!bool(Maker))
79 return Component(value, Ignore{});
80 else
81 return Maker(value);
82 };
83
84 using ComponentType = decltype(Make(0));
85
86 // CompositeBase passes constructor arguments to its Component
87 Container container{ args... };
88 REQUIRE(0 == container);
89 REQUIRE(container.empty());
90
91 constexpr int N = 4;
92
93 // Values for comparison
94 vector<int> values(N);
95 iota(values.begin(), values.end(), 1);
96
97 // Make some components
98 vector<ComponentType> components;
99 // Not yet equal
100 REQUIRE(!compareSequences(values, components));
101
102 for (size_t ii = 1; ii <= N; ++ii)
103 components.push_back(Make(ii));
104
105 // Equal values so far
106 if (!bool(Maker))
107 REQUIRE(compareSequences(values, components));
108
109 // Composite works with push_back and back_inserter
110 move(components.begin(), components.end(), back_inserter(container));
111 REQUIRE(!container.empty());
112 REQUIRE(compareSequences(values, container));
113
114 // Break equality of sequences
115 values.push_back(N + 1);
116 REQUIRE(!compareSequences(values, container));
117
118 // Restore equality (and note, Component can take more arguments)
119 container.push_back(Make(N + 1));
120 REQUIRE(compareSequences(values, container));
121}
const wxChar * values
bool compareSequences(const Container1 &c1, const Container2 &c2)

References anonymous_namespace{CompositeTest.cpp}::compareSequences(), anonymous_namespace{CompositeTest.cpp}::Component, anonymous_namespace{CompositeTest.cpp}::Maker(), and values.

Here is the call graph for this function:

◆ TEST_CASE() [1/4]

TEST_CASE ( "Composite::Base"  )

Definition at line 123 of file CompositeTest.cpp.

124{
125 DoTest<MyCompositeBase>(0);
126 // Also test the extra arguments of MyComponent
127 DoTest<MyCompositeBase2>(0, Ignore{});
128}

◆ TEST_CASE() [2/4]

TEST_CASE ( "Composite::Builder"  )

Definition at line 163 of file CompositeTest.cpp.

164{
165 using namespace placeholders;
166 std::array values{ 0, 1, 2, 3, 4 };
167
168 DoTest<MyBuilder, Maker>(0);
169 // Test compilation of the variadic constructor and the overload of
170 // ItemBuilderType::operator ()
171 // Remember first 0 is not an element
172 MyBuilder builder{ 0, 0, 1, 2, 3, 4 };
173
174 REQUIRE(compareSequences(values, builder));
175
176 // Forward range
177 MyBuilder builder2{ 0, begin(values), end(values) };
178 REQUIRE(compareSequences(values, builder2));
179
180 // Transform range
181 std::array values2{ 1, 2, 3, 4, 5 };
182 MyBuilder builder3{ 0, begin(values2), end(values2),
183 bind( minus{}, _1, -1 ) };
184 REQUIRE(compareSequences(values, builder2));
185}
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101

References details::begin(), anonymous_namespace{CompositeTest.cpp}::compareSequences(), details::end(), and values.

Here is the call graph for this function:

◆ TEST_CASE() [3/4]

TEST_CASE ( "Composite::Extension specialized for void"  )

Definition at line 196 of file CompositeTest.cpp.

197{
199 DoTest<Container>(0);
201 DoTest<Container2>(0, Ignore{});
202}
Extend Base with extra fields, in a second, protected base class.
Definition: Composite.h:177

◆ TEST_CASE() [4/4]

TEST_CASE ( "Composite::Extension"  )

Definition at line 187 of file CompositeTest.cpp.

188{
189 struct X{};
190 using Container = Extension<MyCompositeBase, X, int>;
191 DoTest<Container>(0, X{});
193 DoTest<Container2>(0, Ignore{}, X{});
194}