basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
minmax_element_test.cpp (3681B)
1 #include <catch2/catch_test_macros.hpp> 2 3 #include "based/algorithm.hpp" 4 5 TEST_CASE("minmax_element(empty)", "[algorithm/minmax_element]") 6 { 7 const std::array<int, 0> arr = {}; 8 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 9 REQUIRE(min == std::end(arr)); 10 REQUIRE(max == std::end(arr)); 11 } 12 13 TEST_CASE("minmax_element(1)", "[algorithm/minmax_element]") 14 { 15 const std::array arr = {0}; 16 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 17 const auto mini = std::distance(std::begin(arr), min); 18 const auto maxi = std::distance(std::begin(arr), max); 19 REQUIRE(mini == 0); 20 REQUIRE(maxi == 0); 21 } 22 23 TEST_CASE("minmax_element(increasing even)", "[algorithm/minmax_element]") 24 { 25 const std::array arr = {0, 1, 2, 3}; 26 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 27 const auto mini = std::distance(std::begin(arr), min); 28 const auto maxi = std::distance(std::begin(arr), max); 29 REQUIRE(mini == 0); 30 REQUIRE(maxi == std::size(arr) - 1); 31 } 32 33 TEST_CASE("minmax_element(increasing odd)", "[algorithm/minmax_element]") 34 { 35 const std::array arr = {0, 1, 2, 3, 4}; 36 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 37 const auto mini = std::distance(std::begin(arr), min); 38 const auto maxi = std::distance(std::begin(arr), max); 39 REQUIRE(mini == 0); 40 REQUIRE(maxi == std::size(arr) - 1); 41 } 42 43 TEST_CASE("minmax_element(decreasing even)", "[algorithm/minmax_element]") 44 { 45 const std::array arr = {3, 2, 1, 0}; 46 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 47 const auto mini = std::distance(std::begin(arr), min); 48 const auto maxi = std::distance(std::begin(arr), max); 49 REQUIRE(mini == std::size(arr) - 1); 50 REQUIRE(maxi == 0); 51 } 52 53 TEST_CASE("minmax_element(decreasing odd)", "[algorithm/minmax_element]") 54 { 55 const std::array arr = {4, 3, 2, 1, 0}; 56 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 57 const auto mini = std::distance(std::begin(arr), min); 58 const auto maxi = std::distance(std::begin(arr), max); 59 REQUIRE(mini == std::size(arr) - 1); 60 REQUIRE(maxi == 0); 61 } 62 63 TEST_CASE("minmax_element(stable even)", "[algorithm/minmax_element]") 64 { 65 const std::array arr = {3, 0, 0, 3}; 66 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 67 const auto mini = std::distance(std::begin(arr), min); 68 const auto maxi = std::distance(std::begin(arr), max); 69 REQUIRE(mini == 1); 70 REQUIRE(maxi == std::size(arr) - 1); 71 } 72 73 TEST_CASE("minmax_element(stable odd)", "[algorithm/minmax_element]") 74 { 75 const std::array arr = {3, 0, 3, 3, 0}; 76 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 77 const auto mini = std::distance(std::begin(arr), min); 78 const auto maxi = std::distance(std::begin(arr), max); 79 REQUIRE(mini == 1); 80 REQUIRE(maxi == std::size(arr) - 2); 81 } 82 83 TEST_CASE("minmax_element(stable increasing)", "[algorithm/minmax_element]") 84 { 85 const std::array arr = {0, 0, 1, 2, 3, 4, 4}; 86 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 87 const auto mini = std::distance(std::begin(arr), min); 88 const auto maxi = std::distance(std::begin(arr), max); 89 REQUIRE(mini == 0); 90 REQUIRE(maxi == std::size(arr) - 1); 91 } 92 93 TEST_CASE("minmax_element(stable decreasing)", "[algorithm/minmax_element]") 94 { 95 const std::array arr = {4, 4, 3, 2, 1, 0, 0}; 96 const auto [min, max] = based::minmax_element(std::begin(arr), std::end(arr)); 97 const auto mini = std::distance(std::begin(arr), min); 98 const auto maxi = std::distance(std::begin(arr), max); 99 REQUIRE(mini == std::size(arr) - 2); 100 REQUIRE(maxi == 1); 101 }