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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/css/CSSPrimitiveValue.cpp ('k') | Source/core/css/CSSValue.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/css/CSSPrimitiveValueTest.cpp
diff --git a/Source/core/css/CSSPrimitiveValueTest.cpp b/Source/core/css/CSSPrimitiveValueTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..7a063cb8885f98dee0558eca4f71ad1a575ee3bc
--- /dev/null
+++ b/Source/core/css/CSSPrimitiveValueTest.cpp
@@ -0,0 +1,143 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+
+#include "core/css/CSSPrimitiveValue.h"
+
+#include "core/css/CSSValueTestHelper.h"
+#include <gtest/gtest.h>
+#include <strings.h>
+
+using namespace blink;
+
+namespace {
+
+void clearTaggedPtr(CSSPrimitiveValue::CSSTaggedPtrValue& taggedPtr)
+{
+ taggedPtr.flag = 0;
+ taggedPtr.type = static_cast<CSSPrimitiveValue::UnitType>(0);
+ taggedPtr.setValue(0.0);
+}
+
+TEST(CSSPrimitiveValueTest, CSSPrimitiveValueTest)
+{
+ // 1.0 can be stored in a tagged ptr.
+ CSSPrimitiveValue value = CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::CSS_INTEGER);
+ EXPECT_EQ(1.0, value.getDoubleValue());
+ EXPECT_EQ("1", value.customCSSText());
+ EXPECT_TRUE(value.isNumber());
+ EXPECT_FALSE(value.isAttr());
+
+ CSSValue cssValue(value);
+ EXPECT_TRUE(cssValue.isPrimitiveValue());
+ EXPECT_FALSE(cssValue.isImageValue());
+ EXPECT_EQ("1", cssValue.cssText());
+
+ // 3.3 cannot be stored in a tagged ptr.
+ CSSPrimitiveValue valueInTaggedPtr = CSSPrimitiveValue::create(3.3, CSSPrimitiveValue::CSS_NUMBER);
+ EXPECT_EQ(3.3, valueInTaggedPtr.getDoubleValue());
+ EXPECT_EQ("3.3", valueInTaggedPtr.customCSSText());
+ EXPECT_TRUE(valueInTaggedPtr.isNumber());
+ EXPECT_FALSE(value.isAttr());
+
+ CSSValue cssValueWithTaggedPtr(valueInTaggedPtr);
+ EXPECT_TRUE(cssValueWithTaggedPtr.isPrimitiveValue());
+ EXPECT_FALSE(cssValueWithTaggedPtr.isImageValue());
+ EXPECT_EQ("3.3", cssValueWithTaggedPtr.cssText());
+
+ EXPECT_FALSE(cssValue.equals(cssValueWithTaggedPtr));
+ EXPECT_FALSE(cssValueWithTaggedPtr.equals(cssValue));
+
+ CSSValue cssValueCopy(cssValue);
+ EXPECT_TRUE(cssValue.equals(cssValueCopy));
+ EXPECT_TRUE(cssValueCopy.equals(cssValue));
+
+ CSSValue cssValueWithTaggedPtrCopy(cssValueWithTaggedPtr);
+ EXPECT_TRUE(cssValueWithTaggedPtr.equals(cssValueWithTaggedPtrCopy));
+ EXPECT_TRUE(cssValueWithTaggedPtrCopy.equals(cssValueWithTaggedPtr));
+}
+
+TEST(CSSPrimitiveValueTest, CSSTaggedPtrValueTest)
+{
+ CSSPrimitiveValue::CSSTaggedPtrValue taggedPtr;
+ clearTaggedPtr(taggedPtr);
+
+ // Check that all fields can be get/set correctly.
+ EXPECT_EQ(0UL, taggedPtr.flag);
+ taggedPtr.flag = 1;
+ EXPECT_EQ(1UL, taggedPtr.flag);
+
+ EXPECT_EQ(0UL, taggedPtr.type);
+ taggedPtr.type = CSSPrimitiveValue::CSS_INTEGER;
+ EXPECT_EQ(CSSPrimitiveValue::CSS_INTEGER, taggedPtr.type);
+
+ EXPECT_EQ(0UL, taggedPtr.getValue());
+ taggedPtr.setValue(1.0);
+ EXPECT_EQ(1.0, taggedPtr.getValue());
+
+ taggedPtr.flag = 1;
+ taggedPtr.type = CSSPrimitiveValue::CSS_INTEGER;
+ taggedPtr.setValue(1.0);
+ EXPECT_EQ(1UL, taggedPtr.flag);
+ EXPECT_EQ(CSSPrimitiveValue::CSS_INTEGER, taggedPtr.type);
+ EXPECT_EQ(1.0, taggedPtr.getValue());
+
+ clearTaggedPtr(taggedPtr);
+ EXPECT_EQ(0UL, taggedPtr.flag);
+ EXPECT_EQ(0UL, taggedPtr.type);
+ EXPECT_EQ(0UL, taggedPtr.getValue());
+ EXPECT_EQ(0UL, *reinterpret_cast<uintptr_t*>(&taggedPtr));
+}
+
+TEST(CSSPrimitiveValueTest, FitsWithoutDataLossTest)
+{
+ // Consistent on 32-bit/64-bit
+ EXPECT_EQ(true, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(1.0));
+ EXPECT_EQ(false, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(3.3));
+
+ uint64_t trailing1sOnAllPlatforms = 0xf;
+ EXPECT_EQ(false, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(*reinterpret_cast<double*>(&trailing1sOnAllPlatforms)));
+
+ // Fits on 64-bit, but not on 32-bit
+ uint64_t trailing1sOn32BitOnly = 0xf00;
+ bool expectToFit;
+#if CPU(32BIT)
+ expectToFit = false;
+#elif CPU(64BIT)
+ expectToFit = true;
+#endif
+ EXPECT_EQ(expectToFit, CSSPrimitiveValue::CSSTaggedPtrValue::fitsWithoutDataLoss(*reinterpret_cast<double*>(&trailing1sOn32BitOnly)));
+}
+
+// Tests that the flag bit is in the least-significant bit. This test is important
+// since we implement CSSTaggedPtrValue using bitfields, and the C++ standard specifies
+// that bitfield packing behavior is implementation-specific.
+TEST(CSSPrimitiveValueTest, FlagBitInLSBTest)
+{
+ CSSPrimitiveValue::CSSTaggedPtrValue taggedPtr;
+ clearTaggedPtr(taggedPtr);
+
+ taggedPtr.flag = 1;
+ EXPECT_EQ(1UL, *reinterpret_cast<uintptr_t*>(&taggedPtr));
+
+ taggedPtr.flag = 0;
+ EXPECT_EQ(0UL, *reinterpret_cast<uintptr_t*>(&taggedPtr));
+}
+
+// Test that all valid values for UnitType fit in the UnitType bit field in the
+// tagged pointer.
+TEST(CSSPrimitiveValueTest, UnitTypeSizeTest)
+{
+ CSSPrimitiveValue::CSSTaggedPtrValue taggedPtr;
+ clearTaggedPtr(taggedPtr);
+
+ // We can't take the sizeof() of a bitfield. Instead, try set the largest primitive
+ // value type we have, and see if we can read it out again.
+ taggedPtr.type = CSSPrimitiveValue::CSS_QEM;
+ ASSERT_EQ(CSSPrimitiveValue::CSS_QEM, taggedPtr.type);
+}
+
+
+} // namespace
« 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