InstrProf: Add unit tests for the profile reader and writer
[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 TEST(InstrProfTest, write_and_read_empty_profile) {
19   InstrProfWriter Writer;
20   std::string Profile = Writer.writeString();
21   auto ReaderOrErr =
22       IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
23   ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
24   auto Reader = std::move(ReaderOrErr.get());
25   ASSERT_TRUE(Reader->begin() == Reader->end());
26 }
27
28 TEST(InstrProfTest, write_and_read_one_function) {
29   InstrProfWriter Writer;
30   Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4});
31   std::string Profile = Writer.writeString();
32   auto ReaderOrErr =
33       IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
34   ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
35   auto Reader = std::move(ReaderOrErr.get());
36
37   auto I = Reader->begin(), E = Reader->end();
38   ASSERT_TRUE(I != E);
39   ASSERT_EQ(StringRef("foo"), I->Name);
40   ASSERT_EQ(0x1234U, I->Hash);
41   ASSERT_EQ(4U, I->Counts.size());
42   ASSERT_EQ(1U, I->Counts[0]);
43   ASSERT_EQ(2U, I->Counts[1]);
44   ASSERT_EQ(3U, I->Counts[2]);
45   ASSERT_EQ(4U, I->Counts[3]);
46   ASSERT_TRUE(++I == E);
47 }
48
49 TEST(InstrProfTest, get_function_counts) {
50   InstrProfWriter Writer;
51   Writer.addFunctionCounts("foo", 0x1234, {1, 2});
52   std::string Profile = Writer.writeString();
53   auto ReaderOrErr =
54       IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
55   ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
56   auto Reader = std::move(ReaderOrErr.get());
57
58   std::vector<uint64_t> Counts;
59   std::error_code EC;
60
61   EC = Reader->getFunctionCounts("foo", 0x1234, Counts);
62   ASSERT_EQ(instrprof_error::success, EC);
63   ASSERT_EQ(2U, Counts.size());
64   ASSERT_EQ(1U, Counts[0]);
65   ASSERT_EQ(2U, Counts[1]);
66
67   EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
68   ASSERT_EQ(instrprof_error::hash_mismatch, EC);
69
70   EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
71   ASSERT_EQ(instrprof_error::unknown_function, EC);
72 }
73
74 TEST(InstrProfTest, get_max_function_count) {
75   InstrProfWriter Writer;
76   Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2});
77   Writer.addFunctionCounts("bar", 0, {1ULL << 63});
78   Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0});
79   std::string Profile = Writer.writeString();
80   auto ReaderOrErr =
81       IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
82   ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
83   auto Reader = std::move(ReaderOrErr.get());
84
85   ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
86 }
87
88 } // end anonymous namespace