File indexing completed on 2024-05-12 16:46:15

0001 /***************************************************************************
0002     Copyright (C) 2010 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #undef QT_NO_CAST_FROM_ASCII
0026 
0027 #include "comparisontest.h"
0028 #include "../models/stringcomparison.h"
0029 #include "../models/fieldcomparison.h"
0030 #include "../config/tellico_config.h"
0031 
0032 #include <QTest>
0033 
0034 QTEST_GUILESS_MAIN( ComparisonTest )
0035 
0036 void ComparisonTest::initTestCase() {
0037   Tellico::Config::setArticlesString(QStringLiteral("the,l'"));
0038 }
0039 
0040 void ComparisonTest::testNumber() {
0041   QFETCH(QString, string1);
0042   QFETCH(QString, string2);
0043   QFETCH(int, res);
0044 
0045   Tellico::NumberComparison comp;
0046 
0047   QCOMPARE(comp.compare(string1, string2), res);
0048 }
0049 
0050 void ComparisonTest::testNumber_data() {
0051   QTest::addColumn<QString>("string1");
0052   QTest::addColumn<QString>("string2");
0053   QTest::addColumn<int>("res");
0054 
0055   QTest::newRow("null") << QString() << QString() << 0;
0056   QTest::newRow("< 0") << QString() << QStringLiteral("0") << -1;
0057   QTest::newRow("> 0") << QStringLiteral("0") << QString() << 1;
0058   QTest::newRow("=1 1") << QStringLiteral("1") << QStringLiteral("1") << 0;
0059   QTest::newRow("< 1") << QStringLiteral("0") << QStringLiteral("1") << -1;
0060   QTest::newRow("> 1") << QStringLiteral("1") << QStringLiteral("0") << 1;
0061   QTest::newRow("> -1") << QStringLiteral("0") << QStringLiteral("-1") << 1;
0062   QTest::newRow("< -1") << QStringLiteral("-1") << QStringLiteral("0") << -1;
0063   QTest::newRow("> 10") << QStringLiteral("10") << QStringLiteral("5") << 5;
0064   QTest::newRow("< 10") << QStringLiteral("5") << QStringLiteral("10") << -5;
0065   QTest::newRow("multiple1") << QStringLiteral("1; 2") << QStringLiteral("2") << -1;
0066   QTest::newRow("multiple2") << QStringLiteral("3; 2") << QStringLiteral("2") << 1;
0067   QTest::newRow("multiple3") << QStringLiteral("2") << QStringLiteral("2; 3") << -1;
0068   QTest::newRow("multiple4") << QStringLiteral("1; 2") << QStringLiteral("1; 3") << -1;
0069   QTest::newRow("float1") << QStringLiteral("5.1") << QStringLiteral("6.9") << -2;
0070   QTest::newRow("float2") << QStringLiteral("5.1") << QStringLiteral("5.2") << -1;
0071   QTest::newRow("float3") << QStringLiteral("5.2") << QStringLiteral("5.1") << 1;
0072   QTest::newRow("float4") << QStringLiteral("5.1") << QStringLiteral("5.1") << 0;
0073 }
0074 
0075 void ComparisonTest::testLCC() {
0076   QFETCH(QString, string1);
0077   QFETCH(QString, string2);
0078   QFETCH(int, res);
0079 
0080   Tellico::LCCComparison comp;
0081 
0082   QCOMPARE(comp.compare(string1, string2), res);
0083 }
0084 
0085 void ComparisonTest::testLCC_data() {
0086   QTest::addColumn<QString>("string1");
0087   QTest::addColumn<QString>("string2");
0088   QTest::addColumn<int>("res");
0089 
0090   QTest::newRow("null") << QString() << QString() << 0;
0091   QTest::newRow("null1") << QString() << QStringLiteral("B") << -1;
0092   QTest::newRow("null2") << QStringLiteral("B") << QString() << 1;
0093   QTest::newRow("test1") << QStringLiteral("BX932 .C53 1993") << QStringLiteral("BX2230.3") << -1;
0094   QTest::newRow("test2") << QStringLiteral("BX932 .C53 1993") << QStringLiteral("BX2380 .R67 2002") << -1;
0095   QTest::newRow("test3") << QStringLiteral("AE25 E3 2002") << QStringLiteral("AE5 E333 2003") << 1;
0096 }
0097 
0098 void ComparisonTest::testDate() {
0099   QFETCH(QString, string1);
0100   QFETCH(QString, string2);
0101   QFETCH(int, res);
0102 
0103   Tellico::ISODateComparison comp;
0104 
0105   QCOMPARE(comp.compare(string1, string2), res);
0106 }
0107 
0108 void ComparisonTest::testDate_data() {
0109   QTest::addColumn<QString>("string1");
0110   QTest::addColumn<QString>("string2");
0111   QTest::addColumn<int>("res");
0112 
0113   QTest::newRow("null") << QString() << QString() << 0;
0114   QTest::newRow("null1") << QString() << QStringLiteral("2001") << -1;
0115   QTest::newRow("null2") << QStringLiteral("2001") << QString() << 1;
0116   QTest::newRow("test1") << QStringLiteral("2001-9-8") << QStringLiteral("2001-10-12") << -1;
0117   QTest::newRow("test2") << QStringLiteral("1998") << QStringLiteral("2020-01-01") << -1;
0118   QTest::newRow("test3") << QStringLiteral("2008--") << QStringLiteral("2008-1-1") << 0;
0119   QTest::newRow("test5") << QStringLiteral("2008-2-2") << QStringLiteral("2008-02-02") << 0;
0120   // non-integers get converted to current date
0121   QTest::newRow("words") << QStringLiteral("all-by-myself") << QStringLiteral("---") << 0;
0122   // no longer in 2020, so all dashes converts to current year, after 2020
0123   QTest::newRow("words") << QStringLiteral("2020--") << QStringLiteral("---") << -1;
0124 }
0125 
0126 void ComparisonTest::testTitle() {
0127   QFETCH(QString, string1);
0128   QFETCH(QString, string2);
0129   QFETCH(int, res);
0130 
0131   Tellico::TitleComparison comp;
0132 
0133   QCOMPARE(comp.compare(string1, string2), res);
0134 }
0135 
0136 void ComparisonTest::testTitle_data() {
0137   QTest::addColumn<QString>("string1");
0138   QTest::addColumn<QString>("string2");
0139   QTest::addColumn<int>("res");
0140 
0141   QTest::newRow("null") << QString() << QString() << 0;
0142   QTest::newRow("null1") << QString() << QStringLiteral("The One") << -1;
0143   QTest::newRow("null2") << QStringLiteral("The One") << QString() << 1;
0144   // not that they're equal, but that "The One" should be sorted under "One"
0145   QTest::newRow("test3") << QStringLiteral("The One") << QStringLiteral("One, The") << -1;
0146   QTest::newRow("test4") << QStringLiteral("The One") << QStringLiteral("one, the") << -1;
0147   QTest::newRow("test5") << QStringLiteral("l'One") << QStringLiteral("one, the") << -1;
0148   QTest::newRow("test6") << QStringLiteral("l'One") << QStringLiteral("the one") << 0;
0149   QTest::newRow("test7") << QStringLiteral("All One") << QStringLiteral("the one") << -1;
0150 }
0151 
0152 void ComparisonTest::testString() {
0153   QFETCH(QString, string1);
0154   QFETCH(QString, string2);
0155   QFETCH(int, res);
0156 
0157   Tellico::StringComparison comp;
0158 
0159   QCOMPARE(comp.compare(string1, string2), res);
0160 }
0161 
0162 void ComparisonTest::testString_data() {
0163   QTest::addColumn<QString>("string1");
0164   QTest::addColumn<QString>("string2");
0165   QTest::addColumn<int>("res");
0166 
0167   QTest::newRow("null") << QString() << QString() << 0;
0168   QTest::newRow("null1") << QString() << QStringLiteral("string") << -1;
0169   QTest::newRow("null2") << QStringLiteral("string") << QString() << 1;
0170   QTest::newRow("test1") << QStringLiteral("string1") << QStringLiteral("string1") << 0;
0171   QTest::newRow("test2") << QStringLiteral("string1") << QStringLiteral("string2") << -1;
0172 }
0173 
0174 void ComparisonTest::testBool() {
0175   QFETCH(QString, string1);
0176   QFETCH(QString, string2);
0177   QFETCH(int, res);
0178 
0179   Tellico::BoolComparison comp;
0180 
0181   QCOMPARE(comp.compare(string1, string2), res);
0182 }
0183 
0184 void ComparisonTest::testBool_data() {
0185   QTest::addColumn<QString>("string1");
0186   QTest::addColumn<QString>("string2");
0187   QTest::addColumn<int>("res");
0188 
0189   QTest::newRow("null") << QString() << QString() << 0;
0190   QTest::newRow("null1") << QString() << QStringLiteral("true") << -1;
0191   QTest::newRow("null2") << QStringLiteral("true") << QString() << 1;
0192   QTest::newRow("truetrue") << QStringLiteral("true") << QStringLiteral("true") << 0;
0193   QTest::newRow("truefalse") << QStringLiteral("true") << QStringLiteral("false") << 1;
0194   QTest::newRow("falsetrue") << QStringLiteral("false") << QStringLiteral("true") << -1;
0195 }
0196 
0197 void ComparisonTest::testChoiceField() {
0198   Tellico::Data::CollPtr coll(new Tellico::Data::Collection(true)); // add default field
0199 
0200   QStringList allowed;
0201   allowed << QStringLiteral("choice2") << QStringLiteral("choice1");
0202   QVERIFY(allowed.size() > 1);
0203   Tellico::Data::FieldPtr field(new Tellico::Data::Field(QStringLiteral("choice"), QStringLiteral("Choice"), allowed));
0204   coll->addField(field);
0205 
0206   Tellico::Data::EntryPtr entry1(new Tellico::Data::Entry(coll));
0207   entry1->setField(field->name(), allowed.at(0));
0208   Tellico::Data::EntryPtr entry2(new Tellico::Data::Entry(coll));
0209   entry2->setField(field->name(), allowed.at(1));
0210 
0211   Tellico::FieldComparison* comp = Tellico::FieldComparison::create(field);
0212   // even though the second allowed value would sort first, it comes second in the list
0213   QCOMPARE(comp->compare(entry1, entry2), -1);
0214 }