Skip to content

Commit 89c75e1

Browse files
authored
Better version computation (#290)
1 parent bd56211 commit 89c75e1

2 files changed

Lines changed: 86 additions & 37 deletions

File tree

Framework/include/QualityControl/Version.h.in

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
#include <string>
2020
#include <sstream>
21+
#include <iostream>
22+
23+
using namespace std;
2124

2225
namespace o2::quality_control::core {
2326

2427
/// Represents a software package version.
25-
/// Inspired from https://sourcey.com/articles/comparing-version-strings-in-cpp
28+
/// Version numbers can go up to 999.
2629
class Version
2730
{
2831
public:
@@ -31,10 +34,10 @@ class Version
3134
/// @param version The version in the form X.Y.Z. If minor or patch is missing, it is replaced by 0.
3235
Version(std::string version)
3336
{
34-
std::sscanf(version.c_str(), "%d.%d.%d", &mMajor, &mMinor, &mPatch);
37+
std::sscanf(version.c_str(), "%u.%u.%u", &mMajor, &mMinor, &mPatch);
3538
}
3639

37-
Version(int major, int minor, int patch): mMajor(major), mMinor(minor), mPatch(patch)
40+
Version(unsigned int major, unsigned int minor, unsigned int patch): mMajor(major), mMinor(minor), mPatch(patch)
3841
{
3942
}
4043

@@ -49,57 +52,77 @@ class Version
4952
return qcVersion;
5053
}
5154

52-
int getMajor() const
55+
unsigned int getMajor() const
5356
{
5457
return mMajor;
5558
}
5659

57-
int getMinor() const
60+
unsigned int getMinor() const
5861
{
5962
return mMinor;
6063
}
6164

62-
int getPatch() const
65+
unsigned int getPatch() const
6366
{
6467
return mPatch;
6568
}
6669

6770
bool operator<(const Version &other)
6871
{
69-
if (getMajor() < other.getMajor()) {
70-
return true;
71-
}
72-
if (getMinor() < other.getMinor()) {
73-
return true;
74-
}
75-
if (getPatch() < other.getPatch()) {
76-
return true;
77-
}
78-
return false;
72+
return getIntegerRepresentation() < other.getIntegerRepresentation();
73+
}
74+
75+
bool operator>=(const Version &other)
76+
{
77+
return getIntegerRepresentation() >= other.getIntegerRepresentation();
78+
}
79+
80+
bool operator>(const Version &other)
81+
{
82+
return getIntegerRepresentation() > other.getIntegerRepresentation();
83+
}
84+
85+
bool operator<=(const Version &other)
86+
{
87+
return getIntegerRepresentation() <= other.getIntegerRepresentation();
7988
}
8089

8190
bool operator==(const Version &other)
8291
{
83-
return getMajor() == other.getMajor()
84-
&& getMinor() == other.getMinor()
85-
&& getPatch() == other.getPatch();
92+
return getIntegerRepresentation() == other.getIntegerRepresentation();
93+
}
94+
95+
bool operator!=(const Version &other)
96+
{
97+
return getIntegerRepresentation() != other.getIntegerRepresentation();
8698
}
8799

88100
friend std::ostream &operator<<(std::ostream &stream, const Version &ver)
89101
{
90-
stream << ver.getMajor() << '.' << ver.getMinor() << '.' << ver.getPatch();
102+
stream << ver.getString();
91103
return stream;
92104
}
93105

94-
std::string getString()
106+
std::string getString() const
95107
{
96-
std::ostringstream version;
97-
version << this;
98-
return version.str();
108+
std::stringstream result;
109+
result << getMajor() << '.' << getMinor() << '.' << getPatch();
110+
return result.str();
111+
}
112+
113+
unsigned int getIntegerRepresentation() const
114+
{
115+
return getMajor() * (getMaxVersion() * getMaxVersion()) + getMinor() * getMaxVersion() + getPatch() * 1;
116+
}
117+
118+
unsigned int getMaxVersion() const {
119+
return mMaxVersion;
99120
}
100121

101122
private:
102-
int mMajor = 0, mMinor = 0, mPatch = 0;
123+
unsigned int mMajor = 0, mMinor = 0, mPatch = 0;
124+
unsigned int mMaxVersion = 1000;
125+
103126
};
104127

105128
} // namespace o2::quality_control::core

Framework/test/testVersion.cxx

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,52 @@ using namespace std;
2626
namespace o2::quality_control::core
2727
{
2828

29-
BOOST_AUTO_TEST_CASE(test_original_version)
29+
BOOST_AUTO_TEST_CASE(test_int_repr)
3030
{
31-
assert((Version("3.7.8.0") == Version("3.7.8.0")) == true);
32-
assert((Version("3.7.8.0") == Version("3.7.8")) == true);
33-
assert((Version("3.7.8.0") < Version("3.7.8")) == false);
34-
assert((Version("3.7.9") < Version("3.7.8")) == false);
35-
assert((Version("3") < Version("3.7.9")) == true);
36-
assert((Version("1.7.9") < Version("3.1")) == true);
37-
assert((Version("") == Version("0.0.0")) == true);
38-
assert((Version("0") == Version("0.0.0")) == true);
39-
assert((Version("") != Version("0.0.1")) == true);
40-
41-
std::cout << "Printing version (3.7.8): " << Version("3.7.8.0") << std::endl;
31+
Version v1("0.19.2");
32+
Version v2("1.19.2");
33+
Version v3("2.0.0");
34+
BOOST_CHECK(v1.getIntegerRepresentation() == (19002));
35+
BOOST_CHECK(v2.getIntegerRepresentation() == 1019002);
36+
BOOST_CHECK(v3.getIntegerRepresentation() == 2000000);
4237
}
4338

4439
BOOST_AUTO_TEST_CASE(test_version)
4540
{
41+
BOOST_CHECK((Version("3.7.8.0") == Version("3.7.8.0")) == true);
42+
BOOST_CHECK((Version("3.7.8.0") == Version("3.7.8")) == true);
43+
BOOST_CHECK((Version("3.7.8.0") < Version("3.7.8")) == false);
44+
BOOST_CHECK((Version("3.7.9") < Version("3.7.8")) == false);
45+
BOOST_CHECK((Version("3") < Version("3.7.9")) == true);
46+
BOOST_CHECK((Version("1.7.9") < Version("3.1")) == true);
47+
BOOST_CHECK((Version("") == Version("0.0.0")) == true);
48+
BOOST_CHECK((Version("0") == Version("0.0.0")) == true);
49+
BOOST_CHECK((Version("") != Version("0.0.1")) == true);
50+
BOOST_CHECK((Version("2.0.0") < Version("1.19.0")) == false);
51+
4652
Version v("2.0.0");
4753
BOOST_CHECK(v == Version("2.0.0"));
4854
Version qc = Version::GetQcVersion();
4955
BOOST_CHECK(qc.getMajor() != 0 || qc.getMinor() != 0 || qc.getPatch() != 0);
5056
cout << qc << endl;
57+
58+
Version v2("3.2.1");
59+
BOOST_CHECK(v2.getMajor() == 3);
60+
BOOST_CHECK(v2.getMinor() == 2);
61+
BOOST_CHECK(v2.getPatch() == 1);
62+
63+
BOOST_CHECK(v < Version("2.1.0"));
64+
BOOST_CHECK(v < Version("2.1"));
65+
BOOST_CHECK(v < Version("20"));
66+
BOOST_CHECK(v >= Version("1.19"));
67+
BOOST_CHECK(v >= Version("1"));
68+
BOOST_CHECK(v >= Version("1.8.1"));
69+
BOOST_CHECK(v >= Version("2.0.0"));
70+
BOOST_CHECK(v >= Version("2.0"));
71+
BOOST_CHECK(v > Version("1.19"));
72+
BOOST_CHECK(v > Version("1"));
73+
BOOST_CHECK(v > Version("1.8.1"));
74+
BOOST_CHECK(!(v > Version("2.0.0")));
5175
}
5276

5377
BOOST_AUTO_TEST_CASE(test_output)
@@ -57,6 +81,8 @@ BOOST_AUTO_TEST_CASE(test_output)
5781
output << v;
5882

5983
BOOST_CHECK(output.is_equal("1.2.3"));
84+
85+
BOOST_CHECK(v.getString() == "1.2.3");
6086
}
6187

6288
} // namespace o2::quality_control::core

0 commit comments

Comments
 (0)