WTFString.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "config.h"
  26. #include <limits>
  27. #include <wtf/MathExtras.h>
  28. #include <wtf/text/CString.h>
  29. #include <wtf/text/WTFString.h>
  30. namespace TestWebKitAPI {
  31. TEST(WTF, StringCreationFromLiteral)
  32. {
  33. String stringFromLiteral(ASCIILiteral("Explicit construction syntax"));
  34. ASSERT_EQ(strlen("Explicit construction syntax"), stringFromLiteral.length());
  35. ASSERT_TRUE(stringFromLiteral == "Explicit construction syntax");
  36. ASSERT_TRUE(stringFromLiteral.is8Bit());
  37. ASSERT_TRUE(stringFromLiteral.impl()->hasTerminatingNullCharacter());
  38. ASSERT_TRUE(String("Explicit construction syntax") == stringFromLiteral);
  39. String stringWithTemplate("Template Literal", String::ConstructFromLiteral);
  40. ASSERT_EQ(strlen("Template Literal"), stringWithTemplate.length());
  41. ASSERT_TRUE(stringWithTemplate == "Template Literal");
  42. ASSERT_TRUE(stringWithTemplate.is8Bit());
  43. ASSERT_TRUE(stringWithTemplate.impl()->hasTerminatingNullCharacter());
  44. ASSERT_TRUE(String("Template Literal") == stringWithTemplate);
  45. }
  46. TEST(WTF, StringASCII)
  47. {
  48. CString output;
  49. // Null String.
  50. output = String().ascii();
  51. ASSERT_STREQ("", output.data());
  52. // Empty String.
  53. output = emptyString().ascii();
  54. ASSERT_STREQ("", output.data());
  55. // Regular String.
  56. output = String(ASCIILiteral("foobar")).ascii();
  57. ASSERT_STREQ("foobar", output.data());
  58. }
  59. static void testNumberToStringECMAScript(double number, const char* reference)
  60. {
  61. CString numberString = String::numberToStringECMAScript(number).latin1();
  62. ASSERT_STREQ(reference, numberString.data());
  63. }
  64. TEST(WTF, StringNumberToStringECMAScriptBoundaries)
  65. {
  66. typedef std::numeric_limits<double> Limits;
  67. // Infinity.
  68. testNumberToStringECMAScript(Limits::infinity(), "Infinity");
  69. testNumberToStringECMAScript(-Limits::infinity(), "-Infinity");
  70. // NaN.
  71. testNumberToStringECMAScript(-Limits::quiet_NaN(), "NaN");
  72. // Zeros.
  73. testNumberToStringECMAScript(0, "0");
  74. testNumberToStringECMAScript(-0, "0");
  75. // Min-Max.
  76. testNumberToStringECMAScript(Limits::min(), "2.2250738585072014e-308");
  77. testNumberToStringECMAScript(Limits::max(), "1.7976931348623157e+308");
  78. }
  79. TEST(WTF, StringNumberToStringECMAScriptRegularNumbers)
  80. {
  81. // Pi.
  82. testNumberToStringECMAScript(piDouble, "3.141592653589793");
  83. testNumberToStringECMAScript(piFloat, "3.1415927410125732");
  84. testNumberToStringECMAScript(piOverTwoDouble, "1.5707963267948966");
  85. testNumberToStringECMAScript(piOverTwoFloat, "1.5707963705062866");
  86. testNumberToStringECMAScript(piOverFourDouble, "0.7853981633974483");
  87. testNumberToStringECMAScript(piOverFourFloat, "0.7853981852531433");
  88. // e.
  89. const double e = 2.71828182845904523536028747135266249775724709369995;
  90. testNumberToStringECMAScript(e, "2.718281828459045");
  91. // c, speed of light in m/s.
  92. const double c = 299792458;
  93. testNumberToStringECMAScript(c, "299792458");
  94. // Golen ratio.
  95. const double phi = 1.6180339887498948482;
  96. testNumberToStringECMAScript(phi, "1.618033988749895");
  97. }
  98. TEST(WTF, StringReplaceWithLiteral)
  99. {
  100. // Cases for 8Bit source.
  101. String testString = "1224";
  102. ASSERT_TRUE(testString.is8Bit());
  103. testString.replaceWithLiteral('2', "");
  104. ASSERT_STREQ("14", testString.utf8().data());
  105. testString = "1224";
  106. ASSERT_TRUE(testString.is8Bit());
  107. testString.replaceWithLiteral('2', "3");
  108. ASSERT_STREQ("1334", testString.utf8().data());
  109. testString = "1224";
  110. ASSERT_TRUE(testString.is8Bit());
  111. testString.replaceWithLiteral('2', "555");
  112. ASSERT_STREQ("15555554", testString.utf8().data());
  113. testString = "1224";
  114. ASSERT_TRUE(testString.is8Bit());
  115. testString.replaceWithLiteral('3', "NotFound");
  116. ASSERT_STREQ("1224", testString.utf8().data());
  117. // Cases for 16Bit source.
  118. testString = String::fromUTF8("résumé");
  119. ASSERT_FALSE(testString.is8Bit());
  120. testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is 'é'*/), "e");
  121. ASSERT_STREQ("resume", testString.utf8().data());
  122. testString = String::fromUTF8("résumé");
  123. ASSERT_FALSE(testString.is8Bit());
  124. testString.replaceWithLiteral(UChar(0x00E9 /*U+00E9 is 'é'*/), "");
  125. ASSERT_STREQ("rsum", testString.utf8().data());
  126. testString = String::fromUTF8("résumé");
  127. ASSERT_FALSE(testString.is8Bit());
  128. testString.replaceWithLiteral('3', "NotFound");
  129. ASSERT_STREQ("résumé", testString.utf8().data());
  130. }
  131. } // namespace TestWebKitAPI