OLD | NEW |
| (Empty) |
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 | |
26 #include "wtf/StringExtras.h" | |
27 | |
28 #include "testing/gtest/include/gtest/gtest.h" | |
29 #include "wtf/text/CString.h" | |
30 #include "wtf/text/WTFString.h" | |
31 #include <limits> | |
32 | |
33 namespace WTF { | |
34 | |
35 template <typename IntegerType> | |
36 struct PrintfFormatTrait { | |
37 static const char format[]; | |
38 }; | |
39 | |
40 template <> | |
41 struct PrintfFormatTrait<short> { | |
42 static const char format[]; | |
43 }; | |
44 const char PrintfFormatTrait<short>::format[] = "%hd"; | |
45 | |
46 template <> | |
47 struct PrintfFormatTrait<int> { | |
48 static const char format[]; | |
49 }; | |
50 const char PrintfFormatTrait<int>::format[] = "%d"; | |
51 | |
52 template <> | |
53 struct PrintfFormatTrait<long> { | |
54 static const char format[]; | |
55 }; | |
56 const char PrintfFormatTrait<long>::format[] = "%ld"; | |
57 | |
58 template <> | |
59 struct PrintfFormatTrait<long long> { | |
60 static const char format[]; | |
61 }; | |
62 #if OS(WIN) | |
63 const char PrintfFormatTrait<long long>::format[] = "%I64i"; | |
64 #else | |
65 const char PrintfFormatTrait<long long>::format[] = "%lli"; | |
66 #endif // OS(WIN) | |
67 | |
68 template <> | |
69 struct PrintfFormatTrait<unsigned short> { | |
70 static const char format[]; | |
71 }; | |
72 const char PrintfFormatTrait<unsigned short>::format[] = "%hu"; | |
73 | |
74 template <> | |
75 struct PrintfFormatTrait<unsigned> { | |
76 static const char format[]; | |
77 }; | |
78 const char PrintfFormatTrait<unsigned>::format[] = "%u"; | |
79 | |
80 template <> | |
81 struct PrintfFormatTrait<unsigned long> { | |
82 static const char format[]; | |
83 }; | |
84 const char PrintfFormatTrait<unsigned long>::format[] = "%lu"; | |
85 | |
86 template <> | |
87 struct PrintfFormatTrait<unsigned long long> { | |
88 static const char format[]; | |
89 }; | |
90 #if OS(WIN) | |
91 const char PrintfFormatTrait<unsigned long long>::format[] = "%I64u"; | |
92 #else | |
93 const char PrintfFormatTrait<unsigned long long>::format[] = "%llu"; | |
94 #endif // OS(WIN) | |
95 | |
96 // FIXME: use snprintf from StringExtras.h | |
97 template <typename IntegerType> | |
98 void testBoundaries() { | |
99 const unsigned bufferSize = 256; | |
100 Vector<char, bufferSize> buffer; | |
101 buffer.resize(bufferSize); | |
102 | |
103 const IntegerType min = std::numeric_limits<IntegerType>::min(); | |
104 CString minStringData = String::number(min).latin1(); | |
105 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, | |
106 min); | |
107 EXPECT_STREQ(buffer.data(), minStringData.data()); | |
108 | |
109 const IntegerType max = std::numeric_limits<IntegerType>::max(); | |
110 CString maxStringData = String::number(max).latin1(); | |
111 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, | |
112 max); | |
113 EXPECT_STREQ(buffer.data(), maxStringData.data()); | |
114 } | |
115 | |
116 template <typename IntegerType> | |
117 void testNumbers() { | |
118 const unsigned bufferSize = 256; | |
119 Vector<char, bufferSize> buffer; | |
120 buffer.resize(bufferSize); | |
121 | |
122 for (int i = -100; i < 100; ++i) { | |
123 const IntegerType number = static_cast<IntegerType>(i); | |
124 CString numberStringData = String::number(number).latin1(); | |
125 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, | |
126 number); | |
127 EXPECT_STREQ(buffer.data(), numberStringData.data()); | |
128 } | |
129 } | |
130 | |
131 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerBoundaries) { | |
132 testBoundaries<short>(); | |
133 testBoundaries<int>(); | |
134 testBoundaries<long>(); | |
135 testBoundaries<long long>(); | |
136 } | |
137 | |
138 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerRegularNumbers) { | |
139 testNumbers<short>(); | |
140 testNumbers<int>(); | |
141 testNumbers<long>(); | |
142 testNumbers<long long>(); | |
143 } | |
144 | |
145 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerBoundaries) { | |
146 testBoundaries<unsigned short>(); | |
147 testBoundaries<unsigned>(); | |
148 testBoundaries<unsigned long>(); | |
149 testBoundaries<unsigned long long>(); | |
150 } | |
151 | |
152 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerRegularNumbers) { | |
153 testNumbers<unsigned short>(); | |
154 testNumbers<unsigned>(); | |
155 testNumbers<unsigned long>(); | |
156 testNumbers<unsigned long long>(); | |
157 } | |
158 | |
159 } // namespace WTF | |
OLD | NEW |