2018-08-25 16:28:22 +08:00
|
|
|
// (C) Copyright 2017, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2018-08-25 16:28:22 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
2018-08-24 21:07:48 +08:00
|
|
|
#include <string>
|
2018-08-25 16:28:22 +08:00
|
|
|
|
|
|
|
#include "bitvector.h"
|
|
|
|
|
|
|
|
#include "include_gunit.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
const int kPrimeLimit = 1000;
|
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
class BitVectorTest : public testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2019-05-17 00:12:06 +08:00
|
|
|
void SetUp() override {
|
|
|
|
std::locale::global(std::locale(""));
|
2020-12-31 01:17:58 +08:00
|
|
|
file::MakeTmpdir();
|
2019-05-17 00:12:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
public:
|
|
|
|
std::string OutputNameToPath(const std::string &name) {
|
2018-08-24 21:07:48 +08:00
|
|
|
return file::JoinPath(FLAGS_test_tmpdir, name);
|
|
|
|
}
|
2018-12-11 01:52:47 +08:00
|
|
|
// Computes primes up to kPrimeLimit, using the sieve of Eratosthenes.
|
2021-03-13 05:06:34 +08:00
|
|
|
void ComputePrimes(BitVector *map) {
|
2018-08-24 21:07:48 +08:00
|
|
|
map->Init(kPrimeLimit + 1);
|
|
|
|
TestAll(*map, false);
|
|
|
|
map->SetBit(2);
|
|
|
|
// Set all the odds to true.
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int i = 3; i <= kPrimeLimit; i += 2) {
|
2021-03-13 05:06:34 +08:00
|
|
|
map->SetValue(i, true);
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
int factor_limit = static_cast<int>(sqrt(1.0 + kPrimeLimit));
|
|
|
|
for (int f = 3; f <= factor_limit; f += 2) {
|
|
|
|
if (map->At(f)) {
|
2021-03-22 15:48:50 +08:00
|
|
|
for (int m = 2; m * f <= kPrimeLimit; ++m) {
|
2021-03-13 05:06:34 +08:00
|
|
|
map->ResetBit(f * m);
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void TestPrimes(const BitVector &map) {
|
2018-08-24 21:07:48 +08:00
|
|
|
// Now all primes in the vector are true, and all others false.
|
|
|
|
// According to Wikipedia, there are 168 primes under 1000, the last
|
|
|
|
// of which is 997.
|
|
|
|
int total_primes = 0;
|
|
|
|
for (int i = 0; i <= kPrimeLimit; ++i) {
|
2021-03-22 15:48:50 +08:00
|
|
|
if (map[i]) {
|
2021-03-13 05:06:34 +08:00
|
|
|
++total_primes;
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
EXPECT_EQ(168, total_primes);
|
|
|
|
EXPECT_TRUE(map[997]);
|
|
|
|
EXPECT_FALSE(map[998]);
|
|
|
|
EXPECT_FALSE(map[999]);
|
|
|
|
}
|
|
|
|
// Test that all bits in the vector have the given value.
|
2021-03-13 05:06:34 +08:00
|
|
|
void TestAll(const BitVector &map, bool value) {
|
2018-08-24 21:07:48 +08:00
|
|
|
for (int i = 0; i < map.size(); ++i) {
|
|
|
|
EXPECT_EQ(value, map[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets up a BitVector with bit patterns for byte values in
|
|
|
|
// [start_byte, end_byte) positioned every spacing bytes (for spacing >= 1)
|
|
|
|
// with spacing-1 zero bytes in between the pattern bytes.
|
2021-03-13 05:06:34 +08:00
|
|
|
void SetBitPattern(int start_byte, int end_byte, int spacing, BitVector *bv) {
|
2018-08-24 21:07:48 +08:00
|
|
|
bv->Init((end_byte - start_byte) * 8 * spacing);
|
|
|
|
for (int byte_value = start_byte; byte_value < end_byte; ++byte_value) {
|
|
|
|
for (int bit = 0; bit < 8; ++bit) {
|
2021-03-22 15:48:50 +08:00
|
|
|
if (byte_value & (1 << bit)) {
|
2018-08-24 21:07:48 +08:00
|
|
|
bv->SetBit((byte_value - start_byte) * 8 * spacing + bit);
|
2021-03-22 15:48:50 +08:00
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expects that every return from NextSetBit is really set and that all others
|
|
|
|
// are really not set. Checks the return from NumSetBits also.
|
2021-03-13 05:06:34 +08:00
|
|
|
void ExpectCorrectBits(const BitVector &bv) {
|
2018-08-24 21:07:48 +08:00
|
|
|
int bit_index = -1;
|
|
|
|
int prev_bit_index = -1;
|
|
|
|
int num_bits_tested = 0;
|
|
|
|
while ((bit_index = bv.NextSetBit(bit_index)) >= 0) {
|
|
|
|
EXPECT_LT(bit_index, bv.size());
|
|
|
|
// All bits in between must be 0.
|
|
|
|
for (int i = prev_bit_index + 1; i < bit_index; ++i) {
|
|
|
|
EXPECT_EQ(0, bv[i]) << "i = " << i << " prev = " << prev_bit_index;
|
|
|
|
}
|
|
|
|
// This bit must be 1.
|
|
|
|
EXPECT_EQ(1, bv[bit_index]) << "Bit index = " << bit_index;
|
|
|
|
++num_bits_tested;
|
|
|
|
prev_bit_index = bit_index;
|
|
|
|
}
|
|
|
|
// Check the bits between the last and the end.
|
|
|
|
for (int i = prev_bit_index + 1; i < bv.size(); ++i) {
|
|
|
|
EXPECT_EQ(0, bv[i]);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(num_bits_tested, bv.NumSetBits());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-11 01:52:47 +08:00
|
|
|
// Tests the sieve of Eratosthenes as a way of testing set/reset and I/O.
|
2018-08-24 21:07:48 +08:00
|
|
|
TEST_F(BitVectorTest, Primes) {
|
|
|
|
BitVector map;
|
|
|
|
ComputePrimes(&map);
|
|
|
|
TestPrimes(map);
|
|
|
|
// It still works if we use the copy constructor.
|
|
|
|
BitVector map2(map);
|
|
|
|
TestPrimes(map2);
|
|
|
|
// Or if we assign it.
|
|
|
|
BitVector map3;
|
|
|
|
map3 = map;
|
|
|
|
TestPrimes(map3);
|
|
|
|
// Test file i/o too.
|
2018-08-25 16:28:22 +08:00
|
|
|
std::string filename = OutputNameToPath("primesbitvector");
|
2021-03-13 05:06:34 +08:00
|
|
|
FILE *fp = fopen(filename.c_str(), "wb");
|
2020-12-30 08:39:07 +08:00
|
|
|
ASSERT_TRUE(fp != nullptr);
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_TRUE(map.Serialize(fp));
|
|
|
|
fclose(fp);
|
|
|
|
fp = fopen(filename.c_str(), "rb");
|
2020-12-30 08:39:07 +08:00
|
|
|
ASSERT_TRUE(fp != nullptr);
|
2018-08-24 21:07:48 +08:00
|
|
|
BitVector read_map;
|
|
|
|
EXPECT_TRUE(read_map.DeSerialize(false, fp));
|
|
|
|
fclose(fp);
|
|
|
|
TestPrimes(read_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the many-to-one setup feature.
|
|
|
|
TEST_F(BitVectorTest, SetAll) {
|
|
|
|
// Test the default constructor and set/resetall.
|
|
|
|
BitVector map(42);
|
|
|
|
TestAll(map, false);
|
|
|
|
map.SetAllTrue();
|
|
|
|
TestAll(map, true);
|
|
|
|
map.SetAllFalse();
|
|
|
|
TestAll(map, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the values in the tables offset_table_, next_table_, hamming_table_
|
|
|
|
// by setting all possible byte patterns and verifying that the NextSetBit and
|
|
|
|
// NumSetBits functions return the correct values.
|
|
|
|
TEST_F(BitVectorTest, TestNextSetBit) {
|
|
|
|
BitVector bv;
|
|
|
|
for (int spacing = 1; spacing <= 5; ++spacing) {
|
|
|
|
SetBitPattern(0, 256, spacing, &bv);
|
|
|
|
ExpectCorrectBits(bv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests the values in hamming_table_ more thoroughly by setting single byte
|
|
|
|
// patterns for each byte individually.
|
|
|
|
TEST_F(BitVectorTest, TestNumSetBits) {
|
|
|
|
BitVector bv;
|
|
|
|
for (int byte = 0; byte < 256; ++byte) {
|
|
|
|
SetBitPattern(byte, byte + 1, 1, &bv);
|
|
|
|
ExpectCorrectBits(bv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|