Revamp "Watched folder options" dialog
[qBittorrent.git] / test / testorderedset.cpp
blobf0315ca233a952c04afed4569bd18dcc65858311
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2023 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2022 Mike Tzou (Chocobo1)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include <QSet>
31 #include <QTest>
33 #include "base/global.h"
34 #include "base/orderedset.h"
36 class TestOrderedSet final : public QObject
38 Q_OBJECT
39 Q_DISABLE_COPY_MOVE(TestOrderedSet)
41 public:
42 TestOrderedSet() = default;
44 private slots:
45 #if __cplusplus < 202002L
46 void testContains() const
48 const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
49 QVERIFY(set.contains(u"a"_qs));
50 QVERIFY(set.contains(u"b"_qs));
51 QVERIFY(set.contains(u"c"_qs));
52 QVERIFY(!set.contains(u"z"_qs));
54 const OrderedSet<QString> emptySet;
55 QVERIFY(!emptySet.contains(u"a"_qs));
57 #endif
59 void testCount() const
61 const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs, u"c"_qs};
62 QCOMPARE(set.count(), 3);
64 const OrderedSet<QString> emptySet;
65 QCOMPARE(emptySet.count(), 0);
68 void testIntersect() const
70 OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
71 set.intersect({u"c"_qs, u"a"_qs});
72 QCOMPARE(set.size(), 2);
73 QCOMPARE(set.join(u","_qs), u"a,c"_qs);
75 OrderedSet<QString> emptySet;
76 emptySet.intersect({u"a"_qs}).intersect({u"c"_qs});;
77 QVERIFY(emptySet.isEmpty());
80 void testIsEmpty() const
82 const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
83 QVERIFY(!set.isEmpty());
85 const OrderedSet<QString> emptySet;
86 QVERIFY(emptySet.isEmpty());
89 void testJoin() const
91 const OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
92 QCOMPARE(set.join(u","_qs), u"a,b,c"_qs);
94 const OrderedSet<QString> emptySet;
95 QCOMPARE(emptySet.join(u","_qs), u""_qs);
98 void testRemove() const
100 OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
101 QVERIFY(!set.remove(u"z"_qs));
102 QCOMPARE(set.join(u","_qs), u"a,b,c"_qs);
103 QVERIFY(set.remove(u"b"_qs));
104 QCOMPARE(set.join(u","_qs), u"a,c"_qs);
105 QVERIFY(set.remove(u"a"_qs));
106 QCOMPARE(set.join(u","_qs), u"c"_qs);
107 QVERIFY(set.remove(u"c"_qs));
108 QVERIFY(set.isEmpty());
110 OrderedSet<QString> emptySet;
111 QVERIFY(!emptySet.remove(u"a"_qs));
112 QVERIFY(emptySet.isEmpty());
115 void testUnite() const
117 const OrderedSet<QString> newData1 {u"z"_qs};
118 const OrderedSet<QString> newData2 {u"y"_qs};
119 const QSet<QString> newData3 {u"c"_qs, u"d"_qs, u"e"_qs};
121 OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
122 set.unite(newData1);
123 QCOMPARE(set.join(u","_qs), u"a,b,c,z"_qs);
124 set.unite(newData2);
125 QCOMPARE(set.join(u","_qs), u"a,b,c,y,z"_qs);
126 set.unite(newData3);
127 QCOMPARE(set.join(u","_qs), u"a,b,c,d,e,y,z"_qs);
129 OrderedSet<QString> emptySet;
130 emptySet.unite(newData1).unite(newData2).unite(newData3);
131 QCOMPARE(emptySet.join(u","_qs), u"c,d,e,y,z"_qs);
134 void testUnited() const
136 const OrderedSet<QString> newData1 {u"z"_qs};
137 const OrderedSet<QString> newData2 {u"y"_qs};
138 const QSet<QString> newData3 {u"c"_qs, u"d"_qs, u"e"_qs};
140 OrderedSet<QString> set {u"a"_qs, u"b"_qs, u"c"_qs};
142 QCOMPARE(set.united(newData1).join(u","_qs), u"a,b,c,z"_qs);
143 QCOMPARE(set.united(newData2).join(u","_qs), u"a,b,c,y"_qs);
144 QCOMPARE(set.united(newData3).join(u","_qs), u"a,b,c,d,e"_qs);
146 QCOMPARE(OrderedSet<QString>().united(newData1).united(newData2).united(newData3).join(u","_qs), u"c,d,e,y,z"_qs);
150 QTEST_APPLESS_MAIN(TestOrderedSet)
151 #include "testorderedset.moc"