-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_linear_combination.cc
106 lines (96 loc) · 3.33 KB
/
test_linear_combination.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
/*
* Copyright (C) 2023 Swift Navigation Inc.
* Contact: Swift Navigation <[email protected]>
*
* This source is subject to the license found in the file 'LICENSE' which must
* be distributed together with this source. All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <albatross/Core>
#include <gtest/gtest.h>
namespace albatross {
TEST(test_linear_combination, test_sum) {
int x = 1;
int y = 2;
const auto combo = linear_combination::sum(x, y);
EXPECT_EQ(combo.values.size(), 2);
EXPECT_EQ(combo.values[0], x);
EXPECT_EQ(combo.values[1], y);
EXPECT_EQ(combo.coefficients.size(), 2);
EXPECT_EQ(combo.coefficients[0], 1);
EXPECT_EQ(combo.coefficients[1], 1);
}
TEST(test_linear_combination, test_sum_variant) {
int x = 1;
double y = 3.14159;
const auto combo = linear_combination::sum(x, y);
EXPECT_EQ(combo.values.size(), 2);
EXPECT_EQ(combo.values[0].get<int>(), x);
EXPECT_EQ(combo.values[1].get<double>(), y);
EXPECT_EQ(combo.coefficients.size(), 2);
EXPECT_EQ(combo.coefficients[0], 1);
EXPECT_EQ(combo.coefficients[1], 1);
}
TEST(test_linear_combination, test_difference) {
int x = 1;
int y = 2;
const auto combo = linear_combination::difference(x, y);
EXPECT_EQ(combo.values.size(), 2);
EXPECT_EQ(combo.values[0], x);
EXPECT_EQ(combo.values[1], y);
EXPECT_EQ(combo.coefficients.size(), 2);
EXPECT_EQ(combo.coefficients[0], 1);
EXPECT_EQ(combo.coefficients[1], -1);
}
TEST(test_linear_combination, test_difference_variant) {
int x = 1;
double y = 3.14159;
const auto combo = linear_combination::difference(x, y);
EXPECT_EQ(combo.values.size(), 2);
EXPECT_EQ(combo.values[0].get<int>(), x);
EXPECT_EQ(combo.values[1].get<double>(), y);
EXPECT_EQ(combo.coefficients.size(), 2);
EXPECT_EQ(combo.coefficients[0], 1);
EXPECT_EQ(combo.coefficients[1], -1);
}
TEST(test_linear_combination, test_mean) {
for (std::size_t i = 1; i < 12; ++i) {
std::vector<std::size_t> xs;
for (std::size_t j = 0; j < i; ++j) {
xs.emplace_back(j);
}
const auto combo = linear_combination::mean(xs);
EXPECT_EQ(combo.values.size(), i);
EXPECT_EQ(combo.values, xs);
EXPECT_EQ(combo.coefficients.size(), i);
const double expected_coef = 1.0 / static_cast<double>(i);
for (Eigen::Index j = 0; j < combo.coefficients.size(); ++j) {
EXPECT_EQ(combo.coefficients[j], expected_coef);
}
}
}
TEST(test_linear_combination, test_to_linear_combination) {
int x = 1;
const auto combo = linear_combination::to_linear_combination(x);
EXPECT_EQ(combo.values.size(), 1);
EXPECT_EQ(combo.values[0], x);
EXPECT_EQ(combo.coefficients.size(), 1);
EXPECT_EQ(combo.coefficients[0], 1);
}
TEST(test_linear_combination, test_to_combo) {
int x = 1;
int y = 2;
const auto diff = linear_combination::difference(x, y);
// diff is already a linear combination
const auto combo = linear_combination::to_linear_combination(diff);
EXPECT_EQ(combo.values.size(), 2);
EXPECT_EQ(combo.values[0], x);
EXPECT_EQ(combo.values[1], y);
EXPECT_EQ(combo.coefficients.size(), 2);
EXPECT_EQ(combo.coefficients[0], 1);
EXPECT_EQ(combo.coefficients[1], -1);
}
} // namespace albatross