File indexing completed on 2024-12-22 04:14:01

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_parse_spin_boxes_test.h"
0008 
0009 #include "kis_num_parser.h"
0010 #include "kis_double_parse_spin_box.h"
0011 #include "kis_int_parse_spin_box.h"
0012 
0013 #include <QString>
0014 #include <simpletest.h>
0015 
0016 const QStringList KisParseSpinBoxesTest::doubleExprs = {"1",
0017                                                         "-12",
0018                                                         "7.9 - 12",
0019                                                         "cos(90)*2",
0020                                                         "cos(acos(-1)+1*3^2.0)*2 + sin(3)/2"};
0021 
0022 const QStringList KisParseSpinBoxesTest::doubleWrongExprs = {"abc",
0023                                                              "1/",
0024                                                              "7.9 + 12*",
0025                                                              "cos(90)*2 + ",
0026                                                              "23.0/0",
0027                                                              "0.0/0.0"};
0028 
0029 const QStringList KisParseSpinBoxesTest::intExprs = {"12",
0030                                                      "-12",
0031                                                      "-12.3",
0032                                                      "12.7 - 25",
0033                                                      "12.7",
0034                                                      "12*1.5",
0035                                                      "12/2.5",
0036                                                      "518*2/3"};
0037 
0038 const QStringList KisParseSpinBoxesTest::intWrongExprs = {"abc",
0039                                                           "12.5/2 +",
0040                                                           "12*",
0041                                                           "12/0"};
0042 
0043 KisParseSpinBoxesTest::KisParseSpinBoxesTest() : QObject()
0044 {
0045 
0046 }
0047 
0048 void KisParseSpinBoxesTest::testDoubleParseNormal()
0049 {
0050 
0051     KisDoubleParseSpinBox spinBox;
0052     spinBox.setDecimals(3);
0053     spinBox.setMaximum(9999.0);
0054     spinBox.setMinimum(-9999.0);
0055 
0056     for (int i = 0; i < doubleExprs.size(); i++) {
0057 
0058         spinBox.clearFocus();
0059         spinBox.clear(); //clear all
0060         QTest::keyClicks(&spinBox, doubleExprs[i]);
0061         spinBox.clearFocus();
0062 
0063         double resultParser = KisNumericParser::parseSimpleMathExpr(doubleExprs[i]);
0064         double valueSpinBox = spinBox.value();
0065 
0066         bool test = resultParser == valueSpinBox || qAbs(resultParser - valueSpinBox) < 1e-2;
0067 
0068         QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3")
0069                  .arg(doubleExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str());
0070 
0071         spinBox.setValue(0);
0072 
0073     }
0074 
0075 
0076 }
0077 
0078 void KisParseSpinBoxesTest::testDoubleParseProblem()
0079 {
0080 
0081     //error can happen with incomplete or incorrect expressions, inf or nan values.
0082 
0083     KisDoubleParseSpinBox spinBox;
0084     spinBox.setMaximum(9999.0);
0085     spinBox.setMinimum(-9999.0);
0086 
0087     for (int i = 0; i < doubleWrongExprs.size(); i++) {
0088 
0089         spinBox.clearFocus();
0090         spinBox.clear(); //clear all
0091         QTest::keyClicks(&spinBox, doubleWrongExprs[i]);
0092         spinBox.clearFocus();
0093 
0094         QVERIFY2(!spinBox.isLastValid(), QString("SpinBox is in a valid state with expression %1, but shouldn't.")
0095                  .arg(doubleWrongExprs[i]).toStdString().c_str());
0096 
0097         spinBox.setValue(0.0);
0098 
0099         QVERIFY2(spinBox.isLastValid(), QString("SpinBox unable to recover error free state after a value reset.")
0100                  .toStdString().c_str());
0101 
0102         spinBox.setValue(0);
0103 
0104     }
0105 
0106 }
0107 void KisParseSpinBoxesTest::testDoubleParseWithSuffix(){
0108 
0109     QString suff = "px";
0110 
0111     KisDoubleParseSpinBox spinBox;
0112     spinBox.setDecimals(3);
0113     spinBox.setMaximum(9999.0);
0114     spinBox.setMinimum(-9999.0);
0115     spinBox.setSuffix(suff);
0116 
0117     for (int i = 0; i < doubleExprs.size(); i++) {
0118 
0119         spinBox.clearFocus();
0120         spinBox.clear(); //clear all
0121         QTest::keyClicks(&spinBox, doubleExprs[i]);
0122         spinBox.clearFocus();
0123 
0124         double resultParser = KisNumericParser::parseSimpleMathExpr(doubleExprs[i]);
0125         double valueSpinBox = spinBox.value();
0126 
0127         bool test = resultParser == valueSpinBox || qAbs(resultParser - valueSpinBox) < 1e-2;
0128 
0129         QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3")
0130                  .arg(doubleExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str());
0131 
0132         spinBox.setValue(0);
0133 
0134     }
0135 
0136     //verify that the suffix don't appear in the clean text in case of error.
0137     for (int i = 0; i < doubleWrongExprs.size(); i++) {
0138 
0139         if (doubleWrongExprs[i].endsWith(suff)) {
0140             continue;
0141         }
0142 
0143         spinBox.clearFocus();
0144         spinBox.clear(); //clear all
0145         QTest::keyClicks(&spinBox, doubleWrongExprs[i]);
0146         spinBox.clearFocus();
0147 
0148         QVERIFY2(!spinBox.cleanText().endsWith(suff), "SpinBox failed to remove suffix from clean text in error state.");
0149 
0150         spinBox.setValue(0.0);
0151 
0152     }
0153 }
0154 void KisParseSpinBoxesTest::testDoubleParseWithPrefix()
0155 {
0156 
0157     QString preff = "px";
0158 
0159     KisDoubleParseSpinBox spinBox;
0160     spinBox.setDecimals(3);
0161     spinBox.setMaximum(9999.0);
0162     spinBox.setMinimum(-9999.0);
0163     spinBox.setPrefix(preff);
0164 
0165     for (int i = 0; i < doubleExprs.size(); i++) {
0166 
0167         spinBox.clearFocus();
0168         spinBox.clear(); //clear all
0169         QTest::keyClicks(&spinBox, doubleExprs[i]);
0170         spinBox.clearFocus();
0171 
0172         double resultParser = KisNumericParser::parseSimpleMathExpr(doubleExprs[i]);
0173         double valueSpinBox = spinBox.value();
0174 
0175         bool test = resultParser == valueSpinBox || qAbs(resultParser - valueSpinBox) < 1e-2;
0176 
0177         QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3")
0178                  .arg(doubleExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str());
0179 
0180         spinBox.setValue(0);
0181 
0182     }
0183 
0184     //verify that the prefix don't appear in the clean text in case of error.
0185     for (int i = 0; i < doubleWrongExprs.size(); i++) {
0186 
0187         if (doubleWrongExprs[i].endsWith(preff)) {
0188             continue;
0189         }
0190 
0191         spinBox.clearFocus();
0192         spinBox.clear(); //clear all
0193         QTest::keyClicks(&spinBox, doubleWrongExprs[i]);
0194         spinBox.clearFocus();
0195 
0196         QVERIFY2(!spinBox.cleanText().startsWith(preff), "SpinBox failed to remove prefix from clean text in error state.");
0197 
0198         spinBox.setValue(0.0);
0199 
0200     }
0201 }
0202 
0203 void KisParseSpinBoxesTest::testIntParseNormal()
0204 {
0205 
0206     KisIntParseSpinBox spinBox;
0207     spinBox.setMaximum(999);
0208     spinBox.setMinimum(-999);
0209 
0210     for (int i = 0; i < intExprs.size(); i++) {
0211 
0212         spinBox.clearFocus();
0213         spinBox.clear(); //clear all
0214         QTest::keyClicks(&spinBox, intExprs[i]);
0215         spinBox.clearFocus();
0216 
0217         int resultParser = KisNumericParser::parseIntegerMathExpr(intExprs[i]);
0218         int valueSpinBox = spinBox.value();
0219 
0220         bool test = resultParser == valueSpinBox;
0221 
0222         QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3")
0223                  .arg(intExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str());
0224 
0225         spinBox.setValue(0);
0226     }
0227 
0228 }
0229 
0230 void KisParseSpinBoxesTest::testIntParseProblem()
0231 {
0232 
0233     //errors can happen with incorrect or incomplete expressions, or division by 0.
0234 
0235     KisIntParseSpinBox spinBox;
0236     spinBox.setMaximum(999);
0237     spinBox.setMinimum(-999);
0238 
0239     for (int i = 0; i < intWrongExprs.size(); i++) {
0240 
0241         spinBox.clearFocus();
0242         spinBox.clear(); //clear all
0243         QTest::keyClicks(&spinBox, intWrongExprs[i]);
0244         spinBox.clearFocus();
0245 
0246         QVERIFY2(!spinBox.isLastValid(), QString("SpinBox is in a valid state with expression %1, but shouldn't.")
0247                  .arg(intWrongExprs[i]).toStdString().c_str());
0248 
0249         spinBox.setValue(0);
0250 
0251         QVERIFY2(spinBox.isLastValid(), QString("SpinBox unable to recover error free state after a value reset.")
0252                  .toStdString().c_str());
0253 
0254         spinBox.setValue(0);
0255 
0256     }
0257 
0258 }
0259 
0260 void KisParseSpinBoxesTest::testIntParseWithSuffix()
0261 {
0262     QString suff = "px";
0263 
0264     KisIntParseSpinBox spinBox;
0265     spinBox.setMaximum(999);
0266     spinBox.setMinimum(-999);
0267     spinBox.setSuffix(suff);
0268 
0269     for (int i = 0; i < intExprs.size(); i++) {
0270 
0271         spinBox.clearFocus();
0272         spinBox.clear(); //clear all
0273         QTest::keyClicks(&spinBox, intExprs[i]);
0274         spinBox.clearFocus();
0275 
0276         int resultParser = KisNumericParser::parseIntegerMathExpr(intExprs[i]);
0277         int valueSpinBox = spinBox.value();
0278 
0279         bool test = resultParser == valueSpinBox;
0280 
0281         QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3")
0282                  .arg(intExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str());
0283 
0284         spinBox.setValue(0);
0285     }
0286 
0287     //verify that the suffix don't appear in the clean text in case of error.
0288     for (int i = 0; i < intWrongExprs.size(); i++) {
0289 
0290         if (intWrongExprs[i].endsWith(suff)) {
0291             continue;
0292         }
0293 
0294         spinBox.clearFocus();
0295         spinBox.clear(); //clear all
0296         QTest::keyClicks(&spinBox, intWrongExprs[i]);
0297         spinBox.clearFocus();
0298 
0299         QVERIFY2(!spinBox.cleanText().endsWith(suff), "SpinBox failed to remove suffix from clean text in error state.");
0300 
0301         spinBox.setValue(0.0);
0302 
0303     }
0304 
0305 }
0306 void KisParseSpinBoxesTest::testIntParseWithPrefix()
0307 {
0308     QString preff = "px";
0309 
0310     KisIntParseSpinBox spinBox;
0311     spinBox.setMaximum(999);
0312     spinBox.setMinimum(-999);
0313     spinBox.setPrefix(preff);
0314 
0315     for (int i = 0; i < intExprs.size(); i++) {
0316 
0317         spinBox.clearFocus();
0318         spinBox.clear(); //clear all
0319         QTest::keyClicks(&spinBox, intExprs[i]);
0320         spinBox.clearFocus();
0321 
0322         int resultParser = KisNumericParser::parseIntegerMathExpr(intExprs[i]);
0323         int valueSpinBox = spinBox.value();
0324 
0325         bool test = resultParser == valueSpinBox;
0326 
0327         QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3")
0328                  .arg(intExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str());
0329 
0330         spinBox.setValue(0);
0331     }
0332 
0333     //verify that the prefix don't appear in the clean text in case of error.
0334     for (int i = 0; i < intWrongExprs.size(); i++) {
0335 
0336         if (intWrongExprs[i].startsWith(preff)) {
0337             continue;
0338         }
0339 
0340         spinBox.clearFocus();
0341         spinBox.clear(); //clear all
0342         QTest::keyClicks(&spinBox, intWrongExprs[i]);
0343         spinBox.clearFocus();
0344 
0345         QVERIFY2(!spinBox.cleanText().startsWith(preff), "SpinBox failed to remove prefix from clean text in error state.");
0346 
0347         spinBox.setValue(0.0);
0348 
0349     }
0350 
0351 }
0352 
0353 SIMPLE_TEST_MAIN(KisParseSpinBoxesTest)