tesseract  5.0.0
tabvector_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 <memory>
13 
14 #include "tabvector.h"
15 
16 #include "include_gunit.h"
17 
18 namespace tesseract {
19 
20 class TabVectorTest : public testing::Test {
21 protected:
22  void SetUp() override {
23  std::locale::global(std::locale(""));
24  vector_.reset();
25  }
26 
27  void TearDown() override {}
28 
29  void MakeSimpleTabVector(int x1, int y1, int x2, int y2) {
30  vector_ = std::make_unique<TabVector>();
31  vector_->set_startpt(ICOORD(x1, y1));
32  vector_->set_endpt(ICOORD(x2, y2));
33  }
34 
35  std::unique_ptr<TabVector> vector_;
36 };
37 
38 TEST_F(TabVectorTest, SetStartEndPointsMatch) {
39  vector_ = std::make_unique<TabVector>();
40  ICOORD start(51, 65);
41  ICOORD end(7568, 234);
42  // Test coordinates individually to avoid adding an ostream operator
43  // explicitly to the ICOORD class (Droid doesn't support it).
44  vector_->set_startpt(start);
45  EXPECT_EQ(start.x(), vector_->startpt().x());
46  EXPECT_EQ(start.y(), vector_->startpt().y());
47  vector_->set_endpt(end);
48  EXPECT_EQ(end.x(), vector_->endpt().x());
49  EXPECT_EQ(end.y(), vector_->endpt().y());
50 }
51 
52 TEST_F(TabVectorTest, XAtY45DegreeSlopeInRangeExact) {
53  MakeSimpleTabVector(0, 0, 100, 100);
54  for (int y = 0; y <= 100; ++y) {
55  int x = vector_->XAtY(y);
56  EXPECT_EQ(y, x);
57  }
58 }
59 
60 TEST_F(TabVectorTest, XAtYVerticalInRangeExact) {
61  const int x = 120; // Arbitrary choice
62  MakeSimpleTabVector(x, 0, x, 100);
63  for (int y = 0; y <= 100; ++y) {
64  int result_x = vector_->XAtY(y);
65  EXPECT_EQ(x, result_x);
66  }
67 }
68 
69 TEST_F(TabVectorTest, XAtYHorizontal) {
70  const int y = 76; // arbitrary
71  MakeSimpleTabVector(0, y, 100, y);
72  EXPECT_EQ(0, vector_->XAtY(y));
73  // TODO(nbeato): What's the failure condition?
74  // Undefined! Should not pass! Allow until resolved answer.
75  EXPECT_EQ(0, vector_->XAtY(10));
76 }
77 
78 TEST_F(TabVectorTest, XAtYRoundingSimple) {
79  MakeSimpleTabVector(0, 0, 2, 10000);
80  int x = vector_->XAtY(1);
81  EXPECT_EQ(0, x);
82  x = vector_->XAtY(4999);
83  EXPECT_EQ(0, x);
84  x = vector_->XAtY(5001);
85  EXPECT_EQ(1, x);
86  x = vector_->XAtY(9999);
87  EXPECT_EQ(1, x);
88 }
89 
90 TEST_F(TabVectorTest, XAtYLargeNumbers) {
91  // Assume a document is 800 DPI,
92  // the width of a page is 10 inches across (8000 pixels), and
93  // the height of the page is 15 inches (12000 pixels).
94  MakeSimpleTabVector(7804, 504, 7968, 11768); // Arbitrary for vertical line
95  int x = vector_->XAtY(6136); // test mid point
96  EXPECT_EQ(7886, x);
97 }
98 
99 TEST_F(TabVectorTest, XAtYHorizontalInRangeExact) {
100  const int y = 120; // Arbitrary choice
101  MakeSimpleTabVector(50, y, 150, y);
102 
103  int x = vector_->XAtY(y);
104  EXPECT_EQ(50, x);
105 }
106 
107 TEST_F(TabVectorTest, VOverlapInRangeSimple) {
108  MakeSimpleTabVector(0, 0, 100, 100);
109  int overlap = vector_->VOverlap(90, 10);
110  EXPECT_EQ(80, overlap);
111  overlap = vector_->VOverlap(100, 0);
112  EXPECT_EQ(100, overlap);
113 }
114 
115 TEST_F(TabVectorTest, VOverlapOutOfRange) {
116  MakeSimpleTabVector(0, 10, 100, 90);
117  int overlap = vector_->VOverlap(100, 0);
118  EXPECT_EQ(80, overlap);
119 }
120 
122  MakeSimpleTabVector(1, 2, 3, 4);
123  vector_->XYFlip();
124  EXPECT_EQ(2, vector_->startpt().x());
125  EXPECT_EQ(1, vector_->startpt().y());
126  EXPECT_EQ(4, vector_->endpt().x());
127  EXPECT_EQ(3, vector_->endpt().y());
128 }
129 
130 } // namespace tesseract
TEST_F(EuroText, FastLatinOCR)
integer coordinate
Definition: points.h:36
TDimension y() const
access_function
Definition: points.h:62
TDimension x() const
access function
Definition: points.h:58
void MakeSimpleTabVector(int x1, int y1, int x2, int y2)
void TearDown() override
std::unique_ptr< TabVector > vector_