File indexing completed on 2024-07-14 03:55:45

0001 /*
0002     SPDX-FileCopyrightText: 2013 Sven Brauch <svenbrauch@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "wordcompletiontest.h"
0008 
0009 #include <katedocument.h>
0010 #include <kateglobal.h>
0011 #include <katewordcompletion.h>
0012 #include <ktexteditor/editor.h>
0013 #include <ktexteditor/view.h>
0014 
0015 #include <QTest>
0016 
0017 QTEST_MAIN(WordCompletionTest)
0018 
0019 // was 500000, but that takes 30 seconds on build.kde.org, removed two 0 ;)
0020 static const int count = 5000;
0021 
0022 using namespace KTextEditor;
0023 
0024 void WordCompletionTest::initTestCase()
0025 {
0026     KTextEditor::EditorPrivate::enableUnitTestMode();
0027     Editor *editor = KTextEditor::Editor::instance();
0028     QVERIFY(editor);
0029 
0030     m_doc = editor->createDocument(this);
0031     QVERIFY(m_doc);
0032 
0033     // ensure the spellchecking doesn't mess with the expected results
0034     static_cast<KTextEditor::DocumentPrivate *>(m_doc)->setDefaultDictionary(QStringLiteral("notexistinglanguage"));
0035 }
0036 
0037 void WordCompletionTest::cleanupTestCase()
0038 {
0039 }
0040 
0041 void WordCompletionTest::init()
0042 {
0043     m_doc->clear();
0044 }
0045 
0046 void WordCompletionTest::cleanup()
0047 {
0048 }
0049 
0050 void WordCompletionTest::benchWordRetrievalMixed()
0051 {
0052     const int distinctWordRatio = 100;
0053     QStringList s;
0054     s.reserve(count);
0055     for (int i = 0; i < count; i++) {
0056         s.append(QLatin1String("HelloWorld") + QString::number(i / distinctWordRatio));
0057     }
0058     s.prepend(QStringLiteral("\n"));
0059     m_doc->setText(s);
0060 
0061     // creating the view only after inserting the text makes test execution much faster
0062     std::unique_ptr<KTextEditor::View> v(m_doc->createView(nullptr));
0063     QBENCHMARK {
0064         KateWordCompletionModel m(nullptr);
0065         QCOMPARE(m.allMatches(v.get(), KTextEditor::Range()).size(), count / distinctWordRatio);
0066     }
0067 }
0068 
0069 void WordCompletionTest::benchWordRetrievalSame()
0070 {
0071     QStringList s;
0072     s.reserve(count);
0073     // add a number so the words have roughly the same length as in the other tests
0074     const QString str = QLatin1String("HelloWorld") + QString::number(count);
0075     for (int i = 0; i < count; i++) {
0076         s.append(str);
0077     }
0078     s.prepend(QStringLiteral("\n"));
0079     m_doc->setText(s);
0080 
0081     std::unique_ptr<KTextEditor::View> v(m_doc->createView(nullptr));
0082     QBENCHMARK {
0083         KateWordCompletionModel m(nullptr);
0084         QCOMPARE(m.allMatches(v.get(), KTextEditor::Range()).size(), 1);
0085     }
0086 }
0087 
0088 void WordCompletionTest::benchWordRetrievalDistinct()
0089 {
0090     QStringList s;
0091     s.reserve(count);
0092     for (int i = 0; i < count; i++) {
0093         s.append(QLatin1String("HelloWorld") + QString::number(i));
0094     }
0095     s.prepend(QStringLiteral("\n"));
0096     m_doc->setText(s);
0097 
0098     std::unique_ptr<KTextEditor::View> v(m_doc->createView(nullptr));
0099     QBENCHMARK {
0100         KateWordCompletionModel m(nullptr);
0101         QCOMPARE(m.allMatches(v.get(), KTextEditor::Range()).size(), count);
0102     }
0103 }
0104 
0105 #include "moc_wordcompletiontest.cpp"