-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolynomial.cpp
More file actions
151 lines (128 loc) · 3.39 KB
/
Polynomial.cpp
File metadata and controls
151 lines (128 loc) · 3.39 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
#include "Polynomial.hpp"
#include <iostream>
#include <string>
#include <vector>
// https://google.github.io/googletest/
#include <gtest/gtest.h>
// First, a few framework functions.
/**
* operator<<
*
* @brief Used for printing vectors and polynomials.
* @param os - std::ostream to insert.
* @param v - arbitrary vector whose elements are to be inserted.
* @return std::ostream to continue.
*/
template<typename T> std::ostream& operator<<(std::ostream& os,
const std::vector<T>& v)
{
for (auto t : v)
{
os << t << ' ';
}
return os;
}
template<typename T> std::ostream& operator<<(std::ostream& os,
const Polynomial<T>& p)
{
for (std::size_t i = 0; i < p.size(); ++i)
{
os << p[i] << ' ';
}
return os;
}
/**
* EXPECT_FLOAT_VECTOR_EQ(X, Y)
*
* @brief Verifies that vectors X and Y are approximately equal.
*/
void EXPECT_FLOAT_VECTOR_EQ(const std::vector<float>& x,
const std::vector<float>& y)
{
EXPECT_EQ(x.size(), y.size());
for (std::size_t i = 0; i < x.size(); ++i)
{
// The usual EXPECT_FLOAT_EQ() wasn't sloppy enough.
EXPECT_NEAR(x[i], y[i], std::abs(x[i] * .000001));
}
}
/**
* The unit tests.
*/
/**
* PolynomialTest
*
* @brief This is a test fixture class, used for tests below. PolynomialTest
* is declared as a fried to the Polynomial<> class that's under test so that
* it has access to private members for testing.
*
* All tests using PolynomialTest as a fixture get derived Test classes, so
* that their test bodies are run as class methods. The using statements in
* this declaration promote private members to protected so that these Test
* classes have access too.
*/
class PolynomialTest: public testing::Test, public Polynomial<float>
{
protected:
// Derived-class TEST_F's implicitly call their default constructors.
// This provides a useful one for testing.
PolynomialTest() : Polynomial<float>(3) {}
// Give all fixture derived classes direct access to C.
using Polynomial<float>::C;
};
TEST_F(PolynomialTest, FitQuadratic)
{
// Test 1: a polynomial fit for a quadratic.
std::vector<float> X = { 0, 1, 2, 3 };
std::vector<float> Y = { 2.1, 0.7, -0.1, 1.3 };
// Call the local fit().
fit(X, Y);
std::vector<float> Cexp = { 2.18, -2.42, 0.7 };
// Direct access to C.
EXPECT_FLOAT_VECTOR_EQ(C, Cexp);
}
/**
* Ordinary TEST without a fixture.
*/
TEST(PolyFit, Value)
{
std::vector<float> X = { 0, 1, 2, 3 };
std::vector<float> Y = { 2.1, 0.7, -0.1, 1.3 };
Polynomial<float> p(3);
p[0] = 2.18;
p[1] = -2.42;
p[2] = 0.7;
// Test 2: polynomial from test 1, test value() at various points
std::vector<float> pY(X.size());
for (std::size_t i = 0; i < X.size(); ++i)
{
pY[i] = p.value(X[i]);
}
std::vector<float> pYexp = { 2.18, 0.46, 0.14, 1.22 };
EXPECT_FLOAT_VECTOR_EQ(pY, pYexp);
}
TEST(PolyFit, MSE)
{
std::vector<float> X = { 0, 1, 2, 3 };
std::vector<float> Y = { 2.1, 0.7, -0.1, 1.3 };
Polynomial<float> p(3);
p[0] = 2.18;
p[1] = -2.42;
p[2] = 0.7;
// Test 3: test the mse()
float MSEexp = 0.128;
float MSE = p.mse(X, Y);
EXPECT_FLOAT_EQ(MSE, MSEexp);
}
/**
* main
*
* @summary: Run the GoogleTests.
*
* @return: exit code.
*/
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}