1 #include "FittingStatistics.h" 3 #include "MainWindow.h" 6 #include <QtTest/QtTest> 7 #include "Test/TestFitting.h" 13 const int NOMINAL_ORDER = 6;
14 const int NOMINAL_SIGNIFICANT_DIGITS = 7;
21 void TestFitting::cleanupTestCase ()
26 bool TestFitting::generalFunctionTest (
int order,
29 int orderReduced = qMin (order, numPoints - 1);
31 const double EPSILON = 0.0001;
33 double mse, rms, rSquared;
34 FittingCurveCoefficients coefficientsGot (MAX_POLYNOMIAL_ORDER + 1);
37 bool isOverfitting = (order >= numPoints - 1);
40 FittingPointsConvenient points;
41 for (
int iPoint = 0; iPoint < numPoints; iPoint++) {
44 if (orderReduced > 0) {
46 for (
int ord = 0; ord < orderReduced; ord++) {
51 points.append (QPointF (x, y));
60 NOMINAL_SIGNIFICANT_DIGITS);
65 FittingCurveCoefficients coefficientsExpected (orderReduced + 1);
69 coefficientsExpected [0] = 0;
72 coefficientsExpected [0] = 1;
73 coefficientsExpected [1] = 1;
76 coefficientsExpected [0] = 2;
77 coefficientsExpected [1] = 3;
78 coefficientsExpected [2] = 1;
81 coefficientsExpected [0] = 6;
82 coefficientsExpected [1] = 11;
83 coefficientsExpected [2] = 6;
84 coefficientsExpected [3] = 1;
87 coefficientsExpected [0] = 24;
88 coefficientsExpected [1] = 50;
89 coefficientsExpected [2] = 35;
90 coefficientsExpected [3] = 10;
91 coefficientsExpected [4] = 1;
95 for (
int coef = 0; coef < order + 1; coef++) {
96 double coefGot = coefficientsGot [coef];
98 double coefExpected = 0;
99 if (coef <= orderReduced) {
100 coefExpected = coefficientsExpected [coef];
103 success = (success && ((qAbs (coefGot - coefExpected) < EPSILON)));
108 success = (success && ((qAbs (mse) < EPSILON)));
114 bool TestFitting::generalNonFunctionTest ()
const 116 const double EPSILON = 0.0001;
118 double mse, rms, rSquared;
119 FittingCurveCoefficients coefficientsGot (MAX_POLYNOMIAL_ORDER);
122 FittingPointsConvenient points;
123 const double Y1 = 1, Y2 = 2;
124 points.append (QPointF (1, Y1));
125 points.append (QPointF (1, Y2));
133 NOMINAL_SIGNIFICANT_DIGITS);
138 FittingCurveCoefficients coefficientsExpected (2);
139 coefficientsExpected [0] = (Y1 + Y2) / 2.0;
140 coefficientsExpected [1] = 0;
142 for (
int coef = 0; coef < 2; coef++) {
143 double coefGot = coefficientsGot [coef];
145 double coefExpected = coefficientsExpected [coef];
147 success = (success && ((qAbs (coefGot - coefExpected) < EPSILON)));
153 void TestFitting::initTestCase ()
155 const bool NO_DROP_REGRESSION =
false;
156 const QString NO_ERROR_REPORT_LOG_FILE;
157 const QString NO_REGRESSION_OPEN_FILE;
158 const bool NO_GNUPLOT_LOG_FILES =
false;
159 const bool NO_REGRESSION_IMPORT =
false;
160 const bool NO_RESET =
false;
161 const bool NO_EXPORT_ONLY =
false;
162 const bool NO_EXTRACT_IMAGE_ONLY =
false;
163 const QString NO_EXTRACT_IMAGE_EXTENSION;
164 const bool DEBUG_FLAG =
false;
165 const QStringList NO_LOAD_STARTUP_FILES;
166 const QStringList NO_COMMAND_LINE;
168 initializeLogging (
"engauge_test",
173 NO_REGRESSION_OPEN_FILE,
175 NO_REGRESSION_IMPORT,
176 NO_GNUPLOT_LOG_FILES,
179 NO_EXTRACT_IMAGE_ONLY,
180 NO_EXTRACT_IMAGE_EXTENSION,
181 NO_LOAD_STARTUP_FILES,
186 int TestFitting::orderReducedVersusOrderAndSignificantDigits (
int order,
187 int significantDigits)
const 189 FittingPointsConvenient points;
190 FittingCurveCoefficients coefficients (MAX_POLYNOMIAL_ORDER + 1);
194 for (
double x = 1; x <= 10; x += 1) {
195 double y = 100.0 / x;
196 points.append (QPointF (x, y));
199 fittingStatistics.calculateCurveFit (order,
207 for (orderReduced = MAX_POLYNOMIAL_ORDER; orderReduced > 0; orderReduced--) {
208 if (coefficients [orderReduced] != 0) {
216 void TestFitting::testFunctionExactFit01 ()
218 QVERIFY (generalFunctionTest (0, 1));
221 void TestFitting::testFunctionExactFit12 ()
223 QVERIFY (generalFunctionTest (1, 2));
226 void TestFitting::testFunctionExactFit23 ()
228 QVERIFY (generalFunctionTest (2, 3));
231 void TestFitting::testFunctionExactFit34 ()
233 QVERIFY (generalFunctionTest (3, 4));
236 void TestFitting::testFunctionOverfit11 ()
238 QVERIFY (generalFunctionTest (1, 1));
241 void TestFitting::testFunctionOverfit22 ()
243 QVERIFY (generalFunctionTest (2, 2));
246 void TestFitting::testFunctionOverfit33 ()
248 QVERIFY (generalFunctionTest (3, 3));
251 void TestFitting::testFunctionOverfit44 ()
253 QVERIFY (generalFunctionTest (4, 4));
256 void TestFitting::testFunctionUnderfit02 ()
258 QVERIFY (generalFunctionTest (0, 2));
261 void TestFitting::testFunctionUnderfit13 ()
263 QVERIFY (generalFunctionTest (1, 3));
266 void TestFitting::testFunctionUnderfit24 ()
268 QVERIFY (generalFunctionTest (2, 4));
271 void TestFitting::testFunctionUnderfit35 ()
273 QVERIFY (generalFunctionTest (3, 5));
276 void TestFitting::testNonFunction ()
278 QVERIFY (generalNonFunctionTest ());
281 void TestFitting::testOrderReduced3 ()
283 QVERIFY (orderReducedVersusOrderAndSignificantDigits (3, NOMINAL_SIGNIFICANT_DIGITS) == 3);
286 void TestFitting::testOrderReduced4 ()
288 QVERIFY (orderReducedVersusOrderAndSignificantDigits (4, NOMINAL_SIGNIFICANT_DIGITS) == 4);
291 void TestFitting::testOrderReduced5 ()
293 QVERIFY (orderReducedVersusOrderAndSignificantDigits (5, NOMINAL_SIGNIFICANT_DIGITS) == 5);
296 void TestFitting::testOrderReduced6 ()
298 QVERIFY (orderReducedVersusOrderAndSignificantDigits (6, NOMINAL_SIGNIFICANT_DIGITS) == 6);
301 void TestFitting::testSignificantDigits3 ()
303 QVERIFY (orderReducedVersusOrderAndSignificantDigits (NOMINAL_ORDER, 3) == NOMINAL_ORDER);
306 void TestFitting::testSignificantDigits4 ()
308 QVERIFY (orderReducedVersusOrderAndSignificantDigits (NOMINAL_ORDER, 4) == NOMINAL_ORDER);
311 void TestFitting::testSignificantDigits5 ()
313 QVERIFY (orderReducedVersusOrderAndSignificantDigits (NOMINAL_ORDER, 5) == NOMINAL_ORDER);
316 void TestFitting::testSignificantDigits6 ()
318 QVERIFY (orderReducedVersusOrderAndSignificantDigits (NOMINAL_ORDER, 6) == NOMINAL_ORDER);
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...