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"
20 struct InstrProfTest : ::testing::Test {
21 InstrProfWriter Writer;
22 std::unique_ptr<IndexedInstrProfReader> Reader;
24 void addCounts(StringRef Name, uint64_t Hash, int NumCounts, ...) {
25 SmallVector<uint64_t, 8> Counts;
27 va_start(Args, NumCounts);
28 for (int I = 0; I < NumCounts; ++I)
29 Counts.push_back(va_arg(Args, uint64_t));
31 Writer.addFunctionCounts(Name, Hash, Counts);
34 std::string writeProfile() { return Writer.writeString(); }
35 void readProfile(std::string Profile) {
37 IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
38 ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
39 Reader = std::move(ReaderOrErr.get());
43 TEST_F(InstrProfTest, write_and_read_empty_profile) {
44 std::string Profile = writeProfile();
46 ASSERT_TRUE(Reader->begin() == Reader->end());
49 TEST_F(InstrProfTest, write_and_read_one_function) {
50 addCounts("foo", 0x1234, 4, 1ULL, 2ULL, 3ULL, 4ULL);
51 std::string Profile = writeProfile();
54 auto I = Reader->begin(), E = Reader->end();
56 ASSERT_EQ(StringRef("foo"), I->Name);
57 ASSERT_EQ(0x1234U, I->Hash);
58 ASSERT_EQ(4U, I->Counts.size());
59 ASSERT_EQ(1U, I->Counts[0]);
60 ASSERT_EQ(2U, I->Counts[1]);
61 ASSERT_EQ(3U, I->Counts[2]);
62 ASSERT_EQ(4U, I->Counts[3]);
63 ASSERT_TRUE(++I == E);
66 TEST_F(InstrProfTest, get_function_counts) {
67 addCounts("foo", 0x1234, 2, 1ULL, 2ULL);
68 std::string Profile = writeProfile();
71 std::vector<uint64_t> Counts;
74 EC = Reader->getFunctionCounts("foo", 0x1234, Counts);
75 ASSERT_EQ(instrprof_error::success, EC);
76 ASSERT_EQ(2U, Counts.size());
77 ASSERT_EQ(1U, Counts[0]);
78 ASSERT_EQ(2U, Counts[1]);
80 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
81 ASSERT_EQ(instrprof_error::hash_mismatch, EC);
83 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
84 ASSERT_EQ(instrprof_error::unknown_function, EC);
87 TEST_F(InstrProfTest, get_max_function_count) {
88 addCounts("foo", 0x1234, 2, 1ULL << 31, 2ULL);
89 addCounts("bar", 0, 1, 1ULL << 63);
90 addCounts("baz", 0x5678, 4, 0ULL, 0ULL, 0ULL, 0ULL);
91 std::string Profile = writeProfile();
94 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
97 } // end anonymous namespace