bits_test.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2017 Google
  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 "Firestore/Port/bits.h"
  17. #include <iostream>
  18. #include "base/commandlineflags.h"
  19. #include "testing/base/public/gunit.h"
  20. #include "util/random/mt_random.h"
  21. using Firestore::Bits;
  22. DEFINE_int32(num_iterations, 10000, "Number of test iterations to run.");
  23. class BitsTest : public testing::Test {
  24. public:
  25. BitsTest() : random_(testing::FLAGS_gunit_random_seed) {}
  26. protected:
  27. MTRandom random_;
  28. };
  29. TEST_F(BitsTest, Log2EdgeCases) {
  30. std::cout << "TestLog2EdgeCases" << std::endl;
  31. EXPECT_EQ(-1, Bits::Log2Floor(0));
  32. EXPECT_EQ(-1, Bits::Log2Floor64(0));
  33. for (int i = 0; i < 32; i++) {
  34. uint32 n = 1U << i;
  35. EXPECT_EQ(i, Bits::Log2Floor(n));
  36. EXPECT_EQ(i, Bits::Log2FloorNonZero(n));
  37. if (n > 2) {
  38. EXPECT_EQ(i - 1, Bits::Log2Floor(n - 1));
  39. EXPECT_EQ(i, Bits::Log2Floor(n + 1));
  40. EXPECT_EQ(i - 1, Bits::Log2FloorNonZero(n - 1));
  41. EXPECT_EQ(i, Bits::Log2FloorNonZero(n + 1));
  42. }
  43. }
  44. for (int i = 0; i < 64; i++) {
  45. uint64 n = 1ULL << i;
  46. EXPECT_EQ(i, Bits::Log2Floor64(n));
  47. EXPECT_EQ(i, Bits::Log2FloorNonZero64(n));
  48. if (n > 2) {
  49. EXPECT_EQ(i - 1, Bits::Log2Floor64(n - 1));
  50. EXPECT_EQ(i, Bits::Log2Floor64(n + 1));
  51. EXPECT_EQ(i - 1, Bits::Log2FloorNonZero64(n - 1));
  52. EXPECT_EQ(i, Bits::Log2FloorNonZero64(n + 1));
  53. }
  54. }
  55. }
  56. TEST_F(BitsTest, Log2Random) {
  57. std::cout << "TestLog2Random" << std::endl;
  58. for (int i = 0; i < FLAGS_num_iterations; i++) {
  59. int maxbit = -1;
  60. uint32 n = 0;
  61. while (!random_.OneIn(32)) {
  62. int bit = random_.Uniform(32);
  63. n |= (1U << bit);
  64. maxbit = std::max(bit, maxbit);
  65. }
  66. EXPECT_EQ(maxbit, Bits::Log2Floor(n));
  67. if (n != 0) {
  68. EXPECT_EQ(maxbit, Bits::Log2FloorNonZero(n));
  69. }
  70. }
  71. }
  72. TEST_F(BitsTest, Log2Random64) {
  73. std::cout << "TestLog2Random64" << std::endl;
  74. for (int i = 0; i < FLAGS_num_iterations; i++) {
  75. int maxbit = -1;
  76. uint64 n = 0;
  77. while (!random_.OneIn(64)) {
  78. int bit = random_.Uniform(64);
  79. n |= (1ULL << bit);
  80. maxbit = std::max(bit, maxbit);
  81. }
  82. EXPECT_EQ(maxbit, Bits::Log2Floor64(n));
  83. if (n != 0) {
  84. EXPECT_EQ(maxbit, Bits::Log2FloorNonZero64(n));
  85. }
  86. }
  87. }
  88. TEST(Bits, Port32) {
  89. for (int shift = 0; shift < 32; shift++) {
  90. for (int delta = -1; delta <= +1; delta++) {
  91. const uint32 v = (static_cast<uint32>(1) << shift) + delta;
  92. EXPECT_EQ(Bits::Log2Floor_Portable(v), Bits::Log2Floor(v)) << v;
  93. if (v != 0) {
  94. EXPECT_EQ(Bits::Log2FloorNonZero_Portable(v), Bits::Log2FloorNonZero(v))
  95. << v;
  96. }
  97. }
  98. }
  99. static const uint32 M32 = kuint32max;
  100. EXPECT_EQ(Bits::Log2Floor_Portable(M32), Bits::Log2Floor(M32)) << M32;
  101. EXPECT_EQ(Bits::Log2FloorNonZero_Portable(M32), Bits::Log2FloorNonZero(M32))
  102. << M32;
  103. }
  104. TEST(Bits, Port64) {
  105. for (int shift = 0; shift < 64; shift++) {
  106. for (int delta = -1; delta <= +1; delta++) {
  107. const uint64 v = (static_cast<uint64>(1) << shift) + delta;
  108. EXPECT_EQ(Bits::Log2Floor64_Portable(v), Bits::Log2Floor64(v)) << v;
  109. if (v != 0) {
  110. EXPECT_EQ(Bits::Log2FloorNonZero64_Portable(v),
  111. Bits::Log2FloorNonZero64(v))
  112. << v;
  113. }
  114. }
  115. }
  116. static const uint64 M64 = kuint64max;
  117. EXPECT_EQ(Bits::Log2Floor64_Portable(M64), Bits::Log2Floor64(M64)) << M64;
  118. EXPECT_EQ(Bits::Log2FloorNonZero64_Portable(M64),
  119. Bits::Log2FloorNonZero64(M64))
  120. << M64;
  121. }