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.
34 #include "base/global.h"
35 #include "base/orderedset.h"
36 #include "base/utils/string.h"
38 class TestOrderedSet final
: public QObject
41 Q_DISABLE_COPY_MOVE(TestOrderedSet
)
44 TestOrderedSet() = default;
47 void testCount() const
49 const OrderedSet
<QString
> set
{u
"a"_s
, u
"b"_s
, u
"c"_s
, u
"c"_s
};
50 QCOMPARE(set
.count(), 3);
52 const OrderedSet
<QString
> emptySet
;
53 QCOMPARE(emptySet
.count(), 0);
56 void testIntersect() const
58 OrderedSet
<QString
> set
{u
"a"_s
, u
"b"_s
, u
"c"_s
};
59 set
.intersect({u
"c"_s
, u
"a"_s
});
60 QCOMPARE(set
.size(), 2);
61 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"a,c"_s
);
63 OrderedSet
<QString
> emptySet
;
64 emptySet
.intersect({u
"a"_s
}).intersect({u
"c"_s
});;
65 QVERIFY(emptySet
.isEmpty());
68 void testIsEmpty() const
70 const OrderedSet
<QString
> set
{u
"a"_s
, u
"b"_s
, u
"c"_s
};
71 QVERIFY(!set
.isEmpty());
73 const OrderedSet
<QString
> emptySet
;
74 QVERIFY(emptySet
.isEmpty());
77 void testRemove() const
79 OrderedSet
<QString
> set
{u
"a"_s
, u
"b"_s
, u
"c"_s
};
80 QVERIFY(!set
.remove(u
"z"_s
));
81 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"a,b,c"_s
);
82 QVERIFY(set
.remove(u
"b"_s
));
83 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"a,c"_s
);
84 QVERIFY(set
.remove(u
"a"_s
));
85 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"c"_s
);
86 QVERIFY(set
.remove(u
"c"_s
));
87 QVERIFY(set
.isEmpty());
89 OrderedSet
<QString
> emptySet
;
90 QVERIFY(!emptySet
.remove(u
"a"_s
));
91 QVERIFY(emptySet
.isEmpty());
94 void testUnite() const
96 const OrderedSet
<QString
> newData1
{u
"z"_s
};
97 const OrderedSet
<QString
> newData2
{u
"y"_s
};
98 const QSet
<QString
> newData3
{u
"c"_s
, u
"d"_s
, u
"e"_s
};
100 OrderedSet
<QString
> set
{u
"a"_s
, u
"b"_s
, u
"c"_s
};
102 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"a,b,c,z"_s
);
104 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"a,b,c,y,z"_s
);
106 QCOMPARE(Utils::String::joinIntoString(set
, u
","_s
), u
"a,b,c,d,e,y,z"_s
);
108 OrderedSet
<QString
> emptySet
;
109 emptySet
.unite(newData1
).unite(newData2
).unite(newData3
);
110 QCOMPARE(Utils::String::joinIntoString(emptySet
, u
","_s
), u
"c,d,e,y,z"_s
);
113 void testUnited() const
115 const OrderedSet
<QString
> newData1
{u
"z"_s
};
116 const OrderedSet
<QString
> newData2
{u
"y"_s
};
117 const QSet
<QString
> newData3
{u
"c"_s
, u
"d"_s
, u
"e"_s
};
119 OrderedSet
<QString
> set
{u
"a"_s
, u
"b"_s
, u
"c"_s
};
121 QCOMPARE(Utils::String::joinIntoString(set
.united(newData1
), u
","_s
), u
"a,b,c,z"_s
);
122 QCOMPARE(Utils::String::joinIntoString(set
.united(newData2
), u
","_s
), u
"a,b,c,y"_s
);
123 QCOMPARE(Utils::String::joinIntoString(set
.united(newData3
), u
","_s
), u
"a,b,c,d,e"_s
);
125 QCOMPARE(Utils::String::joinIntoString(OrderedSet
<QString
>().united(newData1
).united(newData2
).united(newData3
), u
","_s
), u
"c,d,e,y,z"_s
);
129 QTEST_APPLESS_MAIN(TestOrderedSet
)
130 #include "testorderedset.moc"