Recommit "rL366894: [yaml2obj] - Allow custom fields for the SHT_UNDEF sections."
[llvm-complete.git] / unittests / IR / ModuleTest.cpp
blobae420bb5406d5eee948b6540b699f4b95eae1891
1 //===- unittests/IR/ModuleTest.cpp - Module unit 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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/Module.h"
10 #include "llvm/IR/GlobalVariable.h"
11 #include "llvm/Support/RandomNumberGenerator.h"
12 #include "gtest/gtest.h"
14 #include <random>
16 using namespace llvm;
18 namespace {
20 bool sortByName(const GlobalVariable &L, const GlobalVariable &R) {
21 return L.getName() < R.getName();
24 bool sortByNameReverse(const GlobalVariable &L, const GlobalVariable &R) {
25 return sortByName(R, L);
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");
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));
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;
58 Module M("R", Context);
60 std::uniform_int_distribution<int> dist;
61 const size_t NBCheck = 10;
63 std::array<int, NBCheck> RandomStreams[2];
64 for (auto &RandomStream : RandomStreams) {
65 std::unique_ptr<RandomNumberGenerator> RNG = M.createRNG(&DP);
66 std::generate(RandomStream.begin(), RandomStream.end(),
67 [&]() { return dist(*RNG); });
70 EXPECT_TRUE(std::equal(RandomStreams[0].begin(), RandomStreams[0].end(),
71 RandomStreams[1].begin()));
74 } // end namespace