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 "sleb128.h"
6 
7 #include <vector>
8 #include "elf_traits.h"
9 #include "gtest/gtest.h"
10 
11 namespace relocation_packer {
12 
TEST(Sleb128,Encoder64)13 TEST(Sleb128, Encoder64) {
14   std::vector<uint64_t> values;
15   values.push_back(624485U);
16   values.push_back(0U);
17   values.push_back(1U);
18   values.push_back(63U);
19   values.push_back(64U);
20   values.push_back(static_cast<uint64_t>(-1));
21   values.push_back(static_cast<uint64_t>(-624485));
22 
23   Sleb128Encoder<uint64_t> encoder;
24   encoder.EnqueueAll(values);
25 
26   encoder.Enqueue(2147483647U);
27   encoder.Enqueue(static_cast<uint64_t>(-2147483648));
28   encoder.Enqueue(9223372036854775807ULL);
29   encoder.Enqueue(static_cast<uint64_t>(-9223372036854775807LL - 1));
30 
31   std::vector<uint8_t> encoding;
32   encoder.GetEncoding(&encoding);
33 
34   EXPECT_EQ(42u, encoding.size());
35   // 624485
36   EXPECT_EQ(0xe5, encoding[0]);
37   EXPECT_EQ(0x8e, encoding[1]);
38   EXPECT_EQ(0x26, encoding[2]);
39   // 0
40   EXPECT_EQ(0x00, encoding[3]);
41   // 1
42   EXPECT_EQ(0x01, encoding[4]);
43   // 63
44   EXPECT_EQ(0x3f, encoding[5]);
45   // 64
46   EXPECT_EQ(0xc0, encoding[6]);
47   EXPECT_EQ(0x00, encoding[7]);
48   // -1
49   EXPECT_EQ(0x7f, encoding[8]);
50   // -624485
51   EXPECT_EQ(0x9b, encoding[9]);
52   EXPECT_EQ(0xf1, encoding[10]);
53   EXPECT_EQ(0x59, encoding[11]);
54   // 2147483647
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]);
60   // -2147483648
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]);
88 }
89 
TEST(Sleb128,Decoder)90 TEST(Sleb128, Decoder) {
91   std::vector<uint8_t> encoding;
92   // 624485
93   encoding.push_back(0xe5);
94   encoding.push_back(0x8e);
95   encoding.push_back(0x26);
96   // 0
97   encoding.push_back(0x00);
98   // 1
99   encoding.push_back(0x01);
100   // 63
101   encoding.push_back(0x3f);
102   // 64
103   encoding.push_back(0xc0);
104   encoding.push_back(0x00);
105   // -1
106   encoding.push_back(0x7f);
107   // -624485
108   encoding.push_back(0x9b);
109   encoding.push_back(0xf1);
110   encoding.push_back(0x59);
111   // 2147483647
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);
117   // -2147483648
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);
145 
146   Sleb128Decoder<uint64_t> decoder(encoding, 0);
147 
148   EXPECT_EQ(624485U, decoder.Dequeue());
149 
150   std::vector<uint64_t> dequeued;
151   decoder.DequeueAll(&dequeued);
152 
153   EXPECT_EQ(10U, dequeued.size());
154   EXPECT_EQ(0U, dequeued[0]);
155   EXPECT_EQ(1U, dequeued[1]);
156   EXPECT_EQ(63U, dequeued[2]);
157   EXPECT_EQ(64U, dequeued[3]);
158   EXPECT_EQ(static_cast<uint64_t>(-1), dequeued[4]);
159   EXPECT_EQ(static_cast<uint64_t>(-624485), dequeued[5]);
160   EXPECT_EQ(2147483647U, dequeued[6]);
161   EXPECT_EQ(static_cast<uint64_t>(-2147483648), dequeued[7]);
162   EXPECT_EQ(9223372036854775807ULL, dequeued[8]);
163   EXPECT_EQ(static_cast<uint64_t>(-9223372036854775807LL - 1), dequeued[9]);
164 }
165 
166 }  // namespace relocation_packer
167