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