Changeset View
Changeset View
Standalone View
Standalone View
autotests/imapsettest.cpp
Show All 19 Lines | |||||
20 | 20 | | |||
21 | #include "kimap/imapset.h" | 21 | #include "kimap/imapset.h" | ||
22 | 22 | | |||
23 | #include <QTest> | 23 | #include <QTest> | ||
24 | #include <QDebug> | 24 | #include <QDebug> | ||
25 | 25 | | |||
26 | using namespace KIMAP; | 26 | using namespace KIMAP; | ||
27 | 27 | | |||
28 | QByteArray operator""_ba(const char *str, std::size_t len) | ||||
29 | { | ||||
30 | return QByteArray{str, static_cast<int>(len)}; | ||||
31 | } | ||||
32 | | ||||
28 | class ImapSetTest : public QObject | 33 | class ImapSetTest : public QObject | ||
29 | { | 34 | { | ||
30 | Q_OBJECT | 35 | Q_OBJECT | ||
31 | 36 | | |||
32 | private Q_SLOTS: | 37 | private Q_SLOTS: | ||
33 | void shouldConvertToAndFromByteArray_data() | 38 | void shouldConvertToAndFromByteArray_data() | ||
34 | { | 39 | { | ||
35 | ImapSet set; | 40 | ImapSet set; | ||
Show All 28 Lines | 68 | { | |||
64 | QFETCH(ImapSet, imapSet); | 69 | QFETCH(ImapSet, imapSet); | ||
65 | QFETCH(QByteArray, byteArray); | 70 | QFETCH(QByteArray, byteArray); | ||
66 | 71 | | |||
67 | QCOMPARE(QString::fromUtf8(imapSet.toImapSequenceSet()), | 72 | QCOMPARE(QString::fromUtf8(imapSet.toImapSequenceSet()), | ||
68 | QString::fromUtf8(byteArray)); | 73 | QString::fromUtf8(byteArray)); | ||
69 | //qDebug() << "Expects" << imapSet << "got" << ImapSet::fromImapSequenceSet( byteArray ); | 74 | //qDebug() << "Expects" << imapSet << "got" << ImapSet::fromImapSequenceSet( byteArray ); | ||
70 | QCOMPARE(ImapSet::fromImapSequenceSet(byteArray), imapSet); | 75 | QCOMPARE(ImapSet::fromImapSequenceSet(byteArray), imapSet); | ||
71 | } | 76 | } | ||
77 | | ||||
78 | void testOptimize_data() | ||||
79 | { | ||||
80 | QTest::addColumn<ImapSet>("imapSet"); | ||||
81 | QTest::addColumn<QByteArray>("originalString"); | ||||
82 | QTest::addColumn<QByteArray>("expectedString"); | ||||
83 | | ||||
84 | { | ||||
85 | ImapSet imapSet; | ||||
86 | for (int i = 1; i <= 10; ++i) { | ||||
87 | imapSet.add(i); | ||||
88 | } | ||||
89 | QTest::newRow("Neighbouring numbers") << imapSet << "1,2,3,4,5,6,7,8,9,10"_ba << "1:10"_ba; | ||||
90 | } | ||||
91 | { | ||||
92 | ImapSet imapSet; | ||||
93 | imapSet.add(ImapInterval{1, 3}); | ||||
94 | imapSet.add(ImapInterval{5, 7}); | ||||
95 | QTest::newRow("Neighbouring intervals with a gap") << imapSet << "1:3,5:7"_ba << "1:3,5:7"_ba; | ||||
96 | } | ||||
97 | { | ||||
98 | ImapSet imapSet; | ||||
99 | for (int i : { 5, 8, 3, 1, 9, 2, 7, 4, 6 }) { | ||||
100 | imapSet.add(i); | ||||
101 | } | ||||
102 | QTest::newRow("Random order") << imapSet << "5,8,3,1,9,2,7,4,6"_ba << "1:9"_ba; | ||||
103 | } | ||||
104 | { | ||||
105 | ImapSet imapSet; | ||||
106 | imapSet.add(ImapInterval{1, 3}); | ||||
107 | imapSet.add(ImapInterval{2, 4}); | ||||
108 | QTest::newRow("Overlapping") << imapSet << "1:3,2:4"_ba << "1:4"_ba; | ||||
109 | } | ||||
110 | { | ||||
111 | ImapSet imapSet; | ||||
112 | imapSet.add(ImapInterval{2, 4}); | ||||
113 | imapSet.add(ImapInterval{1, 3}); | ||||
114 | imapSet.add(4); | ||||
115 | imapSet.add(ImapInterval{7, 8}); | ||||
116 | imapSet.add(ImapInterval{8, 9}); | ||||
117 | QTest::newRow("Multiple overlapping with a gap") << imapSet << "2:4,1:3,4,7:8,8:9"_ba << "1:4,7:9"_ba; | ||||
118 | } | ||||
119 | { | ||||
120 | ImapSet imapSet; | ||||
121 | imapSet.add(5); | ||||
122 | imapSet.add(8); | ||||
123 | imapSet.add(10); | ||||
124 | imapSet.add(ImapInterval{0, 20}); | ||||
125 | QTest::newRow("Overlapping multiple intervals") << imapSet << "5,8,10,0:20"_ba << "0:20"_ba; | ||||
126 | } | ||||
127 | { | ||||
128 | ImapSet imapSet; | ||||
129 | imapSet.add(1); | ||||
130 | imapSet.add(ImapInterval{3, 5}); | ||||
131 | imapSet.add(ImapInterval{4, 0}); | ||||
132 | QTest::newRow("Open end overlap") << imapSet << "1,3:5,4:*"_ba << "1,3:*"_ba; | ||||
133 | } | ||||
134 | { | ||||
135 | ImapSet imapSet; | ||||
136 | imapSet.add(ImapInterval{1, 4}); | ||||
137 | imapSet.add(3); | ||||
138 | QTest::newRow("Value within interval") << imapSet << "1:4,3"_ba << "1:4"_ba; | ||||
139 | } | ||||
140 | { | ||||
141 | ImapSet imapSet; | ||||
142 | imapSet.add(ImapInterval{1, 0}); | ||||
143 | imapSet.add(ImapInterval{3, 0}); | ||||
144 | imapSet.add(5); | ||||
145 | QTest::newRow("Multiple open end intervals") << imapSet << "1:*,3:*,5"_ba << "1:*"_ba; | ||||
146 | } | ||||
147 | { | ||||
148 | ImapSet imapSet; | ||||
149 | for (ImapSet::Id id : {1, 2, 3, 5, 6, 8, 9, 10, 15, 16, 19, 20, 21, 23}) { | ||||
150 | imapSet.add(id); | ||||
151 | } | ||||
152 | QTest::newRow("Merge single values") << imapSet << "1,2,3,5,6,8,9,10,15,16,19,20,21,23"_ba | ||||
153 | << "1:3,5:6,8:10,15:16,19:21,23"_ba; | ||||
154 | } | ||||
155 | } | ||||
156 | | ||||
157 | void testOptimize() | ||||
158 | { | ||||
159 | QFETCH(ImapSet, imapSet); | ||||
160 | QFETCH(QByteArray, originalString); | ||||
161 | QFETCH(QByteArray, expectedString); | ||||
162 | | ||||
163 | QCOMPARE(imapSet.intervals().size(), originalString.count(",") + 1); | ||||
164 | QCOMPARE(imapSet.toImapSequenceSet(), originalString); | ||||
165 | | ||||
166 | imapSet.optimize(); | ||||
167 | | ||||
168 | QCOMPARE(imapSet.intervals().size(), expectedString.count(",") + 1); | ||||
169 | QCOMPARE(imapSet.toImapSequenceSet(), expectedString); | ||||
170 | } | ||||
72 | }; | 171 | }; | ||
73 | 172 | | |||
74 | QTEST_GUILESS_MAIN(ImapSetTest) | 173 | QTEST_GUILESS_MAIN(ImapSetTest) | ||
75 | 174 | | |||
76 | #include "imapsettest.moc" | 175 | #include "imapsettest.moc" |