Add tests for to_integer(), fix to_integer()

This commit is contained in:
Neil MacIntosh 2016-09-20 08:47:49 -07:00 committed by GitHub
commit fd5ad87bf2
2 changed files with 19 additions and 1 deletions

View File

@ -103,7 +103,7 @@ inline constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsi
template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>> template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
inline constexpr IntegerType to_integer(byte b) noexcept inline constexpr IntegerType to_integer(byte b) noexcept
{ {
return {b}; return static_cast<IntegerType>(b);
} }
template<bool E, typename T> template<bool E, typename T>

View File

@ -96,6 +96,24 @@ SUITE(byte_tests)
a >>= 4; a >>= 4;
CHECK(a == to_byte<0x0F>()); CHECK(a == to_byte<0x0F>());
} }
TEST(to_integer)
{
byte b = to_byte<0x12>();
CHECK(0x12 == gsl::to_integer<char>(b));
CHECK(0x12 == gsl::to_integer<short>(b));
CHECK(0x12 == gsl::to_integer<long>(b));
CHECK(0x12 == gsl::to_integer<long long>(b));
CHECK(0x12 == gsl::to_integer<unsigned char>(b));
CHECK(0x12 == gsl::to_integer<unsigned short>(b));
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
}
} }
} }