1 //===- 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 
10 #include "llvm/IR/GlobalVariable.h"
11 #include "llvm/IR/Module.h"
12 #include "gtest/gtest.h"
13 
14 using namespace llvm;
15 
16 namespace {
17 
18 bool sortByName(const GlobalVariable &L, const GlobalVariable &R) {
19   return L.getName() < R.getName();
20 }
21 
22 bool sortByNameReverse(const GlobalVariable &L, const GlobalVariable &R) {
23   return sortByName(R, L);
24 }
25 
26 TEST(ModuleTest, sortGlobalsByName) {
27   LLVMContext Context;
28   for (auto compare : {&sortByName, &sortByNameReverse}) {
29     Module M("M", Context);
30     Type *T = Type::getInt8Ty(Context);
31     GlobalValue::LinkageTypes L = GlobalValue::ExternalLinkage;
32     (void)new GlobalVariable(M, T, false, L, nullptr, "A");
33     (void)new GlobalVariable(M, T, false, L, nullptr, "F");
34     (void)new GlobalVariable(M, T, false, L, nullptr, "G");
35     (void)new GlobalVariable(M, T, false, L, nullptr, "E");
36     (void)new GlobalVariable(M, T, false, L, nullptr, "B");
37     (void)new GlobalVariable(M, T, false, L, nullptr, "H");
38     (void)new GlobalVariable(M, T, false, L, nullptr, "C");
39     (void)new GlobalVariable(M, T, false, L, nullptr, "D");
40 
41     // Sort the globals by name.
42     EXPECT_FALSE(std::is_sorted(M.global_begin(), M.global_end(), compare));
43     M.getGlobalList().sort(compare);
44     EXPECT_TRUE(std::is_sorted(M.global_begin(), M.global_end(), compare));
45   }
46 }
47 
48 } // end namespace
49