-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (53 loc) · 1.96 KB
/
main.cpp
File metadata and controls
66 lines (53 loc) · 1.96 KB
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
66
#include <algorithm>
#include <array>
#include <functional>
#include <iostream>
#include <type_traits>
#include <vector>
#include "evaluator.h"
#include "predicates/all_predicates.h"
// test 1
template <typename T>
bool is_equal_func(const T lhs, const T rhs) {
return lhs == rhs;
}
// Test 2: Namespaced template function predicate
namespace someSpace {
template <typename T>
bool is_greater_equal(const T &lhs, const T &rhs) {
return std::greater_equal<T>{}(lhs, rhs);
}
} // namespace someSpace
// Test 3: Function object (functor) predicate
struct IsEqualFunctor {
template <typename T>
bool operator()(const T &lhs, const T &rhs) const {
return lhs == rhs;
}
};
// Test 4: Lambda expression predicate
auto is_equal_lambda = [](const auto &lhs, const auto &rhs)
{ return tUnit::predicates::is_equal{}(lhs, rhs); };
int main() {
// Test 1: Template function predicate
tUnit::Evaluator test1(1, 1, is_equal_func);
// Test 2: Namespaced template function predicate (ugly implicit deduction)
tUnit::Evaluator test2(1, 1, someSpace::is_greater_equal<int>);
// To avoid implicit function predicates:
int valA{};
int valB{};
tUnit::Evaluator testDecla(valA, valB, someSpace::is_greater_equal<decltype(valA)>); // better but still ugly
// Test 3: Function object (functor) predicate - best option
tUnit::Evaluator test3(1, 1, IsEqualFunctor{});
// Test 4: Lambda expression predicate
tUnit::Evaluator test4(1, 1, is_equal_lambda);
// Test 5: Inline lambda predicate
tUnit::Evaluator test5(1, 1, [](const auto &lhs, const auto &rhs) { return lhs == rhs; });
// Output test results
std::cout << "\ntest1 (template function): " << test1();
std::cout << "\ntest2 (namespace::template_function): " << test2();
std::cout << "\ntest3 (functor: recommended option): " << test3();
std::cout << "\ntest4 (lambda wrapper around namespace::template_func): " << test4();
std::cout << "\ntest5 (inline lambda): " << test5.evaluate() << '\n';
return 0;
}