From 63748dd992c6daa7ba676934072476f55f20d3f9 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 23 Apr 2019 18:00:02 +0000 Subject: [PATCH] [ConstantRangeTest] Move helper methods; NFC Move Test(Unsigned|Signed)BinOpExhaustive() towards the top of the file, so they're easier to reuse. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359018 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/IR/ConstantRangeTest.cpp | 108 ++++++++++++++--------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/unittests/IR/ConstantRangeTest.cpp b/unittests/IR/ConstantRangeTest.cpp index 3306fe3ea69..8e378ff3c75 100644 --- a/unittests/IR/ConstantRangeTest.cpp +++ b/unittests/IR/ConstantRangeTest.cpp @@ -58,6 +58,60 @@ static void ForeachNumInConstantRange(const ConstantRange &CR, Fn TestFn) { } } +template +static void TestUnsignedBinOpExhaustive(Fn1 RangeFn, Fn2 IntFn) { + unsigned Bits = 4; + EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, + const ConstantRange &CR2) { + ConstantRange CR = RangeFn(CR1, CR2); + if (CR1.isEmptySet() || CR2.isEmptySet()) { + EXPECT_TRUE(CR.isEmptySet()); + return; + } + + APInt Min = APInt::getMaxValue(Bits); + APInt Max = APInt::getMinValue(Bits); + ForeachNumInConstantRange(CR1, [&](const APInt &N1) { + ForeachNumInConstantRange(CR2, [&](const APInt &N2) { + APInt N = IntFn(N1, N2); + if (N.ult(Min)) + Min = N; + if (N.ugt(Max)) + Max = N; + }); + }); + + EXPECT_EQ(ConstantRange::getNonEmpty(Min, Max + 1), CR); + }); +} + +template +static void TestSignedBinOpExhaustive(Fn1 RangeFn, Fn2 IntFn) { + unsigned Bits = 4; + EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, + const ConstantRange &CR2) { + ConstantRange CR = RangeFn(CR1, CR2); + if (CR1.isEmptySet() || CR2.isEmptySet()) { + EXPECT_TRUE(CR.isEmptySet()); + return; + } + + APInt Min = APInt::getSignedMaxValue(Bits); + APInt Max = APInt::getSignedMinValue(Bits); + ForeachNumInConstantRange(CR1, [&](const APInt &N1) { + ForeachNumInConstantRange(CR2, [&](const APInt &N2) { + APInt N = IntFn(N1, N2); + if (N.slt(Min)) + Min = N; + if (N.sgt(Max)) + Max = N; + }); + }); + + EXPECT_EQ(ConstantRange::getNonEmpty(Min, Max + 1), CR); + }); +} + ConstantRange ConstantRangeTest::Full(16, true); ConstantRange ConstantRangeTest::Empty(16, false); ConstantRange ConstantRangeTest::One(APInt(16, 0xa)); @@ -1647,60 +1701,6 @@ TEST_F(ConstantRangeTest, Negative) { }); } -template -static void TestUnsignedBinOpExhaustive(Fn1 RangeFn, Fn2 IntFn) { - unsigned Bits = 4; - EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, - const ConstantRange &CR2) { - ConstantRange CR = RangeFn(CR1, CR2); - if (CR1.isEmptySet() || CR2.isEmptySet()) { - EXPECT_TRUE(CR.isEmptySet()); - return; - } - - APInt Min = APInt::getMaxValue(Bits); - APInt Max = APInt::getMinValue(Bits); - ForeachNumInConstantRange(CR1, [&](const APInt &N1) { - ForeachNumInConstantRange(CR2, [&](const APInt &N2) { - APInt N = IntFn(N1, N2); - if (N.ult(Min)) - Min = N; - if (N.ugt(Max)) - Max = N; - }); - }); - - EXPECT_EQ(ConstantRange::getNonEmpty(Min, Max + 1), CR); - }); -} - -template -static void TestSignedBinOpExhaustive(Fn1 RangeFn, Fn2 IntFn) { - unsigned Bits = 4; - EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, - const ConstantRange &CR2) { - ConstantRange CR = RangeFn(CR1, CR2); - if (CR1.isEmptySet() || CR2.isEmptySet()) { - EXPECT_TRUE(CR.isEmptySet()); - return; - } - - APInt Min = APInt::getSignedMaxValue(Bits); - APInt Max = APInt::getSignedMinValue(Bits); - ForeachNumInConstantRange(CR1, [&](const APInt &N1) { - ForeachNumInConstantRange(CR2, [&](const APInt &N2) { - APInt N = IntFn(N1, N2); - if (N.slt(Min)) - Min = N; - if (N.sgt(Max)) - Max = N; - }); - }); - - EXPECT_EQ(ConstantRange::getNonEmpty(Min, Max + 1), CR); - }); -} - TEST_F(ConstantRangeTest, UAddSat) { TestUnsignedBinOpExhaustive( [](const ConstantRange &CR1, const ConstantRange &CR2) { -- 2.50.1