File indexing completed on 2024-04-28 03:53:12

0001 /*  This file is part of the KDE libraries
0002     SPDX-FileCopyrightText: 2024 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <KConfig>
0008 #include <KConfigGroup>
0009 
0010 #include <QObject>
0011 #include <QStandardPaths>
0012 #include <QTest>
0013 
0014 static const QString s_test_subdir{QStringLiteral("kconfigtest_subdir/")};
0015 static const QString s_kconfig_test_subdir(s_test_subdir + QLatin1String("kconfigtest"));
0016 
0017 static const QString s_string_entry1(QStringLiteral("hello"));
0018 
0019 class KConfigBenchmark : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private Q_SLOTS:
0024     void initTestCase();
0025 
0026     void testHasKey();
0027     void testReadEntry();
0028     void testKConfigGroupKeyList();
0029 };
0030 
0031 void KConfigBenchmark::initTestCase()
0032 {
0033     // ensure we don't use files in the real config directory
0034     QStandardPaths::setTestModeEnabled(true);
0035 
0036     KConfig sc(s_kconfig_test_subdir);
0037     KConfigGroup cg(&sc, QStringLiteral("Main"));
0038     cg.deleteGroup();
0039     cg.writeEntry("UsedEntry", s_string_entry1);
0040 }
0041 
0042 void KConfigBenchmark::testHasKey()
0043 {
0044     bool hasUsedKey = false;
0045     bool hasNotUsedKey = true;
0046 
0047     KConfig sc(s_kconfig_test_subdir);
0048     KConfigGroup cg(&sc, QStringLiteral("Main"));
0049 
0050     QBENCHMARK {
0051         hasUsedKey = cg.hasKey("UsedEntry");
0052         hasNotUsedKey = cg.hasKey("NotUsedEntry");
0053     }
0054 
0055     QCOMPARE(hasUsedKey, true);
0056     QCOMPARE(hasNotUsedKey, false);
0057 }
0058 
0059 void KConfigBenchmark::testReadEntry()
0060 {
0061     QString usedEntry;
0062     QString notUsedEntry;
0063     const QString defaultEntry = QStringLiteral("Default");
0064 
0065     KConfig sc(s_kconfig_test_subdir);
0066     KConfigGroup cg(&sc, QStringLiteral("Main"));
0067 
0068     QBENCHMARK {
0069         usedEntry = cg.readEntry("UsedEntry", defaultEntry);
0070         notUsedEntry = cg.readEntry("NotUsedEntry", defaultEntry);
0071     }
0072 
0073     QCOMPARE(usedEntry, s_string_entry1);
0074     QCOMPARE(notUsedEntry, defaultEntry);
0075 }
0076 
0077 void KConfigBenchmark::testKConfigGroupKeyList()
0078 {
0079     QStringList keyList;
0080     const QString defaultEntry = QStringLiteral("Default");
0081 
0082     KConfig sc(s_kconfig_test_subdir);
0083     KConfigGroup cg(&sc, QStringLiteral("Main"));
0084     cg.writeEntry("Entry2", s_string_entry1);
0085     cg.writeEntry("Entry3", s_string_entry1);
0086 
0087     QBENCHMARK {
0088         keyList = cg.keyList();
0089     }
0090 
0091     const QStringList expectedKeyList{
0092         QStringLiteral("Entry2"),
0093         QStringLiteral("Entry3"),
0094         QStringLiteral("UsedEntry"),
0095     };
0096     QCOMPARE(keyList, expectedKeyList);
0097 }
0098 
0099 QTEST_GUILESS_MAIN(KConfigBenchmark)
0100 
0101 #include "kconfig_benchmark.moc"