|
| 1 | +/* Copyright (C) 2020 IBM Corp. |
| 2 | + * This program is Licensed under the Apache License, Version 2.0 |
| 3 | + * (the "License"); you may not use this file except in compliance |
| 4 | + * with the License. You may obtain a copy of the License at |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * Unless required by applicable law or agreed to in writing, software |
| 7 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. See accompanying LICENSE file. |
| 11 | + */ |
| 12 | + |
| 13 | +#include <helib/version.h> |
| 14 | +#include <tuple> |
| 15 | +#include <string> |
| 16 | +#include <regex> |
| 17 | +#include <sstream> |
| 18 | +#include <fstream> |
| 19 | +#include "test_common.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +// Util function to grab version from cmake file. |
| 25 | +std::tuple<int, int, int, std::string> readVersionFromCmakeFile() |
| 26 | +{ |
| 27 | + // File will be in src/, we are in tests/ |
| 28 | + const std::string cmakeFilePath = "@CMAKE_SOURCE_DIR@/src/CMakeLists.txt"; |
| 29 | + |
| 30 | + // Slurp the file. It isn't a large file. |
| 31 | + std::string fileStr; |
| 32 | + |
| 33 | + { |
| 34 | + std::ifstream cmakeFile(cmakeFilePath); |
| 35 | + if (!cmakeFile.is_open()) |
| 36 | + throw std::runtime_error("Could not find '" + cmakeFilePath + "'."); |
| 37 | + |
| 38 | + std::ostringstream oss; |
| 39 | + oss << cmakeFile.rdbuf(); |
| 40 | + fileStr = oss.str(); |
| 41 | + } |
| 42 | + |
| 43 | + // Find the version. |
| 44 | + // e.g. |
| 45 | + // project(helib |
| 46 | + // VERSION x.y.z |
| 47 | + // LANGUAGES CXX) |
| 48 | + |
| 49 | + std::regex re_version( |
| 50 | + R"(project[\s\S]*?helib[\s\S]*?VERSION[\s\S]*?((\d+)\.(\d+)\.(\d)))"); |
| 51 | + std::smatch match; |
| 52 | + std::regex_search(fileStr, match, re_version); |
| 53 | + if (match.size() < 5) { |
| 54 | + std::ostringstream oss; |
| 55 | + oss << "Expected 5 matches, got " << match.size() << "."; |
| 56 | + throw std::runtime_error(oss.str()); |
| 57 | + } |
| 58 | + |
| 59 | + return std::tuple<int, int, int, std::string>(std::stoi(match[2]), |
| 60 | + std::stoi(match[3]), |
| 61 | + std::stoi(match[4]), |
| 62 | + "v" + match[1].str()); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(TestVersion, versionMatchesThatFoundInCMakelists) |
| 66 | +{ |
| 67 | + int major; |
| 68 | + int minor; |
| 69 | + int patch; |
| 70 | + std::string verStr; |
| 71 | + |
| 72 | + std::tie(major, minor, patch, verStr) = readVersionFromCmakeFile(); |
| 73 | + |
| 74 | + // EXPECT_EQ does not like these class static members. |
| 75 | + int cur_maj = helib::version::major; |
| 76 | + int cur_min = helib::version::minor; |
| 77 | + int cur_pat = helib::version::patch; |
| 78 | + |
| 79 | + EXPECT_EQ(cur_maj, major); |
| 80 | + EXPECT_EQ(cur_min, minor); |
| 81 | + EXPECT_EQ(cur_pat, patch); |
| 82 | + EXPECT_STREQ(helib::version::asString, verStr.c_str()); |
| 83 | +} |
| 84 | + |
| 85 | +TEST(TestVersion, versionGreaterThanOrEqualTo) |
| 86 | +{ |
| 87 | + EXPECT_FALSE(helib::version::greaterEquals(3)); |
| 88 | + EXPECT_FALSE(helib::version::greaterEquals(3, 1)); |
| 89 | + EXPECT_FALSE(helib::version::greaterEquals(3, 1, 4)); |
| 90 | + // Shouldn't work for negative numbers |
| 91 | + EXPECT_FALSE(helib::version::greaterEquals(1, -1, 0)); |
| 92 | + EXPECT_FALSE(helib::version::greaterEquals(0, -1, 0)); |
| 93 | + // Older version |
| 94 | + EXPECT_TRUE(helib::version::greaterEquals(1, 0, 0)); |
| 95 | + // Current version |
| 96 | + // clang-format off |
| 97 | + EXPECT_TRUE(helib::version::greaterEquals(@PROJECT_VERSION_MAJOR@, @PROJECT_VERSION_MINOR@, @PROJECT_VERSION_PATCH@)); |
| 98 | + // clang-format on |
| 99 | +} |
| 100 | + |
| 101 | +TEST(TestVersion, versionLibString) |
| 102 | +{ |
| 103 | + const char* libString = helib::version::libString(); |
| 104 | + EXPECT_STREQ(libString, "v@PROJECT_VERSION@"); |
| 105 | +} |
| 106 | + |
| 107 | +} // namespace |
0 commit comments