Home | History | Annotate | Download | only in tests
      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     String stringWithTemplate("Template Literal", String::ConstructFromLiteral);
     45     ASSERT_EQ(strlen("Template Literal"), stringWithTemplate.length());
     46     ASSERT_TRUE(stringWithTemplate == "Template Literal");
     47     ASSERT_TRUE(stringWithTemplate.is8Bit());
     48     ASSERT_TRUE(String("Template Literal") == stringWithTemplate);
     49 }
     50 
     51 TEST(WTF, StringASCII)
     52 {
     53     CString output;
     54 
     55     // Null String.
     56     output = String().ascii();
     57     ASSERT_STREQ("", output.data());
     58 
     59     // Empty String.
     60     output = emptyString().ascii();
     61     ASSERT_STREQ("", output.data());
     62 
     63     // Regular String.
     64     output = String("foobar").ascii();
     65     ASSERT_STREQ("foobar", output.data());
     66 }
     67 
     68 static void testNumberToStringECMAScript(double number, const char* reference)
     69 {
     70     CString numberString = String::numberToStringECMAScript(number).latin1();
     71     ASSERT_STREQ(reference, numberString.data());
     72 }
     73 
     74 TEST(WTF, StringNumberToStringECMAScriptBoundaries)
     75 {
     76     typedef std::numeric_limits<double> Limits;
     77 
     78     // Infinity.
     79     testNumberToStringECMAScript(Limits::infinity(), "Infinity");
     80     testNumberToStringECMAScript(-Limits::infinity(), "-Infinity");
     81 
     82     // NaN.
     83     testNumberToStringECMAScript(-Limits::quiet_NaN(), "NaN");
     84 
     85     // Zeros.
     86     testNumberToStringECMAScript(0, "0");
     87     testNumberToStringECMAScript(-0, "0");
     88 
     89     // Min-Max.
     90     testNumberToStringECMAScript(Limits::min(), "2.2250738585072014e-308");
     91     testNumberToStringECMAScript(Limits::max(), "1.7976931348623157e+308");
     92 }
     93 
     94 TEST(WTF, StringNumberToStringECMAScriptRegularNumbers)
     95 {
     96     // Pi.
     97     testNumberToStringECMAScript(piDouble, "3.141592653589793");
     98     testNumberToStringECMAScript(piFloat, "3.1415927410125732");
     99     testNumberToStringECMAScript(piOverTwoDouble, "1.5707963267948966");
    100     testNumberToStringECMAScript(piOverTwoFloat, "1.5707963705062866");
    101     testNumberToStringECMAScript(piOverFourDouble, "0.7853981633974483");
    102     testNumberToStringECMAScript(piOverFourFloat, "0.7853981852531433");
    103 
    104     // e.
    105     const double e = 2.71828182845904523536028747135266249775724709369995;
    106     testNumberToStringECMAScript(e, "2.718281828459045");
    107 
    108     // c, speed of light in m/s.
    109     const double c = 299792458;
    110     testNumberToStringECMAScript(c, "299792458");
    111 
    112     // Golen ratio.
    113     const double phi = 1.6180339887498948482;
    114     testNumberToStringECMAScript(phi, "1.618033988749895");
    115 }
    116 
    117 TEST(WTF, StringReplaceWithLiteral)
    118 {
    119     // Cases for 8Bit source.
    120     String testString = "1224";
    121     ASSERT_TRUE(testString.is8Bit());
    122     testString.replaceWithLiteral('2', "");
    123     ASSERT_STREQ("14", testString.utf8().data());
    124 
    125     testString = "1224";
    126     ASSERT_TRUE(testString.is8Bit());
    127     testString.replaceWithLiteral('2', "3");
    128     ASSERT_STREQ("1334", testString.utf8().data());
    129 
    130     testString = "1224";
    131     ASSERT_TRUE(testString.is8Bit());
    132     testString.replaceWithLiteral('2', "555");
    133     ASSERT_STREQ("15555554", testString.utf8().data());
    134 
    135     testString = "1224";
    136     ASSERT_TRUE(testString.is8Bit());
    137     testString.replaceWithLiteral('3', "NotFound");
    138     ASSERT_STREQ("1224", testString.utf8().data());
    139 
    140     // Cases for 16Bit source.
    141     testString = String::fromUTF8("rsum");
    142     ASSERT_FALSE(testString.is8Bit());
    143     testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is ''*/), "e");
    144     ASSERT_STREQ("resume", testString.utf8().data());
    145 
    146     testString = String::fromUTF8("rsum");
    147     ASSERT_FALSE(testString.is8Bit());
    148     testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is ''*/), "");
    149     ASSERT_STREQ("rsum", testString.utf8().data());
    150 
    151     testString = String::fromUTF8("rsum");
    152     ASSERT_FALSE(testString.is8Bit());
    153     testString.replaceWithLiteral('3', "NotFound");
    154     ASSERT_STREQ("rsum", testString.utf8().data());
    155 }
    156 
    157 } // namespace
    158