2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2012-12-03 19:17:09 +01:00
|
|
|
|
|
|
|
|
#include "tst_testtrie.h"
|
|
|
|
|
#include <qmljs/persistenttrie.h>
|
|
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QLatin1String>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QStringList>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS::PersistentTrie;
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::initTestCase() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool VERBOSE=false;
|
|
|
|
|
|
2013-12-02 16:55:42 +01:00
|
|
|
tst_TestTrie::tst_TestTrie() { }
|
2012-12-03 19:17:09 +01:00
|
|
|
|
|
|
|
|
void tst_TestTrie::testListAll_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QStringList>("strs");
|
|
|
|
|
|
|
|
|
|
QFile f(QString(TESTSRCDIR)+QString("/listAll.data"));
|
|
|
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
return;
|
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
int iline = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
QStringList list;
|
|
|
|
|
QString line;
|
|
|
|
|
while (true) {
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list.append(line);
|
|
|
|
|
}
|
|
|
|
|
QTest::newRow(QString::number(iline++).toLatin1()) << list;
|
|
|
|
|
list.clear();
|
|
|
|
|
if (stream.atEnd())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::testListAll()
|
|
|
|
|
{
|
2022-12-21 13:05:34 +01:00
|
|
|
QFETCH(const QStringList, strs);
|
2012-12-03 19:17:09 +01:00
|
|
|
Trie trie;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : strs)
|
2012-12-03 19:17:09 +01:00
|
|
|
trie.insert(s);
|
|
|
|
|
QStringList ref=strs;
|
|
|
|
|
ref.sort();
|
|
|
|
|
ref.removeDuplicates();
|
|
|
|
|
QStringList content=trie.stringList();
|
|
|
|
|
content.sort();
|
|
|
|
|
if (VERBOSE && ref != content) {
|
|
|
|
|
QDebug dbg = qDebug();
|
|
|
|
|
dbg << "ERROR inserting [";
|
|
|
|
|
bool comma = false;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : strs) {
|
2012-12-03 19:17:09 +01:00
|
|
|
if (comma)
|
|
|
|
|
dbg << ",";
|
|
|
|
|
else
|
|
|
|
|
comma = true;
|
|
|
|
|
dbg << s;
|
|
|
|
|
}
|
|
|
|
|
dbg << "] one gets " << trie;
|
|
|
|
|
}
|
|
|
|
|
QCOMPARE(ref, content);
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : strs) {
|
2012-12-03 19:17:09 +01:00
|
|
|
if (VERBOSE && ! trie.contains(s)) {
|
|
|
|
|
qDebug() << "ERROR could not find " << s << "in" << trie;
|
|
|
|
|
}
|
|
|
|
|
QVERIFY(trie.contains(s));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::testMerge_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QStringList>("str1");
|
|
|
|
|
QTest::addColumn<QStringList>("str2");
|
|
|
|
|
|
|
|
|
|
QFile f(QString(TESTSRCDIR)+QString("/merge.data"));
|
|
|
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
return;
|
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
int iline = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
QStringList list1;
|
|
|
|
|
QString line;
|
|
|
|
|
while (true) {
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list1.append(line);
|
|
|
|
|
}
|
|
|
|
|
QStringList list2;
|
|
|
|
|
while (true) {
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list2.append(line);
|
|
|
|
|
}
|
|
|
|
|
QTest::newRow(QString::number(iline++).toLatin1()) << list1 << list2;
|
|
|
|
|
list1.clear();
|
|
|
|
|
list2.clear();
|
|
|
|
|
if (stream.atEnd())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::testMerge()
|
|
|
|
|
{
|
2022-12-21 13:05:34 +01:00
|
|
|
QFETCH(const QStringList, str1);
|
|
|
|
|
QFETCH(const QStringList, str2);
|
2012-12-03 19:17:09 +01:00
|
|
|
Trie trie1;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str1)
|
2012-12-03 19:17:09 +01:00
|
|
|
trie1.insert(s);
|
|
|
|
|
Trie trie2;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str2)
|
2012-12-03 19:17:09 +01:00
|
|
|
trie2.insert(s);
|
|
|
|
|
QStringList ref=str1;
|
|
|
|
|
ref.append(str2);
|
|
|
|
|
ref.sort();
|
|
|
|
|
ref.removeDuplicates();
|
|
|
|
|
Trie trie3 = trie1.mergeF(trie2);
|
|
|
|
|
QStringList content=trie3.stringList();
|
|
|
|
|
content.sort();
|
|
|
|
|
if (VERBOSE && ref != content) {
|
|
|
|
|
QDebug dbg=qDebug();
|
|
|
|
|
dbg << "ERROR merging [";
|
|
|
|
|
bool comma = false;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str1) {
|
2012-12-03 19:17:09 +01:00
|
|
|
if (comma)
|
|
|
|
|
dbg << ",";
|
|
|
|
|
else
|
|
|
|
|
comma = true;
|
|
|
|
|
dbg << s;
|
|
|
|
|
}
|
|
|
|
|
dbg << "] => " << trie1 << " and [";
|
|
|
|
|
comma = false;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str2) {
|
2012-12-03 19:17:09 +01:00
|
|
|
if (comma)
|
|
|
|
|
dbg << ",";
|
|
|
|
|
else
|
|
|
|
|
comma = true;
|
|
|
|
|
dbg << s;
|
|
|
|
|
}
|
|
|
|
|
dbg << "] => " << trie2
|
|
|
|
|
<< " to " << trie3;
|
|
|
|
|
}
|
|
|
|
|
QCOMPARE(ref, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::testIntersect_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QStringList>("str1");
|
|
|
|
|
QTest::addColumn<QStringList>("str2");
|
|
|
|
|
|
|
|
|
|
QFile f(QString(TESTSRCDIR)+QString("/intersect.data"));
|
|
|
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
return;
|
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
int iline = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
QStringList list1;
|
|
|
|
|
QString line;
|
|
|
|
|
while (true) {
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list1.append(line);
|
|
|
|
|
}
|
|
|
|
|
QStringList list2;
|
|
|
|
|
while (true) {
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list2.append(line);
|
|
|
|
|
}
|
|
|
|
|
QTest::newRow(QString::number(iline++).toLatin1()) << list1 << list2;
|
|
|
|
|
list1.clear();
|
|
|
|
|
list2.clear();
|
|
|
|
|
if (stream.atEnd())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::testIntersect()
|
|
|
|
|
{
|
2022-12-21 13:05:34 +01:00
|
|
|
QFETCH(const QStringList, str1);
|
|
|
|
|
QFETCH(const QStringList, str2);
|
2012-12-03 19:17:09 +01:00
|
|
|
Trie trie1;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str1)
|
2012-12-03 19:17:09 +01:00
|
|
|
trie1.insert(s);
|
|
|
|
|
Trie trie2;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str2)
|
2012-12-03 19:17:09 +01:00
|
|
|
trie2.insert(s);
|
|
|
|
|
QSet<QString> ref1;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str1)
|
2012-12-03 19:17:09 +01:00
|
|
|
ref1.insert(s);
|
|
|
|
|
QSet<QString> ref2;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str2)
|
2012-12-03 19:17:09 +01:00
|
|
|
ref2.insert(s);
|
|
|
|
|
ref1.intersect(ref2);
|
|
|
|
|
Trie trie3 = trie1.intersectF(trie2);
|
|
|
|
|
ref2.clear();
|
2022-12-21 13:05:34 +01:00
|
|
|
const QStringList str = trie3.stringList();
|
|
|
|
|
for (const QString &s : str)
|
2012-12-03 19:17:09 +01:00
|
|
|
ref2.insert(s);
|
|
|
|
|
if (VERBOSE && ref1 != ref2) {
|
|
|
|
|
QDebug dbg=qDebug();
|
|
|
|
|
dbg << "ERROR intersecting [";
|
|
|
|
|
bool comma = false;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str1) {
|
2012-12-03 19:17:09 +01:00
|
|
|
if (comma)
|
|
|
|
|
dbg << ",";
|
|
|
|
|
else
|
|
|
|
|
comma = true;
|
|
|
|
|
dbg << s;
|
|
|
|
|
}
|
|
|
|
|
dbg << "] => " << trie1 << " and [";
|
|
|
|
|
comma = false;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : str2) {
|
2012-12-03 19:17:09 +01:00
|
|
|
if (comma)
|
|
|
|
|
dbg << ",";
|
|
|
|
|
else
|
|
|
|
|
comma = true;
|
|
|
|
|
dbg << s;
|
|
|
|
|
}
|
|
|
|
|
dbg << "] => " << trie2
|
|
|
|
|
<< " to " << trie3;
|
|
|
|
|
}
|
|
|
|
|
QCOMPARE(ref1, ref2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void tst_TestTrie::testCompletion_data()
|
|
|
|
|
{
|
|
|
|
|
QTest::addColumn<QStringList>("trieContents");
|
|
|
|
|
QTest::addColumn<QString>("str");
|
|
|
|
|
QTest::addColumn<QStringList>("completions");
|
|
|
|
|
QTest::addColumn<int>("flags");
|
|
|
|
|
|
|
|
|
|
QFile f(QString(TESTSRCDIR)+QString("/completion.data"));
|
|
|
|
|
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
|
return;
|
|
|
|
|
QTextStream stream(&f);
|
|
|
|
|
int iline = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
QStringList list1;
|
|
|
|
|
QString line;
|
|
|
|
|
while (true) {
|
|
|
|
|
line = stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list1.append(line);
|
|
|
|
|
}
|
|
|
|
|
QString str = stream.readLine();
|
|
|
|
|
QStringList list2;
|
|
|
|
|
while (true) {
|
|
|
|
|
line = stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
list2.append(line);
|
|
|
|
|
}
|
|
|
|
|
int flags = stream.readLine().toInt();
|
|
|
|
|
QTest::newRow(QString::number(iline++).toLatin1()) << list1 << str << list2 << flags;
|
|
|
|
|
list1.clear();
|
|
|
|
|
list2.clear();
|
|
|
|
|
if (stream.atEnd())
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-21 13:05:34 +01:00
|
|
|
void tst_TestTrie::testCompletion()
|
|
|
|
|
{
|
|
|
|
|
QFETCH(const QStringList, trieContents);
|
2012-12-03 19:17:09 +01:00
|
|
|
QFETCH(QString, str);
|
2022-12-21 13:05:34 +01:00
|
|
|
QFETCH(const QStringList, completions);
|
2012-12-03 19:17:09 +01:00
|
|
|
QFETCH(int, flags);
|
|
|
|
|
|
|
|
|
|
Trie trie;
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : trieContents)
|
2012-12-03 19:17:09 +01:00
|
|
|
trie.insert(s);
|
|
|
|
|
QStringList res = trie.complete(str, QString(), LookupFlags(flags));
|
|
|
|
|
res = matchStrengthSort(str, res);
|
|
|
|
|
if (VERBOSE && res != completions) {
|
|
|
|
|
qDebug() << "unexpected completions for " << str
|
|
|
|
|
<< " in " << trie;
|
|
|
|
|
qDebug() << "expected :[";
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : completions) {
|
2012-12-03 19:17:09 +01:00
|
|
|
qDebug() << s;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "] got [";
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : std::as_const(res)) {
|
2012-12-03 19:17:09 +01:00
|
|
|
qDebug() << s;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "]";
|
|
|
|
|
}
|
|
|
|
|
QCOMPARE(res, completions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interactiveCompletionTester(){
|
|
|
|
|
Trie trie;
|
|
|
|
|
qDebug() << "interactive completion tester, insert the strings int the trie (empty line to stop)";
|
|
|
|
|
QTextStream stream(stdin);
|
|
|
|
|
QString line;
|
|
|
|
|
while (true) {
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
trie.insert(line);
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "testing Complete on " << trie;
|
|
|
|
|
while (true) {
|
|
|
|
|
qDebug() << "insert a string to complete (empty line to stop)";
|
|
|
|
|
line=stream.readLine();
|
|
|
|
|
if (line.isEmpty())
|
|
|
|
|
break;
|
|
|
|
|
QStringList res=trie.complete(line, QString(),
|
|
|
|
|
LookupFlags(CaseInsensitive|SkipChars|SkipSpaces));
|
|
|
|
|
res = matchStrengthSort(line,res);
|
|
|
|
|
qDebug() << "possible completions:[";
|
2022-12-21 13:05:34 +01:00
|
|
|
for (const QString &s : std::as_const(res)) {
|
2013-11-22 00:19:46 +01:00
|
|
|
qDebug() << matchStrength(line,s) << " " << s;
|
2012-12-03 19:17:09 +01:00
|
|
|
}
|
|
|
|
|
qDebug() << "]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef INTERACTIVE_COMPLETION_TEST
|
|
|
|
|
|
|
|
|
|
int main(int , const char *[])
|
|
|
|
|
{
|
|
|
|
|
interactiveCompletionTester();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
2022-06-10 12:21:30 +02:00
|
|
|
QTEST_GUILESS_MAIN(tst_TestTrie);
|
2012-12-03 19:17:09 +01:00
|
|
|
|
|
|
|
|
//#include "moc_tst_testtrie.cpp"
|
|
|
|
|
|
|
|
|
|
#endif
|