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