ClickHouse/src/Common/VersionNumber.cpp

62 lines
1.2 KiB
C++
Raw Normal View History

#include <Common/VersionNumber.h>
#include <Common/Exception.h>
#include <cstdlib>
#include <iostream>
namespace DB
{
namespace ErrorCodes
{
Use LOGICAL_ERROR instead of introducing BAD_VERSION But note, that tests with EXPECT_EXIT() does not work with --gtest_filter: a) only this test is included into unit_tests_dbms $ src/unit_tests_dbms --gtest_filter=VersionNumber* Running main() from ../contrib/googletest/googletest/src/gtest_main.cc Note: Google Test filter = VersionNumber* [==========] Running 2 tests from 1 test suite. [----------] Global test environment set-up. [----------] 2 tests from VersionNumber [ RUN ] VersionNumber.VersionNumber [ OK ] VersionNumber.VersionNumber (0 ms) [ RUN ] VersionNumber.fromString Segmentation fault (core dumped) (gdb) bt 0 0x00007fffda323d22 in raise () from /usr/lib/libc.so.6 1 0x00007fffda30d862 in abort () from /usr/lib/libc.so.6 2 0x00007fffdbe686f5 in DB::handle_error_code (msg=..., code=49, remote=false, trace=...) at Exception.cpp:49 3 0x00007fffdbe68846 in DB::Exception::Exception (this=0x7fffd5c5a240, msg=..., code=49, remote_=false) at Exception.cpp:60 4 0x00007fffe26b2cb3 in DB::Exception::Exception<unsigned long> (this=0x7fffd5c5a240, code=49, fmt=..., args=@0x7fffffffc7e8: 4) at Exception.h:40 5 0x00007fffdbf4d201 in DB::VersionNumber::VersionNumber (this=0x7fffffffcc20, vec=...) at VersionNumber.cpp:17 6 0x00007fffdbf4d650 in DB::VersionNumber::fromString (version=..., strict=true) at VersionNumber.cpp:57 7 0x00005555555db53d in VersionNumber_fromString_Test::TestBody (this=0x7fffd5c12330) at gtest_version_number.cpp:24 b) regular build $ src/unit_tests_dbms --gtest_filter=VersionNumber* Running main() from ../contrib/googletest/googletest/src/gtest_main.cc Note: Google Test filter = VersionNumber* [==========] Running 2 tests from 1 test suite. [----------] Global test environment set-up. [----------] 2 tests from VersionNumber [ RUN ] VersionNumber.VersionNumber [ OK ] VersionNumber.VersionNumber (0 ms) [ RUN ] VersionNumber.fromString [ OK ] VersionNumber.fromString (495 ms) [----------] 2 tests from VersionNumber (495 ms total) [----------] Global test environment tear-down Segmentation fault (core dumped) (gdb) bt 0 testing::TestInfo::should_run (this=0xe0) at gtest.h:760 1 0x00007ffff7f6edd5 in testing::TestSuite::ShouldRunTest (test_info=0xe0) at gtest.h:1009 2 0x00007ffff7f6ebb2 in testing::internal::CountIf<std::__1::vector<testing::TestInfo*, std::__1::allocator<testing::TestInfo*> >, bool (*)(testing::TestInfo const*)> (c=..., predicate=0x7ffff7f6edc0 <testing::TestSuite::ShouldRunTest(testing::TestInfo const*)>) at gtest-internal-inl.h:294 3 0x00007ffff7f41ae5 in testing::TestSuite::test_to_run_count (this=0x7fffd5028500) at gtest.cc:2919 4 0x00007ffff7f41719 in testing::internal::SumOverTestSuiteList (case_list=..., method=(int (testing::TestSuite::*)(const testing::TestSuite * const)) 0x7ffff7f41ac0 <testing::TestSuite::test_to_run_count() const>) at gtest.cc:377 5 0x00007ffff7f41ab9 in testing::internal::UnitTestImpl::test_to_run_count (this=0x7fffd5d5c000) at gtest.cc:1012 6 0x00007ffff7f4b59d in testing::UnitTest::test_to_run_count (this=0x7ffff7fbcb80 <testing::UnitTest::GetInstance()::instance>) at gtest.cc:5059 7 0x00007ffff7f4c4ed in testing::internal::PrettyUnitTestResultPrinter::OnTestIterationEnd (this=0x7fffd5ca4a68, unit_test=...) at gtest.cc:3589 8 0x00007ffff7f4daf9 in testing::internal::TestEventRepeater::OnTestIterationEnd (this=0x7fffd5c83770, unit_test=..., iteration=0) at gtest.cc:3852 9 0x00007ffff7f57570 in testing::internal::UnitTestImpl::RunAllTests (this=0x7fffd5d5c000) at gtest.cc:5737 10 0x00007ffff7fa3a84 in testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool> (object=0x7fffd5d5c000, method=(bool (testing::internal::UnitTestImpl::*)(testing::internal::UnitTestImpl * const)) 0x7ffff7f56f20 <testing::internal::UnitTestImpl::RunAllTests()>, location=0x7ffff7f11d9b "auxiliary test code (environments or event listeners)") at gtest.cc:2589 11 0x00007ffff7f71788 in testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool> (object=0x7fffd5d5c000, method=(bool (testing::internal::UnitTestImpl::*)(testing::internal::UnitTestImpl * const)) 0x7ffff7f56f20 <testing::internal::UnitTestImpl::RunAllTests()>, location=0x7ffff7f11d9b "auxiliary test code (environments or event listeners)") at gtest.cc:2625 12 0x00007ffff7f56ea3 in testing::UnitTest::Run (this=0x7ffff7fbcb80 <testing::UnitTest::GetInstance()::instance>) at gtest.cc:5291
2021-06-08 07:58:10 +00:00
extern const int LOGICAL_ERROR;
}
VersionNumber::VersionNumber(std::string version_string, bool strict)
{
if (version_string.empty())
return;
std::vector<long> comp;
char * start = &version_string.front();
char * end = start;
const char * eos = &version_string.back() + 1;
do
{
long value = strtol(start, &end, 10);
comp.push_back(value);
start = end + 1;
}
while (start < eos && (end < eos && *end == '.'));
if (!strict && comp.size() > SIZE)
{
comp.resize(SIZE);
}
*this = comp;
}
VersionNumber::VersionNumber(const std::vector<long> & vec)
{
if (vec.size() > SIZE)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Too much components ({})", vec.size());
if (vec.size() > 0)
std::get<0>(version) = vec[0];
if (vec.size() > 1)
std::get<1>(version) = vec[1];
if (vec.size() > 2)
std::get<2>(version) = vec[2];
}
std::string VersionNumber::toString() const
{
return fmt::format("{}.{}.{}",
std::get<0>(version), std::get<1>(version), std::get<2>(version));
}
}