0e0141b80ea2f5c959064276137a60f3874fbaf0
[oota-llvm.git] / unittests / ProfileData / InstrProfTest.cpp
1 //===- unittest/ProfileData/InstrProfTest.cpp -------------------------------=//
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/ProfileData/InstrProfReader.h"
11 #include "llvm/ProfileData/InstrProfWriter.h"
12 #include "gtest/gtest.h"
13
14 #include <cstdarg>
15
16 using namespace llvm;
17
18 static ::testing::AssertionResult NoError(std::error_code EC) {
19   if (!EC)
20     return ::testing::AssertionSuccess();
21   return ::testing::AssertionFailure() << "error " << EC.value()
22                                        << ": " << EC.message();
23 }
24
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();
31 }
32
33 namespace {
34
35 struct InstrProfTest : ::testing::Test {
36   InstrProfWriter Writer;
37   std::unique_ptr<IndexedInstrProfReader> Reader;
38
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());
43   }
44 };
45
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());
50 }
51
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));
57
58   auto I = Reader->begin(), E = Reader->end();
59   ASSERT_TRUE(I != E);
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);
68 }
69
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));
77
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]);
83
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]);
88
89   std::error_code EC;
90   EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
91   ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
92
93   EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
94   ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
95 }
96
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));
106
107   ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
108 }
109
110 } // end anonymous namespace