1 #include "FittingStatistics.h" 3 #include "MainWindow.h" 6 #include <QtTest/QtTest> 7 #include "Test/TestFitting.h" 13 const int SIGNIFICANT_DIGITS = 7;
20 void TestFitting::cleanupTestCase ()
25 bool TestFitting::generalFunctionTest (
int order,
28 int orderReduced = qMin (order, numPoints - 1);
30 const double EPSILON = 0.0001;
32 double mse, rms, rSquared;
33 FittingCurveCoefficients coefficientsGot (MAX_POLYNOMIAL_ORDER + 1);
36 bool isOverfitting = (order >= numPoints - 1);
39 FittingPointsConvenient points;
40 for (
int iPoint = 0; iPoint < numPoints; iPoint++) {
43 if (orderReduced > 0) {
45 for (
int ord = 0; ord < orderReduced; ord++) {
50 points.append (QPointF (x, y));
64 FittingCurveCoefficients coefficientsExpected (orderReduced + 1);
68 coefficientsExpected [0] = 0;
71 coefficientsExpected [0] = 1;
72 coefficientsExpected [1] = 1;
75 coefficientsExpected [0] = 2;
76 coefficientsExpected [1] = 3;
77 coefficientsExpected [2] = 1;
80 coefficientsExpected [0] = 6;
81 coefficientsExpected [1] = 11;
82 coefficientsExpected [2] = 6;
83 coefficientsExpected [3] = 1;
86 coefficientsExpected [0] = 24;
87 coefficientsExpected [1] = 50;
88 coefficientsExpected [2] = 35;
89 coefficientsExpected [3] = 10;
90 coefficientsExpected [4] = 1;
94 for (
int coef = 0; coef < order + 1; coef++) {
95 double coefGot = coefficientsGot [coef];
97 double coefExpected = 0;
98 if (coef <= orderReduced) {
99 coefExpected = coefficientsExpected [coef];
102 success = (success && ((qAbs (coefGot - coefExpected) < EPSILON)));
107 success = (success && ((qAbs (mse) < EPSILON)));
113 bool TestFitting::generalNonFunctionTest ()
const 115 const double EPSILON = 0.0001;
117 double mse, rms, rSquared;
118 FittingCurveCoefficients coefficientsGot (MAX_POLYNOMIAL_ORDER);
121 FittingPointsConvenient points;
122 const double Y1 = 1, Y2 = 2;
123 points.append (QPointF (1, Y1));
124 points.append (QPointF (1, Y2));
137 FittingCurveCoefficients coefficientsExpected (2);
138 coefficientsExpected [0] = (Y1 + Y2) / 2.0;
139 coefficientsExpected [1] = 0;
141 for (
int coef = 0; coef < 2; coef++) {
142 double coefGot = coefficientsGot [coef];
144 double coefExpected = coefficientsExpected [coef];
146 success = (success && ((qAbs (coefGot - coefExpected) < EPSILON)));
152 void TestFitting::initTestCase ()
154 const QString NO_ERROR_REPORT_LOG_FILE;
155 const QString NO_REGRESSION_OPEN_FILE;
156 const bool NO_GNUPLOT_LOG_FILES =
false;
157 const bool NO_REGRESSION_IMPORT =
false;
158 const bool NO_RESET =
false;
159 const bool NO_EXPORT_ONLY =
false;
160 const bool DEBUG_FLAG =
false;
161 const QStringList NO_LOAD_STARTUP_FILES;
163 initializeLogging (
"engauge_test",
168 NO_REGRESSION_OPEN_FILE,
169 NO_REGRESSION_IMPORT,
170 NO_GNUPLOT_LOG_FILES,
173 NO_LOAD_STARTUP_FILES);
177 void TestFitting::testFunctionExactFit01 ()
179 QVERIFY (generalFunctionTest (0, 1));
182 void TestFitting::testFunctionExactFit12 ()
184 QVERIFY (generalFunctionTest (1, 2));
187 void TestFitting::testFunctionExactFit23 ()
189 QVERIFY (generalFunctionTest (2, 3));
192 void TestFitting::testFunctionExactFit34 ()
194 QVERIFY (generalFunctionTest (3, 4));
197 void TestFitting::testFunctionOverfit11 ()
199 QVERIFY (generalFunctionTest (1, 1));
202 void TestFitting::testFunctionOverfit22 ()
204 QVERIFY (generalFunctionTest (2, 2));
207 void TestFitting::testFunctionOverfit33 ()
209 QVERIFY (generalFunctionTest (3, 3));
212 void TestFitting::testFunctionOverfit44 ()
214 QVERIFY (generalFunctionTest (4, 4));
217 void TestFitting::testFunctionUnderfit02 ()
219 QVERIFY (generalFunctionTest (0, 2));
222 void TestFitting::testFunctionUnderfit13 ()
224 QVERIFY (generalFunctionTest (1, 3));
227 void TestFitting::testFunctionUnderfit24 ()
229 QVERIFY (generalFunctionTest (2, 4));
232 void TestFitting::testFunctionUnderfit35 ()
234 QVERIFY (generalFunctionTest (3, 5));
237 void TestFitting::testNonFunction ()
239 QVERIFY (generalNonFunctionTest ());
Unit test of Fitting classes.
void calculateCurveFitAndStatistics(unsigned int order, const FittingPointsConvenient &pointsConvenient, FittingCurveCoefficients &coefficients, double &mse, double &rms, double &rSquared, int significantDigits)
Compute the curve fit and the statistics for that curve fit.
TestFitting(QObject *parent=0)
Single constructor.
This class does the math to compute statistics for FittingWindow.
Main window consisting of menu, graphics scene, status bar and optional toolbars as a Single Document...