llvm-project
110 строк · 3.5 Кб
1//===- llvm/unittest/IR/UseTest.cpp - Use 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//===----------------------------------------------------------------------===//
8
9#include "llvm/AsmParser/Parser.h"10#include "llvm/IR/Function.h"11#include "llvm/IR/LLVMContext.h"12#include "llvm/IR/Module.h"13#include "llvm/IR/User.h"14#include "llvm/Support/Format.h"15#include "llvm/Support/SourceMgr.h"16#include "gtest/gtest.h"17
18using namespace llvm;19
20namespace {21
22TEST(UseTest, sort) {23LLVMContext C;24
25const char *ModuleString = "define void @f(i32 %x) {\n"26"entry:\n"27" %v0 = add i32 %x, 0\n"28" %v2 = add i32 %x, 2\n"29" %v5 = add i32 %x, 5\n"30" %v1 = add i32 %x, 1\n"31" %v3 = add i32 %x, 3\n"32" %v7 = add i32 %x, 7\n"33" %v6 = add i32 %x, 6\n"34" %v4 = add i32 %x, 4\n"35" ret void\n"36"}\n";37SMDiagnostic Err;38char vnbuf[8];39std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);40Function *F = M->getFunction("f");41ASSERT_TRUE(F);42ASSERT_TRUE(F->arg_begin() != F->arg_end());43Argument &X = *F->arg_begin();44ASSERT_EQ("x", X.getName());45
46X.sortUseList([](const Use &L, const Use &R) {47return L.getUser()->getName() < R.getUser()->getName();48});49unsigned I = 0;50for (User *U : X.users()) {51format("v%u", I++).snprint(vnbuf, sizeof(vnbuf));52EXPECT_EQ(vnbuf, U->getName());53}54ASSERT_EQ(8u, I);55
56X.sortUseList([](const Use &L, const Use &R) {57return L.getUser()->getName() > R.getUser()->getName();58});59I = 0;60for (User *U : X.users()) {61format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf));62EXPECT_EQ(vnbuf, U->getName());63}64ASSERT_EQ(8u, I);65}
66
67TEST(UseTest, reverse) {68LLVMContext C;69
70const char *ModuleString = "define void @f(i32 %x) {\n"71"entry:\n"72" %v0 = add i32 %x, 0\n"73" %v2 = add i32 %x, 2\n"74" %v5 = add i32 %x, 5\n"75" %v1 = add i32 %x, 1\n"76" %v3 = add i32 %x, 3\n"77" %v7 = add i32 %x, 7\n"78" %v6 = add i32 %x, 6\n"79" %v4 = add i32 %x, 4\n"80" ret void\n"81"}\n";82SMDiagnostic Err;83char vnbuf[8];84std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);85Function *F = M->getFunction("f");86ASSERT_TRUE(F);87ASSERT_TRUE(F->arg_begin() != F->arg_end());88Argument &X = *F->arg_begin();89ASSERT_EQ("x", X.getName());90
91X.sortUseList([](const Use &L, const Use &R) {92return L.getUser()->getName() < R.getUser()->getName();93});94unsigned I = 0;95for (User *U : X.users()) {96format("v%u", I++).snprint(vnbuf, sizeof(vnbuf));97EXPECT_EQ(vnbuf, U->getName());98}99ASSERT_EQ(8u, I);100
101X.reverseUseList();102I = 0;103for (User *U : X.users()) {104format("v%u", (7 - I++)).snprint(vnbuf, sizeof(vnbuf));105EXPECT_EQ(vnbuf, U->getName());106}107ASSERT_EQ(8u, I);108}
109
110} // end anonymous namespace111