From d8f4993cc718d2b6dd0bfa1be37a48e8c7eb62e1 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Wed, 26 Mar 2014 21:46:24 +0000 Subject: Add a unit test for Invoke iteration, similar to the one for Call The tests are refactored to use the same fixture. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204860 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/IR/InstructionsTest.cpp | 51 ++++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 11 deletions(-) (limited to 'unittests') diff --git a/unittests/IR/InstructionsTest.cpp b/unittests/IR/InstructionsTest.cpp index 39a7d77a10..e355d16d7e 100644 --- a/unittests/IR/InstructionsTest.cpp +++ b/unittests/IR/InstructionsTest.cpp @@ -50,29 +50,58 @@ TEST(InstructionsTest, ReturnInst) { delete r1; } -TEST(InstructionsTest, CallInst) { - LLVMContext &C(getGlobalContext()); - std::unique_ptr M(new Module("MyModule", C)); +// Test fixture that provides a module and a single function within it. Useful +// for tests that need to refer to the function in some way. +class ModuleWithFunctionTest : public testing::Test { +protected: + ModuleWithFunctionTest() + : M(new Module("MyModule", Ctx)), + FArgTypes{Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), + Type::getInt64Ty(Ctx)} { + FunctionType *FTy = + FunctionType::get(Type::getVoidTy(Ctx), FArgTypes, false); + F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); + } - Type *ArgTypes[] = {Type::getInt8Ty(C), Type::getInt32Ty(C), - Type::getInt64Ty(C)}; - FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), ArgTypes, false); - Function *F = Function::Create(FTy, Function::ExternalLinkage, "", M.get()); + LLVMContext Ctx; + std::unique_ptr M; + std::vector FArgTypes; + Function *F; +}; - Value *Args[] = {ConstantInt::get(Type::getInt8Ty(C), 20), - ConstantInt::get(Type::getInt32Ty(C), 9999), - ConstantInt::get(Type::getInt64Ty(C), 42)}; +TEST_F(ModuleWithFunctionTest, CallInst) { + Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), + ConstantInt::get(Type::getInt32Ty(Ctx), 9999), + ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; std::unique_ptr Call(CallInst::Create(F, Args)); // Make sure iteration over a call's arguments works as expected. unsigned Idx = 0; for (Value *Arg : Call->arg_operands()) { - EXPECT_EQ(ArgTypes[Idx], Arg->getType()); + EXPECT_EQ(FArgTypes[Idx], Arg->getType()); EXPECT_EQ(Call->getArgOperand(Idx)->getType(), Arg->getType()); Idx++; } } +TEST_F(ModuleWithFunctionTest, InvokeInst) { + BasicBlock *BB1 = BasicBlock::Create(Ctx, "", F); + BasicBlock *BB2 = BasicBlock::Create(Ctx, "", F); + + Value *Args[] = {ConstantInt::get(Type::getInt8Ty(Ctx), 20), + ConstantInt::get(Type::getInt32Ty(Ctx), 9999), + ConstantInt::get(Type::getInt64Ty(Ctx), 42)}; + std::unique_ptr Invoke(InvokeInst::Create(F, BB1, BB2, Args)); + + // Make sure iteration over invoke's arguments works as expected. + unsigned Idx = 0; + for (Value *Arg : Invoke->arg_operands()) { + EXPECT_EQ(FArgTypes[Idx], Arg->getType()); + EXPECT_EQ(Invoke->getArgOperand(Idx)->getType(), Arg->getType()); + Idx++; + } +} + TEST(InstructionsTest, BranchInst) { LLVMContext &C(getGlobalContext()); -- cgit v1.2.3