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.
8 #include "elf_traits.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace relocation_packer
{
13 TEST(Sleb128
, Encoder
) {
14 std::vector
<ELF::Sxword
> values
;
15 values
.push_back(624485);
21 values
.push_back(-624485);
23 Sleb128Encoder encoder
;
24 encoder
.EnqueueAll(values
);
26 encoder
.Enqueue(2147483647);
27 encoder
.Enqueue(-2147483648);
28 encoder
.Enqueue(9223372036854775807ll);
29 encoder
.Enqueue(-9223372036854775807ll - 1);
31 std::vector
<uint8_t> encoding
;
32 encoder
.GetEncoding(&encoding
);
34 EXPECT_EQ(42u, encoding
.size());
36 EXPECT_EQ(0xe5, encoding
[0]);
37 EXPECT_EQ(0x8e, encoding
[1]);
38 EXPECT_EQ(0x26, encoding
[2]);
40 EXPECT_EQ(0x00, encoding
[3]);
42 EXPECT_EQ(0x01, encoding
[4]);
44 EXPECT_EQ(0x3f, encoding
[5]);
46 EXPECT_EQ(0xc0, encoding
[6]);
47 EXPECT_EQ(0x00, encoding
[7]);
49 EXPECT_EQ(0x7f, encoding
[8]);
51 EXPECT_EQ(0x9b, encoding
[9]);
52 EXPECT_EQ(0xf1, encoding
[10]);
53 EXPECT_EQ(0x59, encoding
[11]);
55 EXPECT_EQ(0xff, encoding
[12]);
56 EXPECT_EQ(0xff, encoding
[13]);
57 EXPECT_EQ(0xff, encoding
[14]);
58 EXPECT_EQ(0xff, encoding
[15]);
59 EXPECT_EQ(0x07, encoding
[16]);
61 EXPECT_EQ(0x80, encoding
[17]);
62 EXPECT_EQ(0x80, encoding
[18]);
63 EXPECT_EQ(0x80, encoding
[19]);
64 EXPECT_EQ(0x80, encoding
[20]);
65 EXPECT_EQ(0x78, encoding
[21]);
66 // 9223372036854775807
67 EXPECT_EQ(0xff, encoding
[22]);
68 EXPECT_EQ(0xff, encoding
[23]);
69 EXPECT_EQ(0xff, encoding
[24]);
70 EXPECT_EQ(0xff, encoding
[25]);
71 EXPECT_EQ(0xff, encoding
[26]);
72 EXPECT_EQ(0xff, encoding
[27]);
73 EXPECT_EQ(0xff, encoding
[28]);
74 EXPECT_EQ(0xff, encoding
[29]);
75 EXPECT_EQ(0xff, encoding
[30]);
76 EXPECT_EQ(0x00, encoding
[31]);
77 // -9223372036854775808
78 EXPECT_EQ(0x80, encoding
[32]);
79 EXPECT_EQ(0x80, encoding
[33]);
80 EXPECT_EQ(0x80, encoding
[34]);
81 EXPECT_EQ(0x80, encoding
[35]);
82 EXPECT_EQ(0x80, encoding
[36]);
83 EXPECT_EQ(0x80, encoding
[37]);
84 EXPECT_EQ(0x80, encoding
[38]);
85 EXPECT_EQ(0x80, encoding
[39]);
86 EXPECT_EQ(0x80, encoding
[40]);
87 EXPECT_EQ(0x7f, encoding
[41]);
90 TEST(Sleb128
, Decoder
) {
91 std::vector
<uint8_t> encoding
;
93 encoding
.push_back(0xe5);
94 encoding
.push_back(0x8e);
95 encoding
.push_back(0x26);
97 encoding
.push_back(0x00);
99 encoding
.push_back(0x01);
101 encoding
.push_back(0x3f);
103 encoding
.push_back(0xc0);
104 encoding
.push_back(0x00);
106 encoding
.push_back(0x7f);
108 encoding
.push_back(0x9b);
109 encoding
.push_back(0xf1);
110 encoding
.push_back(0x59);
112 encoding
.push_back(0xff);
113 encoding
.push_back(0xff);
114 encoding
.push_back(0xff);
115 encoding
.push_back(0xff);
116 encoding
.push_back(0x07);
118 encoding
.push_back(0x80);
119 encoding
.push_back(0x80);
120 encoding
.push_back(0x80);
121 encoding
.push_back(0x80);
122 encoding
.push_back(0x78);
123 // 9223372036854775807
124 encoding
.push_back(0xff);
125 encoding
.push_back(0xff);
126 encoding
.push_back(0xff);
127 encoding
.push_back(0xff);
128 encoding
.push_back(0xff);
129 encoding
.push_back(0xff);
130 encoding
.push_back(0xff);
131 encoding
.push_back(0xff);
132 encoding
.push_back(0xff);
133 encoding
.push_back(0x00);
134 // -9223372036854775808
135 encoding
.push_back(0x80);
136 encoding
.push_back(0x80);
137 encoding
.push_back(0x80);
138 encoding
.push_back(0x80);
139 encoding
.push_back(0x80);
140 encoding
.push_back(0x80);
141 encoding
.push_back(0x80);
142 encoding
.push_back(0x80);
143 encoding
.push_back(0x80);
144 encoding
.push_back(0x7f);
146 Sleb128Decoder
decoder(encoding
);
148 EXPECT_EQ(624485, decoder
.Dequeue());
150 std::vector
<ELF::Sxword
> dequeued
;
151 decoder
.DequeueAll(&dequeued
);
153 EXPECT_EQ(10u, dequeued
.size());
154 EXPECT_EQ(0, dequeued
[0]);
155 EXPECT_EQ(1, dequeued
[1]);
156 EXPECT_EQ(63, dequeued
[2]);
157 EXPECT_EQ(64, dequeued
[3]);
158 EXPECT_EQ(-1, dequeued
[4]);
159 EXPECT_EQ(-624485, dequeued
[5]);
160 EXPECT_EQ(2147483647, dequeued
[6]);
161 EXPECT_EQ(-2147483648, dequeued
[7]);
162 EXPECT_EQ(9223372036854775807ll, dequeued
[8]);
163 EXPECT_EQ(-9223372036854775807ll - 1, dequeued
[9]);
166 } // namespace relocation_packer