diff --git a/test/unittests/compiler/change-lowering-unittest.cc b/test/unittests/compiler/change-lowering-unittest.cc index 763a44352f..060b1c1842 100644 --- a/test/unittests/compiler/change-lowering-unittest.cc +++ b/test/unittests/compiler/change-lowering-unittest.cc @@ -15,6 +15,7 @@ using testing::_; using testing::AllOf; +using testing::BitEq; using testing::Capture; using testing::CaptureEq; @@ -78,7 +79,8 @@ class ChangeLoweringTest : public GraphTest { const Matcher& control_matcher) { return IsCall(_, IsHeapConstant(Unique::CreateImmovable( AllocateHeapNumberStub(isolate()).GetCode())), - IsNumberConstant(0.0), effect_matcher, control_matcher); + IsNumberConstant(BitEq(0.0)), effect_matcher, + control_matcher); } Matcher IsLoadHeapNumber(const Matcher& value_matcher, const Matcher& control_matcher) { diff --git a/test/unittests/compiler/js-builtin-reducer-unittest.cc b/test/unittests/compiler/js-builtin-reducer-unittest.cc index 87d1ad53b8..9c572820e7 100644 --- a/test/unittests/compiler/js-builtin-reducer-unittest.cc +++ b/test/unittests/compiler/js-builtin-reducer-unittest.cc @@ -10,6 +10,7 @@ #include "test/unittests/compiler/node-test-utils.h" #include "testing/gmock-support.h" +using testing::BitEq; using testing::Capture; namespace v8 { @@ -83,9 +84,11 @@ TEST_F(JSBuiltinReducerTest, MathAbs) { } else { Capture branch; ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), - IsSelect(kMachNone, IsNumberLessThan(IsNumberConstant(0), p0), - p0, IsNumberSubtract(IsNumberConstant(0), p0))); + EXPECT_THAT( + r.replacement(), + IsSelect(kMachNone, + IsNumberLessThan(IsNumberConstant(BitEq(0.0)), p0), p0, + IsNumberSubtract(IsNumberConstant(BitEq(0.0)), p0))); } } } diff --git a/test/unittests/compiler/js-typed-lowering-unittest.cc b/test/unittests/compiler/js-typed-lowering-unittest.cc index e4ea4a581f..322d0cb5e7 100644 --- a/test/unittests/compiler/js-typed-lowering-unittest.cc +++ b/test/unittests/compiler/js-typed-lowering-unittest.cc @@ -12,6 +12,10 @@ #include "test/unittests/compiler/compiler-test-utils.h" #include "test/unittests/compiler/graph-unittest.h" #include "test/unittests/compiler/node-test-utils.h" +#include "testing/gmock-support.h" + +using testing::BitEq; + namespace v8 { namespace internal { @@ -135,7 +139,7 @@ TEST_F(JSTypedLoweringTest, JSToBooleanWithOrderedNumber) { Reduce(graph()->NewNode(javascript()->ToBoolean(), input, context)); ASSERT_TRUE(r.Changed()); EXPECT_THAT(r.replacement(), - IsBooleanNot(IsNumberEqual(input, IsNumberConstant(0)))); + IsBooleanNot(IsNumberEqual(input, IsNumberConstant(BitEq(0.0))))); } @@ -150,7 +154,7 @@ TEST_F(JSTypedLoweringTest, JSToBooleanWithString) { IsBooleanNot(IsNumberEqual( IsLoadField(AccessBuilder::ForStringLength(), input, graph()->start(), graph()->start()), - IsNumberConstant(0)))); + IsNumberConstant(BitEq(0.0))))); } @@ -165,10 +169,10 @@ TEST_F(JSTypedLoweringTest, JSToBooleanWithPhi) { graph()->NewNode(common()->Phi(kMachAnyTagged, 2), p0, p1, control), context)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT( - r.replacement(), - IsPhi(kMachAnyTagged, - IsBooleanNot(IsNumberEqual(p0, IsNumberConstant(0))), p1, control)); + EXPECT_THAT(r.replacement(), + IsPhi(kMachAnyTagged, IsBooleanNot(IsNumberEqual( + p0, IsNumberConstant(BitEq(0.0)))), + p1, control)); } @@ -184,9 +188,10 @@ TEST_F(JSTypedLoweringTest, JSToBooleanWithSelect) { p1, p2), context)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), - IsSelect(kMachAnyTagged, p0, IsTrueConstant(), - IsBooleanNot(IsNumberEqual(p2, IsNumberConstant(0))))); + EXPECT_THAT( + r.replacement(), + IsSelect(kMachAnyTagged, p0, IsTrueConstant(), + IsBooleanNot(IsNumberEqual(p2, IsNumberConstant(BitEq(0.0)))))); } @@ -202,7 +207,7 @@ TEST_F(JSTypedLoweringTest, JSToNumberWithPlainPrimitive) { Reduction r = Reduce(graph()->NewNode(javascript()->ToNumber(), input, context, effect, control)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), IsToNumber(input, IsNumberConstant(0), + EXPECT_THAT(r.replacement(), IsToNumber(input, IsNumberConstant(BitEq(0.0)), graph()->start(), control)); } @@ -235,12 +240,13 @@ TEST_F(JSTypedLoweringTest, JSShiftLeftWithSigned32AndConstant) { Node* const context = UndefinedConstant(); Node* const effect = graph()->start(); Node* const control = graph()->start(); - TRACED_FORRANGE(int32_t, rhs, 0, 31) { + TRACED_FORRANGE(double, rhs, 0, 31) { Reduction r = Reduce(graph()->NewNode(javascript()->ShiftLeft(), lhs, NumberConstant(rhs), context, effect, control)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), IsWord32Shl(lhs, IsNumberConstant(rhs))); + EXPECT_THAT(r.replacement(), + IsWord32Shl(lhs, IsNumberConstant(BitEq(rhs)))); } } @@ -268,12 +274,13 @@ TEST_F(JSTypedLoweringTest, JSShiftRightWithSigned32AndConstant) { Node* const context = UndefinedConstant(); Node* const effect = graph()->start(); Node* const control = graph()->start(); - TRACED_FORRANGE(int32_t, rhs, 0, 31) { + TRACED_FORRANGE(double, rhs, 0, 31) { Reduction r = Reduce(graph()->NewNode(javascript()->ShiftRight(), lhs, NumberConstant(rhs), context, effect, control)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), IsWord32Sar(lhs, IsNumberConstant(rhs))); + EXPECT_THAT(r.replacement(), + IsWord32Sar(lhs, IsNumberConstant(BitEq(rhs)))); } } @@ -301,12 +308,13 @@ TEST_F(JSTypedLoweringTest, JSShiftRightLogicalWithUnsigned32AndConstant) { Node* const context = UndefinedConstant(); Node* const effect = graph()->start(); Node* const control = graph()->start(); - TRACED_FORRANGE(int32_t, rhs, 0, 31) { + TRACED_FORRANGE(double, rhs, 0, 31) { Reduction r = Reduce(graph()->NewNode(javascript()->ShiftRightLogical(), lhs, NumberConstant(rhs), context, effect, control)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), IsWord32Shr(lhs, IsNumberConstant(rhs))); + EXPECT_THAT(r.replacement(), + IsWord32Shr(lhs, IsNumberConstant(BitEq(rhs)))); } } diff --git a/test/unittests/compiler/machine-operator-reducer-unittest.cc b/test/unittests/compiler/machine-operator-reducer-unittest.cc index b92bd28c97..6fdba35f58 100644 --- a/test/unittests/compiler/machine-operator-reducer-unittest.cc +++ b/test/unittests/compiler/machine-operator-reducer-unittest.cc @@ -12,6 +12,7 @@ #include "testing/gmock-support.h" using testing::AllOf; +using testing::BitEq; using testing::Capture; using testing::CaptureEq; @@ -291,7 +292,7 @@ TEST_F(MachineOperatorReducerTest, ChangeFloat64ToFloat32WithConstant) { Reduction reduction = Reduce(graph()->NewNode( machine()->ChangeFloat32ToFloat64(), Float32Constant(x))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat64Constant(x)); + EXPECT_THAT(reduction.replacement(), IsFloat64Constant(BitEq(x))); } } @@ -355,7 +356,7 @@ TEST_F(MachineOperatorReducerTest, ChangeInt32ToFloat64WithConstant) { Reduction reduction = Reduce( graph()->NewNode(machine()->ChangeInt32ToFloat64(), Int32Constant(x))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat64Constant(FastI2D(x))); + EXPECT_THAT(reduction.replacement(), IsFloat64Constant(BitEq(FastI2D(x)))); } } @@ -384,7 +385,7 @@ TEST_F(MachineOperatorReducerTest, ChangeUint32ToFloat64WithConstant) { Reduce(graph()->NewNode(machine()->ChangeUint32ToFloat64(), Int32Constant(bit_cast(x)))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat64Constant(FastUI2D(x))); + EXPECT_THAT(reduction.replacement(), IsFloat64Constant(BitEq(FastUI2D(x)))); } } @@ -425,7 +426,8 @@ TEST_F(MachineOperatorReducerTest, TruncateFloat64ToFloat32WithConstant) { Reduction reduction = Reduce(graph()->NewNode( machine()->TruncateFloat64ToFloat32(), Float64Constant(x))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat32Constant(DoubleToFloat32(x))); + EXPECT_THAT(reduction.replacement(), + IsFloat32Constant(BitEq(DoubleToFloat32(x)))); } } @@ -1268,13 +1270,15 @@ TEST_F(MachineOperatorReducerTest, Float64MulWithMinusOne) { Reduction r = Reduce( graph()->NewNode(machine()->Float64Mul(), p0, Float64Constant(-1.0))); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), IsFloat64Sub(IsFloat64Constant(-0.0), p0)); + EXPECT_THAT(r.replacement(), + IsFloat64Sub(IsFloat64Constant(BitEq(-0.0)), p0)); } { Reduction r = Reduce( graph()->NewNode(machine()->Float64Mul(), Float64Constant(-1.0), p0)); ASSERT_TRUE(r.Changed()); - EXPECT_THAT(r.replacement(), IsFloat64Sub(IsFloat64Constant(-0.0), p0)); + EXPECT_THAT(r.replacement(), + IsFloat64Sub(IsFloat64Constant(BitEq(-0.0)), p0)); } } diff --git a/test/unittests/compiler/simplified-operator-reducer-unittest.cc b/test/unittests/compiler/simplified-operator-reducer-unittest.cc index 066cbe9337..18f5e64054 100644 --- a/test/unittests/compiler/simplified-operator-reducer-unittest.cc +++ b/test/unittests/compiler/simplified-operator-reducer-unittest.cc @@ -9,6 +9,10 @@ #include "src/types.h" #include "test/unittests/compiler/graph-unittest.h" #include "test/unittests/compiler/node-test-utils.h" +#include "testing/gmock-support.h" + +using testing::BitEq; + namespace v8 { namespace internal { @@ -114,11 +118,6 @@ static const uint32_t kUint32Values[] = { 0xbeb15c0d, 0xc171c53d, 0xc743dd38, 0xc8e2af50, 0xc98e2df0, 0xd9d1cdf9, 0xdcc91049, 0xe46f396d, 0xee991950, 0xef64e521, 0xf7aeefc9, 0xffffffff}; - -MATCHER(IsNaN, std::string(negation ? "isn't" : "is") + " NaN") { - return std::isnan(arg); -} - } // namespace @@ -271,7 +270,7 @@ TEST_F(SimplifiedOperatorReducerTest, ChangeFloat64ToTaggedWithConstant) { Reduction reduction = Reduce(graph()->NewNode( simplified()->ChangeFloat64ToTagged(), Float64Constant(n))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsNumberConstant(n)); + EXPECT_THAT(reduction.replacement(), IsNumberConstant(BitEq(n))); } } @@ -285,7 +284,7 @@ TEST_F(SimplifiedOperatorReducerTest, ChangeInt32ToTaggedWithConstant) { Reduction reduction = Reduce(graph()->NewNode( simplified()->ChangeInt32ToTagged(), Int32Constant(n))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsNumberConstant(FastI2D(n))); + EXPECT_THAT(reduction.replacement(), IsNumberConstant(BitEq(FastI2D(n)))); } } @@ -332,7 +331,7 @@ TEST_F(SimplifiedOperatorReducerTest, ChangeTaggedToFloat64WithConstant) { Reduction reduction = Reduce(graph()->NewNode( simplified()->ChangeTaggedToFloat64(), NumberConstant(n))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat64Constant(n)); + EXPECT_THAT(reduction.replacement(), IsFloat64Constant(BitEq(n))); } } @@ -342,7 +341,8 @@ TEST_F(SimplifiedOperatorReducerTest, ChangeTaggedToFloat64WithNaNConstant1) { Reduce(graph()->NewNode(simplified()->ChangeTaggedToFloat64(), NumberConstant(-base::OS::nan_value()))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat64Constant(IsNaN())); + EXPECT_THAT(reduction.replacement(), + IsFloat64Constant(BitEq(-base::OS::nan_value()))); } @@ -351,7 +351,8 @@ TEST_F(SimplifiedOperatorReducerTest, ChangeTaggedToFloat64WithNaNConstant2) { Reduce(graph()->NewNode(simplified()->ChangeTaggedToFloat64(), NumberConstant(base::OS::nan_value()))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsFloat64Constant(IsNaN())); + EXPECT_THAT(reduction.replacement(), + IsFloat64Constant(BitEq(base::OS::nan_value()))); } @@ -474,7 +475,7 @@ TEST_F(SimplifiedOperatorReducerTest, ChangeUint32ToTagged) { Reduce(graph()->NewNode(simplified()->ChangeUint32ToTagged(), Int32Constant(bit_cast(n)))); ASSERT_TRUE(reduction.Changed()); - EXPECT_THAT(reduction.replacement(), IsNumberConstant(FastUI2D(n))); + EXPECT_THAT(reduction.replacement(), IsNumberConstant(BitEq(FastUI2D(n)))); } } diff --git a/testing/gmock-support.h b/testing/gmock-support.h index 44348b60e0..ad39e9269b 100644 --- a/testing/gmock-support.h +++ b/testing/gmock-support.h @@ -5,6 +5,8 @@ #ifndef V8_TESTING_GMOCK_SUPPORT_H_ #define V8_TESTING_GMOCK_SUPPORT_H_ +#include + #include "testing/gmock/include/gmock/gmock.h" namespace testing { @@ -31,6 +33,25 @@ class Capture { namespace internal { +struct AnyBitEq { + template + bool operator()(A const& a, B const& b) const { + if (sizeof(A) != sizeof(B)) return false; + return std::memcmp(&a, &b, sizeof(A)) == 0; + } +}; + + +template +class BitEqMatcher : public ComparisonBase, Rhs, AnyBitEq> { + public: + explicit BitEqMatcher(Rhs const& rhs) + : ComparisonBase, Rhs, AnyBitEq>(rhs) {} + static const char* Desc() { return "is bitwise equal to"; } + static const char* NegatedDesc() { return "isn't bitwise equal to"; } +}; + + template class CaptureEqMatcher : public MatcherInterface { public: @@ -60,10 +81,18 @@ class CaptureEqMatcher : public MatcherInterface { } // namespace internal +// Creates a polymorphic matcher that matches anything whose bit representation +// is equal to that of x. +template +inline internal::BitEqMatcher BitEq(T const& x) { + return internal::BitEqMatcher(x); +} + + // CaptureEq(capture) captures the value passed in during matching as long as it // is unset, and once set, compares the value for equality with the argument. template -Matcher CaptureEq(Capture* capture) { +inline Matcher CaptureEq(Capture* capture) { return MakeMatcher(new internal::CaptureEqMatcher(capture)); }