based

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

find_n_test.cpp (4378B)


0 #include <array> 1 2 #include <catch2/catch_test_macros.hpp> 3 4 #include "based/algorithm.hpp" 5 6 TEST_CASE("find_n(empty)", "[algorithm/find_n]") 7 { 8 const std::array<int, 0> arr = {}; 9 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 0); 10 const auto idx = std::distance(std::cbegin(arr), itr); 11 12 REQUIRE(idx == 0); 13 REQUIRE(idx + left == std::size(arr)); 14 } 15 16 TEST_CASE("find_n(one)", "[algorithm/find_n]") 17 { 18 const std::array arr = {0}; 19 20 SECTION("found") 21 { 22 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 0); 23 const auto idx = std::distance(std::cbegin(arr), itr); 24 25 REQUIRE(idx == 0); 26 REQUIRE(idx + left == std::size(arr)); 27 } 28 SECTION("not found") 29 { 30 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 1); 31 const auto idx = std::distance(std::cbegin(arr), itr); 32 33 REQUIRE(idx == 1); 34 REQUIRE(idx + left == std::size(arr)); 35 } 36 } 37 38 TEST_CASE("find_n(two)", "[algorithm/find_n]") 39 { 40 const std::array arr = {0, 1}; 41 42 SECTION("found 1") 43 { 44 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 0); 45 const auto idx = std::distance(std::cbegin(arr), itr); 46 47 REQUIRE(idx == 0); 48 REQUIRE(idx + left == std::size(arr)); 49 } 50 51 SECTION("found 2") 52 { 53 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 1); 54 const auto idx = std::distance(std::cbegin(arr), itr); 55 56 REQUIRE(idx == 1); 57 REQUIRE(idx + left == std::size(arr)); 58 } 59 60 SECTION("not found") 61 { 62 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 2); 63 const auto idx = std::distance(std::cbegin(arr), itr); 64 65 REQUIRE(idx == 2); 66 REQUIRE(idx + left == std::size(arr)); 67 } 68 } 69 70 TEST_CASE("find_n(multiple)", "[algorithm/find_n]") 71 { 72 const std::array arr = {0, 0, 0, 0}; 73 74 SECTION("found") 75 { 76 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 0); 77 const auto idx = std::distance(std::cbegin(arr), itr); 78 79 REQUIRE(idx == 0); 80 REQUIRE(idx + left == std::size(arr)); 81 } 82 83 SECTION("not found") 84 { 85 const auto [itr, left] = based::find_n(std::begin(arr), std::size(arr), 1); 86 const auto idx = std::distance(std::cbegin(arr), itr); 87 88 REQUIRE(idx == 4); 89 REQUIRE(idx + left == std::size(arr)); 90 } 91 } 92 93 TEST_CASE("find_not_n(empty)", "[algorithm/find_not_n]") 94 { 95 const std::array<int, 0> arr = {}; 96 97 const auto [itr, left] = 98 based::find_not_n(std::begin(arr), std::size(arr), 0); 99 const auto idx = std::distance(std::cbegin(arr), itr); 100 101 REQUIRE(idx == 0); 102 REQUIRE(idx + left == std::size(arr)); 103 } 104 105 TEST_CASE("find_not_n(one)", "[algorithm/find_not_n]") 106 { 107 const std::array arr = {0}; 108 109 SECTION("found") 110 { 111 const auto [itr, left] = 112 based::find_not_n(std::begin(arr), std::size(arr), 1); 113 const auto idx = std::distance(std::cbegin(arr), itr); 114 115 REQUIRE(idx == 0); 116 REQUIRE(idx + left == std::size(arr)); 117 } 118 119 SECTION("not found") 120 { 121 const auto [itr, left] = 122 based::find_not_n(std::begin(arr), std::size(arr), 0); 123 const auto idx = std::distance(std::cbegin(arr), itr); 124 125 REQUIRE(idx == 1); 126 REQUIRE(idx + left == std::size(arr)); 127 } 128 } 129 130 TEST_CASE("find_not_n(two)", "[algorithm/find_not_n]") 131 { 132 const std::array arr = {0, 1}; 133 134 SECTION("found 1") 135 { 136 const auto [itr, left] = 137 based::find_not_n(std::begin(arr), std::size(arr), 1); 138 const auto idx = std::distance(std::cbegin(arr), itr); 139 140 REQUIRE(idx == 0); 141 REQUIRE(idx + left == std::size(arr)); 142 } 143 144 SECTION("found 2") 145 { 146 const auto [itr, left] = 147 based::find_not_n(std::begin(arr), std::size(arr), 0); 148 const auto idx = std::distance(std::cbegin(arr), itr); 149 150 REQUIRE(idx == 1); 151 REQUIRE(idx + left == std::size(arr)); 152 } 153 } 154 155 TEST_CASE("find_not_n(multiple)", "[algorithm/find_not_n]") 156 { 157 const std::array arr = {0, 0, 0, 0}; 158 159 SECTION("found") 160 { 161 const auto [itr, left] = 162 based::find_not_n(std::begin(arr), std::size(arr), 1); 163 const auto idx = std::distance(std::cbegin(arr), itr); 164 165 REQUIRE(idx == 0); 166 REQUIRE(idx + left == std::size(arr)); 167 } 168 169 SECTION("not found") 170 { 171 const auto [itr, left] = 172 based::find_not_n(std::begin(arr), std::size(arr), 0); 173 const auto idx = std::distance(std::cbegin(arr), itr); 174 175 REQUIRE(idx == 4); 176 REQUIRE(idx + left == std::size(arr)); 177 } 178 }