• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/unittest/Support/DataExtractorTest.cpp - DataExtractor tests --===//
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 "gtest/gtest.h"
11 #include "llvm/Support/DataExtractor.h"
12 using namespace llvm;
13 
14 namespace {
15 
16 const char numberData[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
17 const char stringData[] = "hellohello\0hello";
18 const char leb128data[] = "\xA6\x49";
19 const char bigleb128data[] = "\xAA\xA9\xFF\xAA\xFF\xAA\xFF\x4A";
20 
TEST(DataExtractorTest,OffsetOverflow)21 TEST(DataExtractorTest, OffsetOverflow) {
22   DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
23   EXPECT_FALSE(DE.isValidOffsetForDataOfSize(-2U, 5));
24 }
25 
TEST(DataExtractorTest,UnsignedNumbers)26 TEST(DataExtractorTest, UnsignedNumbers) {
27   DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
28   uint32_t offset = 0;
29 
30   EXPECT_EQ(0x80U, DE.getU8(&offset));
31   EXPECT_EQ(1U, offset);
32   offset = 0;
33   EXPECT_EQ(0x8090U, DE.getU16(&offset));
34   EXPECT_EQ(2U, offset);
35   offset = 0;
36   EXPECT_EQ(0x8090FFFFU, DE.getU32(&offset));
37   EXPECT_EQ(4U, offset);
38   offset = 0;
39   EXPECT_EQ(0x8090FFFF80000000ULL, DE.getU64(&offset));
40   EXPECT_EQ(8U, offset);
41   offset = 0;
42   EXPECT_EQ(0x8090FFFF80000000ULL, DE.getAddress(&offset));
43   EXPECT_EQ(8U, offset);
44   offset = 0;
45 
46   uint32_t data[2];
47   EXPECT_EQ(data, DE.getU32(&offset, data, 2));
48   EXPECT_EQ(0x8090FFFFU, data[0]);
49   EXPECT_EQ(0x80000000U, data[1]);
50   EXPECT_EQ(8U, offset);
51   offset = 0;
52 
53   // Now for little endian.
54   DE = DataExtractor(StringRef(numberData, sizeof(numberData)-1), true, 4);
55   EXPECT_EQ(0x9080U, DE.getU16(&offset));
56   EXPECT_EQ(2U, offset);
57   offset = 0;
58   EXPECT_EQ(0xFFFF9080U, DE.getU32(&offset));
59   EXPECT_EQ(4U, offset);
60   offset = 0;
61   EXPECT_EQ(0x80FFFF9080ULL, DE.getU64(&offset));
62   EXPECT_EQ(8U, offset);
63   offset = 0;
64   EXPECT_EQ(0xFFFF9080U, DE.getAddress(&offset));
65   EXPECT_EQ(4U, offset);
66   offset = 0;
67 
68   EXPECT_EQ(data, DE.getU32(&offset, data, 2));
69   EXPECT_EQ(0xFFFF9080U, data[0]);
70   EXPECT_EQ(0x80U, data[1]);
71   EXPECT_EQ(8U, offset);
72 }
73 
TEST(DataExtractorTest,SignedNumbers)74 TEST(DataExtractorTest, SignedNumbers) {
75   DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
76   uint32_t offset = 0;
77 
78   EXPECT_EQ(-128, DE.getSigned(&offset, 1));
79   EXPECT_EQ(1U, offset);
80   offset = 0;
81   EXPECT_EQ(-32624, DE.getSigned(&offset, 2));
82   EXPECT_EQ(2U, offset);
83   offset = 0;
84   EXPECT_EQ(-2137980929, DE.getSigned(&offset, 4));
85   EXPECT_EQ(4U, offset);
86   offset = 0;
87   EXPECT_EQ(-9182558167379214336LL, DE.getSigned(&offset, 8));
88   EXPECT_EQ(8U, offset);
89 }
90 
TEST(DataExtractorTest,Strings)91 TEST(DataExtractorTest, Strings) {
92   DataExtractor DE(StringRef(stringData, sizeof(stringData)-1), false, 8);
93   uint32_t offset = 0;
94 
95   EXPECT_EQ(stringData, DE.getCStr(&offset));
96   EXPECT_EQ(11U, offset);
97   EXPECT_EQ(nullptr, DE.getCStr(&offset));
98   EXPECT_EQ(11U, offset);
99 }
100 
TEST(DataExtractorTest,LEB128)101 TEST(DataExtractorTest, LEB128) {
102   DataExtractor DE(StringRef(leb128data, sizeof(leb128data)-1), false, 8);
103   uint32_t offset = 0;
104 
105   EXPECT_EQ(9382ULL, DE.getULEB128(&offset));
106   EXPECT_EQ(2U, offset);
107   offset = 0;
108   EXPECT_EQ(-7002LL, DE.getSLEB128(&offset));
109   EXPECT_EQ(2U, offset);
110 
111   DataExtractor BDE(StringRef(bigleb128data, sizeof(bigleb128data)-1), false,8);
112   offset = 0;
113   EXPECT_EQ(42218325750568106ULL, BDE.getULEB128(&offset));
114   EXPECT_EQ(8U, offset);
115   offset = 0;
116   EXPECT_EQ(-29839268287359830LL, BDE.getSLEB128(&offset));
117   EXPECT_EQ(8U, offset);
118 }
119 
120 }
121