blob: 2106e86b59232f21d371c63af75f3f89bcf4818f [file] [log] [blame]
Jeffrey Yasskin0a962312009-08-04 23:53:16 +00001//===- ExecutionEngineTest.cpp - Unit tests for ExecutionEngine -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DerivedTypes.h"
11#include "llvm/GlobalVariable.h"
12#include "llvm/LLVMContext.h"
13#include "llvm/Module.h"
14#include "llvm/ADT/OwningPtr.h"
15#include "llvm/ExecutionEngine/Interpreter.h"
16#include "gtest/gtest.h"
17
18using namespace llvm;
19
20namespace {
21
22class ExecutionEngineTest : public testing::Test {
23protected:
24 ExecutionEngineTest()
25 : M(new Module("<main>", getGlobalContext())),
26 Engine(EngineBuilder(M).create()) {
27 }
28
29 virtual void SetUp() {
30 ASSERT_TRUE(Engine.get() != NULL);
31 }
32
33 GlobalVariable *NewExtGlobal(const Type *T, const Twine &Name) {
34 return new GlobalVariable(*M, T, false, // Not constant.
35 GlobalValue::ExternalLinkage, NULL, Name);
36 }
37
38 Module *const M;
39 const OwningPtr<ExecutionEngine> Engine;
40};
41
42TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
Owen Anderson1d0be152009-08-13 21:58:54 +000043 GlobalVariable *G1 =
44 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000045 int32_t Mem1 = 3;
46 Engine->addGlobalMapping(G1, &Mem1);
47 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
48 int32_t Mem2 = 4;
49 Engine->updateGlobalMapping(G1, &Mem2);
50 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
51 Engine->updateGlobalMapping(G1, NULL);
52 EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G1));
53 Engine->updateGlobalMapping(G1, &Mem2);
54 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
55
Owen Anderson1d0be152009-08-13 21:58:54 +000056 GlobalVariable *G2 =
57 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000058 EXPECT_EQ(NULL, Engine->getPointerToGlobalIfAvailable(G2))
59 << "The NULL return shouldn't depend on having called"
60 << " updateGlobalMapping(..., NULL)";
61 // Check that update...() can be called before add...().
62 Engine->updateGlobalMapping(G2, &Mem1);
63 EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G2));
64 EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1))
65 << "A second mapping shouldn't affect the first.";
66}
67
68TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
Owen Anderson1d0be152009-08-13 21:58:54 +000069 GlobalVariable *G1 =
70 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000071
72 int32_t Mem1 = 3;
73 Engine->addGlobalMapping(G1, &Mem1);
74 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem1));
75 int32_t Mem2 = 4;
76 Engine->updateGlobalMapping(G1, &Mem2);
77 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
78 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
79
Owen Anderson1d0be152009-08-13 21:58:54 +000080 GlobalVariable *G2 =
81 NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
Jeffrey Yasskin0a962312009-08-04 23:53:16 +000082 Engine->updateGlobalMapping(G2, &Mem1);
83 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
84 EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
85 Engine->updateGlobalMapping(G1, NULL);
86 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1))
87 << "Removing one mapping doesn't affect a different one.";
88 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem2));
89 Engine->updateGlobalMapping(G2, &Mem2);
90 EXPECT_EQ(NULL, Engine->getGlobalValueAtAddress(&Mem1));
91 EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem2))
92 << "Once a mapping is removed, we can point another GV at the"
93 << " now-free address.";
94}
95
96}