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 "wtf/MathExtras.h" 29 #include "wtf/text/CString.h" 30 #include "wtf/text/WTFString.h" 31 #include <gtest/gtest.h> 32 #include <limits> 33 34 namespace { 35 36 TEST(WTF, StringCreationFromLiteral) 37 { 38 String stringFromLiteral("Explicit construction syntax"); 39 ASSERT_EQ(strlen("Explicit construction syntax"), stringFromLiteral.length()); 40 ASSERT_TRUE(stringFromLiteral == "Explicit construction syntax"); 41 ASSERT_TRUE(stringFromLiteral.is8Bit()); 42 ASSERT_TRUE(String("Explicit construction syntax") == stringFromLiteral); 43 } 44 45 TEST(WTF, StringASCII) 46 { 47 CString output; 48 49 // Null String. 50 output = String().ascii(); 51 ASSERT_STREQ("", output.data()); 52 53 // Empty String. 54 output = emptyString().ascii(); 55 ASSERT_STREQ("", output.data()); 56 57 // Regular String. 58 output = String("foobar").ascii(); 59 ASSERT_STREQ("foobar", output.data()); 60 } 61 62 static void testNumberToStringECMAScript(double number, const char* reference) 63 { 64 CString numberString = String::numberToStringECMAScript(number).latin1(); 65 ASSERT_STREQ(reference, numberString.data()); 66 } 67 68 TEST(WTF, StringNumberToStringECMAScriptBoundaries) 69 { 70 typedef std::numeric_limits<double> Limits; 71 72 // Infinity. 73 testNumberToStringECMAScript(Limits::infinity(), "Infinity"); 74 testNumberToStringECMAScript(-Limits::infinity(), "-Infinity"); 75 76 // NaN. 77 testNumberToStringECMAScript(-Limits::quiet_NaN(), "NaN"); 78 79 // Zeros. 80 testNumberToStringECMAScript(0, "0"); 81 testNumberToStringECMAScript(-0, "0"); 82 83 // Min-Max. 84 testNumberToStringECMAScript(Limits::min(), "2.2250738585072014e-308"); 85 testNumberToStringECMAScript(Limits::max(), "1.7976931348623157e+308"); 86 } 87 88 TEST(WTF, StringNumberToStringECMAScriptRegularNumbers) 89 { 90 // Pi. 91 testNumberToStringECMAScript(piDouble, "3.141592653589793"); 92 testNumberToStringECMAScript(piFloat, "3.1415927410125732"); 93 testNumberToStringECMAScript(piOverTwoDouble, "1.5707963267948966"); 94 testNumberToStringECMAScript(piOverTwoFloat, "1.5707963705062866"); 95 testNumberToStringECMAScript(piOverFourDouble, "0.7853981633974483"); 96 testNumberToStringECMAScript(piOverFourFloat, "0.7853981852531433"); 97 98 // e. 99 const double e = 2.71828182845904523536028747135266249775724709369995; 100 testNumberToStringECMAScript(e, "2.718281828459045"); 101 102 // c, speed of light in m/s. 103 const double c = 299792458; 104 testNumberToStringECMAScript(c, "299792458"); 105 106 // Golen ratio. 107 const double phi = 1.6180339887498948482; 108 testNumberToStringECMAScript(phi, "1.618033988749895"); 109 } 110 111 TEST(WTF, StringReplaceWithLiteral) 112 { 113 // Cases for 8Bit source. 114 String testString = "1224"; 115 ASSERT_TRUE(testString.is8Bit()); 116 testString.replaceWithLiteral('2', ""); 117 ASSERT_STREQ("14", testString.utf8().data()); 118 119 testString = "1224"; 120 ASSERT_TRUE(testString.is8Bit()); 121 testString.replaceWithLiteral('2', "3"); 122 ASSERT_STREQ("1334", testString.utf8().data()); 123 124 testString = "1224"; 125 ASSERT_TRUE(testString.is8Bit()); 126 testString.replaceWithLiteral('2', "555"); 127 ASSERT_STREQ("15555554", testString.utf8().data()); 128 129 testString = "1224"; 130 ASSERT_TRUE(testString.is8Bit()); 131 testString.replaceWithLiteral('3', "NotFound"); 132 ASSERT_STREQ("1224", testString.utf8().data()); 133 134 // Cases for 16Bit source. 135 testString = String::fromUTF8("rsum"); 136 ASSERT_FALSE(testString.is8Bit()); 137 testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is ''*/), "e"); 138 ASSERT_STREQ("resume", testString.utf8().data()); 139 140 testString = String::fromUTF8("rsum"); 141 ASSERT_FALSE(testString.is8Bit()); 142 testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is ''*/), ""); 143 ASSERT_STREQ("rsum", testString.utf8().data()); 144 145 testString = String::fromUTF8("rsum"); 146 ASSERT_FALSE(testString.is8Bit()); 147 testString.replaceWithLiteral('3', "NotFound"); 148 ASSERT_STREQ("rsum", testString.utf8().data()); 149 } 150 151 TEST(WTF, StringComparisonOfSameStringVectors) 152 { 153 Vector<String> stringVector; 154 stringVector.append("one"); 155 stringVector.append("two"); 156 157 Vector<String> sameStringVector; 158 sameStringVector.append("one"); 159 sameStringVector.append("two"); 160 161 ASSERT_EQ(stringVector, sameStringVector); 162 } 163 164 TEST(WTF, SimplifyWhiteSpace) 165 { 166 String extraSpaces(" Hello world "); 167 ASSERT_EQ(String("Hello world"), extraSpaces.simplifyWhiteSpace()); 168 ASSERT_EQ(String(" Hello world "), extraSpaces.simplifyWhiteSpace(WTF::DoNotStripWhiteSpace)); 169 170 String extraSpacesAndNewlines(" \nHello\n world\n "); 171 ASSERT_EQ(String("Hello world"), extraSpacesAndNewlines.simplifyWhiteSpace()); 172 ASSERT_EQ(String(" Hello world "), extraSpacesAndNewlines.simplifyWhiteSpace(WTF::DoNotStripWhiteSpace)); 173 174 String extraSpacesAndTabs(" \nHello\t world\t "); 175 ASSERT_EQ(String("Hello world"), extraSpacesAndTabs.simplifyWhiteSpace()); 176 ASSERT_EQ(String(" Hello world "), extraSpacesAndTabs.simplifyWhiteSpace(WTF::DoNotStripWhiteSpace)); 177 } 178 179 } // namespace 180