1010/fixed version comparisons (#1622)
Build on RHEL9 docker image / build (push) Successful in 4m44s
Build on RHEL8 docker image / build (push) Successful in 5m3s
Run Simulator Tests on local RHEL9 / build (push) Successful in 19m30s
Run Simulator Tests on local RHEL8 / build (push) Successful in 23m0s

* fixed version comparisons, previously done lexicographically

* using structs and refactoring

* tests and small comment fix

* tests to check prerelease <= release
This commit is contained in:
maliakal_d authored and GitHub committed 2026-10-22 16:32:08 +02:00
1 parent 95c99d0789
commit 6552490232
3 files changed
+268 -40

No files matched your search

+25 -5
View File
@@ -2,32 +2,52 @@
// Copyright (C) 2021 Contributors to the SLS Detector Package
#pragma once
#include <iosfwd>
#include <stdint.h>
#include <string>
namespace sls {
struct SemanticVersion {
int major{0};
int minor{0};
int patch{0};
std::string prerelease{};
std::string toString() const;
bool operator==(const SemanticVersion &other) const;
bool operator<=(const SemanticVersion &other) const;
};
class Version {
private:
std::string version_;
/** Note: "0.0.0" is considered not semantic versioning here */
bool hasSemanticVersioning_{false};
SemanticVersion version_{};
std::string date_;
inline static const std::string defaultVersion_[] = {"developer", "0.0.0"};
SemanticVersion parseSemanticVersion(const std::string &version);
public:
explicit Version(const std::string &s);
std::string toString() const;
bool hasSemanticVersioning() const;
std::string getVersion() const;
std::string getDate() const;
std::string concise() const;
int getMajorVersion() const;
// expects semantic versioning
// throws if not semantic versioning
SemanticVersion getSemanticVersion() const;
int getMajorVersion() const;
int getMinorVersion() const;
int getPatchVersion() const;
std::string getPrereleaseVersion() const;
bool isBackwardCompatible(const Version &other) const;
bool operator!=(const Version &other) const;
bool operator==(const Version &other) const;
bool operator<=(const Version &other) const;
};
std::ostream &operator<<(std::ostream &out, const SemanticVersion &v);
std::ostream &operator<<(std::ostream &out, const Version &v);
} // namespace sls
+124 -34
View File
@@ -1,84 +1,174 @@
// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2021 Contributors to the SLS Detector Package
#include "sls/Version.h"
#include "sls/logger.h"
#include "sls/sls_detector_exceptions.h"
#include "sls/string_utils.h"
#include <cstdio>
#include <sstream>
namespace sls {
std::string SemanticVersion::toString() const {
std::ostringstream oss;
oss << major << "." << minor << "." << patch;
if (!prerelease.empty()) {
oss << prerelease;
}
return oss.str();
}
bool SemanticVersion::operator==(const SemanticVersion &other) const {
return major == other.major && minor == other.minor &&
patch == other.patch && prerelease == other.prerelease;
}
bool SemanticVersion::operator<=(const SemanticVersion &other) const {
if (major != other.major)
return major < other.major;
if (minor != other.minor)
return minor < other.minor;
if (patch != other.patch)
return patch < other.patch;
// A release version is greater than its prerelease
if (prerelease.empty() && !other.prerelease.empty())
return false;
if (!prerelease.empty() && other.prerelease.empty())
return true;
return prerelease <= other.prerelease;
}
SemanticVersion Version::parseSemanticVersion(const std::string &version) {
SemanticVersion ver;
// assuming only released versions have semantic versioning
if (version == "developer" || version == "0.0.0") {
return ver;
}
char prereleaseBuffer[256] = {'\0'};
// Parse major.minor.patch and capture any optional trailing suffix.
const int scanned = sscanf(version.c_str(), "%d.%d.%d%255s", &ver.major,
&ver.minor, &ver.patch, prereleaseBuffer);
if (scanned == 3 || scanned == 4) {
hasSemanticVersioning_ = true;
if (scanned == 4)
ver.prerelease = prereleaseBuffer;
else
ver.prerelease.clear();
}
return ver;
}
Version::Version(const std::string &s) {
auto list = split(s, ' ');
// only date from previous releases
if (list.size() == 1) {
date_ = list[0];
}
// semantic versioning + date
else {
version_ = list[0];
auto version = list[0];
date_ = list[1];
version_ = parseSemanticVersion(version);
}
}
bool Version::hasSemanticVersioning() const {
return (version_ != defaultVersion_[0]) && (version_ != defaultVersion_[1]);
std::string Version::toString() const {
std::ostringstream oss;
oss << version_ << " " << getDate();
return oss.str();
}
std::string Version::getVersion() const { return version_; }
bool Version::hasSemanticVersioning() const { return hasSemanticVersioning_; }
std::string Version::getVersion() const { return version_.toString(); }
std::string Version::getDate() const { return date_; }
std::string Version::concise() const {
if (hasSemanticVersioning())
return version_;
if (hasSemanticVersioning_)
return version_.toString();
return date_;
}
int Version::getMajorVersion() const {
int major = 0, minor = 0, patch = 0;
if (sscanf(version_.c_str(), "%d.%d.%d", &major, &minor, &patch) == 3) {
return major;
SemanticVersion Version::getSemanticVersion() const {
if (!hasSemanticVersioning_) {
throw sls::RuntimeError("No semantic versioning available for " +
toString());
}
throw sls::RuntimeError("Could not get major version from " + version_);
return version_;
}
int Version::getMajorVersion() const {
if (!hasSemanticVersioning_) {
throw sls::RuntimeError("Could not get major version from " +
toString());
}
return version_.major;
}
int Version::getMinorVersion() const {
if (!hasSemanticVersioning_) {
throw sls::RuntimeError("Could not get minor version from " +
toString());
}
return version_.minor;
}
int Version::getPatchVersion() const {
if (!hasSemanticVersioning_) {
throw sls::RuntimeError("Could not get patch version from " +
toString());
}
return version_.patch;
}
std::string Version::getPrereleaseVersion() const {
if (!hasSemanticVersioning_) {
throw sls::RuntimeError("Could not get prerelease version from " +
toString());
}
return version_.prerelease;
}
bool Version::isBackwardCompatible(const Version &other) const {
if (!hasSemanticVersioning_ || !other.hasSemanticVersioning()) {
throw sls::RuntimeError("Cannot check backward compatibility without "
"semantic versioning for " +
toString() + " and " + other.toString());
}
return getMajorVersion() == other.getMajorVersion();
}
bool Version::operator==(const Version &other) const {
if (hasSemanticVersioning() && other.hasSemanticVersioning()) {
return version_ == other.getSemanticVersion();
}
return date_ == other.getDate();
}
bool Version::operator!=(const Version &other) const {
return !(*this == other);
}
bool Version::operator==(const Version &other) const {
// both have semantic versioning
if (hasSemanticVersioning() && other.hasSemanticVersioning()) {
return version_ == other.getVersion();
}
// compare dates
return date_ == other.getDate();
}
bool Version::operator<=(const Version &other) const {
// both have semantic versioning
if (hasSemanticVersioning() && other.hasSemanticVersioning()) {
// release version
std::string otherVersion = other.getVersion();
if (version_ == otherVersion)
return true;
// less than
return (std::lexicographical_compare(version_.begin(), version_.end(),
otherVersion.begin(),
otherVersion.end()));
return version_ <= other.getSemanticVersion();
}
// compare dates
return date_ <= other.getDate();
}
std::ostream &operator<<(std::ostream &out, const SemanticVersion &v) {
return out << v.toString();
}
std::ostream &operator<<(std::ostream &out, const Version &v) {
std::ostringstream oss;
oss << v.getVersion() << " " << v.getDate();
return out << oss.str();
return out << v.toString();
}
} // namespace sls
+119 -1
View File
@@ -9,11 +9,129 @@ TEST_CASE("check if version is semantic", "[version]") {
auto [version_string, has_semantic_version] =
GENERATE(std::make_tuple("developer 0x250512", false),
std::make_tuple("0.0.0 0x250512", false));
std::make_tuple("0.0.0 0x250512", false),
std::make_tuple("10.1.1 0x250512", true),
std::make_tuple("0x250512", false));
Version version(version_string);
CHECK(version.hasSemanticVersioning() == has_semantic_version);
}
TEST_CASE("semantic version ordering compares numerically", "[version]") {
Version v9("9.1.1 0x250512");
Version v10("10.0.1 0x250512");
CHECK(v9 <= v10);
CHECK_FALSE(v10 <= v9);
}
TEST_CASE("semantic version compares prerelease lexicographically",
"[version]") {
Version rc("10.1.1.rc 0x250512");
Version zz("10.1.1.zz 0x250512");
CHECK(rc <= zz);
CHECK_FALSE(zz <= rc);
}
TEST_CASE("semantic version compares prerelease with no prerelease",
"[version]") {
Version prerelease("10.1.1.rc 0x250512");
Version release("10.1.1 0x250512");
CHECK(prerelease <= release);
CHECK_FALSE(release <= prerelease);
}
TEST_CASE(
"semantic comparison falls back to date on malformed semantic version",
"[version]") {
Version malformed("10.1 0x250512");
Version valid("10.1.1 0x250513");
CHECK(malformed <= valid);
CHECK_FALSE(valid <= malformed);
}
TEST_CASE("semantic version compares prerelease and without", "[version]") {
Version v1011rc("10.1.1.rc 0x250512");
Version v1010("10.1.0 0x250512");
CHECK_FALSE(v1011rc <= v1010);
CHECK(v1010 <= v1011rc);
}
TEST_CASE("semantic version", "[version]") {
auto version = Version("10.1.1.rc 0x250512");
CHECK(version.hasSemanticVersioning());
CHECK(version.getDate() == "0x250512");
CHECK(version.toString() == "10.1.1.rc 0x250512");
auto semver = version.getSemanticVersion();
CHECK(semver.toString() == "10.1.1.rc");
CHECK(semver.major == 10);
CHECK(semver.minor == 1);
CHECK(semver.patch == 1);
CHECK(semver.prerelease == ".rc");
auto version2 = Version("9.2.1 0x250512");
CHECK(version2.hasSemanticVersioning());
CHECK(version2.getDate() == "0x250512");
CHECK(version2.toString() == "9.2.1 0x250512");
auto semver2 = version2.getSemanticVersion();
CHECK(semver2.toString() == "9.2.1");
CHECK(semver2.major == 9);
CHECK(semver2.minor == 2);
CHECK(semver2.patch == 1);
CHECK(semver2.prerelease == "");
CHECK(version != version2);
CHECK(version2 <= version);
CHECK_FALSE(version <= version2);
auto version3 = Version("10.1.1.rc2 0x250512");
CHECK(version <= version3);
auto version4 = Version("10.1.1.rc3 0x250512");
CHECK(version3 <= version4);
auto release = Version("10.1.1 0x250512");
CHECK(version <= release);
auto dev_version = Version("developer 0x250512");
CHECK_FALSE(dev_version.hasSemanticVersioning());
CHECK(dev_version.getDate() == "0x250512");
CHECK(dev_version.toString() == "0.0.0 0x250512");
REQUIRE_THROWS(dev_version.getSemanticVersion());
REQUIRE_THROWS(dev_version.getMajorVersion());
REQUIRE_THROWS(dev_version.getMinorVersion());
REQUIRE_THROWS(dev_version.getPatchVersion());
REQUIRE_THROWS(dev_version.getPrereleaseVersion());
REQUIRE_THROWS(dev_version.isBackwardCompatible(version));
CHECK(dev_version == version);
auto zero_version = Version("0.0.0 0x250512");
CHECK_FALSE(zero_version.hasSemanticVersioning());
CHECK(zero_version.getDate() == "0x250512");
CHECK(zero_version.toString() == "0.0.0 0x250512");
REQUIRE_THROWS(zero_version.getSemanticVersion());
REQUIRE_THROWS(zero_version.getMajorVersion());
REQUIRE_THROWS(zero_version.getMinorVersion());
REQUIRE_THROWS(zero_version.getPatchVersion());
REQUIRE_THROWS(zero_version.getPrereleaseVersion());
REQUIRE_THROWS(zero_version.isBackwardCompatible(version));
CHECK(zero_version == dev_version);
auto date_only_version = Version("0x250512");
CHECK_FALSE(date_only_version.hasSemanticVersioning());
CHECK(date_only_version.getDate() == "0x250512");
CHECK(date_only_version.toString() == "0.0.0 0x250512");
REQUIRE_THROWS(date_only_version.getSemanticVersion());
REQUIRE_THROWS(date_only_version.getMajorVersion());
REQUIRE_THROWS(date_only_version.getMinorVersion());
REQUIRE_THROWS(date_only_version.getPatchVersion());
REQUIRE_THROWS(date_only_version.getPrereleaseVersion());
REQUIRE_THROWS(date_only_version.isBackwardCompatible(version));
CHECK(date_only_version == zero_version);
}
} // namespace sls