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