InstrProf: Add missing header from r229478
[oota-llvm.git] / unittests / ProfileData / InstrProfTest.cpp
index f6fd790dccbbb7806c007e771d36788170fa0afc..1a25a1fd90d04690dbd087de4967c787bc186ba9 100644 (file)
 #include "llvm/ProfileData/InstrProfWriter.h"
 #include "gtest/gtest.h"
 
+#include <cstdarg>
+
 using namespace llvm;
 
 namespace {
 
-TEST(InstrProfTest, write_and_read_empty_profile) {
+struct InstrProfTest : ::testing::Test {
   InstrProfWriter Writer;
-  std::string Profile = Writer.writeString();
-  auto ReaderOrErr =
-      IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
-  ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
-  auto Reader = std::move(ReaderOrErr.get());
+  std::unique_ptr<IndexedInstrProfReader> Reader;
+
+  void addCounts(StringRef Name, uint64_t Hash, int NumCounts, ...) {
+    SmallVector<uint64_t, 8> Counts;
+    va_list Args;
+    va_start(Args, NumCounts);
+    for (int I = 0; I < NumCounts; ++I)
+      Counts.push_back(va_arg(Args, uint64_t));
+    va_end(Args);
+    Writer.addFunctionCounts(Name, Hash, Counts);
+  }
+
+  std::string writeProfile() { return Writer.writeString(); }
+  void readProfile(std::string Profile) {
+    auto ReaderOrErr =
+        IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
+    ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
+    Reader = std::move(ReaderOrErr.get());
+  }
+};
+
+TEST_F(InstrProfTest, write_and_read_empty_profile) {
+  std::string Profile = writeProfile();
+  readProfile(Profile);
   ASSERT_TRUE(Reader->begin() == Reader->end());
 }
 
-TEST(InstrProfTest, write_and_read_one_function) {
-  InstrProfWriter Writer;
-  Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4});
-  std::string Profile = Writer.writeString();
-  auto ReaderOrErr =
-      IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
-  ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
-  auto Reader = std::move(ReaderOrErr.get());
+TEST_F(InstrProfTest, write_and_read_one_function) {
+  addCounts("foo", 0x1234, 4, 1ULL, 2ULL, 3ULL, 4ULL);
+  std::string Profile = writeProfile();
+  readProfile(Profile);
 
   auto I = Reader->begin(), E = Reader->end();
   ASSERT_TRUE(I != E);
@@ -46,14 +63,10 @@ TEST(InstrProfTest, write_and_read_one_function) {
   ASSERT_TRUE(++I == E);
 }
 
-TEST(InstrProfTest, get_function_counts) {
-  InstrProfWriter Writer;
-  Writer.addFunctionCounts("foo", 0x1234, {1, 2});
-  std::string Profile = Writer.writeString();
-  auto ReaderOrErr =
-      IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
-  ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
-  auto Reader = std::move(ReaderOrErr.get());
+TEST_F(InstrProfTest, get_function_counts) {
+  addCounts("foo", 0x1234, 2, 1ULL, 2ULL);
+  std::string Profile = writeProfile();
+  readProfile(Profile);
 
   std::vector<uint64_t> Counts;
   std::error_code EC;
@@ -71,16 +84,12 @@ TEST(InstrProfTest, get_function_counts) {
   ASSERT_EQ(instrprof_error::unknown_function, EC);
 }
 
-TEST(InstrProfTest, get_max_function_count) {
-  InstrProfWriter Writer;
-  Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2});
-  Writer.addFunctionCounts("bar", 0, {1ULL << 63});
-  Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0});
-  std::string Profile = Writer.writeString();
-  auto ReaderOrErr =
-      IndexedInstrProfReader::create(MemoryBuffer::getMemBuffer(Profile));
-  ASSERT_EQ(std::error_code(), ReaderOrErr.getError());
-  auto Reader = std::move(ReaderOrErr.get());
+TEST_F(InstrProfTest, get_max_function_count) {
+  addCounts("foo", 0x1234, 2, 1ULL << 31, 2ULL);
+  addCounts("bar", 0, 1, 1ULL << 63);
+  addCounts("baz", 0x5678, 4, 0ULL, 0ULL, 0ULL, 0ULL);
+  std::string Profile = writeProfile();
+  readProfile(Profile);
 
   ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
 }