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_simple_math_parser_test.h"
0008 
0009 #include "kis_num_parser.h"
0010 #include <qnumeric.h> // for qIsNaN
0011 #include <QString>
0012 #include <simpletest.h>
0013 
0014 KisSimpleMathParserTest::KisSimpleMathParserTest() : QObject()
0015 {
0016 
0017 }
0018 
0019 void KisSimpleMathParserTest::testDoubleComputation()
0020 {
0021 
0022     QStringList exprs = {"1",
0023                       "2 + 3.4",
0024                       "2 + -3.4",
0025                       "2 - -3.4",
0026                       "5-2",
0027                       "7 + 2 - 5",
0028                       "4.6 * 2 + 13",
0029                       "4.6 / 2 + 3*3",
0030                       "4.6 / 0.0 + 3*3",
0031                       "-4.6 / 0.0 + 3*3",
0032                       "-4.6 / -0.0 + 3*3",
0033                       "4.6 / -0.0 + 3*3",
0034                       "0.0 / 0.0 + 3*3",
0035                       "2^3 - 4 * 1.5",
0036                       "2^3.0 - 4 * 1.5",
0037                       "cos(1)*2",
0038                       "-cos(1)*2",
0039                       "cos(1)^3*2",
0040                       "cos(1)^3.0*2",
0041                       "cos(1)*2 + sin(3)/2",
0042                       "cos(acos(-1)+1*3^2.0)*2 + sin(3)/2",
0043                       "cos(acos(-1)+1*3^2.0)^2 + sin(3)/2",
0044                       "log10(100)",
0045                       "exp(10)",
0046                       "ln(10)"};
0047 
0048     QVector<double> expected = {1,
0049                              2 + 3.4,
0050                              2 + -3.4,
0051                              2 - -3.4,
0052                              5-2,
0053                              7 + 2 - 5,
0054                              4.6 * 2 + 13,
0055                              4.6 / 2 + 3*3,
0056                              4.6 / 0.0 + 3*3,
0057                              -4.6 / 0.0 + 3*3,
0058                              -4.6 / -0.0 + 3*3,
0059                              4.6 / -0.0 + 3*3,
0060                              0.0 / 0.0 + 3*3,
0061                              qPow(2,3) - 4 * 1.5,
0062                              qPow(2,3.0) - 4 * 1.5,
0063                              qCos(1.0/180*qAcos(-1))*2,
0064                              -qCos(1.0/180*qAcos(-1))*2,
0065                              qPow(qCos(1.0/180*qAcos(-1)),3)*2,
0066                              qPow(qCos(1.0/180*qAcos(-1)),3.0)*2,
0067                              qCos(1.0/180*qAcos(-1))*2 + qSin(3.0/180*qAcos(-1))/2,
0068                              qCos((qAcos(-1.0)*180/qAcos(-1)+1*qPow(3,2.0))/180*qAcos(-1))*2 + qSin(3.0/180*qAcos(-1))/2,
0069                              qPow(qCos((qAcos(-1.0)*180/qAcos(-1)+1*qPow(3,2.0))/180*qAcos(-1)),2) + qSin(3.0/180*qAcos(-1))/2,
0070                              qLn(100)/qLn(10),
0071                              qExp(10),
0072                              qLn(10)};
0073 
0074     for (int i = 0; i < expected.size(); i++) {
0075 
0076         double result = KisNumericParser::parseSimpleMathExpr(exprs[i]);
0077 
0078         bool test = result == expected[i] || qAbs(result - expected[i]) < 1e-12 || (qIsNaN(result) && qIsNaN(expected[i]));
0079 
0080         QVERIFY2(test, QString("Failed when %1 should equal %2 but evaluated to %3.").arg(exprs[i]).arg(expected[i]).arg(result).toStdString().c_str());
0081     }
0082 }
0083 
0084 void KisSimpleMathParserTest::testIntComputation()
0085 {
0086 
0087     QStringList exprs = {"1",
0088                       "2 + 3",
0089                       "2 + -3",
0090                       "2 - -3",
0091                       "5-2",
0092                       "7 + 2 - 5",
0093                       "4/3",
0094                       "12/3",
0095                       "4*3",
0096                       "581*2/3"};
0097 
0098     QVector<int> expected = {1,
0099                              2 + 3,
0100                              2 + -3,
0101                              2 - -3,
0102                              5-2,
0103                              7 + 2 - 5,
0104                              qRound(4.0/3.0),
0105                              qRound(12.0/3.0),
0106                              4*3,
0107                              581*2/3};
0108 
0109     for (int i = 0; i < expected.size(); i++) {
0110 
0111         int result = KisNumericParser::parseIntegerMathExpr(exprs[i]);
0112 
0113         QCOMPARE(result, expected[i]);
0114     }
0115 }
0116 
0117 void KisSimpleMathParserTest::testIntFlooring()
0118 {
0119 
0120     QStringList exprs = {"4.5",
0121                       "-4.5",
0122                       "3.5 + 4.5",
0123                       "2.8 - -3.5",
0124                       "4.5/2.9",
0125                       "7.6*3.2",
0126                       "7.6*3.2 + 4.5"
0127                         };
0128 
0129     QVector<int> expected = {qRound(4.5),
0130                              qRound(-4.5),
0131                              qRound(3.5 + 4.5),
0132                              qRound(2.8 - -3.5),
0133                              qRound(4.5/2.9),
0134                              qRound(7.6*3.2),
0135                              qRound(7.6*3.2 + 4.5)
0136                             };
0137 
0138     for (int i = 0; i < expected.size(); i++) {
0139 
0140         int result = KisNumericParser::parseIntegerMathExpr(exprs[i]);
0141 
0142         QCOMPARE(result, expected[i]);
0143     }
0144 
0145 }
0146 
0147 QTEST_APPLESS_MAIN(KisSimpleMathParserTest)
0148