based

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

for_each_n_test.cpp (1156B)


1 #include <array> 2 3 #include <catch2/catch_test_macros.hpp> 4 5 #include "based/algorithm.hpp" 6 7 struct functor 8 { 9 int operator()(int n) { return sum += n; } 10 11 int sum = 0; 12 }; 13 14 TEST_CASE("for_each_n(empty)", "[algorithm/for_each_n]") 15 { 16 const std::array<int, 0> arr = {}; 17 const auto [f, itr] = 18 based::for_each_n(std::begin(arr), std::size(arr), functor {}); 19 20 REQUIRE(f.sum == 0); 21 REQUIRE(itr == std::end(arr)); 22 } 23 24 TEST_CASE("for_each_n(one)", "[algorithm/for_each_n]") 25 { 26 const std::array arr = {1}; 27 const auto [f, itr] = 28 based::for_each_n(std::begin(arr), std::size(arr), functor {}); 29 30 REQUIRE(f.sum == 1); 31 REQUIRE(itr == std::end(arr)); 32 } 33 34 TEST_CASE("for_each_n(two)", "[algorithm/for_each_n]") 35 { 36 const std::array arr = {1, 2}; 37 const auto [f, itr] = 38 based::for_each_n(std::begin(arr), std::size(arr), functor {}); 39 40 REQUIRE(f.sum == 3); 41 REQUIRE(itr == std::end(arr)); 42 } 43 44 TEST_CASE("for_each_n(three)", "[algorithm/for_each_n]") 45 { 46 const std::array arr = {1, 2, 3}; 47 const auto [f, itr] = 48 based::for_each_n(std::begin(arr), std::size(arr), functor {}); 49 50 REQUIRE(f.sum == 6); 51 REQUIRE(itr == std::end(arr)); 52 }