forked from opencog/cogutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comprehensionUTest.cxxtest
105 lines (96 loc) · 3.29 KB
/
comprehensionUTest.cxxtest
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
/** comprehensionUTest.cxxtest ---
*
* Copyright (C) 2012 Nil Geisweiller
*
* Author: Nil Geisweiller <nilg@desktop>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License v3 as
* published by the Free Software Foundation and including the exceptions
* at http://opencog.org/wiki/Licenses
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, write to:
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <opencog/util/comprehension.h>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
using namespace opencog;
using namespace boost::phoenix;
using boost::phoenix::arg_names::arg1;
class comprehensionUTest : public CxxTest::TestSuite
{
public:
comprehensionUTest() {}
// Test vector comprehension
void test_vector_comp_function_object() {
std::vector<int> ivec = {1,2,3,4};
struct Inc : public std::unary_function<int, int> {
int operator()(int i) {
return i + 1;
}
};
Inc func;
struct Odd : public std::unary_function<int, bool> {
bool operator()(int i) {
return i%2;
}
};
Odd filter;
auto ovec = vector_comp(ivec, func, filter);
std::vector<int> evec = {2,4};
TS_ASSERT_EQUALS(ovec, evec);
}
void test_vector_comp_lambda() {
std::vector<int> ivec = {1,2,3,4};
auto ovec = vector_comp(ivec, [](int x) { return x + 1; },
[](int x) { return x%2; });
std::vector<int> evec = {2,4};
TS_ASSERT_EQUALS(ovec, evec);
}
// Test list comprehension
void test_list_comp_function_object() {
std::list<int> ivec = {1,2,3,4};
struct Func : public std::unary_function<int, int> {
int operator()(int i) {
return i + 1;
}
};
Func func;
auto ovec = list_comp(ivec, func);
std::list<int> evec = {2,3,4,5};
TS_ASSERT_EQUALS(ovec, evec);
}
void test_list_comp_lambda() {
std::list<int> ivec = {1,2,3,4};
auto ovec = list_comp(ivec, [](int x) { return x + 1; });
std::list<int> evec = {2,3,4,5};
TS_ASSERT_EQUALS(ovec, evec);
}
// Test set comprehension
void test_set_comp_function_object() {
std::set<int> ivec = {1,2,3,4};
struct Func : public std::unary_function<int, int> {
int operator()(int i) {
return i + 1;
}
};
Func func;
auto ovec = set_comp(ivec, func);
std::set<int> evec = {2,3,4,5};
TS_ASSERT_EQUALS(ovec, evec);
}
void test_set_comp_lambda() {
std::set<int> ivec = {1,2,3,4};
auto ovec = set_comp(ivec, [](int x) { return x + 1; });
std::set<int> evec = {2,3,4,5};
TS_ASSERT_EQUALS(ovec, evec);
}
};