Skip to content

Commit

Permalink
[map] infrastructure for elevation info is added. review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsentiy Milchakov authored and mpimenov committed Mar 4, 2020
1 parent a3a5080 commit 4683f80
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 97 deletions.
1 change: 1 addition & 0 deletions base/base_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(
buffer_vector_test.cpp
cache_test.cpp
cancellable_tests.cpp
checked_cast_tests.cpp
clustering_map_tests.cpp
collection_cast_test.cpp
condition_test.cpp
Expand Down
69 changes: 69 additions & 0 deletions base/base_tests/checked_cast_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "testing/testing.hpp"

#include "base/checked_cast.hpp"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicitly-unsigned-literal"
UNIT_TEST(IsCastValid)
{
{
int8_t value = -1;
TEST(base::IsCastValid<int8_t>(value), ());
TEST(base::IsCastValid<int16_t>(value), ());
TEST(base::IsCastValid<int32_t>(value), ());
TEST(base::IsCastValid<int64_t>(value), ());

TEST(!base::IsCastValid<uint8_t>(value), ());
TEST(!base::IsCastValid<uint16_t>(value), ());
TEST(!base::IsCastValid<uint32_t>(value), ());
TEST(!base::IsCastValid<uint64_t>(value), ());
}
{
int64_t value = -1;
TEST(base::IsCastValid<int8_t>(value), ());
TEST(base::IsCastValid<int16_t>(value), ());
TEST(base::IsCastValid<int32_t>(value), ());
TEST(base::IsCastValid<int64_t>(value), ());

TEST(!base::IsCastValid<uint8_t>(value), ());
TEST(!base::IsCastValid<uint16_t>(value), ());
TEST(!base::IsCastValid<uint32_t>(value), ());
TEST(!base::IsCastValid<uint64_t>(value), ());
}
{
uint8_t value = 128;
TEST(!base::IsCastValid<int8_t>(value), ());
TEST(base::IsCastValid<int16_t>(value), ());
TEST(base::IsCastValid<int32_t>(value), ());
TEST(base::IsCastValid<int64_t>(value), ());

TEST(base::IsCastValid<uint8_t>(value), ());
TEST(base::IsCastValid<uint16_t>(value), ());
TEST(base::IsCastValid<uint32_t>(value), ());
TEST(base::IsCastValid<uint64_t>(value), ());
}
{
uint64_t value = 9223372036854775808;
TEST(!base::IsCastValid<int8_t>(value), ());
TEST(!base::IsCastValid<int16_t>(value), ());
TEST(!base::IsCastValid<int32_t>(value), ());
TEST(!base::IsCastValid<int64_t>(value), ());

TEST(!base::IsCastValid<uint8_t>(value), ());
TEST(!base::IsCastValid<uint16_t>(value), ());
TEST(!base::IsCastValid<uint32_t>(value), ());
TEST(base::IsCastValid<uint64_t>(value), ());
}
{
int64_t value = -9223372036854775808;
TEST(!base::IsCastValid<int8_t>(value), ());
TEST(!base::IsCastValid<int16_t>(value), ());
TEST(!base::IsCastValid<int32_t>(value), ());
TEST(base::IsCastValid<int64_t>(value), ());

TEST(!base::IsCastValid<uint8_t>(value), ());
TEST(!base::IsCastValid<uint16_t>(value), ());
TEST(!base::IsCastValid<uint32_t>(value), ());
TEST(!base::IsCastValid<uint64_t>(value), ());
}
}
30 changes: 25 additions & 5 deletions base/base_tests/string_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ UNIT_TEST(to_double)

UNIT_TEST(to_float)
{
float kEps = 1E-30f;
std::string s;
float f;

Expand All @@ -192,23 +193,23 @@ UNIT_TEST(to_float)

s = "0.123";
TEST(strings::to_float(s, f), ());
TEST_ALMOST_EQUAL_ULPS(0.123f, f, ());
TEST_ALMOST_EQUAL_ABS(0.123f, f, kEps, ());

s = "1.";
TEST(strings::to_float(s, f), ());
TEST_ALMOST_EQUAL_ULPS(1.0f, f, ());
TEST_ALMOST_EQUAL_ABS(1.0f, f, kEps, ());

s = "0";
TEST(strings::to_float(s, f), ());
TEST_ALMOST_EQUAL_ULPS(0.f, f, ());
TEST_ALMOST_EQUAL_ABS(0.f, f, kEps, ());

s = "5.6843418860808e-14";
TEST(strings::to_float(s, f), ());
TEST_ALMOST_EQUAL_ULPS(5.6843418860808e-14f, f, ());
TEST_ALMOST_EQUAL_ABS(5.6843418860808e-14f, f, kEps, ());

s = "-2";
TEST(strings::to_float(s, f), ());
TEST_ALMOST_EQUAL_ULPS(-2.0f, f, ());
TEST_ALMOST_EQUAL_ABS(-2.0f, f, kEps, ());

s = "labuda";
TEST(!strings::to_float(s, f), ());
Expand Down Expand Up @@ -348,9 +349,11 @@ UNIT_TEST(to_any)

s = "-128";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, -128, ());

s = "127";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 127, ());
}
{
uint8_t i;
Expand All @@ -370,9 +373,11 @@ UNIT_TEST(to_any)

s = "0";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 0, ());

s = "255";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 255, ());
}
{
int16_t i;
Expand All @@ -392,9 +397,11 @@ UNIT_TEST(to_any)

s = "-32768";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, -32768, ());

s = "32767";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 32767, ());
}
{
uint16_t i;
Expand All @@ -414,9 +421,11 @@ UNIT_TEST(to_any)

s = "0";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 0, ());

s = "65535";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 65535, ());
}
{
int32_t i;
Expand All @@ -436,9 +445,11 @@ UNIT_TEST(to_any)

s = "-2147483648";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, -2147483648, ());

s = "2147483647";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 2147483647, ());
}
{
uint32_t i;
Expand All @@ -458,9 +469,11 @@ UNIT_TEST(to_any)

s = "0";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 0, ());

s = "4294967295";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 4294967295, ());
}
{
int64_t i;
Expand All @@ -480,9 +493,14 @@ UNIT_TEST(to_any)

s = "-9223372036854775808";
TEST(strings::to_any(s, i), ());
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicitly-unsigned-literal"
TEST_EQUAL(i, -9223372036854775808LL, ());
#pragma clang diagnostic pop

s = "9223372036854775807";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 9223372036854775807, ());
}
{
uint64_t i;
Expand All @@ -502,9 +520,11 @@ UNIT_TEST(to_any)

s = "0";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 0, ());

s = "18446744073709551615";
TEST(strings::to_any(s, i), ());
TEST_EQUAL(i, 18446744073709551615ULL, ());
}
}

Expand Down
8 changes: 6 additions & 2 deletions base/checked_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ ReturnType asserted_cast(ParameterType v)
}

template <typename ResultType, typename ParameterType>
bool is_cast_valid(ParameterType v)
bool IsCastValid(ParameterType v)
{
return std::numeric_limits<ResultType>::min() <= v && std::numeric_limits<ResultType>::max() >= v;
static_assert(std::is_integral<ParameterType>::value, "ParameterType should be integral");
static_assert(std::is_integral<ResultType>::value, "ReturnType should be integral");

auto const result = static_cast<ResultType>(v);
return static_cast<ParameterType>(result) == v && ((result > 0) == (v > 0));
}
} // namespace base
53 changes: 2 additions & 51 deletions base/string_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,19 @@ namespace strings
{
namespace
{
template <typename T, typename = std::enable_if_t<std::is_signed<T>::value>>
long Int64Converter(char const * start, char ** stop, int base)
{
#ifdef OMIM_OS_WINDOWS_NATIVE
return _strtoi64(start, &stop, base);
#else
return std::strtoll(start, stop, base);
#endif
}

template <typename T, typename = std::enable_if_t<std::is_unsigned<T>::value>>
unsigned long Int64Converter(char const * start, char ** stop, int base)
{
#ifdef OMIM_OS_WINDOWS_NATIVE
return _strtoui64(start, &stop, base);
#else
return std::strtoull(start, stop, base);
#endif
}

template <typename T>
bool ToInt64Impl(char const * start, T & i, int base /*= 10*/)
{
char * stop;
errno = 0;

auto const tmp = Int64Converter<T>(start, &stop, base);

if (errno == EINVAL || errno == ERANGE || *stop != 0 || start == stop ||
!base::is_cast_valid<T>(tmp))
{
errno = 0;
return false;
}

i = tmp;
return true;
}

template <typename T>
T RealConverter(char const * start, char ** stop);

template <>
float RealConverter<float>(char const * start, char ** stop)
{
return strtof(start, stop);
return std::strtof(start, stop);
}

template <>
double RealConverter<double>(char const * start, char ** stop)
{
return strtod(start, stop);
return std::strtod(start, stop);
}

template <typename T>
Expand Down Expand Up @@ -120,16 +81,6 @@ UniChar LastUniChar(std::string const & s)
return *iter;
}

bool to_uint64(char const * start, uint64_t & i, int base /*= 10*/)
{
return ToInt64Impl(start, i, base);
}

bool to_int64(char const * start, int64_t & i)
{
return ToInt64Impl(start, i, 10);
}

bool to_size_t(char const * start, size_t & i, int base)
{
uint64_t num = 0;
Expand Down
Loading

0 comments on commit 4683f80

Please sign in to comment.