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 struct InstrProfTest : ::testing::Test {
19 InstrProfWriter Writer;
20 std::unique_ptr<IndexedInstrProfReader> Reader;
22 void addCounts(StringRef Name, uint64_t Hash, int NumCounts, ...) {
23 SmallVector<uint64_t, 8> Counts;
25 va_start(Args, NumCounts);
26 for (int I = 0; I < NumCounts; ++I)
27 Counts.push_back(va_arg(Args, uint64_t));
29 Writer.addFunctionCounts(Name, Hash, Counts);
32 std::string writeProfile() { return Writer.writeString(); }
33 void readProfile(std::string Profile) {
35 IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
36 ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
37 Reader = std::move(ReaderOrErr.get());
41 TEST_F(InstrProfTest, write_and_read_empty_profile) {
42 std::string Profile = writeProfile();
44 ASSERT_TRUE(Reader->begin() == Reader->end());
47 TEST_F(InstrProfTest, write_and_read_one_function) {
48 addCounts("foo", 0x1234, 4, 1ULL, 2ULL, 3ULL, 4ULL);
49 std::string Profile = writeProfile();
52 auto I = Reader->begin(), E = Reader->end();
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);
64 TEST_F(InstrProfTest, get_function_counts) {
65 addCounts("foo", 0x1234, 2, 1ULL, 2ULL);
66 std::string Profile = writeProfile();
69 std::vector<uint64_t> Counts;
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]);
78 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
79 ASSERT_EQ(instrprof_error::hash_mismatch, EC);
81 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
82 ASSERT_EQ(instrprof_error::unknown_function, EC);
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();
92 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
95 } // end anonymous namespace