File: bit_memory_region_test.cc

package info (click to toggle)
android-platform-art 11.0.0%2Br48-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,932 kB
  • sloc: cpp: 459,858; java: 163,268; asm: 22,644; python: 9,815; sh: 6,330; ansic: 4,117; xml: 2,855; perl: 77; makefile: 73
file content (101 lines) | stat: -rw-r--r-- 4,286 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "bit_memory_region.h"

#include "gtest/gtest.h"

namespace art {

static void CheckBits(uint8_t* data,
                      size_t size,
                      uint32_t init,
                      size_t offset,
                      size_t length,
                      uint32_t value) {
  for (size_t i = 0; i < size * kBitsPerByte; i++) {
    uint8_t expected = (offset <= i && i < offset + length) ? value >> (i - offset) : init;
    uint8_t actual = data[i / kBitsPerByte] >> (i % kBitsPerByte);
    EXPECT_EQ(expected & 1, actual & 1);
  }
}

TEST(BitMemoryRegion, TestVarint) {
  for (size_t start_bit_offset = 0; start_bit_offset <= 32; start_bit_offset++) {
    uint32_t values[] = { 0, 1, 11, 12, 15, 16, 255, 256, 1u << 16, 1u << 24, ~1u, ~0u };
    for (uint32_t value : values) {
      std::vector<uint8_t> buffer;
      BitMemoryWriter<std::vector<uint8_t>> writer(&buffer, start_bit_offset);
      writer.WriteVarint(value);

      BitMemoryReader reader(buffer.data(), start_bit_offset);
      uint32_t result = reader.ReadVarint();
      uint32_t upper_bound = RoundUp(MinimumBitsToStore(value), kBitsPerByte) + kVarintBits;
      EXPECT_EQ(writer.NumberOfWrittenBits(), reader.NumberOfReadBits());
      EXPECT_EQ(value, result);
      EXPECT_GE(upper_bound, writer.NumberOfWrittenBits());
    }
  }
}

TEST(BitMemoryRegion, TestBit) {
  uint8_t data[sizeof(uint32_t) * 2];
  for (size_t bit_offset = 0; bit_offset < 2 * sizeof(uint32_t) * kBitsPerByte; ++bit_offset) {
    for (uint32_t initial_value = 0; initial_value <= 1; initial_value++) {
      for (uint32_t value = 0; value <= 1; value++) {
        // Check Store and Load with bit_offset set on the region.
        std::fill_n(data, sizeof(data), initial_value * 0xFF);
        BitMemoryRegion bmr1(MemoryRegion(&data, sizeof(data)), bit_offset, 1);
        bmr1.StoreBit(0, value);
        EXPECT_EQ(bmr1.LoadBit(0), value);
        CheckBits(data, sizeof(data), initial_value, bit_offset, 1, value);
        // Check Store and Load with bit_offset set on the methods.
        std::fill_n(data, sizeof(data), initial_value * 0xFF);
        BitMemoryRegion bmr2(MemoryRegion(&data, sizeof(data)));
        bmr2.StoreBit(bit_offset, value);
        EXPECT_EQ(bmr2.LoadBit(bit_offset), value);
        CheckBits(data, sizeof(data), initial_value, bit_offset, 1, value);
      }
    }
  }
}

TEST(BitMemoryRegion, TestBits) {
  uint8_t data[sizeof(uint32_t) * 4];
  for (size_t bit_offset = 0; bit_offset < 3 * sizeof(uint32_t) * kBitsPerByte; ++bit_offset) {
    uint32_t mask = 0;
    for (size_t bit_length = 0; bit_length < sizeof(uint32_t) * kBitsPerByte; ++bit_length) {
      const uint32_t value = 0xDEADBEEF & mask;
      for (uint32_t initial_value = 0; initial_value <= 1; initial_value++) {
        // Check Store and Load with bit_offset set on the region.
        std::fill_n(data, sizeof(data), initial_value * 0xFF);
        BitMemoryRegion bmr1(MemoryRegion(&data, sizeof(data)), bit_offset, bit_length);
        bmr1.StoreBits(0, value, bit_length);
        EXPECT_EQ(bmr1.LoadBits(0, bit_length), value);
        CheckBits(data, sizeof(data), initial_value, bit_offset, bit_length, value);
        // Check Store and Load with bit_offset set on the methods.
        std::fill_n(data, sizeof(data), initial_value * 0xFF);
        BitMemoryRegion bmr2(MemoryRegion(&data, sizeof(data)));
        bmr2.StoreBits(bit_offset, value, bit_length);
        EXPECT_EQ(bmr2.LoadBits(bit_offset, bit_length), value);
        CheckBits(data, sizeof(data), initial_value, bit_offset, bit_length, value);
      }
      mask = (mask << 1) | 1;
    }
  }
}

}  // namespace art