File indexing completed on 2024-05-12 05:17:19

0001 /*
0002    Copyright (C) 2009 Kevin Ottens <ervin@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; if not, write to the Free Software
0016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include <qtest.h>
0020 
0021 #include "kimap2/imapset.h"
0022 
0023 #include <QtTest>
0024 #include <QDebug>
0025 
0026 using namespace KIMAP2;
0027 
0028 QByteArray operator""_ba(const char *str, std::size_t len)
0029 {
0030     return QByteArray{str, static_cast<int>(len)};
0031 }
0032 
0033 class ImapSetTest : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void shouldConvertToAndFromByteArray_data()
0039     {
0040         ImapSet set;
0041 
0042         QTest::addColumn<ImapSet>("imapSet");
0043         QTest::addColumn<QByteArray>("byteArray");
0044 
0045         QTest::newRow("empty set") << ImapSet() << QByteArray();
0046         QTest::newRow("unique value") << ImapSet(7) << QByteArray("7");
0047         QTest::newRow("single interval") << ImapSet(7, 10) << QByteArray("7:10");
0048         QTest::newRow("single interval with no upper bound") << ImapSet(1, 0) << QByteArray("1:*");
0049 
0050         set = ImapSet(7, 10);
0051         set.add(ImapInterval(12, 14));
0052         QTest::newRow("two intervals") << set << QByteArray("7:10,12:14");
0053 
0054         set = ImapSet(7, 10);
0055         set.add(ImapInterval(12));
0056         QTest::newRow("two intervals with an infinite one") << set << QByteArray("7:10,12:*");
0057 
0058         set = ImapSet(7, 10);
0059         set.add(5);
0060         QTest::newRow("one interval and a value") << set << QByteArray("7:10,5");
0061 
0062         set = ImapSet(7, 10);
0063         set.add(QVector<ImapSet::Id>() << 5 << 3);
0064         QTest::newRow("one interval and two values") << set << QByteArray("7:10,3,5");
0065     }
0066 
0067     void shouldConvertToAndFromByteArray()
0068     {
0069         QFETCH(ImapSet, imapSet);
0070         QFETCH(QByteArray, byteArray);
0071 
0072         QCOMPARE(QString::fromUtf8(imapSet.toImapSequenceSet()),
0073                  QString::fromUtf8(byteArray));
0074         //qDebug() << "Expects" << imapSet << "got" << ImapSet::fromImapSequenceSet( byteArray );
0075         QCOMPARE(ImapSet::fromImapSequenceSet(byteArray), imapSet);
0076     }
0077 
0078     void testOptimize_data()
0079     {
0080         QTest::addColumn<ImapSet>("imapSet");
0081         QTest::addColumn<QByteArray>("originalString");
0082         QTest::addColumn<QByteArray>("expectedString");
0083 
0084         {
0085             ImapSet imapSet;
0086             for (int i = 1; i <= 10; ++i) {
0087                 imapSet.add(i);
0088             }
0089             QTest::newRow("Neighbouring numbers") << imapSet << "1,2,3,4,5,6,7,8,9,10"_ba << "1:10"_ba;
0090         }
0091         {
0092             ImapSet imapSet;
0093             imapSet.add(ImapInterval{1, 3});
0094             imapSet.add(ImapInterval{5, 7});
0095             QTest::newRow("Neighbouring intervals with a gap") << imapSet << "1:3,5:7"_ba << "1:3,5:7"_ba;
0096         }
0097         {
0098             ImapSet imapSet;
0099             for (int i : { 5, 8, 3, 1, 9, 2, 7, 4, 6 }) {
0100                 imapSet.add(i);
0101             }
0102             QTest::newRow("Random order") << imapSet << "5,8,3,1,9,2,7,4,6"_ba << "1:9"_ba;
0103         }
0104         {
0105             ImapSet imapSet;
0106             imapSet.add(ImapInterval{1, 3});
0107             imapSet.add(ImapInterval{2, 4});
0108             QTest::newRow("Overlapping") << imapSet << "1:3,2:4"_ba << "1:4"_ba;
0109         }
0110         {
0111             ImapSet imapSet;
0112             imapSet.add(ImapInterval{2, 4});
0113             imapSet.add(ImapInterval{1, 3});
0114             imapSet.add(4);
0115             imapSet.add(ImapInterval{7, 8});
0116             imapSet.add(ImapInterval{8, 9});
0117             QTest::newRow("Multiple overlapping with a gap") << imapSet << "2:4,1:3,4,7:8,8:9"_ba << "1:4,7:9"_ba;
0118         }
0119         {
0120             ImapSet imapSet;
0121             imapSet.add(5);
0122             imapSet.add(8);
0123             imapSet.add(10);
0124             imapSet.add(ImapInterval{0, 20});
0125             QTest::newRow("Overlapping multiple intervals") << imapSet << "5,8,10,0:20"_ba << "0:20"_ba;
0126         }
0127         {
0128             ImapSet imapSet;
0129             imapSet.add(1);
0130             imapSet.add(ImapInterval{3, 5});
0131             imapSet.add(ImapInterval{4, 0});
0132             QTest::newRow("Open end overlap") << imapSet << "1,3:5,4:*"_ba << "1,3:*"_ba;
0133         }
0134         {
0135             ImapSet imapSet;
0136             imapSet.add(ImapInterval{1, 4});
0137             imapSet.add(3);
0138             QTest::newRow("Value within interval") << imapSet << "1:4,3"_ba << "1:4"_ba;
0139         }
0140         {
0141             ImapSet imapSet;
0142             imapSet.add(ImapInterval{1, 0});
0143             imapSet.add(ImapInterval{3, 0});
0144             imapSet.add(5);
0145             QTest::newRow("Multiple open end intervals") << imapSet << "1:*,3:*,5"_ba << "1:*"_ba;
0146         }
0147         {
0148             ImapSet imapSet;
0149             for (ImapSet::Id id : {1, 2, 3, 5, 6, 8, 9, 10, 15, 16, 19, 20, 21, 23}) {
0150                 imapSet.add(id);
0151             }
0152             QTest::newRow("Merge single values") << imapSet << "1,2,3,5,6,8,9,10,15,16,19,20,21,23"_ba
0153                                                  << "1:3,5:6,8:10,15:16,19:21,23"_ba;
0154         }
0155     }
0156 
0157     void testOptimize()
0158     {
0159         QFETCH(ImapSet, imapSet);
0160         QFETCH(QByteArray, originalString);
0161         QFETCH(QByteArray, expectedString);
0162 
0163         QCOMPARE(imapSet.intervals().size(), originalString.count(",") + 1);
0164         QCOMPARE(imapSet.toImapSequenceSet(), originalString);
0165 
0166         imapSet.optimize();
0167 
0168         QCOMPARE(imapSet.intervals().size(), expectedString.count(",") + 1);
0169         QCOMPARE(imapSet.toImapSequenceSet(), expectedString);
0170     }
0171 };
0172 
0173 QTEST_GUILESS_MAIN(ImapSetTest)
0174 
0175 #include "imapsettest.moc"