Changeset View
Changeset View
Standalone View
Standalone View
libs/widgetutils/tests/kis_simple_math_parser_test.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU General Public License as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "kis_simple_math_parser_test.h" | ||||
20 | | ||||
21 | #include "kis_num_parser.h" | ||||
22 | | ||||
23 | #include <QString> | ||||
24 | #include <QtTest> | ||||
25 | | ||||
26 | KisSimpleMathParserTest::KisSimpleMathParserTest() : QObject() | ||||
27 | { | ||||
28 | | ||||
29 | } | ||||
30 | | ||||
31 | void KisSimpleMathParserTest::testDoubleComputation() | ||||
32 | { | ||||
33 | | ||||
34 | QStringList exprs = {"1", | ||||
35 | "2 + 3.4", | ||||
36 | "2 + -3.4", | ||||
37 | "2 - -3.4", | ||||
38 | "5-2", | ||||
39 | "7 + 2 - 5", | ||||
40 | "4.6 * 2 + 13", | ||||
41 | "4.6 / 2 + 3*3", | ||||
42 | "4.6 / 0.0 + 3*3", | ||||
43 | "-4.6 / 0.0 + 3*3", | ||||
44 | "-4.6 / -0.0 + 3*3", | ||||
45 | "4.6 / -0.0 + 3*3", | ||||
46 | "0.0 / 0.0 + 3*3", | ||||
47 | "2^3 - 4 * 1.5", | ||||
48 | "2^3.0 - 4 * 1.5", | ||||
49 | "cos(1)*2", | ||||
50 | "-cos(1)*2", | ||||
51 | "cos(1)^3*2", | ||||
52 | "cos(1)^3.0*2", | ||||
53 | "cos(1)*2 + sin(3)/2", | ||||
54 | "cos(acos(-1)+1*3^2.0)*2 + sin(3)/2", | ||||
55 | "cos(acos(-1)+1*3^2.0)^2 + sin(3)/2"}; | ||||
56 | | ||||
57 | QVector<double> expected = {1, | ||||
58 | 2 + 3.4, | ||||
59 | 2 + -3.4, | ||||
60 | 2 - -3.4, | ||||
61 | 5-2, | ||||
62 | 7 + 2 - 5, | ||||
63 | 4.6 * 2 + 13, | ||||
64 | 4.6 / 2 + 3*3, | ||||
65 | 4.6 / 0.0 + 3*3, | ||||
66 | -4.6 / 0.0 + 3*3, | ||||
67 | -4.6 / -0.0 + 3*3, | ||||
68 | 4.6 / -0.0 + 3*3, | ||||
69 | 0.0 / 0.0 + 3*3, | ||||
70 | qPow(2,3) - 4 * 1.5, | ||||
71 | qPow(2,3.0) - 4 * 1.5, | ||||
72 | qCos(1.0/180*qAcos(-1))*2, | ||||
73 | -qCos(1.0/180*qAcos(-1))*2, | ||||
74 | qPow(qCos(1.0/180*qAcos(-1)),3)*2, | ||||
75 | qPow(qCos(1.0/180*qAcos(-1)),3.0)*2, | ||||
76 | qCos(1.0/180*qAcos(-1))*2 + qSin(3.0/180*qAcos(-1))/2, | ||||
77 | qCos((qAcos(-1.0)*180/qAcos(-1)+1*qPow(3,2.0))/180*qAcos(-1))*2 + qSin(3.0/180*qAcos(-1))/2, | ||||
78 | qPow(qCos((qAcos(-1.0)*180/qAcos(-1)+1*qPow(3,2.0))/180*qAcos(-1)),2) + qSin(3.0/180*qAcos(-1))/2}; | ||||
79 | | ||||
80 | for (int i = 0; i < expected.size(); i++) { | ||||
81 | | ||||
82 | double result = KisNumericParser::parseSimpleMathExpr(exprs[i]); | ||||
83 | | ||||
84 | bool test = result == expected[i] || qAbs(result - expected[i]) < 1e-12 || (std::isnan(result) && std::isnan(expected[i])); | ||||
85 | | ||||
86 | QVERIFY2(test, QString("Failed when %1 should equal %2 but evaluated to %3.").arg(exprs[i]).arg(expected[i]).arg(result).toStdString().c_str()); | ||||
87 | } | ||||
88 | } | ||||
89 | | ||||
90 | void KisSimpleMathParserTest::testIntComputation() | ||||
91 | { | ||||
92 | | ||||
93 | QStringList exprs = {"1", | ||||
94 | "2 + 3", | ||||
95 | "2 + -3", | ||||
96 | "2 - -3", | ||||
97 | "5-2", | ||||
98 | "7 + 2 - 5", | ||||
99 | "4/3", | ||||
100 | "12/3", | ||||
101 | "4*3", | ||||
102 | "581*2/3"}; | ||||
103 | | ||||
104 | QVector<int> expected = {1, | ||||
105 | 2 + 3, | ||||
106 | 2 + -3, | ||||
107 | 2 - -3, | ||||
108 | 5-2, | ||||
109 | 7 + 2 - 5, | ||||
110 | qRound(4.0/3.0), | ||||
111 | qRound(12.0/3.0), | ||||
112 | 4*3, | ||||
113 | 581*2/3}; | ||||
114 | | ||||
115 | for (int i = 0; i < expected.size(); i++) { | ||||
116 | | ||||
117 | int result = KisNumericParser::parseIntegerMathExpr(exprs[i]); | ||||
118 | | ||||
119 | QCOMPARE(result, expected[i]); | ||||
120 | } | ||||
121 | } | ||||
122 | | ||||
123 | void KisSimpleMathParserTest::testIntFlooring() | ||||
124 | { | ||||
125 | | ||||
126 | QStringList exprs = {"4.5", | ||||
127 | "-4.5", | ||||
128 | "3.5 + 4.5", | ||||
129 | "2.8 - -3.5", | ||||
130 | "4.5/2.9", | ||||
131 | "7.6*3.2", | ||||
132 | "7.6*3.2 + 4.5" | ||||
133 | }; | ||||
134 | | ||||
135 | QVector<int> expected = {qRound(4.5), | ||||
136 | qRound(-4.5), | ||||
137 | qRound(3.5 + 4.5), | ||||
138 | qRound(2.8 - -3.5), | ||||
139 | qRound(4.5/2.9), | ||||
140 | qRound(7.6*3.2), | ||||
141 | qRound(7.6*3.2 + 4.5) | ||||
142 | }; | ||||
143 | | ||||
144 | for (int i = 0; i < expected.size(); i++) { | ||||
145 | | ||||
146 | int result = KisNumericParser::parseIntegerMathExpr(exprs[i]); | ||||
147 | | ||||
148 | QCOMPARE(result, expected[i]); | ||||
149 | } | ||||
150 | | ||||
151 | } | ||||
152 | | ||||
153 | QTEST_APPLESS_MAIN(KisSimpleMathParserTest) | ||||
154 | |