SparseBitSetTest.cpp 1.6 KB

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