120
|
1 //===- unittests/IR/ModuleTest.cpp - Module unit tests --------------------===//
|
|
2 //
|
147
|
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-exception
|
120
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8
|
121
|
9 #include "llvm/IR/Module.h"
|
120
|
10 #include "llvm/IR/GlobalVariable.h"
|
|
11 #include "llvm/Support/RandomNumberGenerator.h"
|
|
12 #include "gtest/gtest.h"
|
|
13
|
|
14 #include <random>
|
|
15
|
|
16 using namespace llvm;
|
|
17
|
|
18 namespace {
|
|
19
|
|
20 bool sortByName(const GlobalVariable &L, const GlobalVariable &R) {
|
|
21 return L.getName() < R.getName();
|
|
22 }
|
|
23
|
|
24 bool sortByNameReverse(const GlobalVariable &L, const GlobalVariable &R) {
|
|
25 return sortByName(R, L);
|
|
26 }
|
|
27
|
|
28 TEST(ModuleTest, sortGlobalsByName) {
|
|
29 LLVMContext Context;
|
|
30 for (auto compare : {&sortByName, &sortByNameReverse}) {
|
|
31 Module M("M", Context);
|
|
32 Type *T = Type::getInt8Ty(Context);
|
|
33 GlobalValue::LinkageTypes L = GlobalValue::ExternalLinkage;
|
|
34 (void)new GlobalVariable(M, T, false, L, nullptr, "A");
|
|
35 (void)new GlobalVariable(M, T, false, L, nullptr, "F");
|
|
36 (void)new GlobalVariable(M, T, false, L, nullptr, "G");
|
|
37 (void)new GlobalVariable(M, T, false, L, nullptr, "E");
|
|
38 (void)new GlobalVariable(M, T, false, L, nullptr, "B");
|
|
39 (void)new GlobalVariable(M, T, false, L, nullptr, "H");
|
|
40 (void)new GlobalVariable(M, T, false, L, nullptr, "C");
|
|
41 (void)new GlobalVariable(M, T, false, L, nullptr, "D");
|
|
42
|
|
43 // Sort the globals by name.
|
|
44 EXPECT_FALSE(std::is_sorted(M.global_begin(), M.global_end(), compare));
|
|
45 M.getGlobalList().sort(compare);
|
|
46 EXPECT_TRUE(std::is_sorted(M.global_begin(), M.global_end(), compare));
|
|
47 }
|
|
48 }
|
|
49
|
|
50 TEST(ModuleTest, randomNumberGenerator) {
|
|
51 LLVMContext Context;
|
|
52 static char ID;
|
|
53 struct DummyPass : ModulePass {
|
|
54 DummyPass() : ModulePass(ID) {}
|
|
55 bool runOnModule(Module &) { return true; }
|
|
56 } DP;
|
|
57
|
|
58 Module M("R", Context);
|
|
59
|
|
60 std::uniform_int_distribution<int> dist;
|
|
61 const size_t NBCheck = 10;
|
|
62
|
|
63 std::array<int, NBCheck> RandomStreams[2];
|
|
64 for (auto &RandomStream : RandomStreams) {
|
121
|
65 std::unique_ptr<RandomNumberGenerator> RNG = M.createRNG(&DP);
|
120
|
66 std::generate(RandomStream.begin(), RandomStream.end(),
|
|
67 [&]() { return dist(*RNG); });
|
|
68 }
|
|
69
|
|
70 EXPECT_TRUE(std::equal(RandomStreams[0].begin(), RandomStreams[0].end(),
|
|
71 RandomStreams[1].begin()));
|
|
72 }
|
|
73
|
|
74 } // end namespace
|