Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: Source/core/css/CSSPrimitiveValueTest.cpp

Issue 1226273002: CSSValue Immediates: Make CSSPrimitiveValue store a tagged pointer (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@cssvalue_patch_2_attempt_2
Patch Set: Rebase Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.cpp ('k') | Source/core/css/CSSValue.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6
7 #include "core/css/CSSPrimitiveValue.h"
8
9 #include "core/css/CSSValueTestHelper.h"
10 #include <gtest/gtest.h>
11 #include <strings.h>
12
13 using namespace blink;
14
15 namespace {
16
17 void clearTaggedPtr(CSSPrimitiveValue::CSSTaggedPtrValue& taggedPtr)
18 {
19 taggedPtr.flag = 0;
20 taggedPtr.type = static_cast<CSSPrimitiveValue::UnitType>(0);
21 taggedPtr.setValue(0.0);
22 }
23
24 TEST(CSSPrimitiveValueTest, CSSPrimitiveValueTest)
25 {
26 // 1.0 can be stored in a tagged ptr.
27 CSSPrimitiveValue value = CSSPrimitiveValue::create(1.0, CSSPrimitiveValue:: CSS_INTEGER);
28 EXPECT_EQ(1.0, value.getDoubleValue());
29 EXPECT_EQ("1", value.customCSSText());
30 EXPECT_TRUE(value.isNumber());
31 EXPECT_FALSE(value.isAttr());
32
33 CSSValue cssValue(value);
34 EXPECT_TRUE(cssValue.isPrimitiveValue());
35 EXPECT_FALSE(cssValue.isImageValue());
36 EXPECT_EQ("1", cssValue.cssText());
37
38 // 3.3 cannot be stored in a tagged ptr.
39 CSSPrimitiveValue valueInTaggedPtr = CSSPrimitiveValue::create(3.3, CSSPrimi tiveValue::CSS_NUMBER);
40 EXPECT_EQ(3.3, valueInTaggedPtr.getDoubleValue());
41 EXPECT_EQ("3.3", valueInTaggedPtr.customCSSText());
42 EXPECT_TRUE(valueInTaggedPtr.isNumber());
43 EXPECT_FALSE(value.isAttr());
44
45 CSSValue cssValueWithTaggedPtr(valueInTaggedPtr);
46 EXPECT_TRUE(cssValueWithTaggedPtr.isPrimitiveValue());
47 EXPECT_FALSE(cssValueWithTaggedPtr.isImageValue());
48 EXPECT_EQ("3.3", cssValueWithTaggedPtr.cssText());
49
50 EXPECT_FALSE(cssValue.equals(cssValueWithTaggedPtr));
51 EXPECT_FALSE(cssValueWithTaggedPtr.equals(cssValue));
52
53 CSSValue cssValueCopy(cssValue);
54 EXPECT_TRUE(cssValue.equals(cssValueCopy));
55 EXPECT_TRUE(cssValueCopy.equals(cssValue));
56
57 CSSValue cssValueWithTaggedPtrCopy(cssValueWithTaggedPtr);
58 EXPECT_TRUE(cssValueWithTaggedPtr.equals(cssValueWithTaggedPtrCopy));
59 EXPECT_TRUE(cssValueWithTaggedPtrCopy.equals(cssValueWithTaggedPtr));
60 }
61
62 TEST(CSSPrimitiveValueTest, CSSTaggedPtrValueTest)
63 {
64 CSSPrimitiveValue::CSSTaggedPtrValue taggedPtr;
65 clearTaggedPtr(taggedPtr);
66
67 // Check that all fields can be get/set correctly.
68 EXPECT_EQ(0UL, taggedPtr.flag);
69 taggedPtr.flag = 1;
70 EXPECT_EQ(1UL, taggedPtr.flag);
71
72 EXPECT_EQ(0UL, taggedPtr.type);
73 taggedPtr.type = CSSPrimitiveValue::CSS_INTEGER;
74 EXPECT_EQ(CSSPrimitiveValue::CSS_INTEGER, taggedPtr.type);
75
76 EXPECT_EQ(0UL, taggedPtr.getValue());
77 taggedPtr.setValue(1.0);
78 EXPECT_EQ(1.0, taggedPtr.getValue());
79
80 taggedPtr.flag = 1;
81 taggedPtr.type = CSSPrimitiveValue::CSS_INTEGER;
82 taggedPtr.setValue(1.0);
83 EXPECT_EQ(1UL, taggedPtr.flag);
84 EXPECT_EQ(CSSPrimitiveValue::CSS_INTEGER, taggedPtr.type);
85 EXPECT_EQ(1.0, taggedPtr.getValue());
86
87 clearTaggedPtr(taggedPtr);
88 EXPECT_EQ(0UL, taggedPtr.flag);
89 EXPECT_EQ(0UL, taggedPtr.type);
90 EXPECT_EQ(0UL, taggedPtr.getValue());
91 EXPECT_EQ(0UL, *reinterpret_cast<uintptr_t*>(&taggedPtr));
92 }
93
94 TEST(CSSPrimitiveValueTest, FitsWithoutDataLossTest)
95 {
96 // Consistent on 32-bit/64-bit
97 EXPECT_EQ(true, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(1. 0));
98 EXPECT_EQ(false, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(3 .3));
99
100 uint64_t trailing1sOnAllPlatforms = 0xf;
101 EXPECT_EQ(false, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(* reinterpret_cast<double*>(&trailing1sOnAllPlatforms)));
102
103 // Fits on 64-bit, but not on 32-bit
104 uint64_t trailing1sOn32BitOnly = 0xf00;
105 bool expectToFit;
106 #if CPU(32BIT)
107 expectToFit = false;
108 #elif CPU(64BIT)
109 expectToFit = true;
110 #endif
111 EXPECT_EQ(expectToFit, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutData Loss(*reinterpret_cast<double*>(&trailing1sOn32BitOnly)));
112 }
113
114 // Tests that the flag bit is in the least-significant bit. This test is importa nt
115 // since we implement CSSTaggedPtrValue using bitfields, and the C++ standard sp ecifies
116 // that bitfield packing behavior is implementation-specific.
117 TEST(CSSPrimitiveValueTest, FlagBitInLSBTest)
118 {
119 CSSPrimitiveValue::CSSTaggedPtrValue taggedPtr;
120 clearTaggedPtr(taggedPtr);
121
122 taggedPtr.flag = 1;
123 EXPECT_EQ(1UL, *reinterpret_cast<uintptr_t*>(&taggedPtr));
124
125 taggedPtr.flag = 0;
126 EXPECT_EQ(0UL, *reinterpret_cast<uintptr_t*>(&taggedPtr));
127 }
128
129 // Test that all valid values for UnitType fit in the UnitType bit field in the
130 // tagged pointer.
131 TEST(CSSPrimitiveValueTest, UnitTypeSizeTest)
132 {
133 CSSPrimitiveValue::CSSTaggedPtrValue taggedPtr;
134 clearTaggedPtr(taggedPtr);
135
136 // We can't take the sizeof() of a bitfield. Instead, try set the largest pr imitive
137 // value type we have, and see if we can read it out again.
138 taggedPtr.type = CSSPrimitiveValue::CSS_QEM;
139 ASSERT_EQ(CSSPrimitiveValue::CSS_QEM, taggedPtr.type);
140 }
141
142
143 } // namespace
OLDNEW
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.cpp ('k') | Source/core/css/CSSValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698