run_length_encoder_unittest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "run_length_encoder.h" 6 7#include <vector> 8#include "elf.h" 9#include "testing/gtest/include/gtest/gtest.h" 10 11namespace { 12 13void AddRelocation(Elf32_Addr addr, std::vector<Elf32_Rel>* relocations) { 14 Elf32_Rel relocation = {addr, R_ARM_RELATIVE}; 15 relocations->push_back(relocation); 16} 17 18bool CheckRelocation(Elf32_Addr addr, const Elf32_Rel& relocation) { 19 return relocation.r_offset == addr && relocation.r_info == R_ARM_RELATIVE; 20} 21 22} // namespace 23 24namespace relocation_packer { 25 26TEST(Rle, Encode) { 27 std::vector<Elf32_Rel> relocations; 28 std::vector<Elf32_Word> packed; 29 30 RelocationRunLengthCodec codec; 31 32 packed.clear(); 33 codec.Encode(relocations, &packed); 34 35 EXPECT_EQ(0u, packed.size()); 36 37 // Add one relocation (insufficient data to encode). 38 AddRelocation(0xf00d0000, &relocations); 39 40 packed.clear(); 41 codec.Encode(relocations, &packed); 42 43 EXPECT_EQ(0u, packed.size()); 44 45 // Add a second relocation, 4 byte delta (minimum data to encode). 46 AddRelocation(0xf00d0004, &relocations); 47 48 packed.clear(); 49 codec.Encode(relocations, &packed); 50 51 EXPECT_EQ(4u, packed.size()); 52 // One count-delta pair present. 53 EXPECT_EQ(1u, packed[0]); 54 // Initial relocation. 55 EXPECT_EQ(0xf00d0000, packed[1]); 56 // Run of a single relocation, 4 byte delta. 57 EXPECT_EQ(1u, packed[2]); 58 EXPECT_EQ(4u, packed[3]); 59 60 // Add a third relocation, 4 byte delta. 61 AddRelocation(0xf00d0008, &relocations); 62 63 // Add three more relocations, 8 byte deltas. 64 AddRelocation(0xf00d0010, &relocations); 65 AddRelocation(0xf00d0018, &relocations); 66 AddRelocation(0xf00d0020, &relocations); 67 68 packed.clear(); 69 codec.Encode(relocations, &packed); 70 71 EXPECT_EQ(6u, packed.size()); 72 // Two count-delta pairs present. 73 EXPECT_EQ(2u, packed[0]); 74 // Initial relocation. 75 EXPECT_EQ(0xf00d0000, packed[1]); 76 // Run of two relocations, 4 byte deltas. 77 EXPECT_EQ(2u, packed[2]); 78 EXPECT_EQ(4u, packed[3]); 79 // Run of three relocations, 8 byte deltas. 80 EXPECT_EQ(3u, packed[4]); 81 EXPECT_EQ(8u, packed[5]); 82} 83 84TEST(Rle, Decode) { 85 std::vector<Elf32_Word> packed; 86 std::vector<Elf32_Rel> relocations; 87 88 RelocationRunLengthCodec codec; 89 codec.Decode(packed, &relocations); 90 91 EXPECT_EQ(0u, relocations.size()); 92 93 // Two count-delta pairs. 94 packed.push_back(2u); 95 // Initial relocation. 96 packed.push_back(0xc0de0000); 97 // Run of two relocations, 4 byte deltas. 98 packed.push_back(2u); 99 packed.push_back(4u); 100 // Run of three relocations, 8 byte deltas. 101 packed.push_back(3u); 102 packed.push_back(8u); 103 104 relocations.clear(); 105 codec.Decode(packed, &relocations); 106 107 EXPECT_EQ(6u, relocations.size()); 108 // Initial relocation. 109 EXPECT_TRUE(CheckRelocation(0xc0de0000, relocations[0])); 110 // Two relocations, 4 byte deltas. 111 EXPECT_TRUE(CheckRelocation(0xc0de0004, relocations[1])); 112 EXPECT_TRUE(CheckRelocation(0xc0de0008, relocations[2])); 113 // Three relocations, 8 byte deltas. 114 EXPECT_TRUE(CheckRelocation(0xc0de0010, relocations[3])); 115 EXPECT_TRUE(CheckRelocation(0xc0de0018, relocations[4])); 116 EXPECT_TRUE(CheckRelocation(0xc0de0020, relocations[5])); 117} 118 119} // namespace relocation_packer 120