File indexing completed on 2024-07-21 04:35:14

0001 /*
0002     SPDX-FileCopyrightText: 2010 Thomas Baumgart ipwizard @users.sourceforge.net
0003 
0004     This file is part of libalkimia.
0005 
0006     SPDX-License-Identifier: LGPL-2.1-or-later
0007 */
0008 
0009 #include "alkvaluetest.h"
0010 
0011 #include "alkimia/alkvalue.h"
0012 #include "test.h"
0013 
0014 QTEST_GUILESS_MAIN(AlkValueTest)
0015 
0016 namespace QTest {
0017 template<>
0018 char *toString(const AlkValue &val)
0019 {
0020     return qstrdup(val.toString().toUtf8());
0021 }
0022 }
0023 
0024 void AlkValueTest::init()
0025 {
0026 }
0027 
0028 void AlkValueTest::cleanup()
0029 {
0030 }
0031 
0032 void AlkValueTest::emptyCtor()
0033 {
0034     AlkValue *m = new AlkValue();
0035     QCOMPARE(m->toString(), QLatin1String("0/1"));
0036     delete m;
0037 }
0038 
0039 void AlkValueTest::copyCtor()
0040 {
0041     AlkValue a(41, 152);
0042     AlkValue b(a);
0043 
0044     QCOMPARE(a.toString(), QLatin1String("41/152"));
0045     QCOMPARE(b.toString(), QLatin1String("41/152"));
0046 }
0047 
0048 void AlkValueTest::intCtor()
0049 {
0050     AlkValue *m;
0051     m = new AlkValue(10);
0052     QCOMPARE(m->toString(), QLatin1String("10/1"));
0053     delete m;
0054 
0055     m = new AlkValue(-10);
0056     QCOMPARE(m->toString(), QLatin1String("-10/1"));
0057     delete m;
0058 
0059     m = new AlkValue(10, 2);
0060     QCOMPARE(m->toString(), QLatin1String("5/1"));
0061     delete m;
0062 
0063     m = new AlkValue(-10, 2);
0064     QCOMPARE(m->toString(), QLatin1String("-5/1"));
0065     delete m;
0066 
0067     // the denominator is an unsigned value thus sticking in a negative
0068     // one does not make sense
0069     m = new AlkValue(-10, -2);
0070     QVERIFY(m->toString() != QLatin1String("-5/1"));
0071     delete m;
0072 }
0073 
0074 void AlkValueTest::stringCtor()
0075 {
0076     AlkValue *m;
0077 
0078     // mixed mode used for some price information in QIF
0079     m = new AlkValue(QLatin1String("5 8/16"), QLatin1Char('.'));
0080     QCOMPARE(*m, AlkValue(5.5));
0081     delete m;
0082 
0083     // standard representation
0084     m = new AlkValue(QLatin1String("12345/100"), QLatin1Char('.'));
0085     QCOMPARE(m->toString(), QLatin1String("2469/20"));
0086     delete m;
0087 
0088     // negative standard representation
0089     m = new AlkValue(QLatin1String("-8/32"), QLatin1Char('.'));
0090     QCOMPARE(*m, AlkValue(-0.25));
0091     delete m;
0092 
0093     // false negative standard representation
0094     m = new AlkValue(QLatin1String("(8/32)"), QLatin1Char('.'));
0095     QCOMPARE(*m, AlkValue(-832));
0096     delete m;
0097 
0098     // duplicate negative standard representation
0099     m = new AlkValue(QLatin1String("(-8/32)"), QLatin1Char('.'));
0100     QCOMPARE(*m, AlkValue(-832));
0101     delete m;
0102 
0103     // duplicate negative standard representation
0104     m = new AlkValue(QLatin1String("-(8/32)"), QLatin1Char('.'));
0105     QCOMPARE(*m, AlkValue(-832));
0106     delete m;
0107 
0108     // different decimal symbol
0109     m = new AlkValue(QLatin1String("x1.234,568 EUR"), QLatin1Char(','));
0110     QCOMPARE(m->toString(), QLatin1String("154321/125"));
0111     delete m;
0112 
0113     // octal leadin
0114     m = new AlkValue(QLatin1String("0.07"), QLatin1Char('.'));
0115     QCOMPARE(m->toString(), QLatin1String("7/100"));
0116     delete m;
0117     m = new AlkValue(QLatin1String("0.09"), QLatin1Char('.'));
0118     QCOMPARE(m->toString(), QLatin1String("9/100"));
0119     delete m;
0120     m = new AlkValue(QLatin1String("09"), QLatin1Char('.'));
0121     QCOMPARE(m->toString(), QLatin1String("9/1"));
0122     delete m;
0123 
0124     // negative numbers
0125     m = new AlkValue(QLatin1String("x(1,234.)"), QLatin1Char('.'));
0126     QCOMPARE(*m, AlkValue(-1234));
0127     delete m;
0128     m = new AlkValue(QLatin1String("x-1,234."), QLatin1Char('.'));
0129     QCOMPARE(*m, AlkValue(-1234));
0130     delete m;
0131     m = new AlkValue(QLatin1String("x1,234.-"), QLatin1Char('.'));
0132     QCOMPARE(*m, AlkValue(-1234));
0133     delete m;
0134 
0135     // empty string
0136     m = new AlkValue(QLatin1String(""), QLatin1Char('.'));
0137     QCOMPARE(*m, AlkValue());
0138     delete m;
0139     m = new AlkValue(QLatin1String("."), QLatin1Char('.'));
0140     QCOMPARE(*m, AlkValue());
0141     delete m;
0142     m = new AlkValue(QLatin1String(","), QLatin1Char('.'));
0143     QCOMPARE(*m, AlkValue());
0144     delete m;
0145     m = new AlkValue(QLatin1String("."), QLatin1Char(','));
0146     QCOMPARE(*m, AlkValue());
0147     delete m;
0148 }
0149 
0150 void AlkValueTest::doubleCtor()
0151 {
0152     for (int i = -123456; i < 123456; ++i) {
0153         double d = i;
0154         AlkValue r(i, 100);
0155         d /= 100;
0156         AlkValue t(d, 100);
0157         QCOMPARE(t, r);
0158     }
0159 
0160     AlkValue a = AlkValue(1.9999999999998);
0161     QVERIFY(a != AlkValue(2, 1));
0162     QVERIFY(a < AlkValue(2, 1));
0163     QVERIFY(a > AlkValue(QLatin1String("1.999999999"), QLatin1Char('.')));
0164 
0165     a = AlkValue(1.9999999999998, 100);
0166     QCOMPARE(a, AlkValue(2, 1));
0167 
0168     a = AlkValue(1.234, AlkValue::precisionToDenominator(2).get_ui());
0169     QCOMPARE(a, AlkValue(123, 100));
0170 }
0171 
0172 void AlkValueTest::assignment()
0173 {
0174     // const AlkValue& operator=(const AlkValue& val);
0175     AlkValue m0;
0176     AlkValue m1(10, 2);
0177     m0 = m1;
0178     QCOMPARE(m0.toString(), QLatin1String("5/1"));
0179 
0180     // const AlkValue& operator=(int num);
0181     m0 = 6;
0182     QCOMPARE(m0.toString(), QLatin1String("6/1"));
0183     m0 = -12;
0184     QCOMPARE(m0.toString(), QLatin1String("-12/1"));
0185 
0186     // const AlkValue& operator=(double num);
0187     m0 = 123.45;
0188     QCOMPARE(m0.toString(), QLatin1String("8687021468732621/70368744177664"));
0189     m0 = 1.23e7;
0190     QCOMPARE(m0.toString(), QLatin1String("12300000/1"));
0191 
0192     // const AlkValue& operator=(const QString& str);
0193     m0 = QLatin1String("x1234.567");
0194     QCOMPARE(m0.toString(), QLatin1String("1234567/1000"));
0195     m0 = QLatin1String("(x1234.567)");
0196     QCOMPARE(m0.toString(), QLatin1String("-1234567/1000"));
0197     m0 = QLatin1String("-1234.567");
0198     QCOMPARE(m0.toString(), QLatin1String("-1234567/1000"));
0199 }
0200 
0201 void AlkValueTest::equality()
0202 {
0203     AlkValue m0, m1;
0204     m0 = 123;
0205     m1 = QLatin1String("123");
0206     QCOMPARE(m0, m1);
0207 
0208     m0 = QLatin1String("511/100");
0209     m1 = QLatin1String("5753348523965686/1125899906842600");
0210     QCOMPARE(m0, m1);
0211 
0212     m0 = QLatin1String("-14279570/100");
0213     m1 = QLatin1String("-1427957/10");
0214     QCOMPARE(m0, m1);
0215 
0216     m0 = QLatin1String("-7301028/100");
0217     m1 = QLatin1String("-1825257/25");
0218     QCOMPARE(m0, m1);
0219 }
0220 
0221 void AlkValueTest::inequality()
0222 {
0223     AlkValue m0, m1;
0224     m0 = 123;
0225     m1 = QLatin1String("124");
0226     QVERIFY(m0 != m1);
0227 
0228     m0 = QLatin1String("511/100");
0229     m1 = QLatin1String("5753348523965809/1125899906842624");
0230     QVERIFY(m0 != m1);
0231 }
0232 
0233 void AlkValueTest::less()
0234 {
0235     AlkValue m0, m1;
0236     m0 = 12;
0237     m1 = 13;
0238     QVERIFY(m0 < m1);
0239     m0 = -m0;
0240     m1 = -m1;
0241     QVERIFY(m1 < m0);
0242 
0243     m0 = 12;
0244     m1 = AlkValue(QLatin1String("12.0000000000000000000000000000001"), QLatin1Char('.'));
0245     QVERIFY(m0 < m1);
0246     QVERIFY(!(m1 < m0));
0247     m0 = -m0;
0248     m1 = -m1;
0249     QVERIFY(m1 < m0);
0250     QVERIFY(!(m0 < m1));
0251 
0252     m0 = 12;
0253     m1 = m0;
0254     QVERIFY(!(m0 < m1));
0255 }
0256 
0257 void AlkValueTest::greater()
0258 {
0259     AlkValue m0, m1;
0260     m0 = 12;
0261     m1 = 13;
0262     QVERIFY(m1 > m0);
0263     m0 = -m0;
0264     m1 = -m1;
0265     QVERIFY(m0 > m1);
0266 
0267     m0 = 12;
0268     m1 = AlkValue(QLatin1String("12.0000000000000000000000000000001"), QLatin1Char('.'));
0269     QVERIFY(m1 > m0);
0270     QVERIFY(!(m0 > m1));
0271     m0 = -m0;
0272     m1 = -m1;
0273     QVERIFY(m0 > m1);
0274     QVERIFY(!(m1 > m0));
0275 
0276     m0 = 12;
0277     m1 = m0;
0278     QVERIFY(!(m0 > m1));
0279 }
0280 
0281 void AlkValueTest::lessThan()
0282 {
0283     AlkValue m0, m2;
0284     AlkValue m1 = AlkValue(QLatin1String("12.0000000000000000000000000000001"), QLatin1Char('.'));
0285     m0 = 12;
0286     m2 = 12;
0287     QVERIFY(m0 <= m1);
0288     QVERIFY(m0 <= m2);
0289     m0 = -m0;
0290     m1 = -m1;
0291     m2 = -m2;
0292     QVERIFY(m1 <= m0);
0293     QVERIFY(m2 <= m0);
0294 }
0295 
0296 void AlkValueTest::greaterThan()
0297 {
0298     AlkValue m0, m2;
0299     AlkValue m1 = AlkValue(QLatin1String("12.0000000000000000000000000000001"), QLatin1Char('.'));
0300     m0 = 12;
0301     m2 = 12;
0302     QVERIFY(m1 >= m0);
0303     QVERIFY(m2 >= m0);
0304     m0 = -m0;
0305     m1 = -m1;
0306     m2 = -m2;
0307     QVERIFY(m0 >= m1);
0308     QVERIFY(m0 >= m2);
0309 }
0310 
0311 void AlkValueTest::addition()
0312 {
0313     // AlkValue operator+( const AlkValue& summand ) const;
0314     AlkValue m0, m1;
0315     m0 = 100;
0316     m1 = 23;
0317     QCOMPARE((m0 + m1), AlkValue(123));
0318 
0319     // AlkValue& operator+= ( const AlkValue&  val );
0320     m0 += m1;
0321     QCOMPARE(m0, AlkValue(123));
0322 
0323     m0 = 100;
0324     m1 = -23;
0325     QCOMPARE((m0 + m1), AlkValue(77));
0326 
0327     m0 += m1;
0328     QCOMPARE(m0, AlkValue(77));
0329 }
0330 
0331 void AlkValueTest::subtraction()
0332 {
0333     // AlkValue operator-( const AlkValue& minuend ) const;
0334     AlkValue m0, m1;
0335     m0 = 100;
0336     m1 = 23;
0337     QCOMPARE((m0 - m1), AlkValue(77));
0338 
0339     // AlkValue& operator-= ( const AlkValue&  val );
0340     m0 -= m1;
0341     QCOMPARE(m0, AlkValue(77));
0342 
0343     m0 = 100;
0344     m1 = -23;
0345     QCOMPARE((m0 - m1), AlkValue(123));
0346 
0347     m0 -= m1;
0348     QCOMPARE(m0, AlkValue(123));
0349 }
0350 
0351 void AlkValueTest::multiplication()
0352 {
0353     // AlkValue operator*( const AlkValue& factor ) const;
0354     AlkValue m0, m1;
0355     m0 = 100;
0356     m1 = 23;
0357     QCOMPARE((m0 * m1), AlkValue(2300));
0358 
0359     // AlkValue& operator*= ( const AlkValue&  val );
0360     m0 *= m1;
0361     QCOMPARE(m0, AlkValue(2300));
0362 
0363     m0 = 100;
0364     m1 = -23;
0365     QCOMPARE((m0 * m1), AlkValue(-2300));
0366 
0367     m0 *= m1;
0368     QCOMPARE(m0, AlkValue(-2300));
0369 
0370     // AlkValue operator*( int factor) const;
0371     QCOMPARE((m1 * 4), AlkValue(-92));
0372     QCOMPARE((m1 *(-4)), AlkValue(92));
0373 }
0374 
0375 void AlkValueTest::division()
0376 {
0377     // AlkValue operator/( const AlkValue& divisor ) const;
0378     AlkValue m0, m1;
0379     m0 = 100;
0380     m1 = 20;
0381     QCOMPARE((m0 / m1), AlkValue(5));
0382 
0383     // AlkValue& operator/= ( const AlkValue&  val );
0384     m0 /= m1;
0385     QCOMPARE(m0, AlkValue(5));
0386 
0387     m0 = 100;
0388     m1 = -20;
0389     QCOMPARE((m0 / m1), AlkValue(-5));
0390 
0391     m0 /= m1;
0392     QCOMPARE(m0, AlkValue(-5));
0393 }
0394 
0395 void AlkValueTest::modulo()
0396 {
0397     AlkValue m0(1025000), m1;
0398     m1 = m0 % 97;
0399     QCOMPARE(m1.abs(), AlkValue(1));
0400 
0401     m0 = 1024999;
0402     m1 = m0 % 97;
0403     QCOMPARE(m1.abs(), AlkValue(0));
0404 }
0405 
0406 void AlkValueTest::unaryMinus()
0407 {
0408     // AlkValue operator-() const;
0409     AlkValue m0(5);
0410     QCOMPARE(-m0, AlkValue(-5));
0411 }
0412 
0413 void AlkValueTest::abs()
0414 {
0415     AlkValue m0(-5);
0416     AlkValue m1(5);
0417     QCOMPARE(m0.abs(), AlkValue(5));
0418     QCOMPARE(m1.abs(), AlkValue(5));
0419 }
0420 
0421 void AlkValueTest::precision()
0422 {
0423     AlkValue a(QLatin1String("1234567890"), QLatin1Char('.'));
0424     AlkValue b(QLatin1String("1234567890"), QLatin1Char('.'));
0425     AlkValue c;
0426 
0427     // QVERIFY(c.isZero() == true);
0428     c = a * b;
0429     QCOMPARE(c, AlkValue(QLatin1String("1524157875019052100"), QLatin1Char('.')));
0430     c /= b;
0431     QCOMPARE(c, AlkValue(QLatin1String("1234567890"), QLatin1Char('.')));
0432 }
0433 
0434 void AlkValueTest::convertDenominator()
0435 {
0436     AlkValue a(123.456);
0437     QCOMPARE(a.convertDenominator(), AlkValue(12346, 100));
0438 
0439     AlkValue b;
0440     a = QLatin1String("-73010.28");
0441     b = QLatin1String("1.95583");
0442     QCOMPARE((a * b).convertDenominator(100), AlkValue(-14279570, 100));
0443 
0444     a = QLatin1String("-142795.69");
0445     QCOMPARE((a / b).convertDenominator(100), AlkValue(-7301028, 100));
0446 
0447     a = QLatin1String("142795.69");
0448     QCOMPARE((a / b).convertDenominator(100), AlkValue(7301028, 100));
0449 
0450     a = AlkValue(1.9999999999998);
0451     QVERIFY(a != AlkValue(2, 1));
0452     QVERIFY(a < AlkValue(2, 1));
0453     QVERIFY(a > AlkValue(QLatin1String("1.999999999"), QLatin1Char('.')));
0454 
0455     a = AlkValue(1.9999999999998, 100);
0456     QCOMPARE(a, AlkValue(2, 1));
0457 
0458     mpz_class denominator(1000000); // 10^6
0459     denominator *= denominator; // 10^12
0460     denominator *= denominator; // 10^24
0461     const auto s = denominator.get_str();
0462     QCOMPARE(QString::fromUtf8(s.data(), int(s.size())), QLatin1String("1000000000000000000000000"));
0463     a = AlkValue(QLatin1String("1.123456789212345678931234567"), QLatin1Char('.'));
0464     b = AlkValue(QLatin1String("1.123456789212345678931234"), QLatin1Char('.'));
0465     QCOMPARE(a.toString(), QLatin1String("1123456789212345678931234567/1000000000000000000000000000"));
0466     QCOMPARE(a.convertDenominator(denominator, AlkValue::RoundTruncate), b);
0467 }
0468 
0469 void AlkValueTest::convertPrecision()
0470 {
0471     AlkValue a(123.456);
0472     QCOMPARE(a.convertPrecision(), AlkValue(12346, 100));
0473 
0474     AlkValue b;
0475     a = QLatin1String("-73010.28");
0476     b = QLatin1String("1.95583");
0477     QCOMPARE((a * b).convertPrecision(2), AlkValue(-14279570, 100));
0478 
0479     a = QLatin1String("-142795.69");
0480     QCOMPARE((a / b).convertPrecision(2), AlkValue(-7301028, 100));
0481 
0482     a = QLatin1String("142795.69");
0483     QCOMPARE((a / b).convertPrecision(2), AlkValue(7301028, 100));
0484 
0485     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundFloor), AlkValue());
0486     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundFloor), AlkValue(-1));
0487     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundFloor), AlkValue(1));
0488     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundFloor), AlkValue(-2));
0489 
0490     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundCeil), AlkValue(1));
0491     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundCeil), AlkValue());
0492     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundCeil), AlkValue(2));
0493     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundCeil), AlkValue(-1));
0494 
0495     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundTruncate), AlkValue());
0496     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundTruncate), AlkValue());
0497     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundTruncate), AlkValue(1));
0498     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundTruncate), AlkValue(-1));
0499 
0500     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundPromote), AlkValue(1));
0501     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundPromote), AlkValue(-1));
0502     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundPromote), AlkValue(2));
0503     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundPromote), AlkValue(-2));
0504 
0505     QCOMPARE(AlkValue(4, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue());
0506     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue());
0507     QCOMPARE(AlkValue(6, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(1));
0508     QCOMPARE(AlkValue(-4, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue());
0509     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue());
0510     QCOMPARE(AlkValue(-6, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(-1));
0511     QCOMPARE(AlkValue(14, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(1));
0512     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(1));
0513     QCOMPARE(AlkValue(16, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(2));
0514     QCOMPARE(AlkValue(-14, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(-1));
0515     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(-1));
0516     QCOMPARE(AlkValue(-16, 10).convertPrecision(0, AlkValue::RoundHalfDown), AlkValue(-2));
0517 
0518     QCOMPARE(AlkValue(4, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue());
0519     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(1));
0520     QCOMPARE(AlkValue(6, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(1));
0521     QCOMPARE(AlkValue(-4, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue());
0522     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(-1));
0523     QCOMPARE(AlkValue(-6, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(-1));
0524     QCOMPARE(AlkValue(14, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(1));
0525     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(2));
0526     QCOMPARE(AlkValue(16, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(2));
0527     QCOMPARE(AlkValue(-14, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(-1));
0528     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(-2));
0529     QCOMPARE(AlkValue(-16, 10).convertPrecision(0, AlkValue::RoundHalfUp), AlkValue(-2));
0530 
0531     QCOMPARE(AlkValue(5, 10).convertPrecision(0, AlkValue::RoundRound), AlkValue());
0532     QCOMPARE(AlkValue(-5, 10).convertPrecision(0, AlkValue::RoundRound), AlkValue());
0533     QCOMPARE(AlkValue(15, 10).convertPrecision(0, AlkValue::RoundRound), AlkValue(2));
0534     QCOMPARE(AlkValue(-15, 10).convertPrecision(0, AlkValue::RoundRound), AlkValue(-2));
0535     QCOMPARE(AlkValue(25, 10).convertPrecision(0, AlkValue::RoundRound), AlkValue(2));
0536     QCOMPARE(AlkValue(-25, 10).convertPrecision(0, AlkValue::RoundRound), AlkValue(-2));
0537 }
0538 
0539 void AlkValueTest::denominatorToPrecision()
0540 {
0541     QVERIFY(AlkValue::denominatorToPrecision(100) == 2);
0542     QVERIFY(AlkValue::denominatorToPrecision(1000000) == 6);
0543     QVERIFY(AlkValue::denominatorToPrecision(1) == 0);
0544     QVERIFY(AlkValue::denominatorToPrecision(0) == 0);
0545     QVERIFY(AlkValue::denominatorToPrecision(-1) == 0);
0546     QVERIFY(AlkValue::denominatorToPrecision(-188) == 0);
0547     QVERIFY(AlkValue::denominatorToPrecision(200) == 3);
0548 }
0549 
0550 void AlkValueTest::precisionToDenominator()
0551 {
0552     QVERIFY(AlkValue::precisionToDenominator(2) == 100);
0553     QVERIFY(AlkValue::precisionToDenominator(6) == 1000000);
0554     QVERIFY(AlkValue::precisionToDenominator(0) == 1);
0555     QVERIFY(AlkValue::precisionToDenominator(-1) == 1);
0556     QVERIFY(AlkValue::precisionToDenominator(-5) == 1);
0557 }
0558 
0559 void AlkValueTest::valueRef()
0560 {
0561     AlkValue a(5);
0562 
0563     mpq_class &val = a.valueRef();
0564     val = mpq_class(1, 3);
0565 
0566     QCOMPARE(a, AlkValue(1, 3));
0567 
0568     a = QLatin1String("1/7");
0569 
0570     QCOMPARE(val, mpq_class(1, 7));
0571 }
0572 
0573 void AlkValueTest::canonicalize()
0574 {
0575     AlkValue a(5);
0576     mpq_class &val(a.valueRef());
0577     QCOMPARE(val, mpq_class(5, 1));
0578 
0579     mpz_class i;
0580     i = 10;
0581     mpq_set_num(val.get_mpq_t(), i.get_mpz_t());
0582     i = 2;
0583     mpq_set_den(val.get_mpq_t(), i.get_mpz_t());
0584     QVERIFY(val != mpq_class(5, 1));
0585     QCOMPARE(val, mpq_class(10, 2));
0586 
0587     a.canonicalize();
0588     QCOMPARE(val, mpq_class(5, 1));
0589 }