1 // Copyright 2015, Tobias Hermann and the FunctionalPlus contributors. 2 // https://github.com/Dobiasd/FunctionalPlus 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 #include <doctest/doctest.h> 8 #include <fplus/fplus.hpp> 9 #include <vector> 10 11 namespace { 12 int_less(int x,int y)13 bool int_less(int x, int y) 14 { 15 return x < y; 16 } 17 int_less_eq(int x,int y)18 bool int_less_eq(int x, int y) 19 { 20 return x <= y; 21 } 22 __anonf93c081c0202(auto x, auto y) 23 auto generic_less = [](auto x, auto y) { 24 return x < y; 25 }; 26 __anonf93c081c0302(auto x, auto y) 27 auto generic_less_eq = [](auto x, auto y) { 28 return x <= y; 29 }; 30 __anonf93c081c0402(auto x) 31 auto squareGeneric = [](auto x) { return x * x; }; 32 } 33 34 35 TEST_CASE("compare_test - is_equal_to") 36 { 37 using namespace fplus; 38 REQUIRE(is_equal_to(2)(2)); 39 REQUIRE_FALSE(is_equal_to(2)(3)); 40 } 41 42 TEST_CASE("compare_test - is_not_equal_to") 43 { 44 using namespace fplus; 45 REQUIRE_FALSE(is_not_equal_to(2)(2)); 46 REQUIRE(is_not_equal_to(2)(3)); 47 } 48 49 TEST_CASE("compare_test - is_less") 50 { 51 using namespace fplus; 52 REQUIRE_FALSE(is_less(2, 2)); 53 REQUIRE(is_less(2, 3)); 54 REQUIRE_FALSE(is_less(3, 2)); 55 REQUIRE(is_less_than(3)(2)); 56 } 57 58 TEST_CASE("compare_test - is_less_or_equal") 59 { 60 using namespace fplus; 61 REQUIRE(is_less_or_equal(2, 2)); 62 REQUIRE(is_less_or_equal(2, 3)); 63 REQUIRE_FALSE(is_less_or_equal(3, 2)); 64 REQUIRE(is_less_or_equal_than(3)(2)); 65 REQUIRE(is_less_or_equal_by_and_by(squareGeneric, squareGeneric)(2, 2)); 66 REQUIRE(is_less_or_equal_by_than(squareGeneric, 5)(2)); 67 } 68 69 TEST_CASE("compare_test - is_less_by") 70 { 71 using namespace fplus; __anonf93c081c0502(int x) 72 auto square = [](int x) { return x * x; }; 73 REQUIRE(is_less_by_and_by(squareGeneric, square)(2, -3)); 74 REQUIRE(is_less_by(squareGeneric)(2, -3)); 75 } 76 77 TEST_CASE("compare_test - is_less_by_than") 78 { 79 using namespace fplus; __anonf93c081c0602(int x) 80 auto square = [](int x) { return x * x; }; 81 REQUIRE(is_less_by_than(square, 5)(2)); 82 REQUIRE(is_less_by_than(squareGeneric, 5)(2)); 83 } 84 85 TEST_CASE("compare_test - is_greater") 86 { 87 using namespace fplus; 88 REQUIRE_FALSE(is_greater(2, 2)); 89 REQUIRE_FALSE(is_greater(2, 3)); 90 REQUIRE(is_greater(3, 2)); 91 REQUIRE_FALSE(is_greater_than(3)(2)); 92 REQUIRE(is_greater_by_and_by(squareGeneric, squareGeneric)(3, -2)); 93 } 94 95 TEST_CASE("compare_test - is_greater_or_equal") 96 { 97 using namespace fplus; 98 REQUIRE(is_greater_or_equal(2, 2)); 99 REQUIRE_FALSE(is_greater_or_equal(2, 3)); 100 REQUIRE(is_greater_or_equal(3, 2)); 101 REQUIRE_FALSE(is_greater_or_equal_than(3)(2)); 102 REQUIRE(is_greater_or_equal_by_and_by(squareGeneric, squareGeneric)(3, -3)); 103 REQUIRE(is_greater_or_equal_by(squareGeneric)(3, -3)); 104 REQUIRE(is_greater_or_equal_by_than(squareGeneric, 3)(-3)); 105 } 106 107 TEST_CASE("compare_test - is_equal_by") 108 { 109 using namespace fplus; __anonf93c081c0702(int x) 110 auto square = [](int x) { return x * x; }; 111 REQUIRE(is_equal_by_and_by(square, square)(2, -2)); 112 REQUIRE(is_equal_by_and_by(squareGeneric, square)(2, -2)); 113 REQUIRE(is_equal_by(square)(2, -2)); 114 REQUIRE(is_not_equal_by_and_by(square, squareGeneric)(2, 3)); 115 REQUIRE(is_equal_by(squareGeneric)(2, -2)); 116 REQUIRE(is_not_equal_by(square)(2, 3)); 117 REQUIRE(is_not_equal_by(squareGeneric)(2, 3)); 118 REQUIRE(is_equal_by_to(squareGeneric, 4)(2)); 119 REQUIRE(is_not_equal_by_to(squareGeneric, 5)(2)); 120 } 121 122 TEST_CASE("compare_test - always") 123 { 124 using namespace fplus; 125 REQUIRE_EQ(identity(2), 2); 126 REQUIRE_EQ(always<int>(2)(5), 2); 127 REQUIRE_EQ(always_arg_1_of_2(2, 5), 2); 128 REQUIRE_EQ(always_arg_2_of_2(2, 5), 5); 129 } 130 131 TEST_CASE("compare_test - xor_bools") 132 { 133 using namespace fplus; 134 REQUIRE(xor_bools(false, false) == false); 135 REQUIRE(xor_bools(true, false) == true); 136 REQUIRE(xor_bools(false, true) == true); 137 REQUIRE(xor_bools(true, true) == false); 138 } 139 140 TEST_CASE("compare_test - ord_to_eq") 141 { 142 using namespace fplus; 143 REQUIRE(ord_to_eq(int_less)(1, 2) == false); 144 REQUIRE(ord_to_eq(int_less)(2, 2) == true); 145 REQUIRE(ord_to_eq(int_less)(2, 1) == false); 146 REQUIRE(ord_to_eq(generic_less)(2, 1) == false); 147 } 148 149 TEST_CASE("compare_test - ord_to_not_eq") 150 { 151 using namespace fplus; 152 REQUIRE(ord_to_not_eq(int_less)(1, 2) == true); 153 REQUIRE(ord_to_not_eq(int_less)(2, 2) == false); 154 REQUIRE(ord_to_not_eq(int_less)(2, 1) == true); 155 REQUIRE(ord_to_not_eq(generic_less)(2, 1) == true); 156 } 157 158 TEST_CASE("compare_test - ord_eq_to_eq") 159 { 160 using namespace fplus; 161 REQUIRE(ord_eq_to_eq(int_less_eq)(1, 2) == false); 162 REQUIRE(ord_eq_to_eq(int_less_eq)(2, 2) == true); 163 REQUIRE(ord_eq_to_eq(int_less_eq)(2, 1) == false); 164 REQUIRE(ord_eq_to_eq(generic_less_eq)(2, 1) == false); 165 } 166 167 TEST_CASE("compare_test - ord_eq_to_not_eq") 168 { 169 using namespace fplus; 170 REQUIRE(ord_eq_to_not_eq(int_less_eq)(1, 2) == true); 171 REQUIRE(ord_eq_to_not_eq(int_less_eq)(2, 2) == false); 172 REQUIRE(ord_eq_to_not_eq(int_less_eq)(2, 1) == true); 173 } 174 175 TEST_CASE("compare_test - lexicographical_less") 176 { 177 using namespace fplus; 178 REQUIRE(lexicographical_less(std::vector<int>{0,1,2,2,4,5}, std::vector<int>{0,1,2,3,4,5})); 179 REQUIRE(lexicographical_less(std::vector<int>{}, std::vector<int>{1})); 180 REQUIRE_FALSE(lexicographical_less(std::vector<int>{1}, std::vector<int>{})); 181 REQUIRE(lexicographical_less(std::string("012245"), std::string("012345"))); 182 REQUIRE(lexicographical_less(std::string("012245"), std::string("01234"))); 183 REQUIRE(lexicographical_less(std::string("01234"), std::string("012345"))); 184 REQUIRE_FALSE(lexicographical_less(std::string("012345"), std::string("012245"))); 185 REQUIRE_FALSE(lexicographical_less(std::string("01234"), std::string("012245"))); 186 REQUIRE_FALSE(lexicographical_less(std::string("012345"), std::string("01234"))); 187 } 188 189 TEST_CASE("compare_test - lexicographical_sort") 190 { 191 using namespace fplus; 192 std::vector<std::string> xs = {"012245", "012345", "01234"}; 193 std::vector<std::string> xs_lex_sorted = {"012245", "01234", "012345"}; 194 REQUIRE_EQ(lexicographical_sort(xs), xs_lex_sorted); 195 } 196