1 /*
2     SPDX-FileCopyrightText: 2021 Hy Murveit <hy@murveit.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "ekos/focus/focusstars.h"
8 
9 #include <QtTest>
10 #include <memory>
11 
12 #include <QObject>
13 
14 class TestFocusStars : public QObject
15 {
16         Q_OBJECT
17 
18     public:
19         TestFocusStars();
20         ~TestFocusStars() override = default;
21 
22     private slots:
23         void basicTest();
24 };
25 
26 #include "testfocusstars.moc"
27 
28 using Ekos::FocusStars;
29 
TestFocusStars()30 TestFocusStars::TestFocusStars() : QObject()
31 {
32 }
33 
34 #define CompareFloat(d1,d2) QVERIFY(fabs((d1) - (d2)) < .0001)
35 
makeEdge(float x,float y,float hfr)36 Edge makeEdge(float x, float y, float hfr)
37 {
38     Edge e;
39     e.x = x;
40     e.y = y;
41     e.HFR = hfr;
42     return e;
43 }
44 
45 struct Triple
46 {
47     double x, y, h;
48 };
MakeEdges(const std::vector<Triple> triples)49 QList<Edge> MakeEdges(const std::vector<Triple> triples)
50 {
51     QList<Edge> edges;
52     for (const auto &t : triples)
53         edges.append(makeEdge(t.x, t.y, t.h));
54     return edges;
55 };
56 
basicTest()57 void TestFocusStars::basicTest()
58 {
59     double maxDistance = 1.0;
60     FocusStars f1(MakeEdges(
61     {
62         {2.0, 2.0, 3.0},
63         {5.0, 5.0, 4.0},
64         {7.0, 5.0, 4.5},
65         {5.0, 1.0, 1.0},
66         {6.0, 3.0, 2.5}
67     }), maxDistance);
68 
69     FocusStars f2(MakeEdges(
70     {
71         {2.1, 2.2, 3.0},
72         {5.3, 5.2, 2.0},
73         {7.2, 5.1, 4.5},
74         {15.0, 11.0, 11.0},
75         {16.0, 13.0, 12.5}
76     }), maxDistance);
77 
78     CompareFloat(3.0, f1.getHFR());
79     CompareFloat(4.5, f2.getHFR());
80 
81     double h1, h2;
82     f1.commonHFR(f2, &h1, &h2);
83     // The stars in common are the 1st 3 of each.
84     CompareFloat(4.0, h1);
85     CompareFloat(3.0, h2);
86 
87     double hh1, hh2;
88     f2.commonHFR(f1, &hh2, &hh1);
89     CompareFloat(hh1, h1);
90     CompareFloat(hh2, h2);
91 
92     double r1 = f1.relativeHFR(f2, 10);
93     CompareFloat(13.33333, r1);
94 
95     double r2 = f2.relativeHFR(f1, 10);
96     CompareFloat(7.5, r2);
97 }
98 
99 QTEST_GUILESS_MAIN(TestFocusStars)
100