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.
31 #include "base/global.h"
32 #include "base/utils/version.h"
34 class TestUtilsVersion final
: public QObject
37 Q_DISABLE_COPY_MOVE(TestUtilsVersion
)
40 TestUtilsVersion() = default;
43 void testConstructors() const
45 // should not compile:
46 // Utils::Version<-1>();
47 // Utils::Version<0>();
48 // Utils::Version<2, 3>();
49 // Utils::Version<2, -1>();
50 // Utils::Version<2, 0>();
52 using TwoDigits
= Utils::Version
<2, 1>;
57 using ThreeDigits
= Utils::Version
<3, 3>;
58 // should not compile:
61 // ThreeDigits(1.0, 2, 3);
62 // ThreeDigits(1, 2, 3, 4);
66 void testIsValid() const
68 using ThreeDigits
= Utils::Version
<3>;
69 QCOMPARE(ThreeDigits().isValid(), false);
70 QCOMPARE(ThreeDigits(0, 0, 0).isValid(), false);
71 QCOMPARE(ThreeDigits(0, 0, -1).isValid(), false);
72 QCOMPARE(ThreeDigits(0, 0, 1).isValid(), true);
73 QCOMPARE(ThreeDigits(0, 1, 0).isValid(), true);
74 QCOMPARE(ThreeDigits(1, 0, 0).isValid(), true);
75 QCOMPARE(ThreeDigits(10, 11, 12).isValid(), true);
78 void testVersionComponents() const
80 const Utils::Version
<1> version1
{1};
81 QCOMPARE(version1
[0], 1);
82 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
83 QVERIFY_THROWS_EXCEPTION(std::out_of_range
, version1
[1]);
85 QCOMPARE(version1
.majorNumber(), 1);
86 // should not compile:
87 // version1.minorNumber();
88 // version1.revisionNumber();
89 // version1.patchNumber();
91 const Utils::Version
<2, 1> version2
{2};
92 QCOMPARE(version2
[0], 2);
93 QCOMPARE(version2
[1], 0);
94 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
95 QVERIFY_THROWS_EXCEPTION(std::out_of_range
, version2
[2]);
97 QCOMPARE(version2
.majorNumber(), 2);
98 QCOMPARE(version2
.minorNumber(), 0);
99 // should not compile:
100 // version2.revisionNumber();
101 // version2.patchNumber();
103 const Utils::Version
<3, 2> version3
{3, 2};
104 QCOMPARE(version3
[0], 3);
105 QCOMPARE(version3
[1], 2);
106 QCOMPARE(version3
[2], 0);
107 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
108 QVERIFY_THROWS_EXCEPTION(std::out_of_range
, version3
[3]);
110 QCOMPARE(version3
.majorNumber(), 3);
111 QCOMPARE(version3
.minorNumber(), 2);
112 QCOMPARE(version3
.revisionNumber(), 0);
113 // should not compile:
114 // version3.patchNumber();
116 const Utils::Version
<4> version4
{10, 11, 12, 13};
117 QCOMPARE(version4
[0], 10);
118 QCOMPARE(version4
[1], 11);
119 QCOMPARE(version4
[2], 12);
120 QCOMPARE(version4
[3], 13);
121 #if (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
122 QVERIFY_THROWS_EXCEPTION(std::out_of_range
, version4
[4]);
124 QCOMPARE(version4
.majorNumber(), 10);
125 QCOMPARE(version4
.minorNumber(), 11);
126 QCOMPARE(version4
.revisionNumber(), 12);
127 QCOMPARE(version4
.patchNumber(), 13);
130 void testToString() const
132 using OneMandatory
= Utils::Version
<2, 1>;
133 QCOMPARE(OneMandatory(10).toString(), u
"10"_qs
);
134 QCOMPARE(OneMandatory(2).toString(), u
"2"_qs
);
135 QCOMPARE(OneMandatory(2, 0).toString(), u
"2"_qs
);
136 QCOMPARE(OneMandatory(2, 2).toString(), u
"2.2"_qs
);
138 using FourDigits
= Utils::Version
<4>;
139 QCOMPARE(FourDigits(10, 11, 12, 13).toString(), u
"10.11.12.13"_qs
);
142 void testFromString() const
144 using OneMandatory
= Utils::Version
<2, 1>;
145 const OneMandatory default1
{10, 11};
146 QCOMPARE(OneMandatory::fromString(u
"1"_qs
, default1
), OneMandatory(1));
147 QCOMPARE(OneMandatory::fromString(u
"1.2"_qs
, default1
), OneMandatory(1, 2));
148 QCOMPARE(OneMandatory::fromString(u
"100.2000"_qs
, default1
), OneMandatory(100, 2000));
149 QCOMPARE(OneMandatory::fromString(u
"1,2"_qs
), OneMandatory());
150 QCOMPARE(OneMandatory::fromString(u
"1,2"_qs
, default1
), default1
);
151 QCOMPARE(OneMandatory::fromString(u
"1.2a"_qs
), OneMandatory());
152 QCOMPARE(OneMandatory::fromString(u
"1.2.a"_qs
), OneMandatory());
153 QCOMPARE(OneMandatory::fromString(u
""_qs
), OneMandatory());
154 QCOMPARE(OneMandatory::fromString(u
""_qs
, default1
), default1
);
155 QCOMPARE(OneMandatory::fromString(u
"random_string"_qs
), OneMandatory());
156 QCOMPARE(OneMandatory::fromString(u
"random_string"_qs
, default1
), default1
);
158 using FourDigits
= Utils::Version
<4, 3>;
159 const FourDigits default2
{10, 11, 12, 13};
160 QCOMPARE(FourDigits::fromString(u
"1"_qs
, default2
), default2
);
161 QCOMPARE(FourDigits::fromString(u
"1.2"_qs
), FourDigits());
162 QCOMPARE(FourDigits::fromString(u
"1.2.3"_qs
), FourDigits(1, 2, 3));
163 QCOMPARE(FourDigits::fromString(u
"1.2.3.0"_qs
), FourDigits(1, 2, 3));
164 QCOMPARE(FourDigits::fromString(u
"1.2.3.4"_qs
), FourDigits(1, 2, 3, 4));
167 void testComparisons() const
169 using ThreeDigits
= Utils::Version
<3>;
171 QVERIFY(ThreeDigits() == ThreeDigits());
172 QVERIFY(!(ThreeDigits() != ThreeDigits()));
173 QVERIFY(ThreeDigits() <= ThreeDigits());
174 QVERIFY(ThreeDigits() >= ThreeDigits());
176 QVERIFY(ThreeDigits() != ThreeDigits(1, 2, 3));
177 QVERIFY(ThreeDigits() < ThreeDigits(1, 2, 3));
178 QVERIFY(ThreeDigits() <= ThreeDigits(1, 2, 3));
180 QVERIFY(ThreeDigits(1, 2, 3) != ThreeDigits());
181 QVERIFY(ThreeDigits(1, 2, 3) > ThreeDigits());
182 QVERIFY(ThreeDigits(1, 2, 3) >= ThreeDigits());
184 QVERIFY(ThreeDigits(1, 3, 3) != ThreeDigits(2, 2, 3));
185 QVERIFY(ThreeDigits(1, 3, 3) < ThreeDigits(2, 2, 3));
186 QVERIFY(ThreeDigits(1, 3, 3) <= ThreeDigits(2, 2, 3));
188 QVERIFY(ThreeDigits(1, 2, 3) != ThreeDigits(1, 2, 4));
189 QVERIFY(ThreeDigits(1, 2, 3) < ThreeDigits(1, 2, 4));
190 QVERIFY(ThreeDigits(1, 2, 3) <= ThreeDigits(1, 2, 4));
194 QTEST_APPLESS_MAIN(TestUtilsVersion
)
195 #include "testutilsversion.moc"