• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "leb128.h"
6 
7 #include <vector>
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace relocation_packer {
11 
TEST(Leb128,Encoder)12 TEST(Leb128, Encoder) {
13   std::vector<ELF::Xword> values;
14   values.push_back(624485);
15   values.push_back(0);
16   values.push_back(1);
17   values.push_back(127);
18   values.push_back(128);
19 
20   Leb128Encoder encoder;
21   encoder.EnqueueAll(values);
22 
23   encoder.Enqueue(4294967295);
24   encoder.Enqueue(18446744073709551615ul);
25 
26   std::vector<uint8_t> encoding;
27   encoder.GetEncoding(&encoding);
28 
29   EXPECT_EQ(23, encoding.size());
30   // 624485
31   EXPECT_EQ(0xe5, encoding[0]);
32   EXPECT_EQ(0x8e, encoding[1]);
33   EXPECT_EQ(0x26, encoding[2]);
34   // 0
35   EXPECT_EQ(0x00, encoding[3]);
36   // 1
37   EXPECT_EQ(0x01, encoding[4]);
38   // 127
39   EXPECT_EQ(0x7f, encoding[5]);
40   // 128
41   EXPECT_EQ(0x80, encoding[6]);
42   EXPECT_EQ(0x01, encoding[7]);
43   // 4294967295
44   EXPECT_EQ(0xff, encoding[8]);
45   EXPECT_EQ(0xff, encoding[9]);
46   EXPECT_EQ(0xff, encoding[10]);
47   EXPECT_EQ(0xff, encoding[11]);
48   EXPECT_EQ(0x0f, encoding[12]);
49   // 18446744073709551615
50   EXPECT_EQ(0xff, encoding[13]);
51   EXPECT_EQ(0xff, encoding[14]);
52   EXPECT_EQ(0xff, encoding[15]);
53   EXPECT_EQ(0xff, encoding[16]);
54   EXPECT_EQ(0xff, encoding[17]);
55   EXPECT_EQ(0xff, encoding[18]);
56   EXPECT_EQ(0xff, encoding[19]);
57   EXPECT_EQ(0xff, encoding[20]);
58   EXPECT_EQ(0xff, encoding[21]);
59   EXPECT_EQ(0x01, encoding[22]);
60 }
61 
TEST(Leb128,Decoder)62 TEST(Leb128, Decoder) {
63   std::vector<uint8_t> encoding;
64   // 624485
65   encoding.push_back(0xe5);
66   encoding.push_back(0x8e);
67   encoding.push_back(0x26);
68   // 0
69   encoding.push_back(0x00);
70   // 1
71   encoding.push_back(0x01);
72   // 127
73   encoding.push_back(0x7f);
74   // 128
75   encoding.push_back(0x80);
76   encoding.push_back(0x01);
77   // 4294967295
78   encoding.push_back(0xff);
79   encoding.push_back(0xff);
80   encoding.push_back(0xff);
81   encoding.push_back(0xff);
82   encoding.push_back(0x0f);
83   // 18446744073709551615
84   encoding.push_back(0xff);
85   encoding.push_back(0xff);
86   encoding.push_back(0xff);
87   encoding.push_back(0xff);
88   encoding.push_back(0xff);
89   encoding.push_back(0xff);
90   encoding.push_back(0xff);
91   encoding.push_back(0xff);
92   encoding.push_back(0xff);
93   encoding.push_back(0x01);
94 
95   Leb128Decoder decoder(encoding);
96 
97   EXPECT_EQ(624485, decoder.Dequeue());
98 
99   std::vector<ELF::Xword> dequeued;
100   decoder.DequeueAll(&dequeued);
101 
102   EXPECT_EQ(6, dequeued.size());
103   EXPECT_EQ(0, dequeued[0]);
104   EXPECT_EQ(1, dequeued[1]);
105   EXPECT_EQ(127, dequeued[2]);
106   EXPECT_EQ(128, dequeued[3]);
107   EXPECT_EQ(4294967295, dequeued[4]);
108   EXPECT_EQ(18446744073709551615ul, dequeued[5]);
109 }
110 
111 }  // namespace relocation_packer
112