Warning, file /frameworks/ktexteditor/autotests/src/wordcompletiontest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 <kateglobal.h>
0010 #include <katewordcompletion.h>
0011 #include <ktexteditor/editor.h>
0012 #include <ktexteditor/view.h>
0013 
0014 #include <QtTestWidgets>
0015 
0016 QTEST_MAIN(WordCompletionTest)
0017 
0018 // was 500000, but that takes 30 seconds on build.kde.org, removed two 0 ;)
0019 static const int count = 5000;
0020 
0021 using namespace KTextEditor;
0022 
0023 void WordCompletionTest::initTestCase()
0024 {
0025     KTextEditor::EditorPrivate::enableUnitTestMode();
0026     Editor *editor = KTextEditor::Editor::instance();
0027     QVERIFY(editor);
0028 
0029     m_doc = editor->createDocument(this);
0030     QVERIFY(m_doc);
0031 }
0032 
0033 void WordCompletionTest::cleanupTestCase()
0034 {
0035 }
0036 
0037 void WordCompletionTest::init()
0038 {
0039     m_doc->clear();
0040 }
0041 
0042 void WordCompletionTest::cleanup()
0043 {
0044 }
0045 
0046 void WordCompletionTest::benchWordRetrievalMixed()
0047 {
0048     const int distinctWordRatio = 100;
0049     QStringList s;
0050     s.reserve(count);
0051     for (int i = 0; i < count; i++) {
0052         s.append(QLatin1String("HelloWorld") + QString::number(i / distinctWordRatio));
0053     }
0054     s.prepend("\n");
0055     m_doc->setText(s);
0056 
0057     // creating the view only after inserting the text makes test execution much faster
0058     QSharedPointer<KTextEditor::View> v(m_doc->createView(nullptr));
0059     QBENCHMARK {
0060         KateWordCompletionModel m(nullptr);
0061         QCOMPARE(m.allMatches(v.data(), KTextEditor::Range()).size(), count / distinctWordRatio);
0062     }
0063 }
0064 
0065 void WordCompletionTest::benchWordRetrievalSame()
0066 {
0067     QStringList s;
0068     s.reserve(count);
0069     // add a number so the words have roughly the same length as in the other tests
0070     const QString str = QLatin1String("HelloWorld") + QString::number(count);
0071     for (int i = 0; i < count; i++) {
0072         s.append(str);
0073     }
0074     s.prepend("\n");
0075     m_doc->setText(s);
0076 
0077     QSharedPointer<KTextEditor::View> v(m_doc->createView(nullptr));
0078     QBENCHMARK {
0079         KateWordCompletionModel m(nullptr);
0080         QCOMPARE(m.allMatches(v.data(), KTextEditor::Range()).size(), 1);
0081     }
0082 }
0083 
0084 void WordCompletionTest::benchWordRetrievalDistinct()
0085 {
0086     QStringList s;
0087     s.reserve(count);
0088     for (int i = 0; i < count; i++) {
0089         s.append(QLatin1String("HelloWorld") + QString::number(i));
0090     }
0091     s.prepend("\n");
0092     m_doc->setText(s);
0093 
0094     QSharedPointer<KTextEditor::View> v(m_doc->createView(nullptr));
0095     QBENCHMARK {
0096         KateWordCompletionModel m(nullptr);
0097         QCOMPARE(m.allMatches(v.data(), KTextEditor::Range()).size(), count);
0098     }
0099 }
0100 
0101 #include "moc_wordcompletiontest.cpp"