diff --git a/llvm/unittests/Support/FileCheckTest.cpp b/llvm/unittests/Support/FileCheckTest.cpp --- a/llvm/unittests/Support/FileCheckTest.cpp +++ b/llvm/unittests/Support/FileCheckTest.cpp @@ -293,191 +293,300 @@ EXPECT_FALSE(NoFormat != ExpressionFormat::Kind::NoFormat); } -template -static Expected doValueOperation(binop_eval_t Operation, - T1 LeftValue, T2 RightValue) { - ExpressionValue LeftOperand(LeftValue); - ExpressionValue RightOperand(RightValue); - return Operation(LeftOperand, RightOperand); -} +const int64_t MinInt64 = std::numeric_limits::min(); -template -static void expectValueEqual(ExpressionValue ActualValue, T ExpectedValue) { - EXPECT_EQ(ExpectedValue < 0, ActualValue.isNegative()); - if (ExpectedValue < 0) { - Expected SignedActualValue = ActualValue.getSignedValue(); - ASSERT_THAT_EXPECTED(SignedActualValue, Succeeded()); - EXPECT_EQ(*SignedActualValue, static_cast(ExpectedValue)); - } else { - Expected UnsignedActualValue = ActualValue.getUnsignedValue(); - ASSERT_THAT_EXPECTED(UnsignedActualValue, Succeeded()); - EXPECT_EQ(*UnsignedActualValue, static_cast(ExpectedValue)); +template struct ExpressionValueValidGetterFixture { + ExpressionValue InitialValue{0}; + T ExpectedValue; + + void expectValue(Expected (ExpressionValue::*Getter)() const) { + Expected RawValue = (this->InitialValue.*Getter)(); + ASSERT_THAT_EXPECTED(RawValue, Succeeded()); + EXPECT_EQ(*RawValue, ExpectedValue); } -} +}; + +struct ExpressionValueValidGetUnsignedFixture + : ExpressionValueValidGetterFixture, + public ::testing::TestWithParam> { + void SetUp() { std::tie(InitialValue, ExpectedValue) = GetParam(); } +}; -template -static void expectOperationValueResult(binop_eval_t Operation, T1 LeftValue, - T2 RightValue, TR ResultValue) { - Expected OperationResult = - doValueOperation(Operation, LeftValue, RightValue); - ASSERT_THAT_EXPECTED(OperationResult, Succeeded()); - expectValueEqual(*OperationResult, ResultValue); +TEST_P(ExpressionValueValidGetUnsignedFixture, ValidGetUnsigned) { + expectValue(&ExpressionValue::getUnsignedValue); } -template -static void expectOperationValueResult(binop_eval_t Operation, T1 LeftValue, - T2 RightValue) { - expectError( - "overflow error", - doValueOperation(Operation, LeftValue, RightValue).takeError()); +INSTANTIATE_TEST_CASE_P(ValidUnsignedGetters, + ExpressionValueValidGetUnsignedFixture, + ::testing::Values( + // Test positive value. + std::make_tuple(10, 10), + + // Test 0. + std::make_tuple(0, 0), + + // Test max uint64_t. + std::make_tuple(MaxUint64, MaxUint64)), ); + +template struct ExpressionValueInvalidGetterFixture { + ExpressionValue InitialValue{0}; + void expectGetterOverflow(Expected (ExpressionValue::*Getter)() const) { + expectError("overflow error", + (this->InitialValue.*Getter)().takeError()); + } +}; + +struct ExpressionValueInvalidGetUnsignedFixture + : ExpressionValueInvalidGetterFixture, + public ::testing::TestWithParam { + void SetUp() { InitialValue = GetParam(); } +}; + +TEST_P(ExpressionValueInvalidGetUnsignedFixture, InvalidGetUnsigned) { + expectGetterOverflow(&ExpressionValue::getUnsignedValue); } -const int64_t MinInt64 = std::numeric_limits::min(); +INSTANTIATE_TEST_CASE_P(InvalidUnsignedGetters, + ExpressionValueInvalidGetUnsignedFixture, + ::testing::Values( + // Test failure with negative value. + -1, + + // Test failure with min negative value. + MinInt64), ); + const int64_t MaxInt64 = std::numeric_limits::max(); -TEST_F(FileCheckTest, ExpressionValueGetUnsigned) { - // Test positive value. - Expected UnsignedValue = ExpressionValue(10).getUnsignedValue(); - ASSERT_THAT_EXPECTED(UnsignedValue, Succeeded()); - EXPECT_EQ(*UnsignedValue, 10U); - - // Test 0. - UnsignedValue = ExpressionValue(0).getUnsignedValue(); - ASSERT_THAT_EXPECTED(UnsignedValue, Succeeded()); - EXPECT_EQ(*UnsignedValue, 0U); - - // Test max positive value. - UnsignedValue = ExpressionValue(MaxUint64).getUnsignedValue(); - ASSERT_THAT_EXPECTED(UnsignedValue, Succeeded()); - EXPECT_EQ(*UnsignedValue, MaxUint64); - - // Test failure with negative value. - expectError( - "overflow error", ExpressionValue(-1).getUnsignedValue().takeError()); - - // Test failure with min negative value. - expectError( - "overflow error", - ExpressionValue(MinInt64).getUnsignedValue().takeError()); -} +struct ExpressionValueValidGetSignedFixture + : ExpressionValueValidGetterFixture, + public ::testing::TestWithParam> { + void SetUp() { std::tie(InitialValue, ExpectedValue) = GetParam(); } +}; -TEST_F(FileCheckTest, ExpressionValueGetSigned) { - // Test positive value. - Expected SignedValue = ExpressionValue(10).getSignedValue(); - ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); - EXPECT_EQ(*SignedValue, 10); - - // Test 0. - SignedValue = ExpressionValue(0).getSignedValue(); - ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); - EXPECT_EQ(*SignedValue, 0); - - // Test max int64_t. - SignedValue = ExpressionValue(MaxInt64).getSignedValue(); - ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); - EXPECT_EQ(*SignedValue, MaxInt64); - - // Test failure with too big positive value. - expectError( - "overflow error", ExpressionValue(static_cast(MaxInt64) + 1) - .getSignedValue() - .takeError()); - - // Test failure with max uint64_t. - expectError( - "overflow error", - ExpressionValue(MaxUint64).getSignedValue().takeError()); - - // Test negative value. - SignedValue = ExpressionValue(-10).getSignedValue(); - ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); - EXPECT_EQ(*SignedValue, -10); - - // Test min int64_t. - SignedValue = ExpressionValue(MinInt64).getSignedValue(); - ASSERT_THAT_EXPECTED(SignedValue, Succeeded()); - EXPECT_EQ(*SignedValue, MinInt64); +TEST_P(ExpressionValueValidGetSignedFixture, ValidGetSigned) { + expectValue(&ExpressionValue::getSignedValue); } -TEST_F(FileCheckTest, ExpressionValueAbsolute) { - // Test positive value. - expectValueEqual(ExpressionValue(10).getAbsolute(), 10); +INSTANTIATE_TEST_CASE_P(ValidSignedGetters, + ExpressionValueValidGetSignedFixture, + ::testing::Values( + // Test positive value. + std::make_tuple(10, 10), + + // Test 0. + std::make_tuple(0, 0), + + // Test max int64_t. + std::make_tuple(MaxInt64, MaxInt64), + + // Test negative value. + std::make_tuple(-1, -1), std::make_tuple(-10, -10), - // Test 0. - expectValueEqual(ExpressionValue(0).getAbsolute(), 0); + // Test min int64_t. + std::make_tuple(MinInt64, MinInt64)), ); + +struct ExpressionValueInvalidGetSignedFixture + : ExpressionValueInvalidGetterFixture, + public ::testing::TestWithParam { + void SetUp() { InitialValue = GetParam(); } +}; + +TEST_P(ExpressionValueInvalidGetSignedFixture, InvalidGetSigned) { + expectGetterOverflow(&ExpressionValue::getSignedValue); +} + +INSTANTIATE_TEST_CASE_P(InvalidSignedGetters, + ExpressionValueInvalidGetSignedFixture, + ::testing::Values( + // Test failure with too big positive value. + static_cast(MaxInt64) + 1, + + // Test failure with max uint64_t. + MaxUint64), ); + +struct ExpressionValueResultFixtureBase { + ExpressionValue ExpectedResult{0}; + + void expectResult(ExpressionValue ActualResult) { + EXPECT_EQ(ExpectedResult.isNegative(), ActualResult.isNegative()); + if (ExpectedResult.isNegative()) { + Expected SignedExpectedValue = ExpectedResult.getSignedValue(); + ASSERT_THAT_EXPECTED(SignedExpectedValue, Succeeded()); + Expected SignedActualValue = ActualResult.getSignedValue(); + ASSERT_THAT_EXPECTED(SignedActualValue, Succeeded()); + EXPECT_EQ(*SignedExpectedValue, *SignedActualValue); + } else { + Expected UnsignedExpectedValue = + ExpectedResult.getUnsignedValue(); + ASSERT_THAT_EXPECTED(UnsignedExpectedValue, Succeeded()); + Expected UnsignedActualValue = ActualResult.getUnsignedValue(); + ASSERT_THAT_EXPECTED(UnsignedActualValue, Succeeded()); + EXPECT_EQ(*UnsignedExpectedValue, *UnsignedActualValue); + } + } +}; - // Test max uint64_t. - expectValueEqual(ExpressionValue(MaxUint64).getAbsolute(), MaxUint64); +struct ExpressionValueAbsoluteFixture + : ExpressionValueResultFixtureBase, + public ::testing::TestWithParam< + std::tuple> { + void SetUp() { std::tie(InitialValue, ExpectedResult) = GetParam(); } - // Test negative value. - expectValueEqual(ExpressionValue(-10).getAbsolute(), 10); + ExpressionValue InitialValue{0}; +}; - // Test absence of overflow on min int64_t. - expectValueEqual(ExpressionValue(MinInt64).getAbsolute(), - static_cast(-(MinInt64 + 10)) + 10); +TEST_P(ExpressionValueAbsoluteFixture, Absolute) { + expectResult(InitialValue.getAbsolute()); } -TEST_F(FileCheckTest, ExpressionValueAddition) { - // Test both negative values. - expectOperationValueResult(operator+, -10, -10, -20); +INSTANTIATE_TEST_CASE_P( + Absolutes, ExpressionValueAbsoluteFixture, + ::testing::Values( + // Test positive value. + std::make_tuple(10, 10), + + // Test 0. + std::make_tuple(0, 0), + + // Test max uint64_t. + std::make_tuple(MaxUint64, MaxUint64), + + // Test negative value. + std::make_tuple(-10, 10), + + // Test absence of overflow on min int64_t. + std::make_tuple(MinInt64, + static_cast(-(MinInt64 + 10)) + 10)), ); + +struct ExpressionValueOperationFixtureBase { + ExpressionValue LeftOperand{0}; + ExpressionValue RightOperand{0}; + + Expected doOperation(binop_eval_t Operation) { + return Operation(LeftOperand, RightOperand); + } + + void expectOperationOverflow(binop_eval_t Operation) { + expectError("overflow error", + doOperation(Operation).takeError()); + } +}; + +struct ExpressionValueValidOperationFixture + : ExpressionValueOperationFixtureBase, + ExpressionValueResultFixtureBase, + public ::testing::TestWithParam< + std::tuple> { + void SetUp() { + std::tie(LeftOperand, RightOperand, ExpectedResult) = GetParam(); + } + + void expectOperationResult(binop_eval_t Operation) { + Expected OperationResult = doOperation(Operation); + ASSERT_THAT_EXPECTED(OperationResult, Succeeded()); + expectResult(*OperationResult); + } +}; - // Test both negative values with underflow. - expectOperationValueResult(operator+, MinInt64, -1); - expectOperationValueResult(operator+, MinInt64, MinInt64); +struct ExpressionValueValidAdditionFixture + : ExpressionValueValidOperationFixture {}; - // Test negative and positive value. - expectOperationValueResult(operator+, -10, 10, 0); - expectOperationValueResult(operator+, -10, 11, 1); - expectOperationValueResult(operator+, -11, 10, -1); +TEST_P(ExpressionValueValidAdditionFixture, ValidAddition) { + expectOperationResult(operator+); +} - // Test positive and negative value. - expectOperationValueResult(operator+, 10, -10, 0); - expectOperationValueResult(operator+, 10, -11, -1); - expectOperationValueResult(operator+, 11, -10, 1); +INSTANTIATE_TEST_CASE_P(ValidAdditions, ExpressionValueValidAdditionFixture, + ::testing::Values( + // Test both negative values. + std::make_tuple(-10, -10, -20), + + // Test negative and positive values. + std::make_tuple(-10, 10, 0), + std::make_tuple(-10, 11, 1), + std::make_tuple(-11, 10, -1), + + // Test positive and negative values. + std::make_tuple(10, -10, 0), + std::make_tuple(10, -11, -1), + std::make_tuple(11, -10, 1), + + // Test both positive values. + std::make_tuple(10, 10, 20)), ); + +struct ExpressionValueInvalidOperationFixture + : ExpressionValueOperationFixtureBase, + public ::testing::TestWithParam< + std::tuple> { + void SetUp() { std::tie(LeftOperand, RightOperand) = GetParam(); } +}; - // Test both positive values. - expectOperationValueResult(operator+, 10, 10, 20); +struct ExpressionValueInvalidAdditionFixture + : ExpressionValueInvalidOperationFixture {}; - // Test both positive values with overflow. - expectOperationValueResult(operator+, MaxUint64, 1); - expectOperationValueResult(operator+, MaxUint64, MaxUint64); +TEST_P(ExpressionValueInvalidAdditionFixture, InvalidAddition) { + expectOperationOverflow(operator+); } -TEST_F(FileCheckTest, ExpressionValueSubtraction) { - // Test negative value and value bigger than int64_t max. - expectOperationValueResult(operator-, -10, MaxUint64); +INSTANTIATE_TEST_CASE_P(InvalidAdditions, ExpressionValueInvalidAdditionFixture, + ::testing::Values( + // Test both negative values with underflow. + std::make_tuple(MinInt64, -1), + std::make_tuple(MinInt64, MinInt64), + + // Test both positive values with overflow. + std::make_tuple(MaxUint64, 1), + std::make_tuple(MaxUint64, MaxUint64)), ); + +struct ExpressionValueValidSubtractionFixture + : ExpressionValueValidOperationFixture {}; + +TEST_P(ExpressionValueValidSubtractionFixture, ValidSubtraction) { + expectOperationResult(operator-); +} - // Test negative and positive value with underflow. - expectOperationValueResult(operator-, MinInt64, 1); +INSTANTIATE_TEST_CASE_P( + ValidSubtractions, ExpressionValueValidSubtractionFixture, + ::testing::Values( + // Test negative and positive values. + std::make_tuple(-10, 10, -20), - // Test negative and positive value. - expectOperationValueResult(operator-, -10, 10, -20); + // Test both negative values. + std::make_tuple(-10, -10, 0), std::make_tuple(-11, -10, -1), + std::make_tuple(-10, -11, 1), - // Test both negative values. - expectOperationValueResult(operator-, -10, -10, 0); - expectOperationValueResult(operator-, -11, -10, -1); - expectOperationValueResult(operator-, -10, -11, 1); + // Test positive and negative values. + std::make_tuple(10, -10, 20), - // Test positive and negative values. - expectOperationValueResult(operator-, 10, -10, 20); + // Test both positive values with result positive. + std::make_tuple(10, 5, 5), - // Test both positive values with result positive. - expectOperationValueResult(operator-, 10, 5, 5); + // Test both positive values with result < -(max int64_t) + std::make_tuple(10, static_cast(MaxInt64) + 11, + -MaxInt64 - 1), - // Test both positive values with underflow. - expectOperationValueResult(operator-, 0, MaxUint64); - expectOperationValueResult(operator-, 0, - static_cast(-(MinInt64 + 10)) + 11); + // Test both positive values with 0 > result > -(max int64_t) + std::make_tuple(10, 11, -1)), ); - // Test both positive values with result < -(max int64_t) - expectOperationValueResult(operator-, 10, - static_cast(MaxInt64) + 11, - -MaxInt64 - 1); +struct ExpressionValueInvalidSubtractionFixture + : ExpressionValueInvalidOperationFixture {}; - // Test both positive values with 0 > result > -(max int64_t) - expectOperationValueResult(operator-, 10, 11, -1); +TEST_P(ExpressionValueInvalidSubtractionFixture, InvalidSubtraction) { + expectOperationOverflow(operator-); } +INSTANTIATE_TEST_CASE_P( + InvalidSubtractions, ExpressionValueInvalidSubtractionFixture, + ::testing::Values( + // Test negative value and value bigger than int64_t max. + std::make_tuple(-10, MaxUint64), + + // Test negative and positive values with underflow. + std::make_tuple(MinInt64, 1), + + // Test both positive values with underflow. + std::make_tuple(0, MaxUint64), + std::make_tuple(0, static_cast(-(MinInt64 + 10)) + 11)), ); + TEST_F(FileCheckTest, Literal) { SourceMgr SM;