IntegerToStringConversion.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include <limits>
  27. #include <wtf/StringExtras.h>
  28. #include <wtf/text/CString.h>
  29. #include <wtf/text/WTFString.h>
  30. template<typename IntegerType> struct PrintfFormatTrait { static const char format[]; };
  31. template<> struct PrintfFormatTrait<short> { static const char format[]; };
  32. const char PrintfFormatTrait<short>::format[] = "%hd";
  33. template<> struct PrintfFormatTrait<int> { static const char format[]; };
  34. const char PrintfFormatTrait<int>::format[] = "%d";
  35. template<> struct PrintfFormatTrait<long> { static const char format[]; };
  36. const char PrintfFormatTrait<long>::format[] = "%ld";
  37. template<> struct PrintfFormatTrait<long long> { static const char format[]; };
  38. #if OS(WINDOWS) && !PLATFORM(QT)
  39. const char PrintfFormatTrait<long long>::format[] = "%I64i";
  40. #else
  41. const char PrintfFormatTrait<long long>::format[] = "%lli";
  42. #endif // OS(WINDOWS) && !PLATFORM(QT)
  43. template<> struct PrintfFormatTrait<unsigned short> { static const char format[]; };
  44. const char PrintfFormatTrait<unsigned short>::format[] = "%hu";
  45. template<> struct PrintfFormatTrait<unsigned> { static const char format[]; };
  46. const char PrintfFormatTrait<unsigned>::format[] = "%u";
  47. template<> struct PrintfFormatTrait<unsigned long> { static const char format[]; };
  48. const char PrintfFormatTrait<unsigned long>::format[] = "%lu";
  49. template<> struct PrintfFormatTrait<unsigned long long> { static const char format[]; };
  50. #if OS(WINDOWS) && !PLATFORM(QT)
  51. const char PrintfFormatTrait<unsigned long long>::format[] = "%I64u";
  52. #else
  53. const char PrintfFormatTrait<unsigned long long>::format[] = "%llu";
  54. #endif // OS(WINDOWS) && !PLATFORM(QT)
  55. // FIXME: use snprintf from StringExtras.h
  56. template<typename IntegerType>
  57. void testBoundaries()
  58. {
  59. const unsigned bufferSize = 256;
  60. Vector<char, bufferSize> buffer;
  61. buffer.resize(bufferSize);
  62. const IntegerType min = std::numeric_limits<IntegerType>::min();
  63. CString minStringData = String::number(min).latin1();
  64. snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, min);
  65. ASSERT_STREQ(buffer.data(), minStringData.data());
  66. const IntegerType max = std::numeric_limits<IntegerType>::max();
  67. CString maxStringData = String::number(max).latin1();
  68. snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, max);
  69. ASSERT_STREQ(buffer.data(), maxStringData.data());
  70. }
  71. template<typename IntegerType>
  72. void testNumbers()
  73. {
  74. const unsigned bufferSize = 256;
  75. Vector<char, bufferSize> buffer;
  76. buffer.resize(bufferSize);
  77. for (int i = -100; i < 100; ++i) {
  78. const IntegerType number = static_cast<IntegerType>(i);
  79. CString numberStringData = String::number(number).latin1();
  80. snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, number);
  81. ASSERT_STREQ(buffer.data(), numberStringData.data());
  82. }
  83. }
  84. TEST(WTF, IntegerToStringConversionSignedIntegerBoundaries)
  85. {
  86. testBoundaries<short>();
  87. testBoundaries<int>();
  88. testBoundaries<long>();
  89. testBoundaries<long long>();
  90. }
  91. TEST(WTF, IntegerToStringConversionSignedIntegerRegularNumbers)
  92. {
  93. testNumbers<short>();
  94. testNumbers<int>();
  95. testNumbers<long>();
  96. testNumbers<long long>();
  97. }
  98. TEST(WTF, IntegerToStringConversionUnsignedIntegerBoundaries)
  99. {
  100. testBoundaries<unsigned short>();
  101. testBoundaries<unsigned int>();
  102. testBoundaries<unsigned long>();
  103. testBoundaries<unsigned long long>();
  104. }
  105. TEST(WTF, IntegerToStringConversionUnsignedIntegerRegularNumbers)
  106. {
  107. testNumbers<unsigned short>();
  108. testNumbers<unsigned int>();
  109. testNumbers<unsigned long>();
  110. testNumbers<unsigned long long>();
  111. }