based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

reduce_test.cpp (1434B)


0 #include <array> 1 #include <vector> 2 3 #include <catch2/catch_test_macros.hpp> 4 5 #include "based/algorithm.hpp" 6 7 struct op 8 { 9 auto operator()(const auto& lhs, const auto& rhs) { return lhs + rhs; } 10 }; 11 12 struct fun 13 { 14 auto operator()(based::Iterator auto itr) { return *itr; } 15 }; 16 17 TEST_CASE("reduce_nonempty(sequence)", "[algorithm/reduce_nonempty]") 18 { 19 const std::array arr = {0, 1, 2, 3, 4, 5}; 20 21 const auto count = 22 based::reduce_nonempty(std::cbegin(arr), std::cend(arr), op {}, fun {}); 23 24 REQUIRE(count == 15); 25 } 26 27 TEST_CASE("reduce(empty)", "[algorithm/reduce]") 28 { 29 const std::array<int, 0> arr = {}; 30 31 const auto count = 32 based::reduce(std::cbegin(arr), std::cend(arr), op {}, fun {}, 0); 33 34 REQUIRE(count == 0); 35 } 36 37 TEST_CASE("reduce(sequence)", "[algorithm/reduce]") 38 { 39 const std::array arr = {0, 1, 2, 3, 4, 5}; 40 41 const auto count = 42 based::reduce(std::cbegin(arr), std::cend(arr), op {}, fun {}, 0); 43 44 REQUIRE(count == 15); 45 } 46 47 TEST_CASE("reduce_nonzero(empty)", "[algorithm/reduce_nonzero]") 48 { 49 const std::array<int, 0> arr = {}; 50 51 const auto count = 52 based::reduce_nonzero(std::cbegin(arr), std::cend(arr), op {}, fun {}, 0); 53 54 REQUIRE(count == 0); 55 } 56 57 TEST_CASE("reduce_nonzero(sequence)", "[algorithm/reduce_nonzero]") 58 { 59 const std::array arr = {0, 1, 2, 3, 4, 5}; 60 61 const auto count = 62 based::reduce_nonzero(std::cbegin(arr), std::cend(arr), op {}, fun {}, 0); 63 64 REQUIRE(count == 15); 65 }