diff --git a/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.cpp b/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.cpp index a093200b8e..5b82d52d18 100644 --- a/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.cpp +++ b/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.cpp @@ -23,62 +23,93 @@ //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool RiaProjectFileVersionTools::isProjectFileVersionNewerThan(const QString& projectFileVersion, - const RiaProjectFileVersionData& fileVersionData) +bool RiaProjectFileVersionTools::isCandidateVersionNewerThanOther(const QString& candidateProjectFileVersion, + const QString& projectFileVersion) { - return isProjectFileVersionNewerThan(projectFileVersion, fileVersionData.m_majorVersion, fileVersionData.m_minorVersion, - fileVersionData.m_patch, fileVersionData.m_developmentId); + int candidateMajorVersion = 0; + int candidateMinorVersion = 0; + int candidatePatchNumber = 0; + int candidateDevelopmentId = 0; + + RiaProjectFileVersionTools::decodeVersionString(candidateProjectFileVersion, &candidateMajorVersion, &candidateMinorVersion, + &candidatePatchNumber, &candidateDevelopmentId); + + int majorVersion = 0; + int minorVersion = 0; + int patchNumber = 0; + int developmentId = 0; + + RiaProjectFileVersionTools::decodeVersionString(projectFileVersion, &majorVersion, &minorVersion, &patchNumber, + &developmentId); + + return RiaProjectFileVersionTools::isCandidateNewerThanOther(candidateMajorVersion, candidateMinorVersion, + candidatePatchNumber, candidateDevelopmentId, majorVersion, + minorVersion, patchNumber, developmentId); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -bool RiaProjectFileVersionTools::isProjectFileVersionNewerThan(const QString& projectFileVersion, int majorVersion, - int minorVersion, int patchNumber, int developmentId) +void RiaProjectFileVersionTools::decodeVersionString(const QString& projectFileVersion, int* majorVersion, int* minorVersion, + int* patch, int* developmentId) { - int projectMajorVersion = 0; - int projectMinorVersion = 0; - int projectPatchNumber = 0; - int projectDevelopmentId = 0; + if (projectFileVersion.isEmpty()) return; - // Split string and interpret sub strings + QStringList subStrings = projectFileVersion.split("."); + + if (subStrings.size() > 0) { - QStringList subStrings = projectFileVersion.split("."); - - if (subStrings.size() > 0) - { - projectMajorVersion = subStrings[0].toInt(); - } - - if (subStrings.size() > 1) - { - projectMinorVersion = subStrings[1].toInt(); - } - - if (subStrings.size() > 2) - { - projectPatchNumber = subStrings[2].toInt(); - } + *majorVersion = subStrings[0].toInt(); } - if (projectMajorVersion != majorVersion) + if (subStrings.size() > 1) { - return (projectMajorVersion > majorVersion); + *minorVersion = subStrings[1].toInt(); } - if (projectMinorVersion != minorVersion) + if (subStrings.size() > 2) { - return (projectMinorVersion > minorVersion); + QString candidate = subStrings[2]; + QString candidateDigitsOnly = RiaProjectFileVersionTools::stringOfDigits(candidate); + + *patch = candidateDigitsOnly.toInt(); } - if (projectPatchNumber != patchNumber) + if (subStrings.size() > 3) { - return (projectPatchNumber > patchNumber); + QString candidate = subStrings.back(); + QString candidateDigitsOnly = RiaProjectFileVersionTools::stringOfDigits(candidate); + + *developmentId = candidateDigitsOnly.toInt(); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RiaProjectFileVersionTools::isCandidateNewerThanOther(int candidateMajorVersion, int candidateMinorVersion, + int candidatePatchNumber, int candidateDevelopmentId, + int otherMajorVersion, int otherMinorVersion, int otherPatchNumber, + int otherDevelopmentId) +{ + if (candidateMajorVersion != otherMajorVersion) + { + return (candidateMajorVersion > otherMajorVersion); } - if (projectDevelopmentId != developmentId) + if (candidateMinorVersion != otherMinorVersion) { - return (projectDevelopmentId > developmentId); + return (candidateMinorVersion > otherMinorVersion); + } + + if (candidatePatchNumber != otherPatchNumber) + { + return (candidatePatchNumber > otherPatchNumber); + } + + if (candidateDevelopmentId != otherDevelopmentId) + { + return (candidateDevelopmentId > otherDevelopmentId); } return false; @@ -87,68 +118,25 @@ bool RiaProjectFileVersionTools::isProjectFileVersionNewerThan(const QString& pr //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -QStringList RiaProjectFileVersionTools::knownProjectVersionStrings() +QString RiaProjectFileVersionTools::stringOfDigits(const QString& string) { - QStringList versionStrings; + QString digitsOnly; - versionStrings << "2017.05.2-dev.15"; - versionStrings << "2017.05.2-dev.14"; - versionStrings << "2017.05.2-dev.13"; - versionStrings << "2017.05.2-dev.12"; - versionStrings << "2017.05.2-dev.11"; - versionStrings << "2017.05.2-dev.10"; - versionStrings << "2017.05.2-dev.09"; - versionStrings << "2017.05.2-dev.08"; - versionStrings << "2017.05.2-dev.07"; - versionStrings << "2017.05.2-dev.06"; - versionStrings << "2017.05.2-dev.05"; - versionStrings << "2017.05.2-dev.04"; - versionStrings << "2017.05.2-dev.03"; - versionStrings << "2017.05.2-dev.02"; - versionStrings << "2017.05.2-fdev.02"; - versionStrings << "2017.05.2-dev.1"; - versionStrings << "2017.05.2-fdev.01"; - versionStrings << "2017.05.2"; - versionStrings << "2017.05.pre-proto.15"; - versionStrings << "2017.05.1-dev"; - versionStrings << "2017.05.1"; - versionStrings << "2017.05.0"; + for (const auto& c : string) + { + if (c.isDigit()) + { + digitsOnly += c; + } + else + { + if (!digitsOnly.isEmpty()) + { + return digitsOnly; + } + } + } - versionStrings << "2016.11.flow.14"; - versionStrings << "2016.11.flow.12"; - versionStrings << "2016.11.flow.11"; - versionStrings << "2016.11.flow.9"; - versionStrings << "2016.11.flow.8"; - versionStrings << "2016.11.flow.7"; - versionStrings << "2016.11.flow.1"; - versionStrings << "2016.11.m.1"; - versionStrings << "2016.11.0"; - - versionStrings << "1.6.10-dev"; - versionStrings << "1.6.9-dev"; - versionStrings << "1.6.8-dev"; - versionStrings << "1.6.7-gm-beta"; - versionStrings << "1.6.6-dev"; - versionStrings << "1.6.5-dev"; - versionStrings << "1.6.4-dev"; - versionStrings << "1.6.3-dev"; - versionStrings << "1.6.1-dev"; - versionStrings << "1.6.2-dev"; - versionStrings << "1.6.0-RC"; - - versionStrings << "1.5.111-RC"; - versionStrings << "1.5.110-RC"; - versionStrings << "1.5.109-RC"; - versionStrings << "1.5.108-RC"; - versionStrings << "1.5.107-RC"; - versionStrings << "1.5.106-RC"; - versionStrings << "1.5.105-RC"; - versionStrings << "1.5.104-RC"; - versionStrings << "1.5.103-dev"; - versionStrings << "1.5.102-dev"; - versionStrings << "1.5.101-dev"; - versionStrings << "1.5.100-dev"; - versionStrings << "1.5.0"; - - return versionStrings; + return digitsOnly; } + diff --git a/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.h b/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.h index 5a5b16138e..5752728421 100644 --- a/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.h +++ b/ApplicationCode/Application/Tools/RiaProjectFileVersionTools.h @@ -20,37 +20,23 @@ #include -//================================================================================================== -// -//================================================================================================== -struct RiaProjectFileVersionData -{ - int m_majorVersion; - int m_minorVersion; - int m_patch; - int m_developmentId; - - RiaProjectFileVersionData() : m_majorVersion(0), m_minorVersion(0), m_patch(0), m_developmentId(0) {} - - RiaProjectFileVersionData(int majorVersion, int minorVersion, int patch, int developmentId) - : m_majorVersion(majorVersion), m_minorVersion(minorVersion), m_patch(patch), m_developmentId(developmentId) - - { - } -}; - //================================================================================================== // //================================================================================================== class RiaProjectFileVersionTools { public: - static bool isProjectFileVersionNewerThan(const QString& projectFileVersion, - const RiaProjectFileVersionData& fileVersionData); + static bool isCandidateVersionNewerThanOther(const QString& candidateProjectFileVersion, + const QString& otherProjectFileVersion); + + // Public to be able to unit test function, not intended to be used + static void decodeVersionString(const QString& projectFileVersion, int* majorVersion, int* minorVersion, int* patch, + int* developmentId); private: - static bool isProjectFileVersionNewerThan(const QString& projectFileVersion, int majorVersion, int minorVersion, int patch, - int developmentId); + static bool isCandidateNewerThanOther(int candidateMajorVersion, int candidateMinorVersion, int candidatePatchNumber, + int candidateDevelopmentId, int otherMajorVersion, int otherMinorVersion, + int otherPatchNumber, int otherDevelopmentId); - static QStringList knownProjectVersionStrings(); + static QString stringOfDigits(const QString& string); }; diff --git a/ApplicationCode/UnitTests/RiaProjectFileVersionTools-Test.cpp b/ApplicationCode/UnitTests/RiaProjectFileVersionTools-Test.cpp index 0fa3a65963..51555a8f5d 100644 --- a/ApplicationCode/UnitTests/RiaProjectFileVersionTools-Test.cpp +++ b/ApplicationCode/UnitTests/RiaProjectFileVersionTools-Test.cpp @@ -1,30 +1,213 @@ #include "gtest/gtest.h" - +#include "RiaProjectFileVersionTools.h" +#include //-------------------------------------------------------------------------------------------------- -/// +/// //-------------------------------------------------------------------------------------------------- TEST(RiaProjectFileVersionTools, BasicUsage) { -/* - std::vector a(10); - std::iota(a.begin(), a.end(), 10); + /* + { + QString projectFileVersionString = "2017.05.1"; - std::vector b(10); - std::iota(b.begin(), b.end(), 100); + int majorVersion = 2017; + int minorVersion = 5; + int patchNumber = 0; + int developmentId = 0; - std::vector c(10); + bool isNewer = RiaProjectFileVersionTools::isProjectFileVersionNewerThan(projectFileVersionString, majorVersion, + minorVersion, patchNumber, developmentId); + EXPECT_TRUE(isNewer); + } - ExpressionParser parser; - parser.assignVector("a", a); - parser.assignVector("b", b); - parser.assignVector("c", c); + { + QString projectFileVersionString = "2017.05.1.text.13"; - QString expr = "c := a + b"; - EXPECT_TRUE(parser.evaluate(expr)); + { + int majorVersion = 2017; + int minorVersion = 5; + int patchNumber = 1; + int developmentId = 14; - EXPECT_DOUBLE_EQ(c[0], 110.0); - EXPECT_DOUBLE_EQ(c[9], 128.0); -*/ + bool isNewer = RiaProjectFileVersionTools::isProjectFileVersionNewerThan(projectFileVersionString, majorVersion, + minorVersion, patchNumber, + developmentId); EXPECT_FALSE(isNewer); + } + + { + int majorVersion = 2017; + int minorVersion = 5; + int patchNumber = 1; + int developmentId = 13; + + bool isNewer = RiaProjectFileVersionTools::isProjectFileVersionNewerThan(projectFileVersionString, majorVersion, + minorVersion, patchNumber, + developmentId); EXPECT_TRUE(isNewer); + } + + { + int majorVersion = 2017; + int minorVersion = 5; + int patchNumber = 1; + int developmentId = 12; + + bool isNewer = RiaProjectFileVersionTools::isProjectFileVersionNewerThan(projectFileVersionString, majorVersion, + minorVersion, patchNumber, + developmentId); EXPECT_TRUE(isNewer); + } + } + */ +} + +TEST(RiaProjectFileVersionTools, DecodeProjectVersionString) +{ + { + int majorVersion = -1; + int minorVersion = -1; + int patchNumber = -1; + int developmentId = -1; + + QString projectFileVersionString = "2017.05.1"; + RiaProjectFileVersionTools::decodeVersionString(projectFileVersionString, &majorVersion, &minorVersion, &patchNumber, + &developmentId); + + EXPECT_EQ(2017, majorVersion); + EXPECT_EQ(5, minorVersion); + EXPECT_EQ(1, patchNumber); + EXPECT_EQ(-1, developmentId); + } + + { + int majorVersion = -1; + int minorVersion = -1; + int patchNumber = -1; + int developmentId = -1; + + QString projectFileVersionString = ""; + RiaProjectFileVersionTools::decodeVersionString(projectFileVersionString, &majorVersion, &minorVersion, &patchNumber, + &developmentId); + + EXPECT_EQ(-1, majorVersion); + EXPECT_EQ(-1, minorVersion); + EXPECT_EQ(-1, patchNumber); + EXPECT_EQ(-1, developmentId); + } + + { + int majorVersion = -1; + int minorVersion = -1; + int patchNumber = -1; + int developmentId = -1; + + QString projectFileVersionString = "2017.05.2-dev.23"; + RiaProjectFileVersionTools::decodeVersionString(projectFileVersionString, &majorVersion, &minorVersion, &patchNumber, + &developmentId); + + EXPECT_EQ(2017, majorVersion); + EXPECT_EQ(5, minorVersion); + EXPECT_EQ(2, patchNumber); + EXPECT_EQ(23, developmentId); + } + + { + int majorVersion = -1; + int minorVersion = -1; + int patchNumber = -1; + int developmentId = -1; + + QString projectFileVersionString = "2017.05.2-dev.long.text..23"; + RiaProjectFileVersionTools::decodeVersionString(projectFileVersionString, &majorVersion, &minorVersion, &patchNumber, + &developmentId); + + EXPECT_EQ(2017, majorVersion); + EXPECT_EQ(5, minorVersion); + EXPECT_EQ(2, patchNumber); + EXPECT_EQ(23, developmentId); + } +} + +TEST(RiaProjectFileVersionTools, OrderKnownVersionStrings) +{ + QStringList versionStrings; + { + // The following list is taken from traversing history of ResInsightVersion.cmake + + versionStrings << "2017.05.2-dev.15"; + versionStrings << "2017.05.2-dev.14"; + versionStrings << "2017.05.2-dev.13"; + versionStrings << "2017.05.2-dev.12"; + versionStrings << "2017.05.2-dev.11"; + versionStrings << "2017.05.2-dev.10"; + versionStrings << "2017.05.2-dev.09"; + versionStrings << "2017.05.2-dev.08"; + versionStrings << "2017.05.2-dev.07"; + versionStrings << "2017.05.2-dev.06"; + versionStrings << "2017.05.2-dev.05"; + versionStrings << "2017.05.2-dev.04"; + versionStrings << "2017.05.2-dev.03"; + versionStrings << "2017.05.2-dev.02"; + versionStrings << "2017.05.2-fdev.02"; + versionStrings << "2017.05.2-dev.1"; + versionStrings << "2017.05.2-fdev.01"; + versionStrings << "2017.05.2"; + versionStrings << "2017.05.pre-proto.15"; + versionStrings << "2017.05.1-dev"; + versionStrings << "2017.05.1"; + versionStrings << "2017.05.0"; + + versionStrings << "2016.11.flow.14"; + versionStrings << "2016.11.flow.12"; + versionStrings << "2016.11.flow.11"; + versionStrings << "2016.11.flow.9"; + versionStrings << "2016.11.flow.8"; + versionStrings << "2016.11.flow.7"; + versionStrings << "2016.11.flow.1"; + versionStrings << "2016.11.m.1"; + versionStrings << "2016.11.0"; + + versionStrings << "1.6.10-dev"; + versionStrings << "1.6.9-dev"; + versionStrings << "1.6.8-dev"; + versionStrings << "1.6.7-gm-beta"; + versionStrings << "1.6.6-dev"; + versionStrings << "1.6.5-dev"; + versionStrings << "1.6.4-dev"; + versionStrings << "1.6.3-dev"; + versionStrings << "1.6.1-dev"; + versionStrings << "1.6.2-dev"; + versionStrings << "1.6.0-RC"; + + versionStrings << "1.5.111-RC"; + versionStrings << "1.5.110-RC"; + versionStrings << "1.5.109-RC"; + versionStrings << "1.5.108-RC"; + versionStrings << "1.5.107-RC"; + versionStrings << "1.5.106-RC"; + versionStrings << "1.5.105-RC"; + versionStrings << "1.5.104-RC"; + versionStrings << "1.5.103-dev"; + versionStrings << "1.5.102-dev"; + versionStrings << "1.5.101-dev"; + versionStrings << "1.5.100-dev"; + versionStrings << "1.5.0"; + } + + // Additional dummy test versions + versionStrings << "2016.12"; + versionStrings << "2015"; + versionStrings << "2016.10.1.sd flkj....03"; + versionStrings << "2016.10.1.sdf lkj. ...04"; + + QStringList sortedVersionList = versionStrings; + { + qSort(sortedVersionList.begin(), sortedVersionList.end(), + RiaProjectFileVersionTools::isCandidateVersionNewerThanOther); + } + + for (const auto& s : sortedVersionList) + { + // std::cout << s.toStdString() << "\n"; + } }