| 1 | /* |
| 2 | * Copyright (C) 2012-2017 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/text/AtomicString.h> |
| 29 | |
| 30 | namespace TestWebKitAPI { |
| 31 | |
| 32 | TEST(WTF, AtomicStringCreationFromLiteral) |
| 33 | { |
| 34 | AtomicString stringWithTemplate("Template Literal" , AtomicString::ConstructFromLiteral); |
| 35 | ASSERT_EQ(strlen("Template Literal" ), stringWithTemplate.length()); |
| 36 | ASSERT_TRUE(stringWithTemplate == "Template Literal" ); |
| 37 | ASSERT_TRUE(stringWithTemplate.string().is8Bit()); |
| 38 | |
| 39 | const char* programmaticStringData = "Explicit Size Literal" ; |
| 40 | AtomicString programmaticString(programmaticStringData, strlen(programmaticStringData), AtomicString::ConstructFromLiteral); |
| 41 | ASSERT_EQ(strlen(programmaticStringData), programmaticString.length()); |
| 42 | ASSERT_TRUE(programmaticString.string().is8Bit()); |
| 43 | ASSERT_EQ(programmaticStringData, reinterpret_cast<const char*>(programmaticString.string().characters8())); |
| 44 | } |
| 45 | |
| 46 | TEST(WTF, AtomicStringCreationFromLiteralUniqueness) |
| 47 | { |
| 48 | AtomicString string1("Template Literal" , AtomicString::ConstructFromLiteral); |
| 49 | AtomicString string2("Template Literal" , AtomicString::ConstructFromLiteral); |
| 50 | ASSERT_EQ(string1.impl(), string2.impl()); |
| 51 | |
| 52 | AtomicString string3("Template Literal" ); |
| 53 | ASSERT_EQ(string1.impl(), string3.impl()); |
| 54 | } |
| 55 | |
| 56 | TEST(WTF, AtomicStringExistingHash) |
| 57 | { |
| 58 | AtomicString string1("Template Literal" , AtomicString::ConstructFromLiteral); |
| 59 | ASSERT_EQ(string1.existingHash(), string1.impl()->existingHash()); |
| 60 | AtomicString string2; |
| 61 | ASSERT_EQ(string2.existingHash(), 0u); |
| 62 | } |
| 63 | |
| 64 | static inline const char* testAtomicStringNumber(double number) |
| 65 | { |
| 66 | static char testBuffer[100] = { }; |
| 67 | std::strncpy(testBuffer, AtomicString::number(number).string().utf8().data(), 99); |
| 68 | return testBuffer; |
| 69 | } |
| 70 | |
| 71 | TEST(WTF, AtomicStringNumberDouble) |
| 72 | { |
| 73 | using Limits = std::numeric_limits<double>; |
| 74 | |
| 75 | EXPECT_STREQ("Infinity" , testAtomicStringNumber(Limits::infinity())); |
| 76 | EXPECT_STREQ("-Infinity" , testAtomicStringNumber(-Limits::infinity())); |
| 77 | |
| 78 | EXPECT_STREQ("NaN" , testAtomicStringNumber(-Limits::quiet_NaN())); |
| 79 | |
| 80 | EXPECT_STREQ("0" , testAtomicStringNumber(0)); |
| 81 | EXPECT_STREQ("0" , testAtomicStringNumber(-0)); |
| 82 | |
| 83 | EXPECT_STREQ("2.2250738585072014e-308" , testAtomicStringNumber(Limits::min())); |
| 84 | EXPECT_STREQ("-1.7976931348623157e+308" , testAtomicStringNumber(Limits::lowest())); |
| 85 | EXPECT_STREQ("1.7976931348623157e+308" , testAtomicStringNumber(Limits::max())); |
| 86 | |
| 87 | EXPECT_STREQ("3.141592653589793" , testAtomicStringNumber(piDouble)); |
| 88 | EXPECT_STREQ("3.1415927410125732" , testAtomicStringNumber(piFloat)); |
| 89 | EXPECT_STREQ("1.5707963267948966" , testAtomicStringNumber(piOverTwoDouble)); |
| 90 | EXPECT_STREQ("1.5707963705062866" , testAtomicStringNumber(piOverTwoFloat)); |
| 91 | EXPECT_STREQ("0.7853981633974483" , testAtomicStringNumber(piOverFourDouble)); |
| 92 | EXPECT_STREQ("0.7853981852531433" , testAtomicStringNumber(piOverFourFloat)); |
| 93 | |
| 94 | EXPECT_STREQ("2.718281828459045" , testAtomicStringNumber(2.71828182845904523536028747135266249775724709369995)); |
| 95 | |
| 96 | EXPECT_STREQ("299792458" , testAtomicStringNumber(299792458)); |
| 97 | |
| 98 | EXPECT_STREQ("1.618033988749895" , testAtomicStringNumber(1.6180339887498948482)); |
| 99 | |
| 100 | EXPECT_STREQ("1000" , testAtomicStringNumber(1e3)); |
| 101 | EXPECT_STREQ("10000000000" , testAtomicStringNumber(1e10)); |
| 102 | EXPECT_STREQ("100000000000000000000" , testAtomicStringNumber(1e20)); |
| 103 | EXPECT_STREQ("1e+21" , testAtomicStringNumber(1e21)); |
| 104 | EXPECT_STREQ("1e+30" , testAtomicStringNumber(1e30)); |
| 105 | |
| 106 | EXPECT_STREQ("1100" , testAtomicStringNumber(1.1e3)); |
| 107 | EXPECT_STREQ("11000000000" , testAtomicStringNumber(1.1e10)); |
| 108 | EXPECT_STREQ("110000000000000000000" , testAtomicStringNumber(1.1e20)); |
| 109 | EXPECT_STREQ("1.1e+21" , testAtomicStringNumber(1.1e21)); |
| 110 | EXPECT_STREQ("1.1e+30" , testAtomicStringNumber(1.1e30)); |
| 111 | } |
| 112 | |
| 113 | } // namespace TestWebKitAPI |
| 114 | |