From ed9df8621a85329d9cb607e982a6efb2d9c670f5 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Mon, 1 Jun 2020 10:09:58 +0000 Subject: [PATCH] [FileCheck] Implement equality operators for ExpressionValue. Subscribers: hiraditya, thopre, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D81094 --- llvm/lib/Support/FileCheckImpl.h | 17 +++++++++++--- llvm/unittests/Support/FileCheckTest.cpp | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Support/FileCheckImpl.h b/llvm/lib/Support/FileCheckImpl.h index 068de3d..6998df3 100644 --- a/llvm/lib/Support/FileCheckImpl.h +++ b/llvm/lib/Support/FileCheckImpl.h @@ -64,8 +64,8 @@ public: return Value != Kind::NoFormat && Value == Other.Value; } - bool operator!=(const ExpressionFormat &other) const { - return !(*this == other); + bool operator!=(const ExpressionFormat &Other) const { + return !(*this == Other); } bool operator==(Kind OtherValue) const { return Value == OtherValue; } @@ -119,8 +119,19 @@ public: template explicit ExpressionValue(T Val) : Value(Val), Negative(Val < 0) {} + bool operator==(const ExpressionValue &Other) const { + return Value == Other.Value && isNegative() == Other.isNegative(); + } + + bool operator!=(const ExpressionValue &Other) const { + return !(*this == Other); + } + /// Returns true if value is signed and negative, false otherwise. - bool isNegative() const { return Negative; } + bool isNegative() const { + assert((Value != 0 || !Negative) && "Unexpected negative zero!"); + return Negative; + } /// \returns the value as a signed integer or an error if the value is out of /// range. diff --git a/llvm/unittests/Support/FileCheckTest.cpp b/llvm/unittests/Support/FileCheckTest.cpp index 54646a0..c549991 100644 --- a/llvm/unittests/Support/FileCheckTest.cpp +++ b/llvm/unittests/Support/FileCheckTest.cpp @@ -478,6 +478,46 @@ TEST_F(FileCheckTest, ExpressionValueSubtraction) { expectOperationValueResult(operator-, 10, 11, -1); } +TEST_F(FileCheckTest, ExpressionValueEquality) { + // Test negative and positive value. + EXPECT_FALSE(ExpressionValue(5) == ExpressionValue(-3)); + EXPECT_TRUE(ExpressionValue(5) != ExpressionValue(-3)); + EXPECT_FALSE(ExpressionValue(-2) == ExpressionValue(6)); + EXPECT_TRUE(ExpressionValue(-2) != ExpressionValue(6)); + EXPECT_FALSE(ExpressionValue(-7) == ExpressionValue(7)); + EXPECT_TRUE(ExpressionValue(-7) != ExpressionValue(7)); + EXPECT_FALSE(ExpressionValue(4) == ExpressionValue(-4)); + EXPECT_TRUE(ExpressionValue(4) != ExpressionValue(-4)); + EXPECT_FALSE(ExpressionValue(MaxUint64) == ExpressionValue(-1)); + EXPECT_TRUE(ExpressionValue(MaxUint64) != ExpressionValue(-1)); + + // Test both negative values. + EXPECT_FALSE(ExpressionValue(-2) == ExpressionValue(-7)); + EXPECT_TRUE(ExpressionValue(-2) != ExpressionValue(-7)); + EXPECT_TRUE(ExpressionValue(-3) == ExpressionValue(-3)); + EXPECT_FALSE(ExpressionValue(-3) != ExpressionValue(-3)); + EXPECT_FALSE(ExpressionValue(MinInt64) == ExpressionValue(-1)); + EXPECT_TRUE(ExpressionValue(MinInt64) != ExpressionValue(-1)); + EXPECT_FALSE(ExpressionValue(MinInt64) == ExpressionValue(-0)); + EXPECT_TRUE(ExpressionValue(MinInt64) != ExpressionValue(-0)); + + // Test both positive values. + EXPECT_FALSE(ExpressionValue(8) == ExpressionValue(9)); + EXPECT_TRUE(ExpressionValue(8) != ExpressionValue(9)); + EXPECT_TRUE(ExpressionValue(1) == ExpressionValue(1)); + EXPECT_FALSE(ExpressionValue(1) != ExpressionValue(1)); + + // Check the signedness of zero doesn't affect equality. + EXPECT_TRUE(ExpressionValue(0) == ExpressionValue(0)); + EXPECT_FALSE(ExpressionValue(0) != ExpressionValue(0)); + EXPECT_TRUE(ExpressionValue(0) == ExpressionValue(-0)); + EXPECT_FALSE(ExpressionValue(0) != ExpressionValue(-0)); + EXPECT_TRUE(ExpressionValue(-0) == ExpressionValue(0)); + EXPECT_FALSE(ExpressionValue(-0) != ExpressionValue(0)); + EXPECT_TRUE(ExpressionValue(-0) == ExpressionValue(-0)); + EXPECT_FALSE(ExpressionValue(-0) != ExpressionValue(-0)); +} + TEST_F(FileCheckTest, Literal) { SourceMgr SM; -- 2.7.4