File indexing completed on 2024-05-05 16:40:59

0001 /* This file is part of the TikZKit project.
0002  *
0003  * Copyright (C) 2014 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU Library General Public License as published
0007  * by the Free Software Foundation, either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, see
0017  * <http://www.gnu.org/licenses/>.
0018  */
0019 #include "TestValue.h"
0020 
0021 #include <QtTest/QTest>
0022 
0023 #include <tikz/core/Value.h>
0024 
0025 QTEST_MAIN(ValueTest)
0026 
0027 void ValueTest::initTestCase()
0028 {
0029 }
0030 
0031 void ValueTest::cleanupTestCase()
0032 {
0033 }
0034 
0035 void ValueTest::testNaN()
0036 {
0037     tikz::Value val = tikz::Value::invalid();
0038 
0039     // toString
0040     QCOMPARE(val.toString(), QString("nan"));
0041     QCOMPARE(val, tikz::Value::invalid());
0042 
0043     // fromString
0044     val = tikz::Value::fromString("nan");
0045     QCOMPARE(val, tikz::Value::invalid());
0046 }
0047 
0048 void ValueTest::testPoint()
0049 {
0050     tikz::Value val;
0051 
0052     // test initial value
0053     QCOMPARE(val.value(), 0.0);
0054     QCOMPARE(tikz::toString(val.unit()), tikz::toString(tikz::Unit::Point));
0055     QCOMPARE(val.toString(), QString("0pt"));
0056 
0057     // test floating point value
0058     val = 3.567;
0059 
0060     QCOMPARE(val.value(), 3.567);
0061     QCOMPARE(tikz::toString(val.unit()), tikz::toString(tikz::Unit::Point));
0062     QCOMPARE(val.toString(), QString("3.567pt"));
0063 
0064     // operator += and -=
0065     val = 0;
0066     val += 3.567;
0067     QCOMPARE(val.value(), 3.567);
0068     val -= 3.567;
0069     QCOMPARE(val.value(), 0.0);
0070 
0071     // test convertTo() identity
0072     QCOMPARE(val.convertTo(tikz::Unit::Point), val);
0073 
0074     // test Value::invalid()
0075     val = tikz::Value::invalid();
0076     QVERIFY(! val.isValid());
0077 }
0078 
0079 void ValueTest::testFromString()
0080 {
0081     tikz::Value val;
0082 
0083     //
0084     // unit: point
0085     //
0086     QCOMPARE(tikz::toString(val.unit()), tikz::toString(tikz::Unit::Point));
0087 
0088     // test "0pt"
0089     QCOMPARE(val.value(), tikz::Value::fromString("0pt").value());
0090     QCOMPARE(val.value(), tikz::Value::fromString("0.pt").value());
0091     QCOMPARE(val.value(), tikz::Value::fromString("0.0pt").value());
0092     QCOMPARE(val.value(), tikz::Value::fromString("0.00pt").value());
0093     QCOMPARE(val.value(), tikz::Value::fromString("0.000pt").value());
0094     QCOMPARE(val, tikz::Value::fromString("0pt"));
0095 
0096     // test "0 pt" with space
0097     QCOMPARE(val.value(), tikz::Value::fromString("0 pt").value());
0098     QCOMPARE(val.value(), tikz::Value::fromString("0. pt").value());
0099     QCOMPARE(val.value(), tikz::Value::fromString("0.0 pt").value());
0100     QCOMPARE(val.value(), tikz::Value::fromString("0.00 pt").value());
0101     QCOMPARE(val.value(), tikz::Value::fromString("0.000 pt").value());
0102     QCOMPARE(val, tikz::Value::fromString("0 pt"));
0103 
0104     // test static Value::fromString
0105     val = 3.567;
0106     QCOMPARE(val.value(), tikz::Value::fromString("3.567pt").value());
0107     QCOMPARE(val, tikz::Value::fromString("3.567pt"));
0108 
0109     // allow a space after the number
0110     QCOMPARE(val.value(), tikz::Value::fromString("3.567 pt").value());
0111     QCOMPARE(val, tikz::Value::fromString("3.567 pt"));
0112 
0113     //
0114     // unit: Millimeter
0115     //
0116     val = tikz::Value(0.0, tikz::Unit::Millimeter);
0117     QCOMPARE(tikz::toString(val.unit()), tikz::toString(tikz::Unit::Millimeter));
0118 
0119     // test "0pt"
0120     QCOMPARE(val.value(), tikz::Value::fromString("0mm").value());
0121     QCOMPARE(val, tikz::Value::fromString("0mm"));
0122 
0123     // test "0 pt" with space
0124     QCOMPARE(val.value(), tikz::Value::fromString("0 mm").value());
0125     QCOMPARE(val, tikz::Value::fromString("0 mm"));
0126 
0127     // test static Value::fromString
0128     val += 3.567;
0129     QCOMPARE(val.value(), tikz::Value::fromString("3.567mm").value());
0130     QCOMPARE(val, tikz::Value::fromString("3.567mm"));
0131 
0132     // allow a space after the number
0133     QCOMPARE(val.value(), tikz::Value::fromString("3.567 mm").value());
0134     QCOMPARE(val, tikz::Value::fromString("3.567 mm"));
0135 }
0136 
0137 void ValueTest::testStringAccuracy()
0138 {
0139     // test numbers from 0.01 to 99.99
0140     for (int i = 0; i < 100; ++i) {
0141         for (int j = 1; j < 100; ++j) {
0142             if (j % 10 == 0) continue;
0143             QCOMPARE(tikz::Value(i + j * 0.01).toString(), QString("%1.%2pt").arg(i).arg(j, 2, 10, QLatin1Char('0')));
0144         }
0145     }
0146 
0147     // test negative numbers from -0.01 to -99.99
0148     for (int i = 0; i < 100; ++i) {
0149         for (int j = 1; j < 100; ++j) {
0150             if (j % 10 == 0) continue;
0151             QCOMPARE(tikz::Value(-i - j * 0.01).toString(), QString("-%1.%2pt").arg(i).arg(j, 2, 10, QLatin1Char('0')));
0152         }
0153     }
0154 }
0155 
0156 void ValueTest::testLiteralOperators()
0157 {
0158     QCOMPARE(1.01_pt, tikz::Value(1.01, tikz::Unit::Point));
0159     QCOMPARE(2.34_mm, tikz::Value(2.34, tikz::Unit::Millimeter));
0160     QCOMPARE(5.67_cm, tikz::Value(5.67, tikz::Unit::Centimeter));
0161     QCOMPARE(8.99_in, tikz::Value(8.99, tikz::Unit::Inch));
0162 }
0163 
0164 // kate: indent-width 4; replace-tabs on;