94 lines · cpp
1//===- InstructionCostTest.cpp - InstructionCost tests --------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/Support/InstructionCost.h"10#include "gtest/gtest.h"11 12using namespace llvm;13 14namespace {15 16struct CostTest : public testing::Test {17 CostTest() = default;18};19 20} // namespace21 22TEST_F(CostTest, DefaultCtor) {23 InstructionCost DefaultCost;24 25 ASSERT_TRUE(DefaultCost.isValid());26 EXPECT_EQ(DefaultCost.getValue(), 0);27}28 29TEST_F(CostTest, Operators) {30 31 InstructionCost VThree = 3;32 InstructionCost VNegTwo = -2;33 InstructionCost VSix = 6;34 InstructionCost IThreeA = InstructionCost::getInvalid(3);35 InstructionCost IThreeB = InstructionCost::getInvalid(3);36 InstructionCost ITwo = InstructionCost::getInvalid(2);37 InstructionCost TmpCost;38 39 EXPECT_NE(VThree, VNegTwo);40 EXPECT_GT(VThree, VNegTwo);41 EXPECT_NE(VThree, IThreeA);42 EXPECT_EQ(IThreeA, IThreeB);43 EXPECT_GE(IThreeA, VNegTwo);44 EXPECT_LT(VSix, IThreeA);45 EXPECT_LT(VThree, ITwo);46 EXPECT_GE(ITwo, VThree);47 EXPECT_EQ(VSix - IThreeA, IThreeB);48 EXPECT_EQ(VThree - VNegTwo, 5);49 EXPECT_EQ(VThree * VNegTwo, -6);50 EXPECT_EQ(VSix / VThree, 2);51 EXPECT_NE(IThreeA, ITwo);52 EXPECT_LT(ITwo, IThreeA);53 EXPECT_GT(IThreeA, ITwo);54 55 EXPECT_FALSE(IThreeA.isValid());56 EXPECT_EQ(IThreeA.getState(), InstructionCost::Invalid);57 58 TmpCost = VThree + IThreeA;59 EXPECT_FALSE(TmpCost.isValid());60 61 // Test increments, decrements62 EXPECT_EQ(++VThree, 4);63 EXPECT_EQ(VThree++, 4);64 EXPECT_EQ(VThree, 5);65 EXPECT_EQ(--VThree, 4);66 EXPECT_EQ(VThree--, 4);67 EXPECT_EQ(VThree, 3);68 69 TmpCost = VThree * IThreeA;70 EXPECT_FALSE(TmpCost.isValid());71 72 // Test value extraction73 EXPECT_EQ(VThree.getValue(), 3);74 75 EXPECT_EQ(std::min(VThree, VNegTwo), -2);76 EXPECT_EQ(std::max(VThree, VSix), 6);77 78 // Test saturation79 auto Max = InstructionCost::getMax();80 auto Min = InstructionCost::getMin();81 auto MinusOne = InstructionCost(-1);82 auto MinusTwo = InstructionCost(-2);83 auto One = InstructionCost(1);84 auto Two = InstructionCost(2);85 EXPECT_EQ(Max + One, Max);86 EXPECT_EQ(Min + MinusOne, Min);87 EXPECT_EQ(Min - One, Min);88 EXPECT_EQ(Max - MinusOne, Max);89 EXPECT_EQ(Max * Two, Max);90 EXPECT_EQ(Min * Two, Min);91 EXPECT_EQ(Max * MinusTwo, Min);92 EXPECT_EQ(Min * MinusTwo, Max);93}94