Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

old archives should be compatible with newer version #43

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/serialbox/core/SerializerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ void SerializerImpl::constructMetaDataFromJson() {

int serialboxVersion = jsonNode["serialbox_version"];

if(!Version::match(serialboxVersion))
if(!Version::isCompatible(serialboxVersion))
throw Exception(
"serialbox version of MetaData (%s) does not match the version of the library (%s)",
Version::toString(serialboxVersion), SERIALBOX_VERSION_STRING);
Expand Down
23 changes: 14 additions & 9 deletions src/serialbox/core/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ namespace serialbox {

/// \brief Utility to deal with Serialbox versions
struct Version {
private:
static int getVersion() {
return SERIALBOX_VERSION_MAJOR * 100 + SERIALBOX_VERSION_MINOR * 10 + SERIALBOX_VERSION_PATCH;
}

public:
Version() = delete;

/// \brief Convert to string
Expand All @@ -44,16 +50,15 @@ struct Version {
///
/// \return Return true if the versions match
/// @{
static bool match(int version) noexcept {
int major = version / 100;
int minor = (version - major * 100) / 10;
return Version::match(major, minor, version - 100 * major - 10 * minor);
}
static bool match(int version) noexcept { return version == getVersion(); }
/// @}

static bool match(int major, int minor, int patch) noexcept {
return ((major == SERIALBOX_VERSION_MAJOR) && (minor == SERIALBOX_VERSION_MINOR) &&
(patch == SERIALBOX_VERSION_PATCH));
}
/// \brief Check if the given version is compatible with the current library version (i.e. is
/// older)
///
/// \return Return true if the versions is compatible
/// @{
static bool isCompatible(int version) noexcept { return version <= getVersion(); }
/// @}
};

Expand Down
2 changes: 1 addition & 1 deletion src/serialbox/core/archive/BinaryArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void BinaryArchive::readMetaDataFromJson() {
std::string hashAlgorithm = json_["hash_algorithm"];

// Check consistency
if(!Version::match(serialboxVersion))
if(!Version::isCompatible(serialboxVersion))
throw Exception("serialbox version of binary archive (%s) does not match the version "
"of the library (%s)",
Version::toString(serialboxVersion), SERIALBOX_VERSION_STRING);
Expand Down
5 changes: 3 additions & 2 deletions test/serialbox/core/UnittestVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ TEST(VersionTest, Comparison) {
int minor = SERIALBOX_VERSION_MINOR;
int patch = SERIALBOX_VERSION_PATCH;

EXPECT_TRUE(Version::match(major, minor, patch));
EXPECT_FALSE(Version::match(major - 1, minor, patch));
EXPECT_TRUE(Version::match(major * 100 + minor * 10 + patch));
EXPECT_FALSE(Version::match(0));

EXPECT_TRUE(Version::isCompatible(major * 100 + minor * 10 + (patch - 1)));
EXPECT_FALSE(Version::isCompatible(major * 100 + minor * 10 + (patch + 1)));
}

TEST(VersionTest, ToString) {
Expand Down
4 changes: 2 additions & 2 deletions test/serialbox/core/archive/UnittestBinaryArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ TEST_F(BinaryArchiveUtilityTest, MetaData) {
};

// -----------------------------------------------------------------------------------------------
// Invlaid serialbox version
// Invalid serialbox version
// -----------------------------------------------------------------------------------------------
{
json::json corrupted = j;
corrupted["serialbox_version"] = 100 * (SERIALBOX_VERSION_MAJOR - 1) +
corrupted["serialbox_version"] = 100 * (SERIALBOX_VERSION_MAJOR + 1) +
10 * SERIALBOX_VERSION_MINOR + SERIALBOX_VERSION_PATCH;
toFile(corrupted);

Expand Down