-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
168 lines (150 loc) · 3.94 KB
/
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
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
#include "test.h"
#include "assert.h"
#include "autosel.h"
#include <string>
#include <sstream>
#include <map>
#define RUN_TEST(test) { \
std::cerr << "-> " << #test << std::endl; \
test(); \
}
#define ASSERT_EQUAL(expected, actual) { \
assert_equal(expected, actual, #expected ", " #actual); \
}
template<typename T>
void Test::assert_equal(const T& expected, const T& actual, const char *mes)
{
if (expected == actual) return;
std::cerr
<< " !!assert_equal(" << mes << ")" << std::endl
<< " expected : " << expected << std::endl
<< " actual : " << actual << std::endl;
}
void Test::run()
{
RUN_TEST(test_parser);
RUN_TEST(test_autosel_no_overlap);
RUN_TEST(test_autosel_overlap);
RUN_TEST(test_autosel_shortage);
RUN_TEST(test_autosel_triple);
RUN_TEST(test_autosel_zero);
}
void Test::test_parser()
{
int expected[] = { 1, 2, 3, 4, -1, 2 };
std::vector<int> actual = parse_int_array("1,2, 3,4, -1,2");
ASSERT_EQUAL(6, static_cast<int>(actual.size()));
int *ex = expected;
std::vector<int>::const_iterator ni = actual.begin();
for (; ni != actual.end(); ++ni, ++ex) {
ASSERT_EQUAL(*ex, *ni);
}
}
void Test::test_autosel_no_overlap()
{
test_sub("1,11, 1,12, 1,13, 1,14, 1,15,"
"2,21, 2,22, 2,23, 2,14, 2,15",
"1,2", 3, "3,3", 0);
}
void Test::test_autosel_overlap()
{
test_sub("1,11, 1,12, 1,13, 1,14, 1,15,"
"2,11, 2,12, 2,13, 2,14, 2,15",
"1,2", 3, "3,3", 1);
}
void Test::test_autosel_shortage()
{
test_sub("1,11, 1,12, 1,13, 1,14, 1,15, 1,16,"
"2,11, 2,12, 2,13",
"1,2", 5, "5,3", 2);
}
void Test::test_autosel_triple()
{
test_sub("1,11, 1,12, 1,13, 1,14, 1,15,"
"2,21, 2,12, 2,13, 2,24, 2,25,"
"3,31, 3,32, 3,33, 3,14, 3,15",
"1,2,3", 3, "3,3,3", 0);
}
void Test::test_autosel_zero()
{
test_sub("1,11, 1,12, 1,13, 1,14, 1,15,"
"3,11, 3,12, 3,13, 3,14, 3,15",
"1,2,3", 3, "3,0,3", 1);
}
void Test::test_sub(const char *nodes_str, const char *qids_str, int count_req, const char *expected_str, int expected_return)
{
Nodes result_nodes;
Nodes nodes = parse_nodes(nodes_str);
Qids qids = parse_qids(qids_str);
{
AutoSelect autosel;
int result = autosel.detect(result_nodes, nodes, qids, count_req);
ASSERT_EQUAL(expected_return, result);
ASSERT_EQUAL(expected_return, count_overlap_kid(result_nodes));
}
{
std::vector<int> expected = parse_int_array(expected_str);
int i = 0;
for (Qids::const_iterator qi = qids.begin(); qi != qids.end(); ++qi, ++i) {
Qid qid = *qi;
ASSERT_EQUAL(expected[i], count_per_qid(result_nodes, qid));
}
}
}
int Test::count_per_qid(const Nodes& nodes, const Qid& qid) const
{
int count = 0;
for (Nodes::const_iterator ni = nodes.begin(); ni != nodes.end(); ++ni) {
if (ni->qid == qid) ++count;
}
return count;
}
int Test::count_overlap_kid(const Nodes& nodes) const
{
std::map<Kid,int> counter;
for (Nodes::const_iterator ni = nodes.begin(); ni != nodes.end(); ++ni) {
counter[ni->kid]++;
}
int count = 0;
for (std::map<Kid,int>::const_iterator ki = counter.begin(); ki != counter.end(); ++ki) {
if (ki->second > 1) ++count;
}
return count;
}
std::vector<int> Test::parse_int_array(const char *array_str) const
{
std::string str(array_str);
size_t pos;
for (; (pos = str.find(",")) != std::string::npos;) {
str.replace(pos, 1, " ");
}
std::vector<int> array;
std::istringstream ss(str);
for (; !ss.eof();) {
int n;
ss >> n;
array.push_back(n);
}
return array;
}
Nodes Test::parse_nodes(const char *nodes_str) const
{
Nodes nodes;
std::vector<int> array = parse_int_array(nodes_str);
assert(array.size() % 2 == 0);
for (int i = 0; i < array.size(); i += 2) {
int qid(array[i]);
int kid(array[i+1]);
nodes.push_back(Node(qid, kid));
}
return nodes;
}
Qids Test::parse_qids(const char *qids_str) const
{
Qids qids;
std::vector<int> array = parse_int_array(qids_str);
for (std::vector<int>::const_iterator ni = array.begin(); ni != array.end(); ++ni) {
qids.push_back(Qid(*ni));
}
return qids;
}