File indexing completed on 2024-05-12 05:42:41
0001 #include <cutehmi/functions.hpp> 0002 0003 #include <cutehmi/test/random.hpp> 0004 0005 #include <QtTest/QtTest> 0006 0007 namespace cutehmi { 0008 0009 class test_functions: 0010 public QObject 0011 { 0012 Q_OBJECT 0013 0014 private slots: 0015 void testApe(); 0016 0017 void testAle(); 0018 0019 void testClt(); 0020 0021 void testCgt(); 0022 0023 void testMetadata(); 0024 }; 0025 0026 void test_functions::testApe() 0027 { 0028 QCOMPARE(ape(0.0, 1.0e-100, 0.25), true); 0029 0030 QCOMPARE(ape(0.0, 0.24, 0.25), true); 0031 QCOMPARE(ape(0.0, 0.25, 0.25), true); 0032 QCOMPARE(ape(0.0, 0.26, 0.25), false); 0033 0034 QCOMPARE(ape(1.0, 1.49, 0.25), true); 0035 QCOMPARE(ape(1.0, 1.51, 0.25), false); 0036 QCOMPARE(ape(1.0, 1.99, 0.25), false); 0037 QCOMPARE(ape(1.0, 2.0, 0.25), true); // Weirdness coming from the fact that <= is used in comparison. 0038 QCOMPARE(ape(1.0, 2.01, 0.25), false); 0039 0040 QCOMPARE(ape(1.01, 1.5, 0.25), true); 0041 QCOMPARE(ape(1.01, 1.52, 0.25), false); 0042 QCOMPARE(ape(1.01, 1.99, 0.25), false); 0043 QCOMPARE(ape(1.01, 2.0, 0.25), true); 0044 QCOMPARE(ape(1.01, 2.01, 0.25), true); 0045 0046 QCOMPARE(ape(0.0, 0.0), true); 0047 QCOMPARE(ape(0.5, 0.5), true); 0048 QCOMPARE(ape(0.5e16, 0.5e16), true); 0049 QCOMPARE(ape(0.0, 0.0625), false); 0050 QCOMPARE(ape(0.5, 0.5 + 0.0625), false); 0051 0052 qreal r = test::randExp<qreal>(0, std::numeric_limits<qreal>::max_exponent); 0053 QCOMPARE(ape(r, r + std::numeric_limits<qreal>::epsilon() * 0.5), true); 0054 QCOMPARE(ape(r, 1.0625 * r), false); 0055 } 0056 0057 void test_functions::testAle() 0058 { 0059 QCOMPARE(ale(0.0, 1.0e-100, 0.25), false); // Problematic behavior of ale() function. 0060 0061 QCOMPARE(ale(1.0, 1.125, 0.25), true); 0062 QCOMPARE(ale(1.0, 1.99, 0.25), false); 0063 QCOMPARE(ale(1.0, 2.0, 0.25), false); 0064 QCOMPARE(ale(1.0, 2.01, 0.25), false); 0065 QCOMPARE(ale(2.0, 2.01, 0.25), true); 0066 } 0067 0068 void test_functions::testClt() 0069 { 0070 QCOMPARE(clt(0.0, 0.0, 0.25), false); 0071 QCOMPARE(clt(-0.125, 0.0, 0.25), false); 0072 QCOMPARE(clt(-0.5, 0.0, 0.25), true); 0073 0074 QCOMPARE(clt(-1000, -100, 0.25), true); 0075 QCOMPARE(clt(-1000, -999, 0.25), false); 0076 0077 QCOMPARE(clt(1000, 2000, 0.25), true); 0078 QCOMPARE(clt(1000, 1001, 0.25), false); 0079 } 0080 0081 void test_functions::testCgt() 0082 { 0083 QCOMPARE(cgt(0.0, 0.0, 0.25), false); 0084 QCOMPARE(cgt(0.0, -0.125, 0.25), false); 0085 QCOMPARE(cgt(0.0, -0.5, 0.25), true); 0086 0087 QCOMPARE(cgt(-100, -1000, 0.25), true); 0088 QCOMPARE(cgt(-999, -1000, 0.25), false); 0089 0090 QCOMPARE(cgt(2000, 1000, 0.25), true); 0091 QCOMPARE(cgt(1001, 1000, 0.25), false); 0092 } 0093 0094 void test_functions::testMetadata() 0095 { 0096 QJsonObject json = cutehmi::metadata("CuteHMI.2"); 0097 QCOMPARE(json.value("name").toString(), "CuteHMI.2"); 0098 QCOMPARE(json.value("cutehmiType").toString(), "extension"); 0099 QVERIFY(json.contains("dependencies")); 0100 QVERIFY(json.contains("description")); 0101 QVERIFY(json.contains("domain")); 0102 QVERIFY(json.contains("friendlyName")); 0103 QVERIFY(json.contains("major")); 0104 QVERIFY(json.contains("minor")); 0105 QVERIFY(json.contains("micro")); 0106 QVERIFY(json.contains("hash")); 0107 QVERIFY(json.contains("vendor")); 0108 } 0109 0110 } 0111 0112 QTEST_MAIN(cutehmi::test_functions) 0113 #include "test_functions.moc" 0114 0115 //(c)C: Copyright © 2019-2023, Michał Policht <michal@policht.pl>, Yuri Chornoivan <yurchor@ukr.net>. All rights reserved. 0116 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT 0117 //(c)C: This file is a part of CuteHMI. 0118 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 0119 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 0120 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>. 0121 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below. 0122 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 0123 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 0124 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.