blob: 4fa80757c5f99c7e82788d552a716dfc8c20e5d4 [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
45TEST_F(Leb128Test, Singles) {
46 // Test individual encodings.
47 for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
48 UnsignedLeb128EncodingVector builder;
49 builder.PushBack(uleb128_tests[i].decoded);
50 const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
51 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
52 for (size_t j = 0; j < 5; ++j) {
53 if (j < builder.GetData().size()) {
54 EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
55 } else {
56 EXPECT_EQ(data_ptr[j], 0U) << " i = " << i << " j = " << j;
57 }
58 }
59 EXPECT_EQ(DecodeUnsignedLeb128(&data_ptr), uleb128_tests[i].decoded) << " i = " << i;
60 }
61}
62
63TEST_F(Leb128Test, Stream) {
64 // Encode a number of entries.
65 UnsignedLeb128EncodingVector builder;
66 for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
67 builder.PushBack(uleb128_tests[i].decoded);
68 }
69 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
70 for (size_t i = 0; i < arraysize(uleb128_tests); ++i) {
71 const uint8_t* data_ptr = &uleb128_tests[i].leb128_data[0];
72 for (size_t j = 0; j < 5; ++j) {
73 if (data_ptr[j] != 0) {
74 EXPECT_EQ(data_ptr[j], encoded_data_ptr[j]) << " i = " << i << " j = " << j;
75 }
76 }
77 EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), uleb128_tests[i].decoded) << " i = " << i;
78 }
79}
80
81TEST_F(Leb128Test, Speed) {
82 UniquePtr<Histogram<uint64_t> > enc_hist(new Histogram<uint64_t>("Leb128EncodeSpeedTest", 5));
83 UniquePtr<Histogram<uint64_t> > dec_hist(new Histogram<uint64_t>("Leb128DecodeSpeedTest", 5));
84 UnsignedLeb128EncodingVector builder;
85 // Push back 1024 chunks of 1024 values measuring encoding speed.
86 uint64_t last_time = NanoTime();
87 for (size_t i = 0; i < 1024; i++) {
88 for (size_t j = 0; j < 1024; j++) {
89 builder.PushBack((i * 1024) + j);
90 }
91 uint64_t cur_time = NanoTime();
92 enc_hist->AddValue(cur_time - last_time);
93 last_time = cur_time;
94 }
95 // Verify encoding and measure decode speed.
96 const uint8_t* encoded_data_ptr = &builder.GetData()[0];
97 last_time = NanoTime();
98 for (size_t i = 0; i < 1024; i++) {
99 for (size_t j = 0; j < 1024; j++) {
100 EXPECT_EQ(DecodeUnsignedLeb128(&encoded_data_ptr), (i * 1024) + j);
101 }
102 uint64_t cur_time = NanoTime();
103 dec_hist->AddValue(cur_time - last_time);
104 last_time = cur_time;
105 }
106
107 Histogram<uint64_t>::CumulativeData enc_data;
108 enc_hist->CreateHistogram(&enc_data);
109 enc_hist->PrintConfidenceIntervals(std::cout, 0.99, enc_data);
110
111 Histogram<uint64_t>::CumulativeData dec_data;
112 dec_hist->CreateHistogram(&dec_data);
113 dec_hist->PrintConfidenceIntervals(std::cout, 0.99, dec_data);
114}
115
116} // namespace art