blob: 3162ca5f4665644a6730fb43f186bfe1f7f2cc7d [file] [log] [blame]
Ian Rogers500793f2013-11-14 17:49:12 -08001/*
2 * Copyright (C) 2013 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 "base/histogram-inl.h"
18#include "common_test.h"
19#include "leb128.h"
20#include "leb128_encoder.h"
21
22namespace art {
23
24class Leb128Test : public CommonTest {};
25
26struct DecodeUnsignedLeb128TestCase {
27 uint32_t decoded;
28 uint8_t leb128_data[5];
29};
30
31static DecodeUnsignedLeb128TestCase uleb128_tests[] = {
32 {0, {0, 0, 0, 0, 0}},
33 {1, {1, 0, 0, 0, 0}},
34 {0x7F, {0x7F, 0, 0, 0, 0}},
35 {0x80, {0x80, 1, 0, 0, 0}},
36 {0x81, {0x81, 1, 0, 0, 0}},
37 {0xFF, {0xFF, 1, 0, 0, 0}},
38 {0x4000, {0x80, 0x80, 1, 0, 0}},
39 {0x4001, {0x81, 0x80, 1, 0, 0}},
40 {0x4081, {0x81, 0x81, 1, 0, 0}},
41 {0x0FFFFFFF, {0xFF, 0xFF, 0xFF, 0x7F, 0}},
42 {0xFFFFFFFF, {0xFF, 0xFF, 0xFF, 0xFF, 0xF}},
43};
44
Vladimir Marko1e6cb632013-11-28 16:27:29 +000045struct DecodeSignedLeb128TestCase {
46 int32_t decoded;
47 uint8_t leb128_data[5];
48};
49
50static DecodeSignedLeb128TestCase sleb128_tests[] = {
51 {0, {0, 0, 0, 0, 0}},
52 {1, {1, 0, 0, 0, 0}},
53 {0x3F, {0x3F, 0, 0, 0, 0}},
54 {0x40, {0xC0, 0 /* sign bit */, 0, 0, 0}},
55 {0x41, {0xC1, 0 /* sign bit */, 0, 0, 0}},
56 {0x80, {0x80, 1, 0, 0, 0}},
57 {0xFF, {0xFF, 1, 0, 0, 0}},
58 {0x1FFF, {0xFF, 0x3F, 0, 0, 0}},
59 {0x2000, {0x80, 0xC0, 0 /* sign bit */, 0, 0}},
60 {0x2001, {0x81, 0xC0, 0 /* sign bit */, 0, 0}},
61 {0x2081, {0x81, 0xC1, 0 /* sign bit */, 0, 0}},
62 {0x4000, {0x80, 0x80, 1, 0, 0}},
63 {0x0FFFFF, {0xFF, 0xFF, 0x3F, 0, 0}},
64 {0x100000, {0x80, 0x80, 0xC0, 0 /* sign bit */, 0}},
65 {0x100001, {0x81, 0x80, 0xC0, 0 /* sign bit */, 0}},
66 {0x100081, {0x81, 0x81, 0xC0, 0 /* sign bit */, 0}},
67 {0x104081, {0x81, 0x81, 0xC1, 0 /* sign bit */, 0}},
68 {0x200000, {0x80, 0x80, 0x80, 1, 0}},
69 {0x7FFFFFF, {0xFF, 0xFF, 0xFF, 0x3F, 0}},
70 {0x8000000, {0x80, 0x80, 0x80, 0xC0, 0 /* sign bit */}},
71 {0x8000001, {0x81, 0x80, 0x80, 0xC0, 0 /* sign bit */}},
72 {0x8000081, {0x81, 0x81, 0x80, 0xC0, 0 /* sign bit */}},
73 {0x8004081, {0x81, 0x81, 0x81, 0xC0, 0 /* sign bit */}},
74 {0x8204081, {0x81, 0x81, 0x81, 0xC1, 0 /* sign bit */}},
75 {0x0FFFFFFF, {0xFF, 0xFF, 0xFF, 0xFF, 0 /* sign bit */}},
76 {0x10000000, {0x80, 0x80, 0x80, 0x80, 1}},
77 {0x7FFFFFFF, {0xFF, 0xFF, 0xFF, 0xFF, 0x7}},
78 {-1, {0x7F, 0, 0, 0, 0}},
79 {-2, {0x7E, 0, 0, 0, 0}},
80 {-0x3F, {0x41, 0, 0, 0, 0}},
81 {-0x40, {0x40, 0, 0, 0, 0}},
82 {-0x41, {0xBF, 0x7F, 0, 0, 0}},
83 {-0x80, {0x80, 0x7F, 0, 0, 0}},
84 {-0x81, {0xFF, 0x7E, 0, 0, 0}},
85 {-0x00002000, {0x80, 0x40, 0, 0, 0}},
86 {-0x00002001, {0xFF, 0xBF, 0x7F, 0, 0}},
87 {-0x00100000, {0x80, 0x80, 0x40, 0, 0}},
88 {-0x00100001, {0xFF, 0xFF, 0xBF, 0x7F, 0}},
89 {-0x08000000, {0x80, 0x80, 0x80, 0x40, 0}},
90 {-0x08000001, {0xFF, 0xFF, 0xFF, 0xBF, 0x7F}},
91 {-0x20000000, {0x80, 0x80, 0x80, 0x80, 0x7E}},
92 {(-1) << 31, {0x80, 0x80, 0x80, 0x80, 0x78}},
93};
94
95TEST_F(Leb128Test, UnsignedSingles) {
Ian Rogers500793f2013-11-14 17:49:12 -080096 // Test individual encodings.
97 for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +000098 Leb128EncodingVector builder;
99 builder.PushBackUnsigned(uleb128_tests[i].decoded);
Ian Rogers500793f2013-11-14 17:49:12 -0800100 const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
101 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
102 for (size_t j = 0; j < 5; ++j) {
103 if (j < builder.GetData().size()) {
104 EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
105 } else {
106 EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
107 }
108 }
109 EXPECT_EQ(DecodeUnsignedLeb128(&data_ptr), uleb128_tests[i].decoded) << " i = " << i;
110 }
111}
112
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000113TEST_F(Leb128Test, UnsignedStream) {
Ian Rogers500793f2013-11-14 17:49:12 -0800114 // Encode a number of entries.
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000115 Leb128EncodingVector builder;
Ian Rogers500793f2013-11-14 17:49:12 -0800116 for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000117 builder.PushBackUnsigned(uleb128_tests[i].decoded);
Ian Rogers500793f2013-11-14 17:49:12 -0800118 }
119 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
120 for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
121 const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
122 for (size_t j = 0; j < 5; ++j) {
123 if (data_ptr[j] != 0) {
124 EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
125 }
126 }
127 EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), uleb128_tests[i].decoded) << " i = " << i;
128 }
129}
130
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000131TEST_F(Leb128Test, SignedSingles) {
132 // Test individual encodings.
133 for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
134 Leb128EncodingVector builder;
135 builder.PushBackSigned(sleb128_tests[i].decoded);
136 const uint8_t* data_ptr = &sleb128_tests[i].leb128_data[0];
137 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
138 for (size_t j = 0; j < 5; ++j) {
139 if (j < builder.GetData().size()) {
140 EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
141 } else {
142 EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
143 }
144 }
145 EXPECT_EQ(DecodeSignedLeb128(&data_ptr), sleb128_tests[i].decoded) << " i = " << i;
146 }
147}
148
149TEST_F(Leb128Test, SignedStream) {
150 // Encode a number of entries.
151 Leb128EncodingVector builder;
152 for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
153 builder.PushBackSigned(sleb128_tests[i].decoded);
154 }
155 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
156 for (size_t i = 0; i < arraysize(sleb128_tests); ++i) {
157 const uint8_t* data_ptr = &sleb128_tests[i].leb128_data[0];
158 for (size_t j = 0; j < 5; ++j) {
159 if (data_ptr[j] != 0) {
160 EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
161 }
162 }
163 EXPECT_EQ(DecodeSignedLeb128(&encoded_data_ptr), sleb128_tests[i].decoded) << " i = " << i;
164 }
165}
166
Ian Rogers500793f2013-11-14 17:49:12 -0800167TEST_F(Leb128Test, Speed) {
168 UniquePtr<Histogram<uint64_t> > enc_hist(new Histogram<uint64_t>("Leb128EncodeSpeedTest", 5));
169 UniquePtr<Histogram<uint64_t> > dec_hist(new Histogram<uint64_t>("Leb128DecodeSpeedTest", 5));
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000170 Leb128EncodingVector builder;
Ian Rogers500793f2013-11-14 17:49:12 -0800171 // Push back 1024 chunks of 1024 values measuring encoding speed.
172 uint64_t last_time = NanoTime();
173 for (size_t i = 0; i < 1024; i++) {
174 for (size_t j = 0; j < 1024; j++) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000175 builder.PushBackUnsigned((i * 1024) + j);
Ian Rogers500793f2013-11-14 17:49:12 -0800176 }
177 uint64_t cur_time = NanoTime();
178 enc_hist->AddValue(cur_time - last_time);
179 last_time = cur_time;
180 }
181 // Verify encoding and measure decode speed.
182 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
183 last_time = NanoTime();
184 for (size_t i = 0; i < 1024; i++) {
185 for (size_t j = 0; j < 1024; j++) {
186 EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), (i * 1024) + j);
187 }
188 uint64_t cur_time = NanoTime();
189 dec_hist->AddValue(cur_time - last_time);
190 last_time = cur_time;
191 }
192
193 Histogram<uint64_t>::CumulativeData enc_data;
194 enc_hist->CreateHistogram(&enc_data);
195 enc_hist->PrintConfidenceIntervals(std::cout, 0.99, enc_data);
196
197 Histogram<uint64_t>::CumulativeData dec_data;
198 dec_hist->CreateHistogram(&dec_data);
199 dec_hist->PrintConfidenceIntervals(std::cout, 0.99, dec_data);
200}
201
202} // namespace art