SparseBitSetTest.cpp revision db1b6cb7765091453d9b4dc7f6c2fb4d7123ab11
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 <random>
18
19#include <gtest/gtest.h>
20#include <minikin/SparseBitSet.h>
21
22namespace minikin {
23
24TEST(SparseBitSetTest, randomTest) {
25    const uint32_t kTestRangeNum = 4096;
26
27    std::mt19937 mt;  // Fix seeds to be able to reproduce the result.
28    std::uniform_int_distribution<uint16_t> distribution(1, 512);
29
30    std::vector<uint32_t> range { distribution(mt) };
31    for (size_t i = 1; i < kTestRangeNum * 2; ++i) {
32        range.push_back((range.back() - 1) + distribution(mt));
33    }
34
35    SparseBitSet bitset(range.data(), range.size() / 2);
36
37    uint32_t ch = 0;
38    for (size_t i = 0; i < range.size() / 2; ++i) {
39        uint32_t start = range[i * 2];
40        uint32_t end = range[i * 2 + 1];
41
42        for (; ch < start; ch++) {
43            ASSERT_FALSE(bitset.get(ch)) << std::hex << ch;
44        }
45        for (; ch < end; ch++) {
46            ASSERT_TRUE(bitset.get(ch)) << std::hex << ch;
47        }
48    }
49    for (; ch < 0x1FFFFFF; ++ch) {
50        ASSERT_FALSE(bitset.get(ch)) << std::hex << ch;
51    }
52}
53
54}  // namespace minikin
55