-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctional_test.cpp
65 lines (60 loc) · 1.75 KB
/
functional_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "functional.hpp"
#include <catch2/catch_test_macros.hpp>
#include <utility>
#include <vector>
TEST_CASE("fn::minimum object", "[functional]") {
SECTION("default construction") {
fn::minimum min_obj;
REQUIRE(min_obj(1, 2) == 1);
REQUIRE(min_obj(2, 1) == 1);
REQUIRE(min_obj(2, 2) == 2);
}
SECTION("construction with custom comparator") {
using pii = std::pair<int, int>;
fn::minimum min_obj([](pii lhs, pii rhs) {
return lhs.first * rhs.second < rhs.first * lhs.second;
});
REQUIRE(min_obj(pii{3, 5}, pii{1, 2}) == pii{1, 2});
}
SECTION("construction with custom projection") {
std::vector v{3, 2, 5, 7};
fn::minimum min_obj({}, [&](int idx) { return v[idx]; });
REQUIRE(min_obj(0, 1) == 1);
REQUIRE(min_obj(2, 3) == 2);
}
}
TEST_CASE("fn::maximum object", "[functional]") {
SECTION("default construction") {
fn::maximum max_obj;
REQUIRE(max_obj(1, 2) == 2);
REQUIRE(max_obj(2, 1) == 2);
REQUIRE(max_obj(1, 1) == 1);
}
SECTION("construction with custom comparator") {
using pii = std::pair<int, int>;
fn::maximum max_obj([](pii lhs, pii rhs) {
return lhs.first * rhs.second < rhs.first * lhs.second;
});
REQUIRE(max_obj(pii{3, 5}, pii{1, 2}) == pii{3, 5});
}
SECTION("construction with custom projection") {
std::vector v{3, 2, 5, 7};
fn::maximum max_obj({}, [&](int idx) { return v[idx]; });
REQUIRE(max_obj(0, 1) == 0);
REQUIRE(max_obj(2, 3) == 3);
}
}
TEST_CASE("fn::gcd object", "[functional]") {
SECTION("default construction") {
fn::gcd gcd_obj;
REQUIRE(gcd_obj(3, 5) == 1);
REQUIRE(gcd_obj(12LL, 9) == 3);
REQUIRE(gcd_obj(5, 0) == 5);
}
SECTION("typed construction") {
fn::gcd<int> gcd_obj;
REQUIRE(gcd_obj(3, 5) == 1);
REQUIRE(gcd_obj(12, 9) == 3);
REQUIRE(gcd_obj(5, 0) == 5);
}
}