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 auto Profile = Writer.writeBuffer(); 72 readProfile(std::move(Profile)); 73 74 std::vector<uint64_t> Counts; 75 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts))); 76 ASSERT_EQ(2U, Counts.size()); 77 ASSERT_EQ(1U, Counts[0]); 78 ASSERT_EQ(2U, Counts[1]); 79 80 std::error_code EC; 81 EC = Reader->getFunctionCounts("foo", 0x5678, Counts); 82 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); 83 84 EC = Reader->getFunctionCounts("bar", 0x1234, Counts); 85 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); 86 } 87 88 TEST_F(InstrProfTest, get_max_function_count) { 89 Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2}); 90 Writer.addFunctionCounts("bar", 0, {1ULL << 63}); 91 Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0}); 92 auto Profile = Writer.writeBuffer(); 93 readProfile(std::move(Profile)); 94 95 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount()); 96 } 97 98 } // end anonymous namespace 99