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 Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4});
54 auto Profile = Writer.writeBuffer();
55 readProfile(std::move(Profile));
57 auto I = Reader->begin(), E = Reader->end();
59 ASSERT_EQ(StringRef("foo"), I->Name);
60 ASSERT_EQ(0x1234U, I->Hash);
61 ASSERT_EQ(4U, I->Counts.size());
62 ASSERT_EQ(1U, I->Counts[0]);
63 ASSERT_EQ(2U, I->Counts[1]);
64 ASSERT_EQ(3U, I->Counts[2]);
65 ASSERT_EQ(4U, I->Counts[3]);
66 ASSERT_TRUE(++I == E);
69 TEST_F(InstrProfTest, get_function_counts) {
70 Writer.addFunctionCounts("foo", 0x1234, {1, 2});
71 Writer.addFunctionCounts("foo", 0x1235, {3, 4});
72 auto Profile = Writer.writeBuffer();
73 readProfile(std::move(Profile));
75 std::vector<uint64_t> Counts;
76 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
77 ASSERT_EQ(2U, Counts.size());
78 ASSERT_EQ(1U, Counts[0]);
79 ASSERT_EQ(2U, Counts[1]);
81 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
82 ASSERT_EQ(2U, Counts.size());
83 ASSERT_EQ(3U, Counts[0]);
84 ASSERT_EQ(4U, Counts[1]);
87 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
88 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
90 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
91 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
94 TEST_F(InstrProfTest, get_max_function_count) {
95 Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2});
96 Writer.addFunctionCounts("bar", 0, {1ULL << 63});
97 Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0});
98 auto Profile = Writer.writeBuffer();
99 readProfile(std::move(Profile));
101 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
104 } // end anonymous namespace