bitvector_test.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // (C) Copyright 2017, Google Inc.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. // Unless required by applicable law or agreed to in writing, software
  7. // distributed under the License is distributed on an "AS IS" BASIS,
  8. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. // See the License for the specific language governing permissions and
  10. // limitations under the License.
  11. #include <cmath>
  12. #include <cstdio>
  13. #include <string>
  14. #include "bitvector.h"
  15. #include "include_gunit.h"
  16. const int kPrimeLimit = 1000;
  17. namespace tesseract {
  18. class BitVectorTest : public testing::Test {
  19. protected:
  20. void SetUp() override {
  21. std::locale::global(std::locale(""));
  22. file::MakeTmpdir();
  23. }
  24. public:
  25. std::string OutputNameToPath(const std::string &name) {
  26. return file::JoinPath(FLAGS_test_tmpdir, name);
  27. }
  28. // Computes primes up to kPrimeLimit, using the sieve of Eratosthenes.
  29. void ComputePrimes(BitVector *map) {
  30. map->Init(kPrimeLimit + 1);
  31. TestAll(*map, false);
  32. map->SetBit(2);
  33. // Set all the odds to true.
  34. for (int i = 3; i <= kPrimeLimit; i += 2) {
  35. map->SetValue(i, true);
  36. }
  37. int factor_limit = static_cast<int>(sqrt(1.0 + kPrimeLimit));
  38. for (int f = 3; f <= factor_limit; f += 2) {
  39. if (map->At(f)) {
  40. for (int m = 2; m * f <= kPrimeLimit; ++m) {
  41. map->ResetBit(f * m);
  42. }
  43. }
  44. }
  45. }
  46. void TestPrimes(const BitVector &map) {
  47. // Now all primes in the vector are true, and all others false.
  48. // According to Wikipedia, there are 168 primes under 1000, the last
  49. // of which is 997.
  50. int total_primes = 0;
  51. for (int i = 0; i <= kPrimeLimit; ++i) {
  52. if (map[i]) {
  53. ++total_primes;
  54. }
  55. }
  56. EXPECT_EQ(168, total_primes);
  57. EXPECT_TRUE(map[997]);
  58. EXPECT_FALSE(map[998]);
  59. EXPECT_FALSE(map[999]);
  60. }
  61. // Test that all bits in the vector have the given value.
  62. void TestAll(const BitVector &map, bool value) {
  63. for (int i = 0; i < map.size(); ++i) {
  64. EXPECT_EQ(value, map[i]);
  65. }
  66. }
  67. // Sets up a BitVector with bit patterns for byte values in
  68. // [start_byte, end_byte) positioned every spacing bytes (for spacing >= 1)
  69. // with spacing-1 zero bytes in between the pattern bytes.
  70. void SetBitPattern(int start_byte, int end_byte, int spacing, BitVector *bv) {
  71. bv->Init((end_byte - start_byte) * 8 * spacing);
  72. for (int byte_value = start_byte; byte_value < end_byte; ++byte_value) {
  73. for (int bit = 0; bit < 8; ++bit) {
  74. if (byte_value & (1 << bit)) {
  75. bv->SetBit((byte_value - start_byte) * 8 * spacing + bit);
  76. }
  77. }
  78. }
  79. }
  80. // Expects that every return from NextSetBit is really set and that all others
  81. // are really not set. Checks the return from NumSetBits also.
  82. void ExpectCorrectBits(const BitVector &bv) {
  83. int bit_index = -1;
  84. int prev_bit_index = -1;
  85. int num_bits_tested = 0;
  86. while ((bit_index = bv.NextSetBit(bit_index)) >= 0) {
  87. EXPECT_LT(bit_index, bv.size());
  88. // All bits in between must be 0.
  89. for (int i = prev_bit_index + 1; i < bit_index; ++i) {
  90. EXPECT_EQ(0, bv[i]) << "i = " << i << " prev = " << prev_bit_index;
  91. }
  92. // This bit must be 1.
  93. EXPECT_EQ(1, bv[bit_index]) << "Bit index = " << bit_index;
  94. ++num_bits_tested;
  95. prev_bit_index = bit_index;
  96. }
  97. // Check the bits between the last and the end.
  98. for (int i = prev_bit_index + 1; i < bv.size(); ++i) {
  99. EXPECT_EQ(0, bv[i]);
  100. }
  101. EXPECT_EQ(num_bits_tested, bv.NumSetBits());
  102. }
  103. };
  104. // Tests the sieve of Eratosthenes as a way of testing set/reset and I/O.
  105. TEST_F(BitVectorTest, Primes) {
  106. BitVector map;
  107. ComputePrimes(&map);
  108. TestPrimes(map);
  109. // It still works if we use the copy constructor.
  110. BitVector map2(map);
  111. TestPrimes(map2);
  112. // Or if we assign it.
  113. BitVector map3;
  114. map3 = map;
  115. TestPrimes(map3);
  116. // Test file i/o too.
  117. std::string filename = OutputNameToPath("primesbitvector");
  118. FILE *fp = fopen(filename.c_str(), "wb");
  119. ASSERT_TRUE(fp != nullptr);
  120. EXPECT_TRUE(map.Serialize(fp));
  121. fclose(fp);
  122. fp = fopen(filename.c_str(), "rb");
  123. ASSERT_TRUE(fp != nullptr);
  124. BitVector read_map;
  125. EXPECT_TRUE(read_map.DeSerialize(false, fp));
  126. fclose(fp);
  127. TestPrimes(read_map);
  128. }
  129. // Tests the many-to-one setup feature.
  130. TEST_F(BitVectorTest, SetAll) {
  131. // Test the default constructor and set/resetall.
  132. BitVector map(42);
  133. TestAll(map, false);
  134. map.SetAllTrue();
  135. TestAll(map, true);
  136. map.SetAllFalse();
  137. TestAll(map, false);
  138. }
  139. // Tests the values in the tables offset_table_, next_table_, hamming_table_
  140. // by setting all possible byte patterns and verifying that the NextSetBit and
  141. // NumSetBits functions return the correct values.
  142. TEST_F(BitVectorTest, TestNextSetBit) {
  143. BitVector bv;
  144. for (int spacing = 1; spacing <= 5; ++spacing) {
  145. SetBitPattern(0, 256, spacing, &bv);
  146. ExpectCorrectBits(bv);
  147. }
  148. }
  149. // Tests the values in hamming_table_ more thoroughly by setting single byte
  150. // patterns for each byte individually.
  151. TEST_F(BitVectorTest, TestNumSetBits) {
  152. BitVector bv;
  153. for (int byte = 0; byte < 256; ++byte) {
  154. SetBitPattern(byte, byte + 1, 1, &bv);
  155. ExpectCorrectBits(bv);
  156. }
  157. }
  158. } // namespace tesseract