Alien-libpanda
view release on metacpan or search on metacpan
t/from_chars.cc view on Meta::CPAN
using panda::string;
struct Exc : std::exception {};
template <typename T> struct nstr;
template <> struct nstr<int8_t> {
static string min () { return "-128"; }
static string max () { return "127"; }
static string mmin () { return "-129"; }
static string mmax () { return "128"; }
};
template <> struct nstr<uint8_t> {
static string min () { return "0"; }
static string max () { return "255"; }
static string mmin () { return ""; }
static string mmax () { return "256"; }
};
template <> struct nstr<int16_t> {
static string min () { return "-32768"; }
static string max () { return "32767"; }
static string mmin () { return "-32769"; }
static string mmax () { return "32768"; }
};
template <> struct nstr<uint16_t> {
static string min () { return "0"; }
static string max () { return "65535"; }
static string mmin () { return ""; }
static string mmax () { return "65536"; }
};
template <> struct nstr<int32_t> {
static string min () { return "-2147483648"; }
static string max () { return "2147483647"; }
static string mmin () { return "-2147483649"; }
static string mmax () { return "2147483648"; }
};
template <> struct nstr<uint32_t> {
static string min () { return "0"; }
static string max () { return "4294967295"; }
static string mmin () { return ""; }
static string mmax () { return "4294967296"; }
};
template <> struct nstr<int64_t> {
static string min () { return "-9223372036854775808"; }
static string max () { return "9223372036854775807"; }
static string mmin () { return "-9223372036854775809"; }
static string mmax () { return "9223372036854775808"; }
};
template <> struct nstr<uint64_t> {
static string min () { return "0"; }
static string max () { return "18446744073709551615"; }
static string mmin () { return ""; }
static string mmax () { return "18446744073709551616"; }
};
template<typename Int>
Int fci (string str, unsigned& pos, int base = 10) {
Int val;
auto res = panda::from_chars(str.data() + pos, str.data() + str.size() - pos, val, base);
pos = res.ptr - str.data();
if (res.ec) throw Exc();
return val;
}
template <typename Int, bool is_signed = std::numeric_limits<Int>::is_signed>
struct test_sign_dependent;
template <typename Int>
struct test_sign_dependent<Int, true> {
static void run () {
unsigned pos = 0;
SECTION("negative number") {
REQUIRE(fci<Int>("-65", pos) == -65);
REQUIRE(pos == 3);
}
SECTION("under min") {
REQUIRE_THROWS_AS(fci<Int>(nstr<Int>::mmin(), pos), Exc);
REQUIRE(pos == nstr<Int>::mmin().size());
}
SECTION("-dohuya") {
REQUIRE_THROWS_AS(fci<Int>(" -123456789012345678901234567890123456789012345678901234567890", pos), Exc);
REQUIRE(pos == 65);
}
SECTION("negative 8-base") {
REQUIRE(fci<Int>(" -0012", pos, 8) == -10);
REQUIRE(pos == 6);
}
SECTION("negative 16-base") {
REQUIRE(fci<Int>(" -0Dqwe", pos, 16) == -13);
REQUIRE(pos == 5);
}
}
};
template <typename Int>
struct test_sign_dependent<Int, false> {
static void run () {
unsigned pos = 0;
SECTION("negative number") {
REQUIRE_THROWS_AS(fci<Int>("-65", pos), Exc);
REQUIRE(pos == 0);
}
}
};
template <typename Int>
void from_chars_test() {
unsigned pos = 0;
SECTION("just number") {
REQUIRE(fci<Int>("12", pos) == 12);
REQUIRE(pos == 2);
}
SECTION("+ sign not supported") {
( run in 0.523 second using v1.01-cache-2.11-cpan-d7f47b0818f )