kblib 0.2.3
General utilities library for modern C++
simple.cpp
Go to the documentation of this file.
1#include "kblib/simple.h"
2#include "catch.hpp"
3#include "kblib/stats.h"
4
5template <typename>
6struct print;
7
8TEST_CASE("enumerate") {
9 std::vector<unsigned long long> persistent{0, 1, 1, 2, 3, 5, 8};
10 SECTION("lvalue") {
11 for (auto t : kblib::enumerate(persistent)) {
12 auto& v = std::get<0>(t);
13 auto i = static_cast<int>(std::get<1>(t));
14 REQUIRE(v == kblib::fibonacci(i));
15 }
16 }
17 SECTION("const lvalue") {
18 const auto& cp = persistent;
19 for (auto t : kblib::enumerate(cp)) {
20 auto& v = std::get<0>(t);
21 auto i = static_cast<int>(std::get<1>(t));
22 REQUIRE(v == kblib::fibonacci(i));
23 static_assert(
24 std::is_const<
25 typename std::remove_reference<decltype(v)>::type>::value,
26 "v must refer to const when the range is const");
27 }
28 }
29
30 SECTION("iterators") {
31 for (auto t : kblib::enumerate(persistent.cbegin(), persistent.cend())) {
32 auto& v = std::get<0>(t);
33 auto i = static_cast<int>(std::get<1>(t));
34 REQUIRE(v == kblib::fibonacci(i));
35 }
36 }
37 SECTION("mutable iterators") {
38 std::vector<int> range{0, 1, 2, 3, 4, 5, 6, 7};
39 for (auto t : kblib::enumerate(range.begin(), range.end())) {
40 auto& v = std::get<0>(t);
41 [[gnu::unused]] auto& i = std::get<1>(t);
42 v = 0;
43 }
44 REQUIRE(std::all_of(range.begin(), range.end(),
45 [](int v) { return v == 0; }));
46 }
47 SECTION("reverse iterators") {
48 std::vector<std::size_t> reversed{7, 6, 5, 4, 3, 2, 1, 0};
49 for (auto t : kblib::enumerate(reversed.rbegin(), reversed.rend())) {
50 auto& v = std::get<0>(t);
51 auto& i = std::get<1>(t);
52 REQUIRE(v == i);
53 }
54 }
55
56 SECTION("temporary") {
57 for (auto t : kblib::enumerate(
58 std::vector<unsigned long long>{0, 1, 1, 2, 3, 5, 8})) {
59 auto& v = std::get<0>(t);
60 auto i = static_cast<int>(std::get<1>(t));
61 REQUIRE(v == kblib::fibonacci(i));
62 }
63 }
64 SECTION("vector<bool>") {
65 for (auto t : kblib::enumerate(
66 std::vector<bool>{false, true, false, true, false})) {
67 auto& v = std::get<0>(t);
68 auto& i = std::get<1>(t);
69 REQUIRE(v == i % 2);
70 // print<decltype(v)>{};
71 }
72 }
73}
74
75TEST_CASE("safe_auto") {
76 {
77 KBLIB_UNUSED decltype(auto) dangling = kblib::min(1, 2);
78 static_assert(std::is_same<decltype(dangling), int&&>::value, "");
79 }
80 {
81 KBLIB_UNUSED decltype(auto) safe = kblib::safe_auto(kblib::min(1, 2));
82 static_assert(std::is_same<decltype(safe), int>::value, "");
83 }
84}
constexpr struct kblib::nums::min_t min
constexpr auto fibonacci(int n) noexcept -> U
Compile-time table fibonacci function.
Definition: stats.h:177
auto safe_auto(T &&in) -> T
Safely propagate an xvalue or lvalue without dangling references.
Definition: simple.h:217
constexpr auto range(Value min, Value max, Delta step=0) -> range_t< Value, Delta >
Constructs a range from beginning, end, and step amount. The range is half-open, that is min is in th...
Definition: iterators.h:621
constexpr auto all_of(InputIt begin, InputIt end, UnaryPredicate pred) -> enable_if_t< is_input_iterator< InputIt >::value, bool >
Determine if pred is true for every element of the range.
Definition: algorithm.h:970
constexpr auto enumerate(Range &&r) -> enumerate_t< Range && >
Allow access to indexes while using range-based for loops. Safe to use with rvalues.
TEST_CASE("enumerate")
Definition: simple.cpp:8
Provides general utilities which do not fit in any more specific header.
Provides numerical and mathematical utilities.
Definition: bits.cpp:9
#define KBLIB_UNUSED
This internal macro is used to provide a fallback for [[maybe_unused]] in C++14.
Definition: tdecl.h:130