mirror of
https://github.com/red0124/ssp.git
synced 2025-01-23 04:55:20 +01:00
Fix extraction tests
This commit is contained in:
parent
b660310acf
commit
c516a6f826
@ -2,15 +2,31 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <ss/extract.hpp>
|
#include <ss/extract.hpp>
|
||||||
|
|
||||||
template <typename T>
|
namespace {
|
||||||
struct std::numeric_limits<ss::numeric_wrapper<T>>
|
|
||||||
: public std::numeric_limits<T> {};
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct std::is_signed<ss::numeric_wrapper<T>> : public std::is_signed<T> {};
|
struct numeric_limits : public std::numeric_limits<T> {};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct std::is_unsigned<ss::numeric_wrapper<T>> : public std::is_unsigned<T> {};
|
struct numeric_limits<ss::numeric_wrapper<T>> : public std::numeric_limits<T> {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_signed : public std::is_signed<T> {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct is_signed<ss::int8> : public std::true_type {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_unsigned : public std::is_unsigned<T> {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct is_unsigned<ss::uint8> : public std::true_type {};
|
||||||
|
|
||||||
|
} /* namespace */
|
||||||
|
|
||||||
|
static_assert(is_signed<ss::int8>::value);
|
||||||
|
static_assert(is_unsigned<ss::uint8>::value);
|
||||||
|
|
||||||
TEST_CASE("testing extract functions for floating point values") {
|
TEST_CASE("testing extract functions for floating point values") {
|
||||||
CHECK_FLOATING_CONVERSION(123.456, float);
|
CHECK_FLOATING_CONVERSION(123.456, float);
|
||||||
@ -38,7 +54,7 @@ TEST_CASE("testing extract functions for floating point values") {
|
|||||||
CHECK_EQ(value, type(input)); \
|
CHECK_EQ(value, type(input)); \
|
||||||
} \
|
} \
|
||||||
/* check negative too */ \
|
/* check negative too */ \
|
||||||
if (std::is_signed_v<type>) { \
|
if (is_signed<type>::value) { \
|
||||||
std::string s = std::string("-") + #input; \
|
std::string s = std::string("-") + #input; \
|
||||||
type value; \
|
type value; \
|
||||||
bool valid = ss::extract(s.c_str(), s.c_str() + s.size(), value); \
|
bool valid = ss::extract(s.c_str(), s.c_str() + s.size(), value); \
|
||||||
@ -89,7 +105,7 @@ TEST_CASE_TEMPLATE(
|
|||||||
"extract test functions for numbers with out of range inputs", T, short, us,
|
"extract test functions for numbers with out of range inputs", T, short, us,
|
||||||
int, ui, long, ul, ll, ull, ss::uint8) {
|
int, ui, long, ul, ll, ull, ss::uint8) {
|
||||||
{
|
{
|
||||||
std::string s = std::to_string(std::numeric_limits<T>::max());
|
std::string s = std::to_string(numeric_limits<T>::max());
|
||||||
auto t = ss::to_num<T>(s.c_str(), s.c_str() + s.size());
|
auto t = ss::to_num<T>(s.c_str(), s.c_str() + s.size());
|
||||||
CHECK(t.has_value());
|
CHECK(t.has_value());
|
||||||
for (auto& i : s) {
|
for (auto& i : s) {
|
||||||
@ -102,14 +118,14 @@ TEST_CASE_TEMPLATE(
|
|||||||
CHECK_FALSE(t.has_value());
|
CHECK_FALSE(t.has_value());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
std::string s = std::to_string(std::numeric_limits<T>::min());
|
std::string s = std::to_string(numeric_limits<T>::min());
|
||||||
auto t = ss::to_num<T>(s.c_str(), s.c_str() + s.size());
|
auto t = ss::to_num<T>(s.c_str(), s.c_str() + s.size());
|
||||||
CHECK(t.has_value());
|
CHECK(t.has_value());
|
||||||
for (auto& i : s) {
|
for (auto& i : s) {
|
||||||
if (std::is_signed_v<T> && i != '9' && i != '.') {
|
if (is_signed<T>::value && i != '9' && i != '.') {
|
||||||
i = '9';
|
i = '9';
|
||||||
break;
|
break;
|
||||||
} else if (std::is_unsigned_v<T>) {
|
} else if (is_unsigned<T>::value) {
|
||||||
s = "-1";
|
s = "-1";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user