1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************
Audacity: A Digital Audio Editor
CompositeTest.cpp
Paul Licameli
**********************************************************************/
#include <catch2/catch.hpp>
#include "Composite.h"
#include "Callable.h"
#include <algorithm>
#include <array>
#include <functional>
#include <iterator>
#include <numeric>
using namespace Composite;
using namespace std;
namespace {
struct Ignore{};
struct MyComponent {
MyComponent(int value) : value{ value } {}
MyComponent(int value, Ignore ignored) : value{ value } {}
virtual ~MyComponent() = default;
const int value;
operator int() const { return value; }
};
constexpr auto Component = Callable::UniqueMaker<MyComponent, int>();
using MyCompositeBase = Base<MyComponent, unique_ptr<MyComponent>, int>;
using MyCompositeBase2 =
Base<MyComponent, unique_ptr<MyComponent>, int, Ignore>;
inline bool operator== (int n, const unique_ptr<MyComponent> &p)
{
return n == *p;
}
// Test that two sequences are equal, several ways, which also exercises
// compilation of all the STL style accessors
template<bool members = true, typename Container1, typename Container2>
bool compareSequences(const Container1 &c1, const Container2 &c2)
{
bool result = true;
if constexpr(members) {
result =
(equal(c1.begin(), c1.end(), c2.begin(), c2.end()))
&&
(equal(c1.cbegin(), c1.cend(), c2.cbegin(), c2.cend()))
&&
(equal(c1.rbegin(), c1.rend(), c2.rbegin(), c2.rend()))
&&
(equal(c1.crbegin(), c1.crend(), c2.crbegin(), c2.crend()));
}
result = result &&
(equal(begin(c1), end(c1), begin(c2), end(c2)))
&&
(equal(cbegin(c1), cend(c1), cbegin(c2), cend(c2)))
&&
(equal(rbegin(c1), rend(c1), rbegin(c2), rend(c2)))
&&
(equal(crbegin(c1), crend(c1), crbegin(c2), crend(c2)))
;
return result;
}
}
template<typename Container, auto Maker = nullptr, typename... Args>
void DoTest(Args ...args)
{
auto Make = [](int value){
if constexpr (!bool(Maker))
return Component(value, Ignore{});
else
return Maker(value);
};
using ComponentType = decltype(Make(0));
// CompositeBase passes constructor arguments to its Component
Container container{ args... };
REQUIRE(0 == container);
REQUIRE(container.empty());
constexpr int N = 4;
// Values for comparison
vector<int> values(N);
iota(values.begin(), values.end(), 1);
// Make some components
vector<ComponentType> components;
// Not yet equal
REQUIRE(!compareSequences(values, components));
for (size_t ii = 1; ii <= N; ++ii)
components.push_back(Make(ii));
// Equal values so far
if (!bool(Maker))
REQUIRE(compareSequences(values, components));
// Composite works with push_back and back_inserter
move(components.begin(), components.end(), back_inserter(container));
REQUIRE(!container.empty());
REQUIRE(compareSequences(values, container));
// Break equality of sequences
values.push_back(N + 1);
REQUIRE(!compareSequences(values, container));
// Restore equality (and note, Component can take more arguments)
container.push_back(Make(N + 1));
REQUIRE(compareSequences(values, container));
}
TEST_CASE("Composite::Base")
{
DoTest<MyCompositeBase>(0);
// Also test the extra arguments of MyComponent
DoTest<MyCompositeBase2>(0, Ignore{});
}
namespace {
struct MyComponentEx : MyComponent{
// Scramble the given value!!
MyComponentEx(int value) : MyComponent{ -value } {}
};
inline bool operator== (int n, const std::unique_ptr<MyComponentEx> &p)
{
return n == *p;
}
static auto Maker (int value){return std::make_unique<MyComponentEx>(value); };
struct MyBuilder;
}
// But define a trait that negates it again to unscramble it!
// This specialization must be in the global namespace and precede the complete
// definition of Builder
template<> struct Composite::Traits<MyCompositeBase, MyBuilder> {
struct ItemBuilderType {
auto operator () (std::unique_ptr<MyComponent> ptr) const {
return std::make_unique<MyComponentEx>(ptr->value); };
auto operator () (int value) const {
return std::make_unique<MyComponentEx>(-value); };
};
static constexpr auto ItemBuilder = ItemBuilderType{};
template<typename T> static constexpr auto enables_item_type_v = true;
};
namespace {
struct MyBuilder : Composite::Builder<MyCompositeBase, MyBuilder, int> {
using Builder::Builder;
};
}
TEST_CASE("Composite::Builder")
{
using namespace placeholders;
std::array values{ 0, 1, 2, 3, 4 };
DoTest<MyBuilder, Maker>(0);
// Test compilation of the variadic constructor and the overload of
// ItemBuilderType::operator ()
// Remember first 0 is not an element
MyBuilder builder{ 0, 0, 1, 2, 3, 4 };
REQUIRE(compareSequences(values, builder));
// Forward range
MyBuilder builder2{ 0, begin(values), end(values) };
REQUIRE(compareSequences(values, builder2));
// Transform range
std::array values2{ 1, 2, 3, 4, 5 };
MyBuilder builder3{ 0, begin(values2), end(values2),
bind( minus{}, _1, -1 ) };
REQUIRE(compareSequences(values, builder2));
}
TEST_CASE("Composite::Extension")
{
struct X{};
using Container = Extension<MyCompositeBase, X, int>;
DoTest<Container>(0, X{});
using Container2 = Extension<MyCompositeBase2, X, int, Ignore>;
DoTest<Container2>(0, Ignore{}, X{});
}
TEST_CASE("Composite::Extension specialized for void")
{
using Container = Extension<MyCompositeBase, void, int>;
DoTest<Container>(0);
using Container2 = Extension<MyCompositeBase2, void, int, Ignore>;
DoTest<Container2>(0, Ignore{});
}
|