-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctors_example.cpp
More file actions
186 lines (142 loc) · 3.95 KB
/
Copy pathfunctors_example.cpp
File metadata and controls
186 lines (142 loc) · 3.95 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//=======================================================================
// Copyright 2013 University of Warsaw.
// Authors: Piotr Wygocki
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
//
//This file contains set of simple useful functors or functor adapters.
#include <vector>
#include <string>
#include "functors.hpp"
using namespace boost;
template <typename RetType = bool, RetType ret = true, typename F>
void check_ret(F f) {
assert(f(2, 2.3, "abc") == ret);
assert(f(2, std::string("abc")) == ret);
assert(f(2) == ret);
assert(f() == ret);
}
template <typename F>
void check_true(F f) {
check_ret(f);
}
template <typename F>
void check_false(F f) {
check_ret<bool, false>(f);
}
void functors_example() {
//skip
SkipFunctor skip;
skip(2, 2.1, "asda");
//identity
IdentityFunctor id;
assert(id(std::string("asd")) == "asd");
assert(id(7) == 7);
//return something
ReturnTrueFunctor retTrue;
ReturnFalseFunctor retFalse;
ReturnZeroFunctor retZero;
check_true(retTrue);
check_false(retFalse);
check_ret<int, 0>(retZero);
//assert
//AssertFunctor assertFun;
//assertFun(); //aborts
//array to functor
std::vector<int> vec{1,2,3};
auto vecFun = make_ArrayToFunctor(vec);
assert(vecFun(1) == 2);
auto vecFunWithOffset = make_ArrayToFunctor(vec, 1);
assert(vecFunWithOffset(1) == 3);
};
void compare_functors() {
Greater g;
GreaterEqual ge;
Less l;
LessEqual le;
EqualTo e;
NotEqualTo ne;
assert(!g(1,2));
assert(!g(1,1));
assert( g(2,1));
assert(!ge(1,2));
assert( ge(1,1));
assert( ge(2,1));
assert( l(1,2));
assert(!l(1,1));
assert(!l(2,1));
assert( le(1,2));
assert( le(1,1));
assert(!le(2,1));
assert(!e(1,2));
assert( e(1,1));
assert(!e(2,1));
assert( ne(1,2));
assert(!ne(1,1));
assert( ne(2,1));
}
void comparator_functor() {
auto getFirst = [](std::pair<int, int> p){return p.first;};
auto compareFirst = make_FunctorToComparator(getFirst);
assert(!compareFirst(std::make_pair(1,2), std::make_pair(0,1)));
auto compareFirstDesc = make_FunctorToComparator(getFirst, Greater());
assert(compareFirstDesc(std::make_pair(1,2), std::make_pair(0,1)));
}
void boolean_functors() {
Not notFun;
Or orFun;
And andFun;
assert(!notFun(true));
assert( notFun(false));
assert(!orFun(false, false));
assert( orFun(true , false));
assert( orFun(false, true));
assert( orFun(true , true));
assert(!andFun(false, false));
assert(!andFun(true , false));
assert(!andFun(false, true));
assert( andFun(true , true));
}
void lift_operator_functor() {
auto oper = [](int a, int b) {return a + b > 0;};
ReturnZeroFunctor zero;
ReturnSomethingFunctor<int, 5> five;
auto f = make_LiftBinaryOperatorFunctor(zero, five, oper);
check_true(f);
}
void boolean_functors_on_functors() {
ReturnTrueFunctor retTrue;
ReturnFalseFunctor retFalse;
{
auto trueFunctor = make_NotFunctor(retFalse);
check_true(trueFunctor);
}
{
auto falseFunctor = make_NotFunctor(retTrue);
check_false(falseFunctor);
}
{
auto trueFunctor = make_OrFunctor(retTrue, retFalse);
check_true(trueFunctor);
}
{
auto falseFunctor = make_AndFunctor(retTrue, retFalse);
check_false(falseFunctor);
}
{
auto trueFunctor = make_XorFunctor(retTrue, retFalse);
check_true(trueFunctor);
}
}
int main() {
functors_example();
boolean_functors();
comparator_functor();
boolean_functors();
lift_operator_functor();
boolean_functors_on_functors();
return 0;
}