File indexing completed on 2024-05-26 04:41:10

0001 /*
0002     SPDX-FileCopyrightText: 2012-2013 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_identifier.h"
0008 
0009 #include <language/duchain/identifier.h>
0010 #include <serialization/indexedstring.h>
0011 
0012 #include <tests/testcore.h>
0013 #include <tests/autotestshell.h>
0014 
0015 #include <utility>
0016 #include <QTest>
0017 
0018 QTEST_GUILESS_MAIN(TestIdentifier)
0019 
0020 using namespace KDevelop;
0021 
0022 void TestIdentifier::initTestCase()
0023 {
0024     AutoTestShell::init();
0025     TestCore::initialize(Core::NoUi);
0026 }
0027 
0028 void TestIdentifier::cleanupTestCase()
0029 {
0030     TestCore::shutdown();
0031 }
0032 
0033 void TestIdentifier::testIdentifier()
0034 {
0035     QFETCH(QString, stringId);
0036     const IndexedString indexedStringId(stringId);
0037 
0038     Identifier id(stringId);
0039     QCOMPARE(id.isEmpty(), stringId.isEmpty());
0040     QCOMPARE(id, Identifier(stringId));
0041     QVERIFY(!(id != Identifier(stringId)));
0042     QCOMPARE(id, Identifier(stringId));
0043     QCOMPARE(id, Identifier(indexedStringId));
0044     QCOMPARE(id.identifier(), indexedStringId);
0045     QCOMPARE(id.toString(), stringId);
0046     QVERIFY(id.nameEquals(Identifier(stringId)));
0047     QVERIFY(!id.isUnique());
0048 
0049     if (stringId.isEmpty()) {
0050         QVERIFY(id.inRepository());
0051         QVERIFY(Identifier(id).inRepository());
0052         QVERIFY(Identifier(indexedStringId).inRepository());
0053     }
0054 
0055     Identifier copy = id;
0056     QCOMPARE(copy, id);
0057     copy = copy;
0058     QCOMPARE(copy, id);
0059     copy = Identifier();
0060     QVERIFY(copy.isEmpty());
0061     copy = id;
0062     QCOMPARE(copy, id);
0063 
0064     IndexedIdentifier indexedId(id);
0065     QVERIFY(indexedId == id);
0066     QCOMPARE(indexedId, IndexedIdentifier(id));
0067     QCOMPARE(indexedId.isEmpty(), stringId.isEmpty());
0068     QCOMPARE(indexedId.identifier(), id);
0069     IndexedIdentifier indexedCopy = indexedId;
0070     QCOMPARE(indexedCopy, indexedId);
0071     indexedCopy = indexedCopy;
0072     QCOMPARE(indexedCopy, indexedId);
0073     indexedCopy = IndexedIdentifier();
0074     QVERIFY(indexedCopy.isEmpty());
0075     indexedCopy = indexedId;
0076     QCOMPARE(indexedCopy, indexedId);
0077 
0078     Identifier moved = std::move(id);
0079     QVERIFY(id.isEmpty());
0080     QVERIFY(id.inRepository());
0081     QCOMPARE(moved, copy);
0082 
0083     IndexedIdentifier movedIndexed = std::move(indexedId);
0084     QCOMPARE(movedIndexed, indexedCopy);
0085 }
0086 
0087 void TestIdentifier::testIdentifier_data()
0088 {
0089     QTest::addColumn<QString>("stringId");
0090 
0091     QTest::newRow("empty") << QString();
0092     QTest::newRow("foo") << QStringLiteral("foo");
0093     QTest::newRow("bar") << QStringLiteral("bar");
0094     //TODO: test template identifiers
0095 }
0096 
0097 void TestIdentifier::testQualifiedIdentifier()
0098 {
0099     QFETCH(QString, stringId);
0100     const QStringList list = stringId.split(QStringLiteral("::"), Qt::SkipEmptyParts);
0101     QualifiedIdentifier id(stringId);
0102     QCOMPARE(id.isEmpty(), stringId.isEmpty());
0103     QCOMPARE(id, QualifiedIdentifier(stringId));
0104     QVERIFY(!(id != QualifiedIdentifier(stringId)));
0105     QCOMPARE(id, QualifiedIdentifier(stringId));
0106     if (list.size() == 1) {
0107         QCOMPARE(id, QualifiedIdentifier(Identifier(list.last())));
0108     } else if (list.isEmpty()) {
0109         QualifiedIdentifier empty{Identifier()};
0110         QCOMPARE(id, empty);
0111         QVERIFY(empty.isEmpty());
0112         QVERIFY(empty.inRepository());
0113     }
0114     QCOMPARE(id.toString(), stringId);
0115     QCOMPARE(id.toStringList(), list);
0116     QCOMPARE(id.top(), Identifier(list.isEmpty() ? QString() : list.last()));
0117 
0118     if (stringId.isEmpty()) {
0119         QVERIFY(id.inRepository());
0120         QVERIFY(QualifiedIdentifier(id).inRepository());
0121     }
0122 
0123     QualifiedIdentifier copy = id;
0124     QCOMPARE(copy, id);
0125     copy = copy;
0126     QCOMPARE(copy, id);
0127     copy = QualifiedIdentifier();
0128     QVERIFY(copy.isEmpty());
0129     copy = id;
0130     QCOMPARE(copy, id);
0131 
0132     IndexedQualifiedIdentifier indexedId(id);
0133     QVERIFY(indexedId == id);
0134     QCOMPARE(indexedId, IndexedQualifiedIdentifier(id));
0135     QCOMPARE(indexedId.isValid(), !stringId.isEmpty());
0136     QCOMPARE(indexedId.identifier(), id);
0137     IndexedQualifiedIdentifier indexedCopy = indexedId;
0138     QCOMPARE(indexedCopy, indexedId);
0139     indexedCopy = indexedCopy;
0140     QCOMPARE(indexedCopy, indexedId);
0141     indexedCopy = IndexedQualifiedIdentifier();
0142     QVERIFY(!indexedCopy.isValid());
0143     indexedCopy = indexedId;
0144     QCOMPARE(indexedCopy, indexedId);
0145 
0146     QualifiedIdentifier moved = std::move(id);
0147     QVERIFY(id.isEmpty());
0148     QCOMPARE(moved, copy);
0149 
0150     IndexedQualifiedIdentifier movedIndexed = std::move(indexedId);
0151     QCOMPARE(movedIndexed, indexedCopy);
0152 
0153     copy.clear();
0154     QVERIFY(copy.isEmpty());
0155 
0156     copy.push(moved);
0157     QCOMPARE(copy, moved);
0158 
0159     copy.push(Identifier(QStringLiteral("lalala")));
0160     QCOMPARE(copy.count(), moved.count() + 1);
0161 }
0162 
0163 void TestIdentifier::testQualifiedIdentifier_data()
0164 {
0165     QTest::addColumn<QString>("stringId");
0166 
0167     QTest::newRow("empty") << QString();
0168     QTest::newRow("foo") << "foo";
0169     QTest::newRow("foo::bar") << "foo::bar";
0170     //TODO: test template identifiers
0171 }
0172 
0173 void TestIdentifier::benchIdentifierCopyConstant()
0174 {
0175     QBENCHMARK {
0176         Identifier identifier(QStringLiteral("Asdf"));
0177         identifier.index();
0178         Identifier copy(identifier);
0179     }
0180 }
0181 
0182 void TestIdentifier::benchIdentifierCopyDynamic()
0183 {
0184     QBENCHMARK {
0185         Identifier identifier(QStringLiteral("Asdf"));
0186         Identifier copy(identifier);
0187     }
0188 }
0189 
0190 void TestIdentifier::benchQidCopyPush()
0191 {
0192     QBENCHMARK {
0193         Identifier id(QStringLiteral("foo"));
0194         QualifiedIdentifier base(id);
0195         QualifiedIdentifier copy(base);
0196         copy.push(id);
0197     }
0198 }
0199 
0200 #include "moc_test_identifier.cpp"