kblib 0.2.3
General utilities library for modern C++
build.cpp
Go to the documentation of this file.
1#include "kblib/build.h"
2#include "catch.hpp"
3#include <array>
4#include <iostream>
5
6#include "kblib/containers.h"
7
8TEST_CASE("build family", "[build]") {
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};
16
17 const auto equal = [](auto a, auto b) {
18 return std::equal(std::begin(a), std::end(a), std::begin(b), std::end(b));
19 };
20 KBLIB_UNUSED auto print_arr = [&](auto c) {
21 for (const auto& v : c) {
22 std::cout << v << ", ";
23 }
24 std::cout << '\n';
25 };
26
27 SECTION("unary dynamic build") {
28 auto built
29 = kblib::build<std::vector<int>>(input.begin(), input.end(), unary_f);
30 REQUIRE(equal(squared, built));
31 }
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));
36 }
37 SECTION("unary array build") {
38 auto built = kblib::build<arr>(input.begin(), input.end(), unary_f);
39 REQUIRE(equal(squared, built));
40 }
41 SECTION("binary array build") {
42 auto built = kblib::build<arr>(input.begin(), input.end(), input2.begin(),
43 binary_f);
44 REQUIRE(equal(pminusone, built));
45 }
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);
50 REQUIRE(equal(iota, built));
51 }
52 SECTION("array generator build") {
53 auto built = kblib::build<arr>([x = 0]() mutable { return x++; });
54 REQUIRE(equal(iota, built));
55 }
56 SECTION("dynamic buildiota") {
57 auto built = kblib::buildiota<std::vector<int>>(8u, 0);
58 REQUIRE(equal(iota, built));
59 }
60 SECTION("array buildiota") {
61 auto built = kblib::buildiota<arr>(0);
62 REQUIRE(equal(iota, built));
63 }
64}
65
66TEST_CASE("buildiota") {
67 auto equal = [](auto r1, auto r2) {
68 auto r1b = r1.begin();
69 auto r1e = r1.end();
70 auto r2b = r2.begin();
71 auto r2e = r2.end();
72 return (std::distance(r1b, r1e) == std::distance(r2b, r2e))
73 and kblib::equal(r1b, r1e, r2b);
74 };
75
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);
81 auto i5
82 = kblib::buildiota<kblib::construct_with_size<std::vector<int>, 10>>(0);
83
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)));
89}
90
91TEST_CASE("build_copy family") {
93}
94
95TEST_CASE("build.h iterators") {
97}
TEST_CASE("build family", "[build]")
Definition: build.cpp:8
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.
Definition: simple.h:255
constexpr auto iota(ForwardIt first, ForwardIt last, T value) noexcept(noexcept(*first++=value) and noexcept(++value)) -> void
Definition: algorithm.h:1575
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
Definition: fakestd.h:966
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.
Definition: tdecl.h:130