00001 #define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
00002 #include "util/integer_to_string.hh"
00003 #include "util/string_piece.hh"
00004
00005 #define BOOST_TEST_MODULE IntegerToStringTest
00006 #include <boost/test/unit_test.hpp>
00007 #include <boost/lexical_cast.hpp>
00008
00009 #include <limits>
00010
00011 namespace util {
00012 namespace {
00013
00014 template <class T> void TestValue(const T value) {
00015 char buf[ToStringBuf<T>::kBytes];
00016 StringPiece result(buf, ToString(value, buf) - buf);
00017 BOOST_REQUIRE_GE(static_cast<std::size_t>(ToStringBuf<T>::kBytes), result.size());
00018 if (value) {
00019 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), result);
00020 } else {
00021
00022 BOOST_CHECK(result == "0x0" || result == "0");
00023 }
00024 }
00025
00026 template <class T> void TestCorners() {
00027 TestValue(std::numeric_limits<T>::min());
00028 TestValue(std::numeric_limits<T>::max());
00029 TestValue((T)0);
00030 TestValue((T)-1);
00031 TestValue((T)1);
00032 }
00033
00034 BOOST_AUTO_TEST_CASE(Corners) {
00035 TestCorners<uint16_t>();
00036 TestCorners<uint32_t>();
00037 TestCorners<uint64_t>();
00038 TestCorners<int16_t>();
00039 TestCorners<int32_t>();
00040 TestCorners<int64_t>();
00041 TestCorners<const void*>();
00042 }
00043
00044 template <class T> void TestAll() {
00045 for (T i = std::numeric_limits<T>::min(); i < std::numeric_limits<T>::max(); ++i) {
00046 TestValue(i);
00047 }
00048 TestValue(std::numeric_limits<T>::max());
00049 }
00050
00051 BOOST_AUTO_TEST_CASE(Short) {
00052 TestAll<uint16_t>();
00053 TestAll<int16_t>();
00054 }
00055
00056 template <class T> void Test10s() {
00057 for (T i = 1; i < std::numeric_limits<T>::max() / 10; i *= 10) {
00058 TestValue(i);
00059 TestValue(i - 1);
00060 TestValue(i + 1);
00061 }
00062 }
00063
00064 BOOST_AUTO_TEST_CASE(Tens) {
00065 Test10s<uint64_t>();
00066 Test10s<int64_t>();
00067 Test10s<uint32_t>();
00068 Test10s<int32_t>();
00069 }
00070
00071 BOOST_AUTO_TEST_CASE(Pointers) {
00072 for (uintptr_t i = 1; i < std::numeric_limits<uintptr_t>::max() / 10; i *= 10) {
00073 TestValue((const void*)i);
00074 }
00075 for (uintptr_t i = 0; i < 256; ++i) {
00076 TestValue((const void*)i);
00077 TestValue((const void*)(i + 0xf00));
00078 }
00079 }
00080
00081 }}