tabvector_test.cc 3.7 KB

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