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