aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/ProfileData/InstrProfTest.cpp
diff options
context:
space:
mode:
authorStephen Hines <srhines@google.com>2015-03-23 12:10:34 -0700
committerStephen Hines <srhines@google.com>2015-03-23 12:10:34 -0700
commitebe69fe11e48d322045d5949c83283927a0d790b (patch)
treec92f1907a6b8006628a4b01615f38264d29834ea /unittests/ProfileData/InstrProfTest.cpp
parentb7d2e72b02a4cb8034f32f8247a2558d2434e121 (diff)
downloadexternal_llvm-ebe69fe11e48d322045d5949c83283927a0d790b.zip
external_llvm-ebe69fe11e48d322045d5949c83283927a0d790b.tar.gz
external_llvm-ebe69fe11e48d322045d5949c83283927a0d790b.tar.bz2
Update aosp/master LLVM for rebase to r230699.
Change-Id: I2b5be30509658cb8266be782de0ab24f9099f9b9
Diffstat (limited to 'unittests/ProfileData/InstrProfTest.cpp')
-rw-r--r--unittests/ProfileData/InstrProfTest.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/unittests/ProfileData/InstrProfTest.cpp b/unittests/ProfileData/InstrProfTest.cpp
new file mode 100644
index 0000000..26ea0e4
--- /dev/null
+++ b/unittests/ProfileData/InstrProfTest.cpp
@@ -0,0 +1,98 @@
+//===- unittest/ProfileData/InstrProfTest.cpp -------------------------------=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ProfileData/InstrProfReader.h"
+#include "llvm/ProfileData/InstrProfWriter.h"
+#include "gtest/gtest.h"
+
+#include <cstdarg>
+
+using namespace llvm;
+
+static ::testing::AssertionResult NoError(std::error_code EC) {
+ if (!EC)
+ return ::testing::AssertionSuccess();
+ return ::testing::AssertionFailure() << "error " << EC.value()
+ << ": " << EC.message();
+}
+
+static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
+ std::error_code Found) {
+ if (Expected == Found)
+ return ::testing::AssertionSuccess();
+ return ::testing::AssertionFailure() << "error " << Found.value()
+ << ": " << Found.message();
+}
+
+namespace {
+
+struct InstrProfTest : ::testing::Test {
+ InstrProfWriter Writer;
+ std::unique_ptr<IndexedInstrProfReader> Reader;
+
+ void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
+ auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
+ ASSERT_TRUE(NoError(ReaderOrErr.getError()));
+ Reader = std::move(ReaderOrErr.get());
+ }
+};
+
+TEST_F(InstrProfTest, write_and_read_empty_profile) {
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+ ASSERT_TRUE(Reader->begin() == Reader->end());
+}
+
+TEST_F(InstrProfTest, write_and_read_one_function) {
+ Writer.addFunctionCounts("foo", 0x1234, {1, 2, 3, 4});
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+
+ auto I = Reader->begin(), E = Reader->end();
+ ASSERT_TRUE(I != E);
+ ASSERT_EQ(StringRef("foo"), I->Name);
+ ASSERT_EQ(0x1234U, I->Hash);
+ ASSERT_EQ(4U, I->Counts.size());
+ ASSERT_EQ(1U, I->Counts[0]);
+ ASSERT_EQ(2U, I->Counts[1]);
+ ASSERT_EQ(3U, I->Counts[2]);
+ ASSERT_EQ(4U, I->Counts[3]);
+ ASSERT_TRUE(++I == E);
+}
+
+TEST_F(InstrProfTest, get_function_counts) {
+ Writer.addFunctionCounts("foo", 0x1234, {1, 2});
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+
+ std::vector<uint64_t> Counts;
+ ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
+ ASSERT_EQ(2U, Counts.size());
+ ASSERT_EQ(1U, Counts[0]);
+ ASSERT_EQ(2U, Counts[1]);
+
+ std::error_code EC;
+ EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
+ ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
+
+ EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
+ ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
+}
+
+TEST_F(InstrProfTest, get_max_function_count) {
+ Writer.addFunctionCounts("foo", 0x1234, {1ULL << 31, 2});
+ Writer.addFunctionCounts("bar", 0, {1ULL << 63});
+ Writer.addFunctionCounts("baz", 0x5678, {0, 0, 0, 0});
+ auto Profile = Writer.writeBuffer();
+ readProfile(std::move(Profile));
+
+ ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
+}
+
+} // end anonymous namespace