Revamp alerts handling
[qBittorrent.git] / test / testutilsversion.cpp
blobfcabedca6cc6bdfdc38e9c33903d85c856e101b0
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include <QObject>
30 #include <QTest>
32 #include "base/global.h"
33 #include "base/utils/version.h"
35 class TestUtilsVersion final : public QObject
37 Q_OBJECT
38 Q_DISABLE_COPY_MOVE(TestUtilsVersion)
40 public:
41 TestUtilsVersion() = default;
43 private slots:
44 void testConstructors() const
46 // should not compile:
47 // Utils::Version<-1>();
48 // Utils::Version<0>();
49 // Utils::Version<2, 3>();
50 // Utils::Version<2, -1>();
51 // Utils::Version<2, 0>();
53 using TwoDigits = Utils::Version<2, 1>;
54 QCOMPARE(TwoDigits(0), TwoDigits(u"0"_s));
55 QCOMPARE(TwoDigits(50), TwoDigits(u"50"_s));
56 QCOMPARE(TwoDigits(0, 1), TwoDigits(u"0.1"_s));
58 using ThreeDigits = Utils::Version<3, 3>;
59 // should not compile:
60 // ThreeDigits(1);
61 // ThreeDigits(1, 2);
62 // ThreeDigits(1.0, 2, 3);
63 // ThreeDigits(1, 2, 3, 4);
64 QCOMPARE(ThreeDigits(1, 2, 3), ThreeDigits(u"1.2.3"_s));
67 void testIsValid() const
69 using ThreeDigits = Utils::Version<3>;
70 QCOMPARE(ThreeDigits().isValid(), false);
71 QCOMPARE(ThreeDigits(0, 0, 0).isValid(), false);
72 QCOMPARE(ThreeDigits(0, 0, -1).isValid(), false);
73 QCOMPARE(ThreeDigits(0, 0, 1).isValid(), true);
74 QCOMPARE(ThreeDigits(0, 1, 0).isValid(), true);
75 QCOMPARE(ThreeDigits(1, 0, 0).isValid(), true);
76 QCOMPARE(ThreeDigits(10, 11, 12).isValid(), true);
79 void testVersionComponents() const
81 const Utils::Version<1> version1 {1};
82 QCOMPARE(version1[0], 1);
83 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
84 QVERIFY_THROWS_EXCEPTION(std::out_of_range, version1[1]);
85 #endif
86 QCOMPARE(version1.majorNumber(), 1);
87 // should not compile:
88 // version1.minorNumber();
89 // version1.revisionNumber();
90 // version1.patchNumber();
92 const Utils::Version<2, 1> version2 {2};
93 QCOMPARE(version2[0], 2);
94 QCOMPARE(version2[1], 0);
95 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
96 QVERIFY_THROWS_EXCEPTION(std::out_of_range, version2[2]);
97 #endif
98 QCOMPARE(version2.majorNumber(), 2);
99 QCOMPARE(version2.minorNumber(), 0);
100 // should not compile:
101 // version2.revisionNumber();
102 // version2.patchNumber();
104 const Utils::Version<3, 2> version3 {3, 2};
105 QCOMPARE(version3[0], 3);
106 QCOMPARE(version3[1], 2);
107 QCOMPARE(version3[2], 0);
108 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
109 QVERIFY_THROWS_EXCEPTION(std::out_of_range, version3[3]);
110 #endif
111 QCOMPARE(version3.majorNumber(), 3);
112 QCOMPARE(version3.minorNumber(), 2);
113 QCOMPARE(version3.revisionNumber(), 0);
114 // should not compile:
115 // version3.patchNumber();
117 const Utils::Version<4> version4 {10, 11, 12, 13};
118 QCOMPARE(version4[0], 10);
119 QCOMPARE(version4[1], 11);
120 QCOMPARE(version4[2], 12);
121 QCOMPARE(version4[3], 13);
122 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
123 QVERIFY_THROWS_EXCEPTION(std::out_of_range, version4[4]);
124 #endif
125 QCOMPARE(version4.majorNumber(), 10);
126 QCOMPARE(version4.minorNumber(), 11);
127 QCOMPARE(version4.revisionNumber(), 12);
128 QCOMPARE(version4.patchNumber(), 13);
131 void testToString() const
133 using OneMandatory = Utils::Version<2, 1>;
134 QCOMPARE(OneMandatory(10).toString(), u"10"_s);
135 QCOMPARE(OneMandatory(2).toString(), u"2"_s);
136 QCOMPARE(OneMandatory(2, 0).toString(), u"2"_s);
137 QCOMPARE(OneMandatory(2, 2).toString(), u"2.2"_s);
139 using FourDigits = Utils::Version<4>;
140 QCOMPARE(FourDigits(10, 11, 12, 13).toString(), u"10.11.12.13"_s);
143 void testFromString() const
145 using OneMandatory = Utils::Version<2, 1>;
146 const OneMandatory default1 {10, 11};
147 QCOMPARE(OneMandatory::fromString(u"1"_s, default1), OneMandatory(1));
148 QCOMPARE(OneMandatory::fromString(u"1.2"_s, default1), OneMandatory(1, 2));
149 QCOMPARE(OneMandatory::fromString(u"100.2000"_s, default1), OneMandatory(100, 2000));
150 QCOMPARE(OneMandatory::fromString(u"1,2"_s), OneMandatory());
151 QCOMPARE(OneMandatory::fromString(u"1,2"_s, default1), default1);
152 QCOMPARE(OneMandatory::fromString(u"1.2a"_s), OneMandatory());
153 QCOMPARE(OneMandatory::fromString(u"1.2.a"_s), OneMandatory());
154 QCOMPARE(OneMandatory::fromString(u""_s), OneMandatory());
155 QCOMPARE(OneMandatory::fromString(u""_s, default1), default1);
156 QCOMPARE(OneMandatory::fromString(u"random_string"_s), OneMandatory());
157 QCOMPARE(OneMandatory::fromString(u"random_string"_s, default1), default1);
159 using FourDigits = Utils::Version<4, 3>;
160 const FourDigits default2 {10, 11, 12, 13};
161 QCOMPARE(FourDigits::fromString(u"1"_s, default2), default2);
162 QCOMPARE(FourDigits::fromString(u"1.2"_s), FourDigits());
163 QCOMPARE(FourDigits::fromString(u"1.2.3"_s), FourDigits(1, 2, 3));
164 QCOMPARE(FourDigits::fromString(u"1.2.3.0"_s), FourDigits(1, 2, 3));
165 QCOMPARE(FourDigits::fromString(u"1.2.3.4"_s), FourDigits(1, 2, 3, 4));
168 void testComparisons() const
170 using ThreeDigits = Utils::Version<3>;
172 QVERIFY(ThreeDigits() == ThreeDigits());
173 QVERIFY(!(ThreeDigits() != ThreeDigits()));
174 QVERIFY(ThreeDigits() <= ThreeDigits());
175 QVERIFY(ThreeDigits() >= ThreeDigits());
177 QVERIFY(ThreeDigits() != ThreeDigits(1, 2, 3));
178 QVERIFY(ThreeDigits() < ThreeDigits(1, 2, 3));
179 QVERIFY(ThreeDigits() <= ThreeDigits(1, 2, 3));
181 QVERIFY(ThreeDigits(1, 2, 3) != ThreeDigits());
182 QVERIFY(ThreeDigits(1, 2, 3) > ThreeDigits());
183 QVERIFY(ThreeDigits(1, 2, 3) >= ThreeDigits());
185 QVERIFY(ThreeDigits(1, 3, 3) != ThreeDigits(2, 2, 3));
186 QVERIFY(ThreeDigits(1, 3, 3) < ThreeDigits(2, 2, 3));
187 QVERIFY(ThreeDigits(1, 3, 3) <= ThreeDigits(2, 2, 3));
189 QVERIFY(ThreeDigits(1, 2, 3) != ThreeDigits(1, 2, 4));
190 QVERIFY(ThreeDigits(1, 2, 3) < ThreeDigits(1, 2, 4));
191 QVERIFY(ThreeDigits(1, 2, 3) <= ThreeDigits(1, 2, 4));
195 QTEST_APPLESS_MAIN(TestUtilsVersion)
196 #include "testutilsversion.moc"