edfef63a377bd88cee6fad0280035c366891aad6
[oota-llvm.git] / unittests / IR / UseTest.cpp
1 //===- llvm/unittest/IR/UseTest.cpp - Use 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/AsmParser/Parser.h"
11 #include "llvm/IR/Function.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/IR/User.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gtest/gtest.h"
18 #include <string>
19
20 using namespace llvm;
21
22 namespace {
23
24 TEST(UseTest, sort) {
25   LLVMContext C;
26
27   const char *ModuleString = "define void @f(i32 %x) {\n"
28                              "entry:\n"
29                              "  %v0 = add i32 %x, 0\n"
30                              "  %v2 = add i32 %x, 2\n"
31                              "  %v5 = add i32 %x, 5\n"
32                              "  %v1 = add i32 %x, 1\n"
33                              "  %v3 = add i32 %x, 3\n"
34                              "  %v7 = add i32 %x, 7\n"
35                              "  %v6 = add i32 %x, 6\n"
36                              "  %v4 = add i32 %x, 4\n"
37                              "  ret void\n"
38                              "}\n";
39   SMDiagnostic Err;
40   Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
41   Function *F = M->getFunction("f");
42   ASSERT_TRUE(F);
43   ASSERT_TRUE(F->arg_begin() != F->arg_end());
44   Argument &X = *F->arg_begin();
45   ASSERT_EQ("x", X.getName());
46
47   X.sortUseList([](const Use &L, const Use &R) {
48     return L.getUser()->getName() < R.getUser()->getName();
49   });
50   unsigned I = 0;
51   for (User *U : X.users())
52     EXPECT_EQ("v" + std::to_string(I++), U->getName());
53   ASSERT_EQ(8u, I);
54
55   X.sortUseList([](const Use &L, const Use &R) {
56     return L.getUser()->getName() > R.getUser()->getName();
57   });
58   I = 0;
59   for (User *U : X.users())
60     EXPECT_EQ("v" + std::to_string((7 - I++)), U->getName());
61   ASSERT_EQ(8u, I);
62 }
63
64 } // end anonymous namespace