File indexing completed on 2022-10-04 14:00:18

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Ilya Kowalewski <illya.kovalevskyy@gmail.com>
0004 //
0005 
0006 #include <QTest>
0007 #include "MarbleLocale.h"
0008 #include "MarbleGlobal.h"
0009 #include "TestUtils.h"
0010 
0011 Q_DECLARE_METATYPE(Marble::MarbleLocale::MeasurementSystem)
0012 Q_DECLARE_METATYPE(Marble::MarbleLocale::MeasureUnit)
0013 
0014 namespace Marble {
0015 
0016 class LocaleTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void initTestCase();
0022     void meterToCustomUnit();
0023     void meterToCustomUnit_data();
0024 
0025 private:
0026     MarbleLocale *locale;
0027 };
0028 
0029 void LocaleTest::initTestCase()
0030 {
0031     // Get rid of translation
0032     QLocale::setDefault(QLocale::C);
0033 
0034     locale = MarbleGlobal::getInstance()->locale();
0035 }
0036 
0037 void LocaleTest::meterToCustomUnit_data()
0038 {
0039     QTest::addColumn<MarbleLocale::MeasurementSystem>("targetSystem");
0040     QTest::addColumn<qreal>("meters");
0041     QTest::addColumn<qreal>("targetValue");
0042     QTest::addColumn<MarbleLocale::MeasureUnit>("targetUnit");
0043 
0044     // Testing metric system
0045     QTest::newRow("1") << MarbleLocale::MetricSystem << 0.003 << 3.0 << MarbleLocale::Milimeter;
0046     QTest::newRow("2") << MarbleLocale::MetricSystem << 0.017 << 1.7 << MarbleLocale::Centimeter;
0047     QTest::newRow("3") << MarbleLocale::MetricSystem << 12.3 << 12.3 << MarbleLocale::Meter;
0048     QTest::newRow("4") << MarbleLocale::MetricSystem << 7834.139 << 7.83 << MarbleLocale::Kilometer;
0049 
0050     // Testing imperial system
0051     QTest::newRow("5") << MarbleLocale::ImperialSystem << 0.1 << 3.93 << MarbleLocale::Inch;
0052     QTest::newRow("6") << MarbleLocale::ImperialSystem << 0.8 << 2.62 << MarbleLocale::Foot;
0053     QTest::newRow("7") << MarbleLocale::ImperialSystem << 1.8288 << 2.0 << MarbleLocale::Yard;
0054     QTest::newRow("8") << MarbleLocale::ImperialSystem << 37550.0 << 23.3 << MarbleLocale::Mile;
0055 
0056     // Testing nautical mile
0057     QTest::newRow("9") << MarbleLocale::NauticalSystem << 3500.0 << 1.89 << MarbleLocale::NauticalMile;
0058     QTest::newRow("10") << MarbleLocale::NauticalSystem << 1200.0 << 0.648 << MarbleLocale::NauticalMile;
0059 }
0060 
0061 void LocaleTest::meterToCustomUnit()
0062 {
0063     QFETCH(MarbleLocale::MeasurementSystem, targetSystem);
0064     QFETCH(qreal, meters);
0065     QFETCH(qreal, targetValue);
0066     QFETCH(MarbleLocale::MeasureUnit, targetUnit);
0067 
0068     qreal value;
0069     MarbleLocale::MeasureUnit unit;
0070 
0071     locale->meterToTargetUnit(meters, targetSystem, value, unit);
0072 
0073     QFUZZYCOMPARE(value, targetValue, 0.033);
0074     QCOMPARE(targetUnit, unit);
0075 }
0076 
0077 } // namespace Marble
0078 
0079 QTEST_MAIN( Marble::LocaleTest )
0080 
0081 #include "LocaleTest.moc"