9 using arr = std::array<int, 8>;
10 const arr input = {2, 3, 5, 7, 11, 13, 17, 19};
11 const arr input2 = {1, 1, 1, 1, 1, 1, 1, 1};
12 const auto unary_f = [](
int x) {
return x * x; };
13 const auto binary_f = [](
int a,
int b) {
return a - b; };
14 const arr squared = {4, 9, 25, 49, 121, 169, 289, 361};
15 const arr pminusone = {1, 2, 4, 6, 10, 12, 16, 18};
17 const auto equal = [](
auto a,
auto b) {
18 return std::equal(std::begin(
a), std::end(
a), std::begin(b), std::end(b));
21 for (
const auto& v : c) {
22 std::cout << v <<
", ";
27 SECTION(
"unary dynamic build") {
29 = kblib::build<std::vector<int>>(input.begin(), input.end(), unary_f);
30 REQUIRE(
equal(squared, built));
32 SECTION(
"binary dynamic build") {
33 auto built = kblib::build<std::vector<int>>(input.begin(), input.end(),
34 input2.begin(), binary_f);
35 REQUIRE(
equal(pminusone, built));
37 SECTION(
"unary array build") {
38 auto built = kblib::build<arr>(input.begin(), input.end(), unary_f);
39 REQUIRE(
equal(squared, built));
41 SECTION(
"binary array build") {
42 auto built = kblib::build<arr>(input.begin(), input.end(), input2.begin(),
44 REQUIRE(
equal(pminusone, built));
46 const arr
iota = {0, 1, 2, 3, 4, 5, 6, 7};
47 SECTION(
"dynamic generator build") {
48 auto built = kblib::build<std::vector<int>>(
49 [x = 0]()
mutable {
return x++; }, 8u);
52 SECTION(
"array generator build") {
53 auto built = kblib::build<arr>([x = 0]()
mutable {
return x++; });
56 SECTION(
"dynamic buildiota") {
57 auto built = kblib::buildiota<std::vector<int>>(8u, 0);
60 SECTION(
"array buildiota") {
61 auto built = kblib::buildiota<arr>(0);
67 auto equal = [](
auto r1,
auto r2) {
68 auto r1b = r1.begin();
70 auto r2b = r2.begin();
72 return (std::distance(r1b, r1e) == std::distance(r2b, r2e))
76 constexpr auto target = std::array<int, 10>{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
77 auto i1 = kblib::buildiota<std::vector<int>>(10u, 0);
78 auto i2 = kblib::buildiota<std::vector<int>>(10u, 0, 1);
79 auto i3 = kblib::buildiota<std::array<int, 10>>(0);
80 auto i4 = kblib::buildiota<std::array<int, 10>>(0, 1);
82 = kblib::buildiota<kblib::construct_with_size<std::vector<int>, 10>>(0);
84 REQUIRE((i1.size() == target.size() and
equal(i1, target)));
85 REQUIRE((i2.size() == target.size() and
equal(i2, target)));
86 REQUIRE((i3.size() == target.size() and
equal(i3, target)));
87 REQUIRE((i4.size() == target.size() and
equal(i4, target)));
88 REQUIRE((i5.size() == target.size() and
equal(i5, target)));
TEST_CASE("build family", "[build]")
Provides by-value algorithms which produce containers.
Provides generic operations for containers, as well as kblib::stack.
constexpr auto a(const std::initializer_list< T > &a) -> auto
Index an array literal without naming its type.
constexpr auto iota(ForwardIt first, ForwardIt last, T value) noexcept(noexcept(*first++=value) and noexcept(++value)) -> void
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.