tesseract  5.0.0
indexmapbidi_test.cc
Go to the documentation of this file.
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 
12 #include <cmath>
13 #include <cstdio>
14 #include <string>
15 
16 #include "indexmapbidi.h"
17 
18 #include "include_gunit.h"
19 
20 const int kPrimeLimit = 1000;
21 
22 namespace tesseract {
23 
24 class IndexMapBiDiTest : public testing::Test {
25 protected:
26  void SetUp() override {
27  std::locale::global(std::locale(""));
29  }
30 
31 public:
32  std::string OutputNameToPath(const std::string &name) {
33  return file::JoinPath(FLAGS_test_tmpdir, name);
34  }
35  // Computes primes up to kPrimeLimit, using the sieve of Eratosthenes.
37  map->Init(kPrimeLimit + 1, false);
38  map->SetMap(2, true);
39  // Set all the odds to true.
40  for (int i = 3; i <= kPrimeLimit; i += 2) {
41  map->SetMap(i, true);
42  }
43  int factor_limit = static_cast<int>(sqrt(1.0 + kPrimeLimit));
44  for (int f = 3; f <= factor_limit; f += 2) {
45  if (map->SparseToCompact(f) >= 0) {
46  for (int m = 2; m * f <= kPrimeLimit; ++m) {
47  map->SetMap(f * m, false);
48  }
49  }
50  }
51  map->Setup();
52  }
53 
54  void TestPrimes(const IndexMap &map) {
55  // Now all primes are mapped in the sparse map to their index.
56  // According to Wikipedia, the 168th prime is 997, and it has compact
57  // index 167 because we are indexing from 0.
58  EXPECT_EQ(167, map.SparseToCompact(997));
59  EXPECT_EQ(997, map.CompactToSparse(167));
60  // 995, 996, 998, 999 are not prime.
61  EXPECT_EQ(-1, map.SparseToCompact(995));
62  EXPECT_EQ(-1, map.SparseToCompact(996));
63  EXPECT_EQ(-1, map.SparseToCompact(998));
64  EXPECT_EQ(-1, map.SparseToCompact(999));
65  // The 167th prime is 991.
66  EXPECT_EQ(991, map.CompactToSparse(166));
67  // There are 168 primes in 0..1000.
68  EXPECT_EQ(168, map.CompactSize());
69  EXPECT_EQ(kPrimeLimit + 1, map.SparseSize());
70  }
71 };
72 
73 // Tests the sieve of Eratosthenes as a way of testing setup.
75  IndexMapBiDi map;
76  ComputePrimes(&map);
77  TestPrimes(map);
78  // It still works if we assign it to another.
79  IndexMapBiDi map2;
80  map2.CopyFrom(map);
81  TestPrimes(map2);
82  // Or if we assign it to a base class.
83  IndexMap base_map;
84  base_map.CopyFrom(map);
85  TestPrimes(base_map);
86  // Test file i/o too.
87  std::string filename = OutputNameToPath("primesmap");
88  FILE *fp = fopen(filename.c_str(), "wb");
89  CHECK(fp != nullptr);
90  EXPECT_TRUE(map.Serialize(fp));
91  fclose(fp);
92  fp = fopen(filename.c_str(), "rb");
93  CHECK(fp != nullptr);
94  IndexMapBiDi read_map;
95  EXPECT_TRUE(read_map.DeSerialize(false, fp));
96  fclose(fp);
97  TestPrimes(read_map);
98 }
99 
100 // Tests the many-to-one setup feature.
102  // Test the example in the comment on CompleteMerges.
103  IndexMapBiDi map;
104  map.Init(13, false);
105  map.SetMap(2, true);
106  map.SetMap(4, true);
107  map.SetMap(7, true);
108  map.SetMap(9, true);
109  map.SetMap(11, true);
110  map.Setup();
111  map.Merge(map.SparseToCompact(2), map.SparseToCompact(9));
112  map.Merge(map.SparseToCompact(4), map.SparseToCompact(11));
113  map.CompleteMerges();
114  EXPECT_EQ(3, map.CompactSize());
115  EXPECT_EQ(13, map.SparseSize());
116  EXPECT_EQ(1, map.SparseToCompact(4));
117  EXPECT_EQ(4, map.CompactToSparse(1));
118  EXPECT_EQ(1, map.SparseToCompact(11));
119 }
120 
121 } // namespace tesseract
#define CHECK(condition)
Definition: include_gunit.h:76
const int kPrimeLimit
TEST_F(EuroText, FastLatinOCR)
virtual int SparseSize() const
Definition: indexmapbidi.h:59
int CompactSize() const
Definition: indexmapbidi.h:63
void CopyFrom(const IndexMap &src)
virtual int SparseToCompact(int sparse_index) const
int CompactToSparse(int compact_index) const
Definition: indexmapbidi.h:55
void Init(int size, bool all_mapped)
bool Merge(int compact_index1, int compact_index2)
void CopyFrom(const IndexMapBiDi &src)
void SetMap(int sparse_index, bool mapped)
int SparseSize() const override
Definition: indexmapbidi.h:144
bool Serialize(FILE *fp) const
bool DeSerialize(bool swap, FILE *fp)
int SparseToCompact(int sparse_index) const override
Definition: indexmapbidi.h:140
static void MakeTmpdir()
Definition: include_gunit.h:38
static std::string JoinPath(const std::string &s1, const std::string &s2)
Definition: include_gunit.h:65
void TestPrimes(const IndexMap &map)
void ComputePrimes(IndexMapBiDi *map)
std::string OutputNameToPath(const std::string &name)