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