-
Notifications
You must be signed in to change notification settings - Fork 8
/
vectest.cc
283 lines (247 loc) · 7.04 KB
/
vectest.cc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//
// Tests for vector operation
//
// Copyright(C) 2010 Mizuki Fujisawa <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// 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 General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
#include <algorithm>
#include <map>
#include <vector>
#include <gtest/gtest.h>
#include "byvector.h"
namespace {
/* typedef */
typedef std::map<bayon::VecKey, bayon::VecValue> TestData;
/* function prototypes */
static void init_vector(bayon::Vector &vec, const TestData &m);
static void set_input_values();
/* global variables */
TestData input1, input2, input3, input4, normalized1, normalized2;
std::vector<bayon::VecKey> sorted_keys, sorted_keys_abs;
/* initialize vector */
static void init_vector(bayon::Vector &vec, const TestData &m) {
vec.clear();
for (TestData::const_iterator it = m.begin(); it != m.end(); ++it) {
vec.set(it->first, it->second);
}
}
/* set test data */
static void set_input_values() {
input1[1] = 1.0;
input1[2] = 2.0;
input1[3] = 3.0;
sorted_keys.push_back(3);
sorted_keys.push_back(2);
sorted_keys.push_back(1);
normalized1[1] = 1.0 / sqrt(1 * 1 + 2 * 2 + 3 * 3);
normalized1[2] = 2.0 / sqrt(1 * 1 + 2 * 2 + 3 * 3);
normalized1[3] = 3.0 / sqrt(1 * 1 + 2 * 2 + 3 * 3);
input2[1] = 3.0;
input2[2] = 6.0;
input2[3] = 9.0;
input3[1] = 3.0;
input3[2] = 2.0;
input3[3] = 1.0;
input4[1] = 1.0;
input4[2] = -2.0;
input4[3] = 3.0;
sorted_keys_abs.push_back(3);
sorted_keys_abs.push_back(2);
sorted_keys_abs.push_back(1);
}
} /* namespace */
/* copy */
TEST(VectorTest, CopyTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
vec1.copy(vec2);
std::vector<bayon::VecItem> items;
vec1.sorted_items(items);
for (size_t i = 0; i < items.size(); i++) {
EXPECT_EQ(items[i].second, vec2.get(items[i].first));
}
}
/* get, set */
TEST(VectorTest, GetSetTest) {
bayon::Vector vec;
for (bayon::VecKey key = 0; key < 10; key++) {
vec.set(key, key * 10);
}
for (bayon::VecKey key = 0; key < 10; key++) {
EXPECT_EQ(vec.get(key), key * 10);
}
}
/* size */
TEST(VectorTest, SizeTest) {
bayon::Vector vec;
init_vector(vec, input1);
EXPECT_EQ(vec.size(), input1.size());
}
/* clear */
TEST(VectorTest, ClearTest) {
bayon::Vector vec;
init_vector(vec, input1);
vec.clear();
EXPECT_EQ(vec.size(), static_cast<size_t>(0));
}
/* hash_map */
TEST(VectorTest, HashMapTest) {
bayon::Vector vec;
std::vector<bayon::VecKey> keys;
for (TestData::iterator it = input1.begin(); it != input1.end(); ++it) {
keys.push_back(it->first);
}
std::reverse(keys.begin(), keys.end());
TestData reversed;
for (size_t i = 0; i < keys.size(); i++) {
reversed[keys[i]] = input1[keys[i]];
}
init_vector(vec, reversed);
const bayon::VecHashMap *hash = vec.hash_map();
for (TestData::const_iterator it = input1.begin(); it != input1.end(); ++it) {
bayon::VecHashMap::const_iterator ith = hash->find(it->first);
EXPECT_EQ(it->second, ith->second);
}
}
/* sorted_items */
TEST(VectorTest, SortedItemsTest) {
bayon::Vector vec;
init_vector(vec, input1);
std::vector<bayon::VecItem> items;
vec.sorted_items(items);
EXPECT_EQ(items.size(), sorted_keys.size());
for (size_t i = 0; i < items.size(); i++) {
EXPECT_EQ(items[i].first, sorted_keys[i]);
}
}
/* sorted_items_abs */
TEST(VectorTest, SortedItemsAbsTest) {
bayon::Vector vec;
init_vector(vec, input4);
std::vector<bayon::VecItem> items;
vec.sorted_items_abs(items);
EXPECT_EQ(items.size(), sorted_keys_abs.size());
for (size_t i = 0; i < items.size(); i++) {
EXPECT_EQ(items[i].first, sorted_keys_abs[i]);
}
}
/* normalize */
TEST(VectorTest, NormalizeTest) {
bayon::Vector vec;
init_vector(vec, input1);
vec.normalize();
for (TestData::iterator it = normalized1.begin();
it != normalized1.end(); ++it) {
EXPECT_EQ(it->second, vec.get(it->first));
}
}
/* resize */
TEST(VectorTest, ResizeTest) {
bayon::Vector vec;
for (bayon::VecKey i = 1; i <= 100; i++) {
vec.set(i, i);
}
size_t size = 30;
vec.resize(size);
EXPECT_EQ(vec.size(), size);
}
/* norm */
TEST(VectorTest, NormTest) {
bayon::Vector vec;
init_vector(vec, input1);
double norm = vec.norm();
EXPECT_EQ(sqrt(1 * 1 + 2 * 2 + 3 * 3), norm);
}
/* multiply_constant */
TEST(VectorTest, MultiplyConstantTest) {
bayon::Vector vec;
init_vector(vec, input1);
double x = 3;
vec.multiply_constant(x);
for (TestData::iterator it = input1.begin();
it != input1.end(); ++it) {
EXPECT_EQ(it->second * x, vec.get(it->first));
}
}
/* add_vector */
TEST(VectorTest, AddVectorTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
init_vector(vec2, input2);
vec1.add_vector(vec2);
for (TestData::iterator it = input1.begin();
it != input1.end(); ++it) {
EXPECT_EQ(it->second + input2[it->first], vec1.get(it->first));
}
}
/* delete_vector */
TEST(VectorTest, DeleteVectorTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
init_vector(vec2, input2);
vec1.delete_vector(vec2);
for (TestData::iterator it = input1.begin();
it != input1.end(); ++it) {
EXPECT_EQ(it->second - input2[it->first], vec1.get(it->first));
}
}
/* euclid_distance */
TEST(VectorTest, EuclidDistanceTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
init_vector(vec2, input2);
double dist = bayon::Vector::euclid_distance(vec1, vec2);
EXPECT_EQ(dist, sqrt((1-3)*(1-3) + (2-6)*(2-6) + (3-9)*(3-9)));
}
/* inner_product */
TEST(VectorTest, InnerProductTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
init_vector(vec2, input2);
double dist = bayon::Vector::inner_product(vec1, vec2);
EXPECT_EQ(dist, (1*3 + 2*6 + 3*9));
}
/* cosine */
TEST(VectorTest, CosineTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
init_vector(vec2, input2);
double cos = bayon::Vector::cosine(vec1, vec2);
EXPECT_EQ(
cos,
(1*3 + 2*6 + 3*9) / (sqrt(1*1 + 2*2 + 3*3) * sqrt(3*3 + 6*6 + 9*9)));
}
/* jaccard */
TEST(VectorTest, JaccardTest) {
bayon::Vector vec1, vec2;
init_vector(vec1, input1);
init_vector(vec2, input3);
double jaccard = bayon::Vector::jaccard(vec1, vec2);
double prod = bayon::Vector::inner_product(vec1, vec2);
double norm1 = vec1.norm();
double norm2 = vec2.norm();
double denom = norm1 + norm2 - prod;
if (!denom) {
EXPECT_EQ(jaccard, 0);
} else {
EXPECT_EQ((int)(jaccard * 10), (int)(prod/denom * 10));
}
}
/* main function */
int main(int argc, char **argv) {
set_input_values();
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}