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 #include <cstdarg> 15 16 using namespace llvm; 17 18 static ::testing::AssertionResult NoError(std::error_code EC) { 19 if (!EC) 20 return ::testing::AssertionSuccess(); 21 return ::testing::AssertionFailure() << "error " << EC.value() 22 << ": " << EC.message(); 23 } 24 25 static ::testing::AssertionResult ErrorEquals(std::error_code Expected, 26 std::error_code Found) { 27 if (Expected == Found) 28 return ::testing::AssertionSuccess(); 29 return ::testing::AssertionFailure() << "error " << Found.value() 30 << ": " << Found.message(); 31 } 32 33 namespace { 34 35 struct InstrProfTest : ::testing::Test { 36 InstrProfWriter Writer; 37 std::unique_ptr<IndexedInstrProfReader> Reader; 38 39 void readProfile(std::unique_ptr<MemoryBuffer> Profile) { 40 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); 41 ASSERT_TRUE(NoError(ReaderOrErr.getError())); 42 Reader = std::move(ReaderOrErr.get()); 43 } 44 }; 45 46 TEST_F(InstrProfTest, write_and_read_empty_profile) { 47 auto Profile = Writer.writeBuffer(); 48 readProfile(std::move(Profile)); 49 ASSERT_TRUE(Reader->begin() == Reader->end()); 50 } 51 52 TEST_F(InstrProfTest, write_and_read_one_function) { 53 Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4}); 54 auto Profile = Writer.writeBuffer(); 55 readProfile(std::move(Profile)); 56 57 auto I = Reader->begin(), E = Reader->end(); 58 ASSERT_TRUE(I != E); 59 ASSERT_EQ(StringRef("foo"), I->Name); 60 ASSERT_EQ(0x1234U, I->Hash); 61 ASSERT_EQ(4U, I->Counts.size()); 62 ASSERT_EQ(1U, I->Counts[0]); 63 ASSERT_EQ(2U, I->Counts[1]); 64 ASSERT_EQ(3U, I->Counts[2]); 65 ASSERT_EQ(4U, I->Counts[3]); 66 ASSERT_TRUE(++I == E); 67 } 68 69 TEST_F(InstrProfTest, get_function_counts) { 70 Writer.addFunctionCounts("foo", 0x1234, {1, 2}); 71 Writer.addFunctionCounts("foo", 0x1235, {3, 4}); 72 auto Profile = Writer.writeBuffer(); 73 readProfile(std::move(Profile)); 74 75 std::vector<uint64_t> Counts; 76 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts))); 77 ASSERT_EQ(2U, Counts.size()); 78 ASSERT_EQ(1U, Counts[0]); 79 ASSERT_EQ(2U, Counts[1]); 80 81 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts))); 82 ASSERT_EQ(2U, Counts.size()); 83 ASSERT_EQ(3U, Counts[0]); 84 ASSERT_EQ(4U, Counts[1]); 85 86 std::error_code EC; 87 EC = Reader->getFunctionCounts("foo", 0x5678, Counts); 88 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); 89 90 EC = Reader->getFunctionCounts("bar", 0x1234, Counts); 91 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); 92 } 93 94 TEST_F(InstrProfTest, get_max_function_count) { 95 Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2}); 96 Writer.addFunctionCounts("bar", 0, {1ULL << 63}); 97 Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0}); 98 auto Profile = Writer.writeBuffer(); 99 readProfile(std::move(Profile)); 100 101 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount()); 102 } 103 104 } // end anonymous namespace 105