summaryrefslogtreecommitdiffstats
path: root/unittests
diff options
context:
space:
mode:
authorBenjamin Kramer <benny.kra@googlemail.com>2011-09-13 19:42:16 +0000
committerBenjamin Kramer <benny.kra@googlemail.com>2011-09-13 19:42:16 +0000
commit8c74f7f2991a10977fd5a003b2901b56eb2e19a8 (patch)
treee9c25ae260bcc06a6bbd5cd8c2fddd316352a1cc /unittests
parentdf24e1fb0812edd2bd33a3b279edb030dd30023d (diff)
downloadexternal_llvm-8c74f7f2991a10977fd5a003b2901b56eb2e19a8.zip
external_llvm-8c74f7f2991a10977fd5a003b2901b56eb2e19a8.tar.gz
external_llvm-8c74f7f2991a10977fd5a003b2901b56eb2e19a8.tar.bz2
Add the DataExtractor utility class.
It is an endian-aware helper that can read data from a StringRef. It will come in handy for DWARF parsing. This class is inspired by LLDB's DataExtractor, but is stripped down to the bare minimum needed for DWARF. Comes with unit tests! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139626 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Support/DataExtractorTest.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/unittests/Support/DataExtractorTest.cpp b/unittests/Support/DataExtractorTest.cpp
new file mode 100644
index 0000000..6de96c4
--- /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(0x8090FFFF80000000U, DE.getU64(&offset));
+ EXPECT_EQ(8U, offset);
+ offset = 0;
+ EXPECT_EQ(0x8090FFFF80000000U, 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(0x80FFFF9080U, 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);
+}
+
+}