blob: af55a098adda031788dd46e7f2277cf021687d89 [file] [log] [blame]
Duncan P. N. Exon Smithf1f27bb2016-08-30 16:23:55 +00001//===- unittests/IR/ModuleTest.cpp - Module unit tests --------------------===//
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
Duncan P. N. Exon Smithf1f27bb2016-08-30 16:23:55 +000010#include "llvm/IR/Module.h"
Chandler Carruth3c0d6072017-06-06 11:06:56 +000011#include "llvm/IR/GlobalVariable.h"
Mehdi Amini6a59bc72016-10-11 07:13:01 +000012#include "llvm/Support/RandomNumberGenerator.h"
Duncan P. N. Exon Smithf1f27bb2016-08-30 16:23:55 +000013#include "gtest/gtest.h"
14
Mehdi Amini6a59bc72016-10-11 07:13:01 +000015#include <random>
16
Duncan P. N. Exon Smithf1f27bb2016-08-30 16:23:55 +000017using namespace llvm;
18
19namespace {
20
21bool sortByName(const GlobalVariable &L, const GlobalVariable &R) {
22 return L.getName() < R.getName();
23}
24
25bool sortByNameReverse(const GlobalVariable &L, const GlobalVariable &R) {
26 return sortByName(R, L);
27}
28
29TEST(ModuleTest, sortGlobalsByName) {
30 LLVMContext Context;
Duncan P. N. Exon Smithb161c802016-08-30 17:34:58 +000031 for (auto compare : {&sortByName, &sortByNameReverse}) {
Duncan P. N. Exon Smithf1f27bb2016-08-30 16:23:55 +000032 Module M("M", Context);
33 Type *T = Type::getInt8Ty(Context);
34 GlobalValue::LinkageTypes L = GlobalValue::ExternalLinkage;
35 (void)new GlobalVariable(M, T, false, L, nullptr, "A");
36 (void)new GlobalVariable(M, T, false, L, nullptr, "F");
37 (void)new GlobalVariable(M, T, false, L, nullptr, "G");
38 (void)new GlobalVariable(M, T, false, L, nullptr, "E");
39 (void)new GlobalVariable(M, T, false, L, nullptr, "B");
40 (void)new GlobalVariable(M, T, false, L, nullptr, "H");
41 (void)new GlobalVariable(M, T, false, L, nullptr, "C");
42 (void)new GlobalVariable(M, T, false, L, nullptr, "D");
43
44 // Sort the globals by name.
45 EXPECT_FALSE(std::is_sorted(M.global_begin(), M.global_end(), compare));
46 M.getGlobalList().sort(compare);
47 EXPECT_TRUE(std::is_sorted(M.global_begin(), M.global_end(), compare));
48 }
49}
50
Mehdi Amini6a59bc72016-10-11 07:13:01 +000051TEST(ModuleTest, randomNumberGenerator) {
52 LLVMContext Context;
53 static char ID;
54 struct DummyPass : ModulePass {
55 DummyPass() : ModulePass(ID) {}
56 bool runOnModule(Module &) { return true; }
57 } DP;
58
59 Module M("R", Context);
60
61 std::uniform_int_distribution<int> dist;
Reid Kleckneref587f52016-10-11 18:35:13 +000062 const size_t NBCheck = 10;
Mehdi Amini6a59bc72016-10-11 07:13:01 +000063
64 std::array<int, NBCheck> RandomStreams[2];
65 for (auto &RandomStream : RandomStreams) {
Serge Gueltonfbec1c92017-07-12 08:03:44 +000066 std::unique_ptr<RandomNumberGenerator> RNG = M.createRNG(&DP);
Mehdi Amini6a59bc72016-10-11 07:13:01 +000067 std::generate(RandomStream.begin(), RandomStream.end(),
68 [&]() { return dist(*RNG); });
69 }
70
71 EXPECT_TRUE(std::equal(RandomStreams[0].begin(), RandomStreams[0].end(),
72 RandomStreams[1].begin()));
73}
74
Duncan P. N. Exon Smithf1f27bb2016-08-30 16:23:55 +000075} // end namespace