1 //===- unittest/ProfileData/InstrProfTest.cpp -------------------------------=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ProfileData/InstrProfReader.h"
11 #include "llvm/ProfileData/InstrProfWriter.h"
12 #include "gtest/gtest.h"
18 static ::testing::AssertionResult NoError(std::error_code EC) {
20 return ::testing::AssertionSuccess();
21 return ::testing::AssertionFailure() << "error " << EC.value()
22 << ": " << EC.message();
25 static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
26 std::error_code Found) {
27 if (Expected == Found)
28 return ::testing::AssertionSuccess();
29 return ::testing::AssertionFailure() << "error " << Found.value()
30 << ": " << Found.message();
35 struct InstrProfTest : ::testing::Test {
36 InstrProfWriter Writer;
37 std::unique_ptr<IndexedInstrProfReader> Reader;
39 void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
40 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
41 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
42 Reader = std::move(ReaderOrErr.get());
46 TEST_F(InstrProfTest, write_and_read_empty_profile) {
47 auto Profile = Writer.writeBuffer();
48 readProfile(std::move(Profile));
49 ASSERT_TRUE(Reader->begin() == Reader->end());
52 TEST_F(InstrProfTest, write_and_read_one_function) {
53 InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4});
54 Writer.addRecord(std::move(Record));
55 auto Profile = Writer.writeBuffer();
56 readProfile(std::move(Profile));
58 auto I = Reader->begin(), E = Reader->end();
60 ASSERT_EQ(StringRef("foo"), I->Name);
61 ASSERT_EQ(0x1234U, I->Hash);
62 ASSERT_EQ(4U, I->Counts.size());
63 ASSERT_EQ(1U, I->Counts[0]);
64 ASSERT_EQ(2U, I->Counts[1]);
65 ASSERT_EQ(3U, I->Counts[2]);
66 ASSERT_EQ(4U, I->Counts[3]);
67 ASSERT_TRUE(++I == E);
70 TEST_F(InstrProfTest, get_function_counts) {
71 InstrProfRecord Record1("foo", 0x1234, {1, 2});
72 InstrProfRecord Record2("foo", 0x1235, {3, 4});
73 Writer.addRecord(std::move(Record1));
74 Writer.addRecord(std::move(Record2));
75 auto Profile = Writer.writeBuffer();
76 readProfile(std::move(Profile));
78 std::vector<uint64_t> Counts;
79 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
80 ASSERT_EQ(2U, Counts.size());
81 ASSERT_EQ(1U, Counts[0]);
82 ASSERT_EQ(2U, Counts[1]);
84 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
85 ASSERT_EQ(2U, Counts.size());
86 ASSERT_EQ(3U, Counts[0]);
87 ASSERT_EQ(4U, Counts[1]);
90 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
91 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
93 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
94 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
97 TEST_F(InstrProfTest, get_max_function_count) {
98 InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2});
99 InstrProfRecord Record2("bar", 0, {1ULL << 63});
100 InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0});
101 Writer.addRecord(std::move(Record1));
102 Writer.addRecord(std::move(Record2));
103 Writer.addRecord(std::move(Record3));
104 auto Profile = Writer.writeBuffer();
105 readProfile(std::move(Profile));
107 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
110 } // end anonymous namespace