File indexing completed on 2024-04-28 03:48:08
0001 /* 0002 File : RangeTest.cpp 0003 Project : LabPlot 0004 Description : Tests for Range 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2021-2023 Stefan Gerlach <stefan.gerlach@uni.kn> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 */ 0010 0011 #include "RangeTest.h" 0012 #include "backend/lib/Range.h" 0013 #include "backend/lib/macros.h" 0014 0015 //********************************************************** 0016 //****************** Function tests ************************ 0017 //********************************************************** 0018 0019 void RangeTest::testNiceExtend() { 0020 QVector<QPair<Range<double>, Range<double>>> tests{{{0., .95}, {0., 1.}}, 0021 {{0, .91}, {0., 1.}}, 0022 {{0, .9}, {0., .9}}, 0023 {{0., .85}, {0., .9}}, 0024 {{0, .81}, {0., .9}}, 0025 {{0, .71}, {0., .8}}, 0026 {{0, .46}, {0., .5}}, 0027 {{0., .41}, {0., .45}}, 0028 {{0, .36}, {0., .4}}, 0029 {{0, .19}, {0., .2}}, 0030 {{0., .17}, {0., .18}}, 0031 {{0, 995.}, {0, 1000.}}, 0032 {{0, .21}, {0., .25}}, 0033 {{0.7, 104.9}, {0, 120}}}; 0034 QVector<QPair<Range<double>, Range<double>>> tests2{// QCOMPARE is too strict 0035 {{0., .13}, {0., .14}}, 0036 {{0, .15}, {0., .16}}, 0037 {{0., .61}, {0., .7}}, 0038 {{0, .51}, {0., .6}}, 0039 {{0, .31}, {0., .35}}, 0040 {{0.75, 2.25}, {0.6, 2.4}}, 0041 {{0., .26}, {0., .3}}}; 0042 0043 for (auto& test : tests) { 0044 DEBUG(Q_FUNC_INFO << ", " << test.first.toStdString()) 0045 test.first.niceExtend(); 0046 WARN(std::setprecision(19) << test.first.start() << " == " << test.second.start()) 0047 WARN(std::setprecision(19) << test.first.end() << " == " << test.second.end()) 0048 QCOMPARE(test.first, test.second); 0049 } 0050 for (auto& test : tests2) { 0051 DEBUG(Q_FUNC_INFO << ", " << test.first.toStdString()) 0052 test.first.niceExtend(); 0053 // WARN(std::setprecision(19) << test.first.start() << " == " << test.second.start()) 0054 // WARN(std::setprecision(19) << test.first.end() << " == " << test.second.end()) 0055 FuzzyCompare(test.first.start(), test.second.start(), DBL_EPSILON); 0056 FuzzyCompare(test.first.end(), test.second.end(), 1.e-15); 0057 } 0058 } 0059 0060 void RangeTest::testTickCount() { 0061 const QVector<QPair<Range<double>, int>> tests{ 0062 {{0., 1.}, 6}, 0063 {{0., 0.01}, 6}, 0064 {{0., 100.}, 6}, 0065 {{0., 2.}, 5}, 0066 {{0., 3.}, 4}, 0067 {{0., 4.}, 5}, 0068 {{0., 5.}, 6}, 0069 {{0., 6.}, 4}, 0070 {{0., 7.}, 8}, 0071 {{0., 8.}, 5}, 0072 {{0., 9.}, 4}, 0073 {{1.6, 2.2}, 4}, 0074 {{1.3, 2.7}, 8}, 0075 {{0., 3.2}, 5}, 0076 {{0.4, 2.}, 5}, 0077 {{0.5, 2.5}, 5} 0078 // size=29,41 should not happen when nice extended 0079 }; 0080 0081 for (auto& test : tests) { 0082 DEBUG(test.second); 0083 QCOMPARE(test.first.autoTickCount(), test.second); 0084 } 0085 } 0086 0087 void RangeTest::testLimits() { 0088 using Format = RangeT::Format; 0089 QVector<QPair<Range<double>, Range<double>>> tests{{{0, 1, Format::Numeric, RangeT::Scale::Log10}, {0.1, 1., Format::Numeric, RangeT::Scale::Log10}}, 0090 {{-1, 0, Format::Numeric, RangeT::Scale::Log10}, {1., 10., Format::Numeric, RangeT::Scale::Log10}}, 0091 {{0, 1, Format::Numeric, RangeT::Scale::Log2}, {0.5, 1., Format::Numeric, RangeT::Scale::Log2}}, 0092 {{-1, 0, Format::Numeric, RangeT::Scale::Log2}, {1., 2., Format::Numeric, RangeT::Scale::Log2}}, 0093 {{0, 1, Format::Numeric, RangeT::Scale::Ln}, {1 / M_E, 1., Format::Numeric, RangeT::Scale::Ln}}, 0094 {{-1, 0, Format::Numeric, RangeT::Scale::Ln}, {1., M_E, Format::Numeric, RangeT::Scale::Ln}}, 0095 {{-1, 0, Format::Numeric, RangeT::Scale::Sqrt}, {0., 1., Format::Numeric, RangeT::Scale::Sqrt}}, 0096 {{-1, 0, Format::Numeric, RangeT::Scale::Square}, {0., 1., Format::Numeric, RangeT::Scale::Square}}, 0097 {{-1, 0, Format::Numeric, RangeT::Scale::Inverse}, {0., 1., Format::Numeric, RangeT::Scale::Inverse}}}; 0098 0099 for (auto& test : tests) { 0100 test.first.fixLimits(); 0101 DEBUG(test.first.toStdString() << " -> " << test.second.toStdString()) 0102 QCOMPARE(test.first, test.second); 0103 } 0104 } 0105 0106 /////////////////////////////////// 0107 0108 void RangeTest::testNiceExtendLog10() { 0109 QVector<QPair<Range<double>, Range<double>>> tests{{{0.2, 201.}, {0.1, 1000.}}}; 0110 QVector<QPair<Range<double>, Range<double>>> tests2{{{0.005, 56789.}, {0.001, 100000.}}}; 0111 0112 for (auto& test : tests) { 0113 test.first.setScale(RangeT::Scale::Log10); 0114 test.second.setScale(RangeT::Scale::Log10); 0115 DEBUG(Q_FUNC_INFO << ", " << test.first.toStdString()) 0116 test.first.niceExtend(); 0117 WARN(std::setprecision(19) << test.first.start() << " == " << test.second.start()) 0118 WARN(std::setprecision(19) << test.first.end() << " == " << test.second.end()) 0119 QCOMPARE(test.first, test.second); 0120 } 0121 for (auto& test : tests2) { 0122 test.first.setScale(RangeT::Scale::Log10); 0123 test.second.setScale(RangeT::Scale::Log10); 0124 DEBUG(Q_FUNC_INFO << ", " << test.first.toStdString()) 0125 test.first.niceExtend(); 0126 // WARN(std::setprecision(19) << test.first.start() << " == " << test.second.start()) 0127 // WARN(std::setprecision(19) << test.first.end() << " == " << test.second.end()) 0128 FuzzyCompare(test.first.start(), test.second.start(), DBL_EPSILON); 0129 FuzzyCompare(test.first.end(), test.second.end(), 1.e-15); 0130 } 0131 } 0132 void RangeTest::testTickCountLog10() { 0133 QVector<QPair<Range<double>, int>> tests{{{1., 1000.}, 4}, {{0.1, 100.}, 4}, {{100., 100000.}, 4}, {{0.001, 10000.}, 8}}; 0134 0135 for (auto& test : tests) { 0136 test.first.setScale(RangeT::Scale::Log10); 0137 DEBUG(test.second); 0138 QCOMPARE(test.first.autoTickCount(), test.second); 0139 } 0140 } 0141 0142 void RangeTest::testNiceExtendLog2() { 0143 QVector<QPair<Range<double>, Range<double>>> tests{{{1.5, 7.2}, {1., 8.}}}; 0144 0145 for (auto& test : tests) { 0146 test.first.setScale(RangeT::Scale::Log2); 0147 test.second.setScale(RangeT::Scale::Log2); 0148 DEBUG(Q_FUNC_INFO << ", " << test.first.toStdString()) 0149 test.first.niceExtend(); 0150 WARN(std::setprecision(19) << test.first.start() << " == " << test.second.start()) 0151 WARN(std::setprecision(19) << test.first.end() << " == " << test.second.end()) 0152 QCOMPARE(test.first, test.second); 0153 } 0154 } 0155 void RangeTest::testTickCountLog2() { 0156 QVector<QPair<Range<double>, int>> tests{{{1., 8.}, 4}, {{.5, 4.}, 4}, {{4., 32.}, 4}, {{.25, 32.}, 8}}; 0157 0158 for (auto& test : tests) { 0159 test.first.setScale(RangeT::Scale::Log2); 0160 DEBUG(test.second); 0161 QCOMPARE(test.first.autoTickCount(), test.second); 0162 } 0163 } 0164 void RangeTest::testNiceExtendLn() { 0165 QVector<QPair<Range<double>, Range<double>>> tests{{{4., 32.}, {M_E, pow(M_E, 4.)}}}; 0166 0167 for (auto& test : tests) { 0168 test.first.setScale(RangeT::Scale::Ln); 0169 test.second.setScale(RangeT::Scale::Ln); 0170 DEBUG(Q_FUNC_INFO << ", " << test.first.toStdString()) 0171 test.first.niceExtend(); 0172 WARN(std::setprecision(19) << test.first.start() << " == " << test.second.start()) 0173 WARN(std::setprecision(19) << test.first.end() << " == " << test.second.end()) 0174 QCOMPARE(test.first, test.second); 0175 } 0176 } 0177 void RangeTest::testTickCountLn() { 0178 QVector<QPair<Range<double>, int>> tests{{{1., pow(M_E, 3)}, 4}, 0179 {{1. / M_E, pow(M_E, 2)}, 4}, 0180 {{pow(M_E, 2), pow(M_E, 5)}, 4}, 0181 {{pow(M_E, -2.), pow(M_E, 5)}, 8}}; 0182 0183 for (auto& test : tests) { 0184 test.first.setScale(RangeT::Scale::Ln); 0185 DEBUG(test.second); 0186 QCOMPARE(test.first.autoTickCount(), test.second); 0187 } 0188 } 0189 ///////////// Performance //////////////////////////////// 0190 /* 0191 void ParserTest::testPerformance1() { 0192 const int N = 1e5; 0193 0194 QBENCHMARK { 0195 for (int i = 0; i < N; i++) { 0196 const double x = i/100.; 0197 assign_symbol("x", i/100.); 0198 QCOMPARE(parse("x+1.", "C"), x+1.); 0199 } 0200 } 0201 } 0202 0203 void ParserTest::testPerformance2() { 0204 const int N = 1e5; 0205 0206 QBENCHMARK { 0207 for (int i = 0; i < N; i++) { 0208 assign_symbol("alpha", i/100.); 0209 QCOMPARE(parse("sin(alpha)^2 + cos(alpha)^2", "C"), 1.); 0210 } 0211 } 0212 }*/ 0213 0214 void RangeTest::zoomInOutIncreasingLinearRangeCenter() { 0215 Range<double> range(0., 10., RangeT::Format::Numeric, RangeT::Scale::Linear); 0216 0217 range.zoom(1. / 2, false, 0.5); 0218 0219 QCOMPARE(range.start(), 2.5); 0220 QCOMPARE(range.end(), 7.5); 0221 0222 range.zoom(2, false, 0.5); 0223 0224 QCOMPARE(range.start(), 0.); 0225 QCOMPARE(range.end(), 10.); 0226 0227 range.zoom(0.9, false, 0.5); 0228 // TODO 0229 // QCOMPARE(range.start(), 10.); 0230 // QCOMPARE(range.end(), 0.); 0231 range.zoom(1. / 0.9, false, 0.5); 0232 QCOMPARE(range.start(), 0.); 0233 QCOMPARE(range.end(), 10.); 0234 } 0235 0236 void RangeTest::zoomInOutDecreasingLinearRangeCenter() { 0237 Range<double> range(10., 0., RangeT::Format::Numeric, RangeT::Scale::Linear); 0238 0239 range.zoom(1. / 2, false, 0.5); 0240 0241 QCOMPARE(range.start(), 7.5); 0242 QCOMPARE(range.end(), 2.5); 0243 0244 range.zoom(2., false, 0.5); 0245 0246 QCOMPARE(range.start(), 10.); 0247 QCOMPARE(range.end(), 0.); 0248 0249 range.zoom(0.9, false, 0.5); 0250 // TODO 0251 // QCOMPARE(range.start(), 10.); 0252 // QCOMPARE(range.end(), 0.); 0253 range.zoom(1. / 0.9, false, 0.5); 0254 QCOMPARE(range.start(), 10.); 0255 QCOMPARE(range.end(), 0.); 0256 } 0257 0258 void RangeTest::zoomInOutIncreasingLinearRangeNotCenter() { 0259 Range<double> range(0., 10., RangeT::Format::Numeric, RangeT::Scale::Linear); 0260 0261 range.zoom(1. / 2, false, 0.9); 0262 0263 QCOMPARE(range.start(), 4.5); 0264 QCOMPARE(range.end(), 9.5); 0265 0266 range.zoom(2., false, 0.9); 0267 0268 QCOMPARE(range.start(), 0.); 0269 QCOMPARE(range.end(), 10.); 0270 0271 range.zoom(0.9, false, 0.9); 0272 // TODO 0273 // QCOMPARE(range.start(), 10.); 0274 // QCOMPARE(range.end(), 0.); 0275 range.zoom(1. / 0.9, false, 0.9); 0276 QCOMPARE(range.start(), 0.); 0277 QCOMPARE(range.end(), 10.); 0278 } 0279 0280 void RangeTest::zoomInOutDecreasingLinearRangeNotCenter() { 0281 Range<double> range(10., 0., RangeT::Format::Numeric, RangeT::Scale::Linear); 0282 0283 range.zoom(1. / 2, false, 0.1); 0284 0285 QCOMPARE(range.start(), 9.5); 0286 QCOMPARE(range.end(), 4.5); 0287 0288 range.zoom(2., false, 0.1); 0289 0290 QCOMPARE(range.start(), 10.); 0291 QCOMPARE(range.end(), 0.); 0292 0293 range.zoom(0.9, false, 0.1); 0294 // TODO 0295 // QCOMPARE(range.start(), 10.); 0296 // QCOMPARE(range.end(), 0.); 0297 range.zoom(1. / 0.9, false, 0.1); 0298 QCOMPARE(range.start(), 10.); 0299 QCOMPARE(range.end(), 0.); 0300 } 0301 0302 QTEST_MAIN(RangeTest)