File indexing completed on 2024-05-12 05:17:10
0001 /* 0002 SPDX-FileCopyrightText: 2009 Kevin Ottens <ervin@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include <QTest> 0008 0009 #include "kimap/imapset.h" 0010 0011 #include <QDebug> 0012 #include <QTest> 0013 0014 using namespace KIMAP; 0015 0016 QByteArray operator""_ba(const char *str, std::size_t len) 0017 { 0018 return QByteArray{str, static_cast<int>(len)}; 0019 } 0020 0021 class ImapSetTest : public QObject 0022 { 0023 Q_OBJECT 0024 0025 private Q_SLOTS: 0026 void shouldConvertToAndFromByteArray_data() 0027 { 0028 ImapSet set; 0029 0030 QTest::addColumn<ImapSet>("imapSet"); 0031 QTest::addColumn<QByteArray>("byteArray"); 0032 0033 QTest::newRow("empty set") << ImapSet() << QByteArray(); 0034 QTest::newRow("unique value") << ImapSet(7) << QByteArray("7"); 0035 QTest::newRow("single interval") << ImapSet(7, 10) << QByteArray("7:10"); 0036 QTest::newRow("single interval with no upper bound") << ImapSet(1, 0) << QByteArray("1:*"); 0037 0038 set = ImapSet(7, 10); 0039 set.add(ImapInterval(12, 14)); 0040 QTest::newRow("two intervals") << set << QByteArray("7:10,12:14"); 0041 0042 set = ImapSet(7, 10); 0043 set.add(ImapInterval(12)); 0044 QTest::newRow("two intervals with an infinite one") << set << QByteArray("7:10,12:*"); 0045 0046 set = ImapSet(7, 10); 0047 set.add(5); 0048 QTest::newRow("one interval and a value") << set << QByteArray("7:10,5"); 0049 0050 set = ImapSet(7, 10); 0051 set.add(QList<ImapSet::Id>() << 5 << 3); 0052 QTest::newRow("one interval and two values") << set << QByteArray("7:10,3,5"); 0053 } 0054 0055 void shouldConvertToAndFromByteArray() 0056 { 0057 QFETCH(ImapSet, imapSet); 0058 QFETCH(QByteArray, byteArray); 0059 0060 QCOMPARE(QString::fromUtf8(imapSet.toImapSequenceSet()), QString::fromUtf8(byteArray)); 0061 // qDebug() << "Expects" << imapSet << "got" << ImapSet::fromImapSequenceSet( byteArray ); 0062 QCOMPARE(ImapSet::fromImapSequenceSet(byteArray), imapSet); 0063 } 0064 0065 void testOptimize_data() 0066 { 0067 QTest::addColumn<ImapSet>("imapSet"); 0068 QTest::addColumn<QByteArray>("originalString"); 0069 QTest::addColumn<QByteArray>("expectedString"); 0070 0071 { 0072 ImapSet imapSet; 0073 for (int i = 1; i <= 10; ++i) { 0074 imapSet.add(i); 0075 } 0076 QTest::newRow("Neighbouring numbers") << imapSet << "1,2,3,4,5,6,7,8,9,10"_ba 0077 << "1:10"_ba; 0078 } 0079 { 0080 ImapSet imapSet; 0081 imapSet.add(ImapInterval{1, 3}); 0082 imapSet.add(ImapInterval{5, 7}); 0083 QTest::newRow("Neighbouring intervals with a gap") << imapSet << "1:3,5:7"_ba 0084 << "1:3,5:7"_ba; 0085 } 0086 { 0087 ImapSet imapSet; 0088 for (int i : {5, 8, 3, 1, 9, 2, 7, 4, 6}) { 0089 imapSet.add(i); 0090 } 0091 QTest::newRow("Random order") << imapSet << "5,8,3,1,9,2,7,4,6"_ba 0092 << "1:9"_ba; 0093 } 0094 { 0095 ImapSet imapSet; 0096 imapSet.add(ImapInterval{1, 3}); 0097 imapSet.add(ImapInterval{2, 4}); 0098 QTest::newRow("Overlapping") << imapSet << "1:3,2:4"_ba 0099 << "1:4"_ba; 0100 } 0101 { 0102 ImapSet imapSet; 0103 imapSet.add(ImapInterval{2, 4}); 0104 imapSet.add(ImapInterval{1, 3}); 0105 imapSet.add(4); 0106 imapSet.add(ImapInterval{7, 8}); 0107 imapSet.add(ImapInterval{8, 9}); 0108 QTest::newRow("Multiple overlapping with a gap") << imapSet << "2:4,1:3,4,7:8,8:9"_ba 0109 << "1:4,7:9"_ba; 0110 } 0111 { 0112 ImapSet imapSet; 0113 imapSet.add(5); 0114 imapSet.add(8); 0115 imapSet.add(10); 0116 imapSet.add(ImapInterval{0, 20}); 0117 QTest::newRow("Overlapping multiple intervals") << imapSet << "5,8,10,0:20"_ba 0118 << "0:20"_ba; 0119 } 0120 { 0121 ImapSet imapSet; 0122 imapSet.add(1); 0123 imapSet.add(ImapInterval{3, 5}); 0124 imapSet.add(ImapInterval{4, 0}); 0125 QTest::newRow("Open end overlap") << imapSet << "1,3:5,4:*"_ba 0126 << "1,3:*"_ba; 0127 } 0128 { 0129 ImapSet imapSet; 0130 imapSet.add(ImapInterval{1, 4}); 0131 imapSet.add(3); 0132 QTest::newRow("Value within interval") << imapSet << "1:4,3"_ba 0133 << "1:4"_ba; 0134 } 0135 { 0136 ImapSet imapSet; 0137 imapSet.add(ImapInterval{1, 0}); 0138 imapSet.add(ImapInterval{3, 0}); 0139 imapSet.add(5); 0140 QTest::newRow("Multiple open end intervals") << imapSet << "1:*,3:*,5"_ba 0141 << "1:*"_ba; 0142 } 0143 { 0144 ImapSet imapSet; 0145 for (ImapSet::Id id : {1, 2, 3, 5, 6, 8, 9, 10, 15, 16, 19, 20, 21, 23}) { 0146 imapSet.add(id); 0147 } 0148 QTest::newRow("Merge single values") << imapSet << "1,2,3,5,6,8,9,10,15,16,19,20,21,23"_ba 0149 << "1:3,5:6,8:10,15:16,19:21,23"_ba; 0150 } 0151 } 0152 0153 void testOptimize() 0154 { 0155 QFETCH(ImapSet, imapSet); 0156 QFETCH(QByteArray, originalString); 0157 QFETCH(QByteArray, expectedString); 0158 0159 QCOMPARE(imapSet.intervals().size(), originalString.count(",") + 1); 0160 QCOMPARE(imapSet.toImapSequenceSet(), originalString); 0161 0162 imapSet.optimize(); 0163 0164 QCOMPARE(imapSet.intervals().size(), expectedString.count(",") + 1); 0165 QCOMPARE(imapSet.toImapSequenceSet(), expectedString); 0166 } 0167 }; 0168 0169 QTEST_GUILESS_MAIN(ImapSetTest) 0170 0171 #include "imapsettest.moc"