18 class LLSQTest :
public testing::Test {
21 std::locale::global(std::locale(
""));
27 void ExpectCorrectLine(
const LLSQ& llsq,
double m,
double c,
double rms,
28 double pearson,
double tolerance) {
29 EXPECT_NEAR(m, llsq.
m(), tolerance);
30 EXPECT_NEAR(c, llsq.
c(llsq.
m()), tolerance);
31 EXPECT_NEAR(rms, llsq.
rms(llsq.
m(), llsq.
c(llsq.
m())), tolerance);
32 EXPECT_NEAR(pearson, llsq.
pearson(), tolerance);
34 FCOORD PtsMean(
const std::vector<FCOORD>& pts) {
36 for (
const auto& p : pts) {
39 return (pts.size() > 0) ? total / pts.size() : total;
41 void VerifyRmsOrth(
const std::vector<FCOORD>& pts,
const FCOORD& orth) {
43 FCOORD xavg = PtsMean(pts);
46 double expected_answer = 0;
47 for (
const auto& p : pts) {
48 llsq.
add(p.x(), p.y());
49 double dot = nvec % (p - xavg);
50 expected_answer += dot * dot;
52 expected_answer /= pts.size();
53 expected_answer = sqrt(expected_answer);
54 EXPECT_NEAR(expected_answer, llsq.
rms_orth(orth), 0.0001);
56 void ExpectCorrectVector(
const LLSQ& llsq,
FCOORD correct_mean_pt,
57 FCOORD correct_vector,
float tolerance) {
60 EXPECT_NEAR(correct_mean_pt.
x(), mean_pt.
x(), tolerance);
61 EXPECT_NEAR(correct_mean_pt.
y(), mean_pt.
y(), tolerance);
62 EXPECT_NEAR(correct_vector.
x(), vector.
x(), tolerance);
63 EXPECT_NEAR(correct_vector.
y(), vector.
y(), tolerance);
68 TEST_F(LLSQTest, BasicLines) {
72 ExpectCorrectLine(llsq, 1.0, 0.0, 0.0, 1.0, 1e-6);
73 float half_root_2 = sqrt(2.0) / 2.0f;
74 ExpectCorrectVector(llsq,
FCOORD(1.5f, 1.5f),
75 FCOORD(half_root_2, half_root_2), 1e-6);
82 ExpectCorrectLine(llsq, 0.0, 1.25, 0.433, 0.0, 1e-2);
83 ExpectCorrectVector(llsq,
FCOORD(1.0f, 1.25f),
FCOORD(1.0f, 0.0f), 1e-3);
84 llsq.
add(1.0, 2.0, 10.0);
86 ExpectCorrectLine(llsq, 0.0, 1.786, 0.41, 0.0, 1e-2);
87 ExpectCorrectVector(llsq,
FCOORD(1.0f, 1.786f),
FCOORD(1.0f, 0.0f), 1e-3);
91 TEST_F(LLSQTest, Vectors) {
95 ExpectCorrectVector(llsq,
FCOORD(1.0f, 0.0f),
FCOORD(0.0f, 1.0f), 1e-6);
99 llsq.
add(1.10001, 3.0);
100 ExpectCorrectVector(llsq,
FCOORD(1.0f, 0.0f),
FCOORD(0.0f, 1.0f), 1e-3);
105 TEST_F(LLSQTest, RmsOrthWorksAsIntended) {
106 std::vector<FCOORD> pts;
107 pts.push_back(
FCOORD(0.56, 0.95));
108 pts.push_back(
FCOORD(0.09, 0.09));
109 pts.push_back(
FCOORD(0.13, 0.77));
110 pts.push_back(
FCOORD(0.16, 0.83));
111 pts.push_back(
FCOORD(0.45, 0.79));
112 VerifyRmsOrth(pts,
FCOORD(1, 0));
113 VerifyRmsOrth(pts,
FCOORD(1, 1));
114 VerifyRmsOrth(pts,
FCOORD(1, 2));
115 VerifyRmsOrth(pts,
FCOORD(2, 1));