|
| 1 | +#include "pch.h" |
| 2 | +#include <kf/algorithm/Algorithm.h> |
| 3 | + |
| 4 | +SCENARIO("Algorithm kf::binary_search_it") |
| 5 | +{ |
| 6 | + GIVEN("A sorted array of int values") |
| 7 | + { |
| 8 | + constexpr std::array kArr = { 111, 222, 333, 444 }; |
| 9 | + |
| 10 | + WHEN("Searching for existing value") |
| 11 | + { |
| 12 | + THEN("Returns iterator to the element") |
| 13 | + { |
| 14 | + auto it = kf::binary_search_it(kArr.begin(), kArr.end(), kArr[1]); |
| 15 | + REQUIRE(it != kArr.end()); |
| 16 | + REQUIRE(*it == kArr[1]); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + WHEN("Searching for non-existing value") |
| 21 | + { |
| 22 | + constexpr int kNotInArray = 555; |
| 23 | + |
| 24 | + THEN("Returns end iterator") |
| 25 | + { |
| 26 | + auto it = kf::binary_search_it(kArr.begin(), kArr.end(), kNotInArray); |
| 27 | + REQUIRE(it == kArr.end()); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + WHEN("Searching for the first element") |
| 32 | + { |
| 33 | + THEN("Returns iterator to beginning") |
| 34 | + { |
| 35 | + auto it = kf::binary_search_it(kArr.begin(), kArr.end(), kArr[0]); |
| 36 | + REQUIRE(it == kArr.begin()); |
| 37 | + REQUIRE(*it == kArr[0]); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + WHEN("Searching for the last element") |
| 42 | + { |
| 43 | + THEN("Returns iterator to the end - 1") |
| 44 | + { |
| 45 | + auto it = kf::binary_search_it(kArr.begin(), kArr.end(), kArr.back()); |
| 46 | + REQUIRE(it == kArr.end() - 1); |
| 47 | + REQUIRE(*it == kArr.back()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + WHEN("Searching in empty vector") |
| 52 | + { |
| 53 | + THEN("Returns iterator to the end") |
| 54 | + { |
| 55 | + constexpr std::array<int, 0> kEmpty; |
| 56 | + auto it = kf::binary_search_it(kEmpty.begin(), kEmpty.end(), kArr[2]); |
| 57 | + REQUIRE(it == kEmpty.end()); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments