based

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

for_each_test.cpp (944B)


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(empty)", "[algorithm/for_each]") 15 { 16 const std::array<int, 0> arr = {}; 17 const auto f = based::for_each(std::begin(arr), std::end(arr), functor {}); 18 19 REQUIRE(f.sum == 0); 20 } 21 22 TEST_CASE("for_each(one)", "[algorithm/for_each]") 23 { 24 const std::array arr = {1}; 25 const auto f = based::for_each(std::begin(arr), std::end(arr), functor {}); 26 27 REQUIRE(f.sum == 1); 28 } 29 30 TEST_CASE("for_each(two)", "[algorithm/for_each]") 31 { 32 const std::array arr = {1, 2}; 33 const auto f = based::for_each(std::begin(arr), std::end(arr), functor {}); 34 35 REQUIRE(f.sum == 3); 36 } 37 38 TEST_CASE("for_each(three)", "[algorithm/for_each]") 39 { 40 const std::array arr = {1, 2, 3}; 41 const auto f = based::for_each(std::begin(arr), std::end(arr), functor {}); 42 43 REQUIRE(f.sum == 6); 44 }