linker_sleb128_test.cpp revision fa4aeed2c60fb97a06e2550481b9ef4d0e100b7e
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdlib.h> 18#include <string.h> 19#include <sys/mman.h> 20 21#include <gtest/gtest.h> 22 23#include "../linker_sleb128.h" 24 25TEST(linker_sleb128, smoke) { 26 std::vector<uint8_t> encoding; 27 // 624485 28 encoding.push_back(0xe5); 29 encoding.push_back(0x8e); 30 encoding.push_back(0x26); 31 // 0 32 encoding.push_back(0x00); 33 // 1 34 encoding.push_back(0x01); 35 // 63 36 encoding.push_back(0x3f); 37 // 64 38 encoding.push_back(0xc0); 39 encoding.push_back(0x00); 40 // -1 41 encoding.push_back(0x7f); 42 // -624485 43 encoding.push_back(0x9b); 44 encoding.push_back(0xf1); 45 encoding.push_back(0x59); 46 // 2147483647 47 encoding.push_back(0xff); 48 encoding.push_back(0xff); 49 encoding.push_back(0xff); 50 encoding.push_back(0xff); 51 encoding.push_back(0x07); 52 // -2147483648 53 encoding.push_back(0x80); 54 encoding.push_back(0x80); 55 encoding.push_back(0x80); 56 encoding.push_back(0x80); 57 encoding.push_back(0x78); 58#if defined(__LP64__) 59 // 9223372036854775807 60 encoding.push_back(0xff); 61 encoding.push_back(0xff); 62 encoding.push_back(0xff); 63 encoding.push_back(0xff); 64 encoding.push_back(0xff); 65 encoding.push_back(0xff); 66 encoding.push_back(0xff); 67 encoding.push_back(0xff); 68 encoding.push_back(0xff); 69 encoding.push_back(0x00); 70 // -9223372036854775808 71 encoding.push_back(0x80); 72 encoding.push_back(0x80); 73 encoding.push_back(0x80); 74 encoding.push_back(0x80); 75 encoding.push_back(0x80); 76 encoding.push_back(0x80); 77 encoding.push_back(0x80); 78 encoding.push_back(0x80); 79 encoding.push_back(0x80); 80 encoding.push_back(0x7f); 81#endif 82 sleb128_decoder decoder(&encoding[0], encoding.size()); 83 84 EXPECT_EQ(624485U, decoder.pop_front()); 85 86 EXPECT_EQ(0U, decoder.pop_front()); 87 EXPECT_EQ(1U, decoder.pop_front()); 88 EXPECT_EQ(63U, decoder.pop_front()); 89 EXPECT_EQ(64U, decoder.pop_front()); 90 EXPECT_EQ(static_cast<size_t>(-1), decoder.pop_front()); 91 EXPECT_EQ(static_cast<size_t>(-624485), decoder.pop_front()); 92 EXPECT_EQ(2147483647U, decoder.pop_front()); 93 EXPECT_EQ(static_cast<size_t>(-2147483648), decoder.pop_front()); 94#if defined(__LP64__) 95 EXPECT_EQ(9223372036854775807ULL, decoder.pop_front()); 96 EXPECT_EQ(static_cast<uint64_t>(-9223372036854775807LL - 1), decoder.pop_front()); 97#endif 98} 99