| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 #include "wtf/StringExtras.h" | 26 #include "wtf/StringExtras.h" |
| 27 | 27 |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 29 #include "wtf/text/CString.h" | 29 #include "wtf/text/CString.h" |
| 30 #include "wtf/text/WTFString.h" | 30 #include "wtf/text/WTFString.h" |
| 31 #include <limits> | 31 #include <limits> |
| 32 | 32 |
| 33 namespace WTF { | 33 namespace WTF { |
| 34 | 34 |
| 35 template<typename IntegerType> struct PrintfFormatTrait { static const char form
at[]; }; | 35 template <typename IntegerType> |
| 36 struct PrintfFormatTrait { |
| 37 static const char format[]; |
| 38 }; |
| 36 | 39 |
| 37 template<> struct PrintfFormatTrait<short> { static const char format[]; }; | 40 template <> |
| 41 struct PrintfFormatTrait<short> { |
| 42 static const char format[]; |
| 43 }; |
| 38 const char PrintfFormatTrait<short>::format[] = "%hd"; | 44 const char PrintfFormatTrait<short>::format[] = "%hd"; |
| 39 | 45 |
| 40 template<> struct PrintfFormatTrait<int> { static const char format[]; }; | 46 template <> |
| 47 struct PrintfFormatTrait<int> { |
| 48 static const char format[]; |
| 49 }; |
| 41 const char PrintfFormatTrait<int>::format[] = "%d"; | 50 const char PrintfFormatTrait<int>::format[] = "%d"; |
| 42 | 51 |
| 43 template<> struct PrintfFormatTrait<long> { static const char format[]; }; | 52 template <> |
| 53 struct PrintfFormatTrait<long> { |
| 54 static const char format[]; |
| 55 }; |
| 44 const char PrintfFormatTrait<long>::format[] = "%ld"; | 56 const char PrintfFormatTrait<long>::format[] = "%ld"; |
| 45 | 57 |
| 46 template<> struct PrintfFormatTrait<long long> { static const char format[]; }; | 58 template <> |
| 59 struct PrintfFormatTrait<long long> { |
| 60 static const char format[]; |
| 61 }; |
| 47 #if OS(WIN) | 62 #if OS(WIN) |
| 48 const char PrintfFormatTrait<long long>::format[] = "%I64i"; | 63 const char PrintfFormatTrait<long long>::format[] = "%I64i"; |
| 49 #else | 64 #else |
| 50 const char PrintfFormatTrait<long long>::format[] = "%lli"; | 65 const char PrintfFormatTrait<long long>::format[] = "%lli"; |
| 51 #endif // OS(WIN) | 66 #endif // OS(WIN) |
| 52 | 67 |
| 53 template<> struct PrintfFormatTrait<unsigned short> { static const char format[]
; }; | 68 template <> |
| 69 struct PrintfFormatTrait<unsigned short> { |
| 70 static const char format[]; |
| 71 }; |
| 54 const char PrintfFormatTrait<unsigned short>::format[] = "%hu"; | 72 const char PrintfFormatTrait<unsigned short>::format[] = "%hu"; |
| 55 | 73 |
| 56 template<> struct PrintfFormatTrait<unsigned> { static const char format[]; }; | 74 template <> |
| 75 struct PrintfFormatTrait<unsigned> { |
| 76 static const char format[]; |
| 77 }; |
| 57 const char PrintfFormatTrait<unsigned>::format[] = "%u"; | 78 const char PrintfFormatTrait<unsigned>::format[] = "%u"; |
| 58 | 79 |
| 59 template<> struct PrintfFormatTrait<unsigned long> { static const char format[];
}; | 80 template <> |
| 81 struct PrintfFormatTrait<unsigned long> { |
| 82 static const char format[]; |
| 83 }; |
| 60 const char PrintfFormatTrait<unsigned long>::format[] = "%lu"; | 84 const char PrintfFormatTrait<unsigned long>::format[] = "%lu"; |
| 61 | 85 |
| 62 template<> struct PrintfFormatTrait<unsigned long long> { static const char form
at[]; }; | 86 template <> |
| 87 struct PrintfFormatTrait<unsigned long long> { |
| 88 static const char format[]; |
| 89 }; |
| 63 #if OS(WIN) | 90 #if OS(WIN) |
| 64 const char PrintfFormatTrait<unsigned long long>::format[] = "%I64u"; | 91 const char PrintfFormatTrait<unsigned long long>::format[] = "%I64u"; |
| 65 #else | 92 #else |
| 66 const char PrintfFormatTrait<unsigned long long>::format[] = "%llu"; | 93 const char PrintfFormatTrait<unsigned long long>::format[] = "%llu"; |
| 67 #endif // OS(WIN) | 94 #endif // OS(WIN) |
| 68 | |
| 69 | 95 |
| 70 // FIXME: use snprintf from StringExtras.h | 96 // FIXME: use snprintf from StringExtras.h |
| 71 template<typename IntegerType> | 97 template <typename IntegerType> |
| 72 void testBoundaries() | 98 void testBoundaries() { |
| 73 { | 99 const unsigned bufferSize = 256; |
| 74 const unsigned bufferSize = 256; | 100 Vector<char, bufferSize> buffer; |
| 75 Vector<char, bufferSize> buffer; | 101 buffer.resize(bufferSize); |
| 76 buffer.resize(bufferSize); | |
| 77 | 102 |
| 78 const IntegerType min = std::numeric_limits<IntegerType>::min(); | 103 const IntegerType min = std::numeric_limits<IntegerType>::min(); |
| 79 CString minStringData = String::number(min).latin1(); | 104 CString minStringData = String::number(min).latin1(); |
| 80 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format,
min); | 105 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, |
| 81 EXPECT_STREQ(buffer.data(), minStringData.data()); | 106 min); |
| 107 EXPECT_STREQ(buffer.data(), minStringData.data()); |
| 82 | 108 |
| 83 const IntegerType max = std::numeric_limits<IntegerType>::max(); | 109 const IntegerType max = std::numeric_limits<IntegerType>::max(); |
| 84 CString maxStringData = String::number(max).latin1(); | 110 CString maxStringData = String::number(max).latin1(); |
| 85 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format,
max); | 111 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, |
| 86 EXPECT_STREQ(buffer.data(), maxStringData.data()); | 112 max); |
| 113 EXPECT_STREQ(buffer.data(), maxStringData.data()); |
| 87 } | 114 } |
| 88 | 115 |
| 89 template<typename IntegerType> | 116 template <typename IntegerType> |
| 90 void testNumbers() | 117 void testNumbers() { |
| 91 { | 118 const unsigned bufferSize = 256; |
| 92 const unsigned bufferSize = 256; | 119 Vector<char, bufferSize> buffer; |
| 93 Vector<char, bufferSize> buffer; | 120 buffer.resize(bufferSize); |
| 94 buffer.resize(bufferSize); | |
| 95 | 121 |
| 96 for (int i = -100; i < 100; ++i) { | 122 for (int i = -100; i < 100; ++i) { |
| 97 const IntegerType number = static_cast<IntegerType>(i); | 123 const IntegerType number = static_cast<IntegerType>(i); |
| 98 CString numberStringData = String::number(number).latin1(); | 124 CString numberStringData = String::number(number).latin1(); |
| 99 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::form
at, number); | 125 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, |
| 100 EXPECT_STREQ(buffer.data(), numberStringData.data()); | 126 number); |
| 101 } | 127 EXPECT_STREQ(buffer.data(), numberStringData.data()); |
| 128 } |
| 102 } | 129 } |
| 103 | 130 |
| 104 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerBoundaries) | 131 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerBoundaries) { |
| 105 { | 132 testBoundaries<short>(); |
| 106 testBoundaries<short>(); | 133 testBoundaries<int>(); |
| 107 testBoundaries<int>(); | 134 testBoundaries<long>(); |
| 108 testBoundaries<long>(); | 135 testBoundaries<long long>(); |
| 109 testBoundaries<long long>(); | |
| 110 } | 136 } |
| 111 | 137 |
| 112 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerRegularNumbers) | 138 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerRegularNumbers) { |
| 113 { | 139 testNumbers<short>(); |
| 114 testNumbers<short>(); | 140 testNumbers<int>(); |
| 115 testNumbers<int>(); | 141 testNumbers<long>(); |
| 116 testNumbers<long>(); | 142 testNumbers<long long>(); |
| 117 testNumbers<long long>(); | |
| 118 } | 143 } |
| 119 | 144 |
| 120 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerBoundaries) | 145 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerBoundaries) { |
| 121 { | 146 testBoundaries<unsigned short>(); |
| 122 testBoundaries<unsigned short>(); | 147 testBoundaries<unsigned>(); |
| 123 testBoundaries<unsigned>(); | 148 testBoundaries<unsigned long>(); |
| 124 testBoundaries<unsigned long>(); | 149 testBoundaries<unsigned long long>(); |
| 125 testBoundaries<unsigned long long>(); | |
| 126 } | 150 } |
| 127 | 151 |
| 128 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerRegularNumbers) | 152 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerRegularNumbers) { |
| 129 { | 153 testNumbers<unsigned short>(); |
| 130 testNumbers<unsigned short>(); | 154 testNumbers<unsigned>(); |
| 131 testNumbers<unsigned>(); | 155 testNumbers<unsigned long>(); |
| 132 testNumbers<unsigned long>(); | 156 testNumbers<unsigned long long>(); |
| 133 testNumbers<unsigned long long>(); | |
| 134 } | 157 } |
| 135 | 158 |
| 136 } // namespace WTF | 159 } // namespace WTF |
| OLD | NEW |