summaryrefslogtreecommitdiffstats
path: root/unittests/Support
diff options
context:
space:
mode:
Diffstat (limited to 'unittests/Support')
-rw-r--r--unittests/Support/BlockFrequencyTest.cpp56
-rw-r--r--unittests/Support/DataExtractorTest.cpp111
-rw-r--r--unittests/Support/TypeBuilderTest.cpp14
3 files changed, 174 insertions, 7 deletions
diff --git a/unittests/Support/BlockFrequencyTest.cpp b/unittests/Support/BlockFrequencyTest.cpp
new file mode 100644
index 0000000..edeea9b
--- /dev/null
+++ b/unittests/Support/BlockFrequencyTest.cpp
@@ -0,0 +1,56 @@
+#include "llvm/Support/DataTypes.h"
+#include "llvm/Support/BlockFrequency.h"
+#include "llvm/Support/BranchProbability.h"
+
+#include "gtest/gtest.h"
+#include <climits>
+
+using namespace llvm;
+
+namespace {
+
+TEST(BlockFrequencyTest, OneToZero) {
+ BlockFrequency Freq(1);
+ BranchProbability Prob(UINT32_MAX - 1, UINT32_MAX);
+ Freq *= Prob;
+ EXPECT_EQ(Freq.getFrequency(), 0u);
+}
+
+TEST(BlockFrequencyTest, OneToOne) {
+ BlockFrequency Freq(1);
+ BranchProbability Prob(UINT32_MAX, UINT32_MAX);
+ Freq *= Prob;
+ EXPECT_EQ(Freq.getFrequency(), 1u);
+}
+
+TEST(BlockFrequencyTest, ThreeToOne) {
+ BlockFrequency Freq(3);
+ BranchProbability Prob(3000000, 9000000);
+ Freq *= Prob;
+ EXPECT_EQ(Freq.getFrequency(), 1u);
+}
+
+TEST(BlockFrequencyTest, MaxToHalfMax) {
+ BlockFrequency Freq(UINT64_MAX);
+ BranchProbability Prob(UINT32_MAX / 2, UINT32_MAX);
+ Freq *= Prob;
+ EXPECT_EQ(Freq.getFrequency(), 9223372034707292159LLu);
+}
+
+TEST(BlockFrequencyTest, BigToBig) {
+ const uint64_t Big = 387246523487234346LL;
+ const uint32_t P = 123456789;
+ BlockFrequency Freq(Big);
+ BranchProbability Prob(P, P);
+ Freq *= Prob;
+ EXPECT_EQ(Freq.getFrequency(), Big);
+}
+
+TEST(BlockFrequencyTest, MaxToMax) {
+ BlockFrequency Freq(UINT64_MAX);
+ BranchProbability Prob(UINT32_MAX, UINT32_MAX);
+ Freq *= Prob;
+ EXPECT_EQ(Freq.getFrequency(), UINT64_MAX);
+}
+
+}
diff --git a/unittests/Support/DataExtractorTest.cpp b/unittests/Support/DataExtractorTest.cpp
new file mode 100644
index 0000000..9813e46
--- /dev/null
+++ b/unittests/Support/DataExtractorTest.cpp
@@ -0,0 +1,111 @@
+//===- llvm/unittest/Support/DataExtractorTest.cpp - DataExtractor tests --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/Support/DataExtractor.h"
+using namespace llvm;
+
+namespace {
+
+const char numberData[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
+const char stringData[] = "hellohello\0hello";
+const char leb128data[] = "\xA6\x49";
+
+TEST(DataExtractorTest, OffsetOverflow) {
+ DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
+ EXPECT_FALSE(DE.isValidOffsetForDataOfSize(-2U, 5));
+}
+
+TEST(DataExtractorTest, UnsignedNumbers) {
+ DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
+ uint32_t offset = 0;
+
+ EXPECT_EQ(0x80U, DE.getU8(&offset));
+ EXPECT_EQ(1U, offset);
+ offset = 0;
+ EXPECT_EQ(0x8090U, DE.getU16(&offset));
+ EXPECT_EQ(2U, offset);
+ offset = 0;
+ EXPECT_EQ(0x8090FFFFU, DE.getU32(&offset));
+ EXPECT_EQ(4U, offset);
+ offset = 0;
+ EXPECT_EQ(0x8090FFFF80000000ULL, DE.getU64(&offset));
+ EXPECT_EQ(8U, offset);
+ offset = 0;
+ EXPECT_EQ(0x8090FFFF80000000ULL, DE.getAddress(&offset));
+ EXPECT_EQ(8U, offset);
+ offset = 0;
+
+ uint32_t data[2];
+ EXPECT_EQ(data, DE.getU32(&offset, data, 2));
+ EXPECT_EQ(0x8090FFFFU, data[0]);
+ EXPECT_EQ(0x80000000U, data[1]);
+ EXPECT_EQ(8U, offset);
+ offset = 0;
+
+ // Now for little endian.
+ DE = DataExtractor(StringRef(numberData, sizeof(numberData)-1), true, 4);
+ EXPECT_EQ(0x9080U, DE.getU16(&offset));
+ EXPECT_EQ(2U, offset);
+ offset = 0;
+ EXPECT_EQ(0xFFFF9080U, DE.getU32(&offset));
+ EXPECT_EQ(4U, offset);
+ offset = 0;
+ EXPECT_EQ(0x80FFFF9080ULL, DE.getU64(&offset));
+ EXPECT_EQ(8U, offset);
+ offset = 0;
+ EXPECT_EQ(0xFFFF9080U, DE.getAddress(&offset));
+ EXPECT_EQ(4U, offset);
+ offset = 0;
+
+ EXPECT_EQ(data, DE.getU32(&offset, data, 2));
+ EXPECT_EQ(0xFFFF9080U, data[0]);
+ EXPECT_EQ(0x80U, data[1]);
+ EXPECT_EQ(8U, offset);
+}
+
+TEST(DataExtractorTest, SignedNumbers) {
+ DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
+ uint32_t offset = 0;
+
+ EXPECT_EQ(-128, DE.getSigned(&offset, 1));
+ EXPECT_EQ(1U, offset);
+ offset = 0;
+ EXPECT_EQ(-32624, DE.getSigned(&offset, 2));
+ EXPECT_EQ(2U, offset);
+ offset = 0;
+ EXPECT_EQ(-2137980929, DE.getSigned(&offset, 4));
+ EXPECT_EQ(4U, offset);
+ offset = 0;
+ EXPECT_EQ(-9182558167379214336LL, DE.getSigned(&offset, 8));
+ EXPECT_EQ(8U, offset);
+}
+
+TEST(DataExtractorTest, Strings) {
+ DataExtractor DE(StringRef(stringData, sizeof(stringData)-1), false, 8);
+ uint32_t offset = 0;
+
+ EXPECT_EQ(stringData, DE.getCStr(&offset));
+ EXPECT_EQ(11U, offset);
+ EXPECT_EQ(NULL, DE.getCStr(&offset));
+ EXPECT_EQ(11U, offset);
+}
+
+TEST(DataExtractorTest, LEB128) {
+ DataExtractor DE(StringRef(leb128data, sizeof(leb128data)-1), false, 8);
+ uint32_t offset = 0;
+
+ EXPECT_EQ(9382ULL, DE.getULEB128(&offset));
+ EXPECT_EQ(2U, offset);
+ offset = 0;
+ EXPECT_EQ(-7002LL, DE.getSLEB128(&offset));
+ EXPECT_EQ(2U, offset);
+}
+
+}
diff --git a/unittests/Support/TypeBuilderTest.cpp b/unittests/Support/TypeBuilderTest.cpp
index 0609178..20d0e73 100644
--- a/unittests/Support/TypeBuilderTest.cpp
+++ b/unittests/Support/TypeBuilderTest.cpp
@@ -184,14 +184,14 @@ class MyPortableType {
namespace llvm {
template<bool cross> class TypeBuilder<MyType, cross> {
public:
- static const StructType *get(LLVMContext &Context) {
+ static StructType *get(LLVMContext &Context) {
// Using the static result variable ensures that the type is
// only looked up once.
std::vector<Type*> st;
st.push_back(TypeBuilder<int, cross>::get(Context));
st.push_back(TypeBuilder<int*, cross>::get(Context));
st.push_back(TypeBuilder<void*[], cross>::get(Context));
- static const StructType *const result = StructType::get(Context, st);
+ static StructType *const result = StructType::get(Context, st);
return result;
}
@@ -207,14 +207,14 @@ public:
template<bool cross> class TypeBuilder<MyPortableType, cross> {
public:
- static const StructType *get(LLVMContext &Context) {
+ static StructType *get(LLVMContext &Context) {
// Using the static result variable ensures that the type is
// only looked up once.
std::vector<Type*> st;
st.push_back(TypeBuilder<types::i<32>, cross>::get(Context));
st.push_back(TypeBuilder<types::i<32>*, cross>::get(Context));
st.push_back(TypeBuilder<types::i<8>*[], cross>::get(Context));
- static const StructType *const result = StructType::get(Context, st);
+ static StructType *const result = StructType::get(Context, st);
return result;
}
@@ -235,19 +235,19 @@ TEST(TypeBuilderTest, Extensions) {
TypeBuilder<int, false>::get(getGlobalContext()),
TypeBuilder<int*, false>::get(getGlobalContext()),
TypeBuilder<void*[], false>::get(getGlobalContext()),
- NULL)),
+ (void*)0)),
(TypeBuilder<MyType*, false>::get(getGlobalContext())));
EXPECT_EQ(PointerType::getUnqual(StructType::get(
TypeBuilder<types::i<32>, false>::get(getGlobalContext()),
TypeBuilder<types::i<32>*, false>::get(getGlobalContext()),
TypeBuilder<types::i<8>*[], false>::get(getGlobalContext()),
- NULL)),
+ (void*)0)),
(TypeBuilder<MyPortableType*, false>::get(getGlobalContext())));
EXPECT_EQ(PointerType::getUnqual(StructType::get(
TypeBuilder<types::i<32>, false>::get(getGlobalContext()),
TypeBuilder<types::i<32>*, false>::get(getGlobalContext()),
TypeBuilder<types::i<8>*[], false>::get(getGlobalContext()),
- NULL)),
+ (void*)0)),
(TypeBuilder<MyPortableType*, true>::get(getGlobalContext())));
}
OpenPOWER on IntegriCloud