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 TEST(InstrProfTest, write_and_read_empty_profile) {
19 InstrProfWriter Writer;
20 std::string Profile = Writer.writeString();
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());
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();
33 IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
34 ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
35 auto Reader = std::move(ReaderOrErr.get());
37 auto I = Reader->begin(), E = Reader->end();
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);
49 TEST(InstrProfTest, get_function_counts) {
50 InstrProfWriter Writer;
51 Writer.addFunctionCounts("foo", 0x1234, {1, 2});
52 std::string Profile = Writer.writeString();
54 IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
55 ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
56 auto Reader = std::move(ReaderOrErr.get());
58 std::vector<uint64_t> Counts;
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]);
67 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
68 ASSERT_EQ(instrprof_error::hash_mismatch, EC);
70 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
71 ASSERT_EQ(instrprof_error::unknown_function, EC);
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();
81 IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
82 ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
83 auto Reader = std::move(ReaderOrErr.get());
85 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
88 } // end anonymous namespace