21 using tesseract::ColPartition_LIST;
44 void InjectCellY(
int y) {
48 void InjectCellX(
int x) {
53 void ExpectCellX(
int x_min,
int second,
int add,
int almost_done,
int x_max) {
54 ASSERT_EQ(0, (almost_done - second) % add);
55 EXPECT_EQ(3 + (almost_done - second) / add, cell_x_.size());
56 EXPECT_EQ(x_min, cell_x_.get(0));
57 EXPECT_EQ(x_max, cell_x_.get(cell_x_.size() - 1));
58 for (
int i = 1; i < cell_x_.size() - 1; ++i) {
59 EXPECT_EQ(second + add * (i - 1), cell_x_.get(i));
63 void ExpectSortedX() {
64 EXPECT_GT(cell_x_.size(), 0);
65 for (
int i = 1; i < cell_x_.size(); ++i) {
66 EXPECT_LT(cell_x_.get(i - 1), cell_x_.get(i));
71 class SharedTest :
public testing::Test {
74 std::locale::global(std::locale(
""));
82 tesseract::ColPartition_IT memory(&allocated_parts_);
83 for (memory.mark_cycle_pt(); !memory.cycled_list(); memory.forward()) {
84 memory.data()->DeleteBoxes();
88 void InsertPartitions() {
89 for (
int row = 0; row < 800; row += 20)
90 for (
int col = 0; col < 500; col += 25)
91 InsertPartition(col + 1, row + 1, col + 24, row + 19);
94 void InsertPartition(
int left,
int bottom,
int right,
int top) {
95 TBOX box(left, bottom, right, top);
100 text_grid_->InsertBBox(
true,
true, part);
102 tesseract::ColPartition_IT add_it(&allocated_parts_);
103 add_it.add_after_stay_put(part);
107 line_box_.set_to_given_coords(
108 100 - line_grid_->gridsize(), 10 - line_grid_->gridsize(),
109 450 + line_grid_->gridsize(), 50 + line_grid_->gridsize());
110 for (
int i = 10; i <= 50; i += 10) InsertHorizontalLine(100, 450, i);
111 for (
int i = 100; i <= 450; i += 50) InsertVerticalLine(i, 10, 50);
113 for (
int i = 100; i <= 200; i += 20) InsertHorizontalLine(0, 100, i);
116 void InsertHorizontalLine(
int left,
int right,
int y) {
117 TBOX box(left, y - line_grid_->gridsize(), right,
118 y + line_grid_->gridsize());
121 line_grid_->InsertBBox(
true,
true, part);
123 tesseract::ColPartition_IT add_it(&allocated_parts_);
124 add_it.add_after_stay_put(part);
126 void InsertVerticalLine(
int x,
int bottom,
int top) {
127 TBOX box(x - line_grid_->gridsize(), bottom, x + line_grid_->gridsize(),
131 line_grid_->InsertBBox(
true,
true, part);
133 tesseract::ColPartition_IT add_it(&allocated_parts_);
134 add_it.add_after_stay_put(part);
137 void InsertCellsInLines() {
138 for (
int y = 10; y <= 50; y += 10)
139 for (
int x = 100; x <= 450; x += 50)
140 InsertPartition(x + 1, y + 1, x + 49, y + 9);
144 std::unique_ptr<ColPartitionGrid> text_grid_;
145 std::unique_ptr<ColPartitionGrid> line_grid_;
146 ColPartition_LIST allocated_parts_;
149 class TableRecognizerTest :
public SharedTest {
153 recognizer_.reset(
new TestableTableRecognizer());
155 recognizer_->set_text_grid(text_grid_.get());
156 recognizer_->set_line_grid(line_grid_.get());
159 std::unique_ptr<TestableTableRecognizer> recognizer_;
162 class StructuredTableTest :
public SharedTest {
166 table_.reset(
new TestableStructuredTable());
168 table_->set_text_grid(text_grid_.get());
169 table_->set_line_grid(line_grid_.get());
172 std::unique_ptr<TestableStructuredTable> table_;
175 TEST_F(TableRecognizerTest, HasSignificantLinesBasicPass) {
177 TBOX smaller_guess(120, 15, 370, 45);
178 TBOX larger_guess(90, 5, 490, 70);
179 EXPECT_TRUE(recognizer_->HasSignificantLines(line_box_));
180 EXPECT_TRUE(recognizer_->HasSignificantLines(larger_guess));
181 EXPECT_TRUE(recognizer_->HasSignificantLines(smaller_guess));
184 TEST_F(TableRecognizerTest, HasSignificantLinesBasicFail) {
186 TBOX box(370, 35, 500, 45);
187 EXPECT_FALSE(recognizer_->HasSignificantLines(box));
190 TEST_F(TableRecognizerTest, HasSignificantLinesHorizontalOnlyFails) {
192 TBOX box(0, 100, 200, 200);
193 EXPECT_FALSE(recognizer_->HasSignificantLines(box));
196 TEST_F(TableRecognizerTest, FindLinesBoundingBoxBasic) {
198 TBOX box(0, 0, 200, 50);
199 bool result = recognizer_->FindLinesBoundingBox(&box);
201 EXPECT_EQ(line_box_.left(), box.left());
202 EXPECT_EQ(line_box_.right(), box.right());
203 EXPECT_EQ(line_box_.bottom(), box.bottom());
204 EXPECT_EQ(line_box_.top(), box.top());
207 TEST_F(TableRecognizerTest, RecognizeLinedTableBasic) {
209 TBOX guess(120, 15, 370, 45);
214 EXPECT_TRUE(recognizer_->RecognizeLinedTable(guess, &table));
226 TEST_F(TableRecognizerTest, RecognizeWhitespacedTableBasic) {
228 TBOX guess(0, 0, 500, 800);
233 EXPECT_TRUE(recognizer_->RecognizeWhitespacedTable(guess, &table));
241 EXPECT_EQ(500 * 800 / 20 / 25, table.
cell_count());
245 TEST_F(StructuredTableTest, CountVerticalIntersectionsAll) {
246 table_->set_bounding_box(
TBOX(0, 0, 1000, 1000));
247 InsertPartition(0, 0, 100, 10);
248 InsertPartition(1, 12, 43, 21);
249 EXPECT_EQ(2, table_->CountVerticalIntersections(4));
250 EXPECT_EQ(2, table_->CountVerticalIntersections(20));
251 EXPECT_EQ(2, table_->CountVerticalIntersections(40));
252 EXPECT_EQ(1, table_->CountVerticalIntersections(50));
253 EXPECT_EQ(1, table_->CountVerticalIntersections(60));
254 EXPECT_EQ(1, table_->CountVerticalIntersections(80));
255 EXPECT_EQ(1, table_->CountVerticalIntersections(95));
256 EXPECT_EQ(0, table_->CountVerticalIntersections(104));
257 EXPECT_EQ(0, table_->CountVerticalIntersections(150));
260 TEST_F(StructuredTableTest, CountHorizontalIntersectionsAll) {
261 table_->set_bounding_box(
TBOX(0, 0, 1000, 1000));
262 InsertPartition(0, 3, 100, 10);
263 InsertPartition(110, 5, 200, 16);
265 EXPECT_EQ(0, table_->CountHorizontalIntersections(0));
266 EXPECT_EQ(1, table_->CountHorizontalIntersections(4));
267 EXPECT_EQ(2, table_->CountHorizontalIntersections(8));
268 EXPECT_EQ(1, table_->CountHorizontalIntersections(12));
269 EXPECT_EQ(0, table_->CountHorizontalIntersections(20));
272 TEST_F(StructuredTableTest, VerifyLinedTableBasicPass) {
273 for (
int y = 10; y <= 50; y += 10) table_->InjectCellY(y);
274 for (
int x = 100; x <= 450; x += 50) table_->InjectCellX(x);
276 InsertCellsInLines();
277 table_->set_bounding_box(line_box_);
278 EXPECT_TRUE(table_->VerifyLinedTableCells());
281 TEST_F(StructuredTableTest, VerifyLinedTableHorizontalFail) {
282 for (
int y = 10; y <= 50; y += 10) table_->InjectCellY(y);
283 for (
int x = 100; x <= 450; x += 50) table_->InjectCellX(x);
285 InsertCellsInLines();
286 InsertPartition(101, 11, 299, 19);
287 table_->set_bounding_box(line_box_);
288 EXPECT_FALSE(table_->VerifyLinedTableCells());
291 TEST_F(StructuredTableTest, VerifyLinedTableVerticalFail) {
292 for (
int y = 10; y <= 50; y += 10) table_->InjectCellY(y);
293 for (
int x = 100; x <= 450; x += 50) table_->InjectCellX(x);
295 InsertCellsInLines();
296 InsertPartition(151, 21, 199, 39);
297 table_->set_bounding_box(line_box_);
298 EXPECT_FALSE(table_->VerifyLinedTableCells());
301 TEST_F(StructuredTableTest, FindWhitespacedColumnsBasic) {
303 TBOX guess(0, 0, 500, 800);
304 table_->set_bounding_box(guess);
305 table_->FindWhitespacedColumns();
306 table_->ExpectCellX(1, 25, 25, 475, 499);
309 TEST_F(StructuredTableTest, FindWhitespacedColumnsSorted) {
311 TBOX guess(0, 0, 500, 800);
312 table_->set_bounding_box(guess);
313 table_->FindWhitespacedColumns();
314 table_->ExpectSortedX();