File indexing completed on 2024-05-12 16:36:00
0001 /* This file is part of the KDE project 0002 Copyright 2009 Marijn Kruisselbrink <mkruisselbrink@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; only 0007 version 2 of the License. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 #include "TestValueFormatter.h" 0020 0021 #include <ValueFormatter.h> 0022 0023 #include <CalculationSettings.h> 0024 #include <ValueConverter.h> 0025 #include <ValueParser.h> 0026 0027 #include <KLocale> 0028 0029 #include <QTest> 0030 0031 Q_DECLARE_METATYPE(Calligra::Sheets::Format::Type) 0032 Q_DECLARE_METATYPE(Calligra::Sheets::Style::FloatFormat) 0033 0034 using namespace Calligra::Sheets; 0035 0036 class PublicValueFormatter : public ValueFormatter 0037 { 0038 public: 0039 explicit PublicValueFormatter(const ValueConverter* converter) 0040 : ValueFormatter(converter) {} 0041 0042 using ValueFormatter::fractionFormat; 0043 using ValueFormatter::createNumberFormat; 0044 }; 0045 0046 void TestValueFormatter::initTestCase() 0047 { 0048 qRegisterMetaType<Format::Type>(); 0049 0050 m_calcsettings = new CalculationSettings(); 0051 m_parser = new ValueParser(m_calcsettings); 0052 m_converter = new ValueConverter(m_parser); 0053 } 0054 0055 void TestValueFormatter::cleanupTestCase() 0056 { 0057 delete m_converter; 0058 delete m_parser; 0059 delete m_calcsettings; 0060 } 0061 0062 void TestValueFormatter::testFractionFormat_data() 0063 { 0064 QTest::addColumn<double>("value"); 0065 QTest::addColumn<Format::Type>("formatType"); 0066 QTest::addColumn<QString>("result"); 0067 0068 // fraction_half 0069 QTest::newRow("half 0.0") << 0.0 << Format::fraction_half << "0"; 0070 QTest::newRow("half 1.0") << 1.0 << Format::fraction_half << "1"; 0071 QTest::newRow("half 1.5") << 1.5 << Format::fraction_half << "1 1/2"; 0072 QTest::newRow("half 1.4") << 1.4 << Format::fraction_half << "1 1/2"; 0073 QTest::newRow("half 1.6") << 1.6 << Format::fraction_half << "1 1/2"; 0074 QTest::newRow("half 0.9") << 0.9 << Format::fraction_half << "1"; 0075 QTest::newRow("half 1.1") << 1.1 << Format::fraction_half << "1"; 0076 QTest::newRow("half -0.2") << -0.2 << Format::fraction_half << "-0"; 0077 QTest::newRow("half -0.4") << -0.4 << Format::fraction_half << "-1/2"; 0078 QTest::newRow("half -0.6") << -0.6 << Format::fraction_half << "-1/2"; 0079 QTest::newRow("half -0.9") << -0.9 << Format::fraction_half << "-1"; 0080 0081 // fraction_quarter 0082 QTest::newRow("quarter 0.0") << 0.0 << Format::fraction_quarter << "0"; 0083 QTest::newRow("quarter 1.0") << 1.0 << Format::fraction_quarter << "1"; 0084 QTest::newRow("quarter 0.1") << 0.1 << Format::fraction_quarter << "0"; 0085 QTest::newRow("quarter 0.2") << 0.2 << Format::fraction_quarter << "1/4"; 0086 QTest::newRow("quarter 0.3") << 0.3 << Format::fraction_quarter << "1/4"; 0087 QTest::newRow("quarter 0.5") << 0.5 << Format::fraction_quarter << "2/4"; 0088 QTest::newRow("quarter 0.8") << 0.8 << Format::fraction_quarter << "3/4"; 0089 QTest::newRow("quarter 0.9") << 0.9 << Format::fraction_quarter << "1"; 0090 QTest::newRow("quarter 1.1") << 1.1 << Format::fraction_quarter << "1"; 0091 QTest::newRow("quarter 1.2") << 1.2 << Format::fraction_quarter << "1 1/4"; 0092 QTest::newRow("quarter -0.1") << -0.1 << Format::fraction_quarter << "-0"; 0093 QTest::newRow("quarter -0.2") << -0.2 << Format::fraction_quarter << "-1/4"; 0094 QTest::newRow("quarter -0.3") << -0.3 << Format::fraction_quarter << "-1/4"; 0095 QTest::newRow("quarter -0.5") << -0.5 << Format::fraction_quarter << "-2/4"; 0096 QTest::newRow("quarter -0.8") << -0.8 << Format::fraction_quarter << "-3/4"; 0097 QTest::newRow("quarter -0.9") << -0.9 << Format::fraction_quarter << "-1"; 0098 0099 // fraction_eighth 0100 QTest::newRow("eighth 0.0") << 0.0 << Format::fraction_eighth << "0"; 0101 QTest::newRow("eighth 1.0") << 1.0 << Format::fraction_eighth << "1"; 0102 QTest::newRow("eighth 0.05") << 0.05 << Format::fraction_eighth << "0"; 0103 QTest::newRow("eighth 0.10") << 0.10 << Format::fraction_eighth << "1/8"; 0104 QTest::newRow("eighth 0.15") << 0.15 << Format::fraction_eighth << "1/8"; 0105 QTest::newRow("eighth 0.25") << 0.25 << Format::fraction_eighth << "2/8"; 0106 QTest::newRow("eighth 0.50") << 0.50 << Format::fraction_eighth << "4/8"; 0107 QTest::newRow("eighth 0.90") << 0.90 << Format::fraction_eighth << "7/8"; 0108 QTest::newRow("eighth 0.95") << 0.95 << Format::fraction_eighth << "1"; 0109 QTest::newRow("eighth 1.05") << 1.05 << Format::fraction_eighth << "1"; 0110 QTest::newRow("eighth 1.10") << 1.10 << Format::fraction_eighth << "1 1/8"; 0111 QTest::newRow("eighth -0.05") << -0.05 << Format::fraction_eighth << "-0"; 0112 QTest::newRow("eighth -0.10") << -0.10 << Format::fraction_eighth << "-1/8"; 0113 QTest::newRow("eighth -0.15") << -0.15 << Format::fraction_eighth << "-1/8"; 0114 QTest::newRow("eighth -0.10") << -0.25 << Format::fraction_eighth << "-2/8"; 0115 QTest::newRow("eighth -0.90") << -0.90 << Format::fraction_eighth << "-7/8"; 0116 QTest::newRow("eighth -0.95") << -0.95 << Format::fraction_eighth << "-1"; 0117 0118 // fraction_sixteenth 0119 0120 // fraction_tenth 0121 QTest::newRow("tenth 0.0") << 0.0 << Format::fraction_tenth << "0"; 0122 QTest::newRow("tenth 1.0") << 1.0 << Format::fraction_tenth << "1"; 0123 QTest::newRow("tenth 0.04") << 0.04 << Format::fraction_tenth << "0"; 0124 QTest::newRow("tenth 0.06") << 0.06 << Format::fraction_tenth << "1/10"; 0125 QTest::newRow("tenth 0.14") << 0.14 << Format::fraction_tenth << "1/10"; 0126 QTest::newRow("tenth 0.53") << 0.53 << Format::fraction_tenth << "5/10"; 0127 QTest::newRow("tenth 0.94") << 0.94 << Format::fraction_tenth << "9/10"; 0128 QTest::newRow("tenth 0.97") << 0.97 << Format::fraction_tenth << "1"; 0129 QTest::newRow("tenth 1.02") << 1.02 << Format::fraction_tenth << "1"; 0130 QTest::newRow("tenth 1.47") << 1.47 << Format::fraction_tenth << "1 5/10"; 0131 QTest::newRow("tenth -0.04") << -0.04 << Format::fraction_tenth << "-0"; 0132 QTest::newRow("tenth -0.06") << -0.06 << Format::fraction_tenth << "-1/10"; 0133 QTest::newRow("tenth -0.14") << -0.14 << Format::fraction_tenth << "-1/10"; 0134 QTest::newRow("tenth -0.53") << -0.53 << Format::fraction_tenth << "-5/10"; 0135 QTest::newRow("tenth -0.94") << -0.94 << Format::fraction_tenth << "-9/10"; 0136 QTest::newRow("tenth -0.97") << -0.97 << Format::fraction_tenth << "-1"; 0137 0138 // fraction_hundredth 0139 0140 // fraction_one_digit 0141 QTest::newRow("one_digit 0.0") << 0.0 << Format::fraction_one_digit << "0"; 0142 QTest::newRow("one_digit 0.05") << 0.05 << Format::fraction_one_digit << "0"; 0143 QTest::newRow("one_digit 0.1") << 0.1 << Format::fraction_one_digit << "1/9"; 0144 QTest::newRow("one_digit 0.2") << 0.2 << Format::fraction_one_digit << "1/5"; 0145 QTest::newRow("one_digit 0.3") << 0.3 << Format::fraction_one_digit << "2/7"; 0146 QTest::newRow("one_digit 0.4") << 0.4 << Format::fraction_one_digit << "2/5"; 0147 QTest::newRow("one_digit 0.5") << 0.5 << Format::fraction_one_digit << "1/2"; 0148 QTest::newRow("one_digit 0.6") << 0.6 << Format::fraction_one_digit << "3/5"; 0149 QTest::newRow("one_digit 0.7") << 0.7 << Format::fraction_one_digit << "5/7"; 0150 QTest::newRow("one_digit 0.8") << 0.8 << Format::fraction_one_digit << "4/5"; 0151 QTest::newRow("one_digit 0.9") << 0.9 << Format::fraction_one_digit << "8/9"; 0152 QTest::newRow("one_digit 0.95") << 0.95 << Format::fraction_one_digit << "1"; 0153 QTest::newRow("one_digit 1.0") << 1.0 << Format::fraction_one_digit << "1"; 0154 QTest::newRow("one_digit 1.1") << 1.1 << Format::fraction_one_digit << "1 1/9"; 0155 QTest::newRow("one_digit 1.2") << 1.2 << Format::fraction_one_digit << "1 1/5"; 0156 QTest::newRow("one_digit 1.3") << 1.3 << Format::fraction_one_digit << "1 2/7"; 0157 QTest::newRow("one_digit -0.05") << -0.05 << Format::fraction_one_digit << "-0"; 0158 QTest::newRow("one_digit -0.1") << -0.1 << Format::fraction_one_digit << "-1/9"; 0159 QTest::newRow("one_digit -0.2") << -0.2 << Format::fraction_one_digit << "-1/5"; 0160 QTest::newRow("one_digit -0.3") << -0.3 << Format::fraction_one_digit << "-2/7"; 0161 0162 // fraction_two_digits 0163 QTest::newRow("two_digits 0.00") << 0.00 << Format::fraction_two_digits << "0"; 0164 QTest::newRow("two_digits 0.005") << 0.005 << Format::fraction_two_digits << "0"; 0165 QTest::newRow("two_digits 0.01") << 0.01 << Format::fraction_two_digits << "1/99"; 0166 QTest::newRow("two_digits 0.02") << 0.02 << Format::fraction_two_digits << "1/50"; 0167 QTest::newRow("two_digits 0.03") << 0.03 << Format::fraction_two_digits << "2/67"; 0168 QTest::newRow("two_digits 0.07") << 0.07 << Format::fraction_two_digits << "4/57"; 0169 QTest::newRow("two_digits 0.09") << 0.09 << Format::fraction_two_digits << "8/89"; 0170 QTest::newRow("two_digits 0.11") << 0.11 << Format::fraction_two_digits << "10/91"; 0171 QTest::newRow("two_digits 0.995") << 0.995 << Format::fraction_two_digits << "1"; 0172 QTest::newRow("two_digits 1.00") << 1.00 << Format::fraction_two_digits << "1"; 0173 QTest::newRow("two_digits 1.01") << 1.01 << Format::fraction_two_digits << "1 1/99"; 0174 QTest::newRow("two_digits 1.02") << 1.02 << Format::fraction_two_digits << "1 1/50"; 0175 QTest::newRow("two_digits 1.03") << 1.03 << Format::fraction_two_digits << "1 2/67"; 0176 QTest::newRow("two_digits -0.005") << -0.005 << Format::fraction_two_digits << "-0"; 0177 QTest::newRow("two_digits -0.01") << -0.01 << Format::fraction_two_digits << "-1/99"; 0178 QTest::newRow("two_digits -0.02") << -0.02 << Format::fraction_two_digits << "-1/50"; 0179 QTest::newRow("two_digits -0.03") << -0.03 << Format::fraction_two_digits << "-2/67"; 0180 QTest::newRow("two_digits -0.07") << -0.07 << Format::fraction_two_digits << "-4/57"; 0181 0182 // fraction_three_digits 0183 } 0184 0185 void TestValueFormatter::testFractionFormat() 0186 { 0187 QFETCH(double, value); 0188 QFETCH(Format::Type, formatType); 0189 QFETCH(QString, result); 0190 0191 Number num(value); 0192 PublicValueFormatter fmt(m_converter); 0193 QCOMPARE(fmt.fractionFormat(num, formatType), result); 0194 } 0195 0196 void TestValueFormatter::testCreateNumberFormat_data() 0197 { 0198 QTest::addColumn<double>("value"); 0199 QTest::addColumn<int>("precision"); 0200 QTest::addColumn<Format::Type>("formatType"); 0201 QTest::addColumn<Style::FloatFormat>("floatFormat"); 0202 QTest::addColumn<QString>("currencySymbol"); 0203 QTest::addColumn<QString>("formatString"); 0204 QTest::addColumn<bool>("thousandsSep"); 0205 QTest::addColumn<QString>("result"); 0206 0207 Style::FloatFormat def = Style::DefaultFloatFormat; 0208 0209 QTest::newRow("negative sign in format string") << 0210 -5.0 << 0 << Format::Number << Style::DefaultFloatFormat << "" << "(-.)" << false << "(-5)"; 0211 0212 QTest::newRow("unspecified precision 1") << 0213 1.0 << -1 << Format::Number << Style::DefaultFloatFormat << "" << "0" << false << "1"; 0214 QTest::newRow("unspecified precision 0.5") << 0215 0.5 << -1 << Format::Number << Style::DefaultFloatFormat << "" << "0" << false << "0.5"; 0216 0217 QTest::newRow("no thousands separators") << 0218 3000.0 << 0 << Format::Number << Style::DefaultFloatFormat << "" << "" << false << "3000"; 0219 QTest::newRow("with thousands separator") << 0220 3000.0 << 0 << Format::Number << Style::DefaultFloatFormat << "" << "" << true << "3,000"; 0221 0222 // scientific 0223 Format::Type scient = Format::Scientific; 0224 QTest::newRow("scientific: precision=0") << 123.0 << 0 << scient << def << "" << "" << false << "1E+02"; 0225 QTest::newRow("scientific: precision=1") << 123.0 << 1 << scient << def << "" << "" << false << "1.2E+02"; 0226 QTest::newRow("scientific: precision=2") << 123.0 << 2 << scient << def << "" << "" << false << "1.23E+02"; 0227 QTest::newRow("scientific: precision=3") << 123.0 << 3 << scient << def << "" << "" << false << "1.230E+02"; 0228 QTest::newRow("scientific: precision=-1") << 123.0 << -1 << scient << def << "" << "" << false << "1.23E+02"; 0229 0230 QTest::newRow("scientific: format=0.E+0") << 123.0 << 0 << scient << def << "" << "0.E+0" << false << "1E+2"; 0231 QTest::newRow("scientific: format=0.0E+0") << 123.0 << 0 << scient << def << "" << "0.0E+0" << false << "1.2E+2"; 0232 QTest::newRow("scientific: format=0.00E+0") << 123.0 << 0 << scient << def << "" << "0.00E+0" << false << "1.23E+2"; 0233 QTest::newRow("scientific: format=0.000E+0") << 123.0 << 0 << scient << def << "" << "0.000E+0" << false << "1.230E+2"; 0234 // pre/post 0235 QTest::newRow("scientific: format=aaa 0.E+0 bbb") << 123.0 << 0 << scient << def << "" << "aaa 0.E+0 bbb" << false << "aaa 1E+2 bbb"; 0236 0237 // min number of exponent digits 0238 QTest::newRow("scientific: exponent=1") << 123.0 << 0 << scient << def << "" << "0.E+0" << false << "1E+2"; 0239 QTest::newRow("scientific: exponent=2") << 123.0 << 0 << scient << def << "" << "0.E+00" << false << "1E+02"; 0240 QTest::newRow("scientific: exponent=3") << 123.0 << 0 << scient << def << "" << "0.E+000" << false << "1E+002"; 0241 0242 // big numbers 0243 QTest::newRow("scientific: big, exponent=1") << 1234567890123456789012345678901.0 << 0 << scient << def << "" << "0.E+0" << false << "1E+30"; 0244 QTest::newRow("scientific: big, exponent=2") << 1234567890123456789012345678901.0 << 0 << scient << def << "" << "0.E+00" << false << "1E+30"; 0245 QTest::newRow("scientific: big, exponent=3") << 1234567890123456789012345678901.0 << 0 << scient << def << "" << "0.E+000" << false << "1E+030"; 0246 QTest::newRow("scientific: big, round up") << 1234567890123456789012345678901.0 << 0 << scient << def << "" << "0.000000E+000" << false << "1.234568E+030"; 0247 0248 QTest::newRow("scientific: 10, exponent=1") << 12345678901.0 << 0 << scient << def << "" << "0.0E+0" << false << "1.2E+10"; 0249 QTest::newRow("scientific: 10, exponent=2") << 12345678901.0 << 0 << scient << def << "" << "0.0E+00" << false << "1.2E+10"; 0250 QTest::newRow("scientific: 10, exponent=3") << 12345678901.0 << 0 << scient << def << "" << "0.0E+000" << false << "1.2E+010"; 0251 0252 QTest::newRow("scientific: dec pnt + exp end in 0") << 12345678901.0 << 0 << scient << def << "" << "0.0E+000" << false << "1.2E+010"; 0253 QTest::newRow("scientific: dec pnt + value end in 0") << 12300000000.0 << 0 << scient << def << "" << "0.00000E+000" << false << "1.23000E+010"; 0254 0255 QTest::newRow("with thousands separators and decimal point") << 0256 3000123.456 << 3 << Format::Number << Style::DefaultFloatFormat << "" << "" << true << "3,000,123.456"; 0257 0258 } 0259 0260 void TestValueFormatter::testCreateNumberFormat() 0261 { 0262 QFETCH(double, value); 0263 QFETCH(int, precision); 0264 QFETCH(Format::Type, formatType); 0265 QFETCH(Style::FloatFormat, floatFormat); 0266 QFETCH(QString, currencySymbol); 0267 QFETCH(QString, formatString); 0268 QFETCH(bool, thousandsSep); 0269 QFETCH(QString, result); 0270 0271 Number num(value); 0272 PublicValueFormatter fmt(m_converter); 0273 0274 // FIXME: fix tests when ported to QLocale 0275 QString res = result; 0276 QString decpoint = m_calcsettings->locale()->decimalSymbol(); 0277 QString thousep = m_calcsettings->locale()->thousandsSeparator(); 0278 int decimalpos = res.indexOf('.'); 0279 if (thousep != ",") { 0280 if (res.contains(',')) { 0281 res = res.replace(',', thousep.at(0)); 0282 } 0283 } 0284 if (decpoint != ".") { 0285 if (decimalpos != -1) { 0286 decimalpos = res.lastIndexOf('.'); // the thousand separator may now be '.' 0287 res = res.replace(decimalpos, 1, decpoint.at(0)); 0288 } 0289 } 0290 QCOMPARE(fmt.createNumberFormat(num, precision, formatType, floatFormat, currencySymbol, formatString, thousandsSep), res); 0291 } 0292 0293 0294 QTEST_MAIN(TestValueFormatter)