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