2a6713176b7e169283e7c66d84502a23fe27b7b6
[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 using namespace llvm;
15
16 namespace {
17
18 struct InstrProfTest : ::testing::Test {
19   InstrProfWriter Writer;
20   std::unique_ptr<IndexedInstrProfReader> Reader;
21
22   void addCounts(StringRef Name, uint64_t Hash, int NumCounts, ...) {
23     SmallVector<uint64_t, 8> Counts;
24     va_list Args;
25     va_start(Args, NumCounts);
26     for (int I = 0; I < NumCounts; ++I)
27       Counts.push_back(va_arg(Args, uint64_t));
28     va_end(Args);
29     Writer.addFunctionCounts(Name, Hash, Counts);
30   }
31
32   std::string writeProfile() { return Writer.writeString(); }
33   void readProfile(std::string Profile) {
34     auto ReaderOrErr =
35         IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
36     ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
37     Reader = std::move(ReaderOrErr.get());
38   }
39 };
40
41 TEST_F(InstrProfTest, write_and_read_empty_profile) {
42   std::string Profile = writeProfile();
43   readProfile(Profile);
44   ASSERT_TRUE(Reader->begin() == Reader->end());
45 }
46
47 TEST_F(InstrProfTest, write_and_read_one_function) {
48   addCounts("foo", 0x1234, 4, 1ULL, 2ULL, 3ULL, 4ULL);
49   std::string Profile = writeProfile();
50   readProfile(Profile);
51
52   auto I = Reader->begin(), E = Reader->end();
53   ASSERT_TRUE(I != E);
54   ASSERT_EQ(StringRef("foo"), I->Name);
55   ASSERT_EQ(0x1234U, I->Hash);
56   ASSERT_EQ(4U, I->Counts.size());
57   ASSERT_EQ(1U, I->Counts[0]);
58   ASSERT_EQ(2U, I->Counts[1]);
59   ASSERT_EQ(3U, I->Counts[2]);
60   ASSERT_EQ(4U, I->Counts[3]);
61   ASSERT_TRUE(++I == E);
62 }
63
64 TEST_F(InstrProfTest, get_function_counts) {
65   addCounts("foo", 0x1234, 2, 1ULL, 2ULL);
66   std::string Profile = writeProfile();
67   readProfile(Profile);
68
69   std::vector<uint64_t> Counts;
70   std::error_code EC;
71
72   EC = Reader->getFunctionCounts("foo", 0x1234, Counts);
73   ASSERT_EQ(instrprof_error::success, EC);
74   ASSERT_EQ(2U, Counts.size());
75   ASSERT_EQ(1U, Counts[0]);
76   ASSERT_EQ(2U, Counts[1]);
77
78   EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
79   ASSERT_EQ(instrprof_error::hash_mismatch, EC);
80
81   EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
82   ASSERT_EQ(instrprof_error::unknown_function, EC);
83 }
84
85 TEST_F(InstrProfTest, get_max_function_count) {
86   addCounts("foo", 0x1234, 2, 1ULL << 31, 2ULL);
87   addCounts("bar", 0, 1, 1ULL << 63);
88   addCounts("baz", 0x5678, 4, 0ULL, 0ULL, 0ULL, 0ULL);
89   std::string Profile = writeProfile();
90   readProfile(Profile);
91
92   ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
93 }
94
95 } // end anonymous namespace