diff options
author | Elliott Hughes <enh@google.com> | 2017-12-19 10:27:27 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2017-12-19 13:55:54 -0800 |
commit | 1921dce886c8ea17fb7958b59a18a71b6dc7ff96 (patch) | |
tree | 7e28abec7cf31f9e06ebe79472e35a8b12af5ed2 /tests/stdlib_test.cpp | |
parent | 721a5305e24b69b68ecc9431526f32131abc8f6c (diff) |
Refactor the ato* and strto* family.
There are no meaningful changes here, just a minimal conversion to two
C++ templates to make further changes easier.
Bug: N/A
Test: ran tests, benchmarks
Change-Id: I958fbf17a85f19dd8f17bfb4bbb9314d220daa3b
Diffstat (limited to 'tests/stdlib_test.cpp')
-rw-r--r-- | tests/stdlib_test.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp index 1a3fc03d6..e429de6a9 100644 --- a/tests/stdlib_test.cpp +++ b/tests/stdlib_test.cpp @@ -32,6 +32,9 @@ #include <sys/types.h> #include <sys/wait.h> +#include <limits> +#include <sstream> + // The random number generator tests all set the seed, get four values, reset the seed and check // that they get the first two values repeated, and then reset the seed and check two more values // to rule out the possibility that we're just going round a cycle of four values. @@ -672,6 +675,26 @@ static void CheckStrToInt(T fn(const char* s, char** end, int base)) { // If we see "0x" *not* followed by a hex digit, we shouldn't swallow the 'x'. ASSERT_EQ(T(0), fn("0xy", &end_p, 16)); ASSERT_EQ('x', *end_p); + + if (std::numeric_limits<T>::is_signed) { + // Minimum (such as -128). + std::string min{(std::stringstream{} << std::numeric_limits<T>::min()).str()}; + ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); + // Too negative (such as -129). + min.back() = (min.back() + 1); + errno = 0; + ASSERT_EQ(std::numeric_limits<T>::min(), fn(min.c_str(), &end_p, 0)); + ASSERT_EQ(ERANGE, errno); + } + + // Maximum (such as 127). + std::string max{(std::stringstream{} << std::numeric_limits<T>::max()).str()}; + ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); + // Too positive (such as 128). + max.back() = (max.back() + 1); + errno = 0; + ASSERT_EQ(std::numeric_limits<T>::max(), fn(max.c_str(), &end_p, 0)); + ASSERT_EQ(ERANGE, errno); } TEST(stdlib, strtol_smoke) { |