[llvm-objdump] - Cleanup the error reporting.
[llvm-complete.git] / unittests / Support / DataExtractorTest.cpp
blobd1c23cd15e5b24fc26332924902663732fce88c4
1 //===- llvm/unittest/Support/DataExtractorTest.cpp - DataExtractor tests --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/DataExtractor.h"
10 #include "gtest/gtest.h"
11 using namespace llvm;
13 namespace {
15 const char numberData[] = "\x80\x90\xFF\xFF\x80\x00\x00\x00";
16 const char stringData[] = "hellohello\0hello";
17 const char leb128data[] = "\xA6\x49";
18 const char bigleb128data[] = "\xAA\xA9\xFF\xAA\xFF\xAA\xFF\x4A";
20 TEST(DataExtractorTest, OffsetOverflow) {
21 DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
22 EXPECT_FALSE(DE.isValidOffsetForDataOfSize(-2U, 5));
25 TEST(DataExtractorTest, UnsignedNumbers) {
26 DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
27 uint64_t offset = 0;
29 EXPECT_EQ(0x80U, DE.getU8(&offset));
30 EXPECT_EQ(1U, offset);
31 offset = 0;
32 EXPECT_EQ(0x8090U, DE.getU16(&offset));
33 EXPECT_EQ(2U, offset);
34 offset = 0;
35 EXPECT_EQ(0x8090FFFFU, DE.getU32(&offset));
36 EXPECT_EQ(4U, offset);
37 offset = 0;
38 EXPECT_EQ(0x8090FFFF80000000ULL, DE.getU64(&offset));
39 EXPECT_EQ(8U, offset);
40 offset = 0;
41 EXPECT_EQ(0x8090FFFF80000000ULL, DE.getAddress(&offset));
42 EXPECT_EQ(8U, offset);
43 offset = 0;
45 uint32_t data[2];
46 EXPECT_EQ(data, DE.getU32(&offset, data, 2));
47 EXPECT_EQ(0x8090FFFFU, data[0]);
48 EXPECT_EQ(0x80000000U, data[1]);
49 EXPECT_EQ(8U, offset);
50 offset = 0;
52 // Now for little endian.
53 DE = DataExtractor(StringRef(numberData, sizeof(numberData)-1), true, 4);
54 EXPECT_EQ(0x9080U, DE.getU16(&offset));
55 EXPECT_EQ(2U, offset);
56 offset = 0;
57 EXPECT_EQ(0xFFFF9080U, DE.getU32(&offset));
58 EXPECT_EQ(4U, offset);
59 offset = 0;
60 EXPECT_EQ(0x80FFFF9080ULL, DE.getU64(&offset));
61 EXPECT_EQ(8U, offset);
62 offset = 0;
63 EXPECT_EQ(0xFFFF9080U, DE.getAddress(&offset));
64 EXPECT_EQ(4U, offset);
65 offset = 0;
67 EXPECT_EQ(data, DE.getU32(&offset, data, 2));
68 EXPECT_EQ(0xFFFF9080U, data[0]);
69 EXPECT_EQ(0x80U, data[1]);
70 EXPECT_EQ(8U, offset);
73 TEST(DataExtractorTest, SignedNumbers) {
74 DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8);
75 uint64_t offset = 0;
77 EXPECT_EQ(-128, DE.getSigned(&offset, 1));
78 EXPECT_EQ(1U, offset);
79 offset = 0;
80 EXPECT_EQ(-32624, DE.getSigned(&offset, 2));
81 EXPECT_EQ(2U, offset);
82 offset = 0;
83 EXPECT_EQ(-2137980929, DE.getSigned(&offset, 4));
84 EXPECT_EQ(4U, offset);
85 offset = 0;
86 EXPECT_EQ(-9182558167379214336LL, DE.getSigned(&offset, 8));
87 EXPECT_EQ(8U, offset);
90 TEST(DataExtractorTest, Strings) {
91 DataExtractor DE(StringRef(stringData, sizeof(stringData)-1), false, 8);
92 uint64_t offset = 0;
94 EXPECT_EQ(stringData, DE.getCStr(&offset));
95 EXPECT_EQ(11U, offset);
96 EXPECT_EQ(nullptr, DE.getCStr(&offset));
97 EXPECT_EQ(11U, offset);
100 TEST(DataExtractorTest, LEB128) {
101 DataExtractor DE(StringRef(leb128data, sizeof(leb128data)-1), false, 8);
102 uint64_t offset = 0;
104 EXPECT_EQ(9382ULL, DE.getULEB128(&offset));
105 EXPECT_EQ(2U, offset);
106 offset = 0;
107 EXPECT_EQ(-7002LL, DE.getSLEB128(&offset));
108 EXPECT_EQ(2U, offset);
110 DataExtractor BDE(StringRef(bigleb128data, sizeof(bigleb128data)-1), false,8);
111 offset = 0;
112 EXPECT_EQ(42218325750568106ULL, BDE.getULEB128(&offset));
113 EXPECT_EQ(8U, offset);
114 offset = 0;
115 EXPECT_EQ(-29839268287359830LL, BDE.getSLEB128(&offset));
116 EXPECT_EQ(8U, offset);
119 TEST(DataExtractorTest, LEB128_error) {
120 DataExtractor DE(StringRef("\x81"), false, 8);
121 uint64_t Offset = 0;
122 EXPECT_EQ(0U, DE.getULEB128(&Offset));
123 EXPECT_EQ(0U, Offset);
125 Offset = 0;
126 EXPECT_EQ(0U, DE.getSLEB128(&Offset));
127 EXPECT_EQ(0U, Offset);