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 20 TEST(DataExtractorTest, OffsetOverflow) { 21 DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8); 22 EXPECT_FALSE(DE.isValidOffsetForDataOfSize(-2U, 5)); 23 } 24 25 TEST(DataExtractorTest, UnsignedNumbers) { 26 DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8); 27 uint32_t offset = 0; 28 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; 44 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; 51 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; 66 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); 71 } 72 73 TEST(DataExtractorTest, SignedNumbers) { 74 DataExtractor DE(StringRef(numberData, sizeof(numberData)-1), false, 8); 75 uint32_t offset = 0; 76 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); 88 } 89 90 TEST(DataExtractorTest, Strings) { 91 DataExtractor DE(StringRef(stringData, sizeof(stringData)-1), false, 8); 92 uint32_t offset = 0; 93 94 EXPECT_EQ(stringData, DE.getCStr(&offset)); 95 EXPECT_EQ(11U, offset); 96 EXPECT_EQ(NULL, DE.getCStr(&offset)); 97 EXPECT_EQ(11U, offset); 98 } 99 100 TEST(DataExtractorTest, LEB128) { 101 DataExtractor DE(StringRef(leb128data, sizeof(leb128data)-1), false, 8); 102 uint32_t offset = 0; 103 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); 109 } 110 111 } 112