From c664c2a5ec6db3ad637d4520d2afe15f64796b19 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sun, 7 Apr 2019 18:55:45 +0000 Subject: [PATCH] [ConstantRangeTest] Generalize intersection testing code; NFC Extract the exhaustive intersection tests into a separate function, so that it may be reused for unions as well. llvm-svn: 357874 --- llvm/unittests/IR/ConstantRangeTest.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp index 0b20456664bf..84411076ea30 100644 --- a/llvm/unittests/IR/ConstantRangeTest.cpp +++ b/llvm/unittests/IR/ConstantRangeTest.cpp @@ -363,7 +363,8 @@ TEST_F(ConstantRangeTest, IntersectWith) { EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0))); } -TEST_F(ConstantRangeTest, IntersectWithExhaustive) { +template +void testBinarySetOperationExhaustive(Fn1 OpFn, Fn2 InResultFn) { unsigned Bits = 4; EnumerateTwoConstantRanges(Bits, [=](const ConstantRange &CR1, const ConstantRange &CR2) { @@ -379,7 +380,7 @@ TEST_F(ConstantRangeTest, IntersectWithExhaustive) { APInt Num(Bits, 0); for (unsigned I = 0, Limit = 1 << Bits; I < Limit; ++I, ++Num) { - if (!CR1.contains(Num) || !CR2.contains(Num)) { + if (!InResultFn(CR1, CR2, Num)) { if (HaveRange3) HaveInterrupt3 = true; else if (HaveRange2) @@ -409,12 +410,9 @@ TEST_F(ConstantRangeTest, IntersectWithExhaustive) { assert(!HaveInterrupt3 && "Should have at most three ranges"); - ConstantRange SmallestCR = - CR1.intersectWith(CR2, ConstantRange::Smallest); - ConstantRange UnsignedCR = - CR1.intersectWith(CR2, ConstantRange::Unsigned); - ConstantRange SignedCR = - CR1.intersectWith(CR2, ConstantRange::Signed); + ConstantRange SmallestCR = OpFn(CR1, CR2, ConstantRange::Smallest); + ConstantRange UnsignedCR = OpFn(CR1, CR2, ConstantRange::Unsigned); + ConstantRange SignedCR = OpFn(CR1, CR2, ConstantRange::Signed); if (!HaveRange1) { EXPECT_TRUE(SmallestCR.isEmptySet()); @@ -492,6 +490,17 @@ TEST_F(ConstantRangeTest, IntersectWithExhaustive) { }); } +TEST_F(ConstantRangeTest, IntersectWithExhaustive) { + testBinarySetOperationExhaustive( + [](const ConstantRange &CR1, const ConstantRange &CR2, + ConstantRange::PreferredRangeType Type) { + return CR1.intersectWith(CR2, Type); + }, + [](const ConstantRange &CR1, const ConstantRange &CR2, const APInt &N) { + return CR1.contains(N) && CR2.contains(N); + }); +} + TEST_F(ConstantRangeTest, UnionWith) { EXPECT_EQ(Wrap.unionWith(One), ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb)));