1 /*
2 * Copyright (C) 2017 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 "minikin/SparseBitSet.h"
18
19 #include <random>
20
21 #include <gtest/gtest.h>
22
23 #include "BufferUtils.h"
24
25 namespace minikin {
26
TEST(SparseBitSetTest,randomTest)27 TEST(SparseBitSetTest, randomTest) {
28 const uint32_t kTestRangeNum = 4096;
29
30 std::mt19937 mt; // Fix seeds to be able to reproduce the result.
31 std::uniform_int_distribution<uint16_t> distribution(1, 512);
32
33 std::vector<uint32_t> range{distribution(mt)};
34 for (size_t i = 1; i < kTestRangeNum * 2; ++i) {
35 range.push_back((range.back() - 1) + distribution(mt));
36 }
37
38 SparseBitSet bitset(range.data(), range.size() / 2);
39
40 uint32_t ch = 0;
41 for (size_t i = 0; i < range.size() / 2; ++i) {
42 uint32_t start = range[i * 2];
43 uint32_t end = range[i * 2 + 1];
44
45 for (; ch < start; ch++) {
46 ASSERT_FALSE(bitset.get(ch)) << std::hex << ch;
47 }
48 for (; ch < end; ch++) {
49 ASSERT_TRUE(bitset.get(ch)) << std::hex << ch;
50 }
51 }
52 for (; ch < 0x1FFFFFF; ++ch) {
53 ASSERT_FALSE(bitset.get(ch)) << std::hex << ch;
54 }
55 }
56
TEST(SparseBitSetTest,bufferTest)57 TEST(SparseBitSetTest, bufferTest) {
58 std::vector<uint32_t> range({10, 20});
59 SparseBitSet originalBitset(range.data(), range.size() / 2);
60 std::vector<uint8_t> buffer = writeToBuffer(originalBitset);
61 BufferReader reader(buffer.data());
62 SparseBitSet bitset(&reader);
63
64 for (size_t i = 0; i < 10; ++i) ASSERT_FALSE(bitset.get(i)) << i;
65 for (size_t i = 10; i < 20; ++i) ASSERT_TRUE(bitset.get(i)) << i;
66 for (size_t i = 20; i < 30; ++i) ASSERT_FALSE(bitset.get(i)) << i;
67 std::vector<uint8_t> newBuffer = writeToBuffer(bitset);
68 ASSERT_EQ(buffer, newBuffer);
69 }
70
TEST(SparseBitSetTest,emptyBitSetBufferTest)71 TEST(SparseBitSetTest, emptyBitSetBufferTest) {
72 SparseBitSet empty;
73 std::vector<uint8_t> buffer = writeToBuffer(empty);
74 BufferReader reader(buffer.data());
75 SparseBitSet bitset(&reader);
76
77 ASSERT_FALSE(bitset.get(0));
78 std::vector<uint8_t> newBuffer = writeToBuffer(bitset);
79 ASSERT_EQ(buffer, newBuffer);
80 }
81
82 } // namespace minikin
83