1 /*
2  * Copyright (C) 2018 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 "bit_memory_region.h"
18 
19 #include "gtest/gtest.h"
20 
21 namespace art {
22 
CheckBits(uint8_t * data,size_t size,uint32_t init,size_t offset,size_t length,uint32_t value)23 static void CheckBits(uint8_t* data,
24                       size_t size,
25                       uint32_t init,
26                       size_t offset,
27                       size_t length,
28                       uint32_t value) {
29   for (size_t i = 0; i < size * kBitsPerByte; i++) {
30     uint8_t expected = (offset <= i && i < offset + length) ? value >> (i - offset) : init;
31     uint8_t actual = data[i / kBitsPerByte] >> (i % kBitsPerByte);
32     EXPECT_EQ(expected & 1, actual & 1);
33   }
34 }
35 
TEST(BitMemoryRegion,TestVarint)36 TEST(BitMemoryRegion, TestVarint) {
37   for (size_t start_bit_offset = 0; start_bit_offset <= 32; start_bit_offset++) {
38     uint32_t values[] = { 0, 1, 11, 12, 15, 16, 255, 256, 1u << 16, 1u << 24, ~1u, ~0u };
39     for (uint32_t value : values) {
40       std::vector<uint8_t> buffer;
41       BitMemoryWriter<std::vector<uint8_t>> writer(&buffer, start_bit_offset);
42       writer.WriteVarint(value);
43 
44       BitMemoryReader reader(buffer.data(), start_bit_offset);
45       uint32_t result = reader.ReadVarint();
46       uint32_t upper_bound = RoundUp(MinimumBitsToStore(value), kBitsPerByte) + kVarintBits;
47       EXPECT_EQ(writer.NumberOfWrittenBits(), reader.NumberOfReadBits());
48       EXPECT_EQ(value, result);
49       EXPECT_GE(upper_bound, writer.NumberOfWrittenBits());
50     }
51   }
52 }
53 
TEST(BitMemoryRegion,TestBit)54 TEST(BitMemoryRegion, TestBit) {
55   uint8_t data[sizeof(uint32_t) * 2];
56   for (size_t bit_offset = 0; bit_offset < 2 * sizeof(uint32_t) * kBitsPerByte; ++bit_offset) {
57     for (uint32_t initial_value = 0; initial_value <= 1; initial_value++) {
58       for (uint32_t value = 0; value <= 1; value++) {
59         // Check Store and Load with bit_offset set on the region.
60         std::fill_n(data, sizeof(data), initial_value * 0xFF);
61         BitMemoryRegion bmr1(MemoryRegion(&data, sizeof(data)), bit_offset, 1);
62         bmr1.StoreBit(0, value);
63         EXPECT_EQ(bmr1.LoadBit(0), value);
64         CheckBits(data, sizeof(data), initial_value, bit_offset, 1, value);
65         // Check Store and Load with bit_offset set on the methods.
66         std::fill_n(data, sizeof(data), initial_value * 0xFF);
67         BitMemoryRegion bmr2(MemoryRegion(&data, sizeof(data)));
68         bmr2.StoreBit(bit_offset, value);
69         EXPECT_EQ(bmr2.LoadBit(bit_offset), value);
70         CheckBits(data, sizeof(data), initial_value, bit_offset, 1, value);
71       }
72     }
73   }
74 }
75 
TEST(BitMemoryRegion,TestBits)76 TEST(BitMemoryRegion, TestBits) {
77   uint8_t data[sizeof(uint32_t) * 4];
78   for (size_t bit_offset = 0; bit_offset < 3 * sizeof(uint32_t) * kBitsPerByte; ++bit_offset) {
79     uint32_t mask = 0;
80     for (size_t bit_length = 0; bit_length < sizeof(uint32_t) * kBitsPerByte; ++bit_length) {
81       const uint32_t value = 0xDEADBEEF & mask;
82       for (uint32_t initial_value = 0; initial_value <= 1; initial_value++) {
83         // Check Store and Load with bit_offset set on the region.
84         std::fill_n(data, sizeof(data), initial_value * 0xFF);
85         BitMemoryRegion bmr1(MemoryRegion(&data, sizeof(data)), bit_offset, bit_length);
86         bmr1.StoreBits(0, value, bit_length);
87         EXPECT_EQ(bmr1.LoadBits(0, bit_length), value);
88         CheckBits(data, sizeof(data), initial_value, bit_offset, bit_length, value);
89         // Check Store and Load with bit_offset set on the methods.
90         std::fill_n(data, sizeof(data), initial_value * 0xFF);
91         BitMemoryRegion bmr2(MemoryRegion(&data, sizeof(data)));
92         bmr2.StoreBits(bit_offset, value, bit_length);
93         EXPECT_EQ(bmr2.LoadBits(bit_offset, bit_length), value);
94         CheckBits(data, sizeof(data), initial_value, bit_offset, bit_length, value);
95       }
96       mask = (mask << 1) | 1;
97     }
98   }
99 }
100 
101 }  // namespace art
102