#define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE #include "integer_to_string.hh" #include "string_piece.hh" #define BOOST_TEST_MODULE IntegerToStringTest #include #include #include namespace util { namespace { template void TestValue(const T value) { char buf[ToStringBuf::kBytes]; StringPiece result(buf, ToString(value, buf) - buf); BOOST_REQUIRE_GE(static_cast(ToStringBuf::kBytes), result.size()); if (value) { BOOST_CHECK_EQUAL(boost::lexical_cast(value), result); } else { // Platforms can do void * as 0x0 or 0. BOOST_CHECK(result == "0x0" || result == "0"); } } template void TestCorners() { TestValue(std::numeric_limits::min()); TestValue(std::numeric_limits::max()); TestValue((T)0); TestValue((T)-1); TestValue((T)1); } BOOST_AUTO_TEST_CASE(Corners) { TestCorners(); TestCorners(); TestCorners(); TestCorners(); TestCorners(); TestCorners(); TestCorners(); } template void TestAll() { for (T i = std::numeric_limits::min(); i < std::numeric_limits::max(); ++i) { TestValue(i); } TestValue(std::numeric_limits::max()); } BOOST_AUTO_TEST_CASE(Short) { TestAll(); TestAll(); } template void Test10s() { for (T i = 1; i < std::numeric_limits::max() / 10; i *= 10) { TestValue(i); TestValue(i - 1); TestValue(i + 1); } } BOOST_AUTO_TEST_CASE(Tens) { Test10s(); Test10s(); Test10s(); Test10s(); } BOOST_AUTO_TEST_CASE(Pointers) { for (uintptr_t i = 1; i < std::numeric_limits::max() / 10; i *= 10) { TestValue((const void*)i); } for (uintptr_t i = 0; i < 256; ++i) { TestValue((const void*)i); TestValue((const void*)(i + 0xf00)); } } }} // namespaces