Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / StringExtrasTest.cpp
blob5c9e0b1bc05b28f28041f52bbf50d4322b636126
1 /*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
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.
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.
26 #include "config.h"
27 #include "wtf/StringExtras.h"
29 #include "wtf/text/CString.h"
30 #include "wtf/text/WTFString.h"
31 #include <gtest/gtest.h>
32 #include <limits>
34 namespace WTF {
36 template<typename IntegerType> struct PrintfFormatTrait { static const char format[]; };
38 template<> struct PrintfFormatTrait<short> { static const char format[]; };
39 const char PrintfFormatTrait<short>::format[] = "%hd";
41 template<> struct PrintfFormatTrait<int> { static const char format[]; };
42 const char PrintfFormatTrait<int>::format[] = "%d";
44 template<> struct PrintfFormatTrait<long> { static const char format[]; };
45 const char PrintfFormatTrait<long>::format[] = "%ld";
47 template<> struct PrintfFormatTrait<long long> { static const char format[]; };
48 #if OS(WIN)
49 const char PrintfFormatTrait<long long>::format[] = "%I64i";
50 #else
51 const char PrintfFormatTrait<long long>::format[] = "%lli";
52 #endif // OS(WIN)
54 template<> struct PrintfFormatTrait<unsigned short> { static const char format[]; };
55 const char PrintfFormatTrait<unsigned short>::format[] = "%hu";
57 template<> struct PrintfFormatTrait<unsigned> { static const char format[]; };
58 const char PrintfFormatTrait<unsigned>::format[] = "%u";
60 template<> struct PrintfFormatTrait<unsigned long> { static const char format[]; };
61 const char PrintfFormatTrait<unsigned long>::format[] = "%lu";
63 template<> struct PrintfFormatTrait<unsigned long long> { static const char format[]; };
64 #if OS(WIN)
65 const char PrintfFormatTrait<unsigned long long>::format[] = "%I64u";
66 #else
67 const char PrintfFormatTrait<unsigned long long>::format[] = "%llu";
68 #endif // OS(WIN)
71 // FIXME: use snprintf from StringExtras.h
72 template<typename IntegerType>
73 void testBoundaries()
75 const unsigned bufferSize = 256;
76 Vector<char, bufferSize> buffer;
77 buffer.resize(bufferSize);
79 const IntegerType min = std::numeric_limits<IntegerType>::min();
80 CString minStringData = String::number(min).latin1();
81 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, min);
82 EXPECT_STREQ(buffer.data(), minStringData.data());
84 const IntegerType max = std::numeric_limits<IntegerType>::max();
85 CString maxStringData = String::number(max).latin1();
86 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, max);
87 EXPECT_STREQ(buffer.data(), maxStringData.data());
90 template<typename IntegerType>
91 void testNumbers()
93 const unsigned bufferSize = 256;
94 Vector<char, bufferSize> buffer;
95 buffer.resize(bufferSize);
97 for (int i = -100; i < 100; ++i) {
98 const IntegerType number = static_cast<IntegerType>(i);
99 CString numberStringData = String::number(number).latin1();
100 snprintf(buffer.data(), bufferSize, PrintfFormatTrait<IntegerType>::format, number);
101 EXPECT_STREQ(buffer.data(), numberStringData.data());
105 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerBoundaries)
107 testBoundaries<short>();
108 testBoundaries<int>();
109 testBoundaries<long>();
110 testBoundaries<long long>();
113 TEST(StringExtraTest, IntegerToStringConversionSignedIntegerRegularNumbers)
115 testNumbers<short>();
116 testNumbers<int>();
117 testNumbers<long>();
118 testNumbers<long long>();
121 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerBoundaries)
123 testBoundaries<unsigned short>();
124 testBoundaries<unsigned>();
125 testBoundaries<unsigned long>();
126 testBoundaries<unsigned long long>();
129 TEST(StringExtraTest, IntegerToStringConversionUnsignedIntegerRegularNumbers)
131 testNumbers<unsigned short>();
132 testNumbers<unsigned>();
133 testNumbers<unsigned long>();
134 testNumbers<unsigned long long>();
137 } // namespace WTF