tesseract  5.0.0-alpha-619-ge9db
nthitem_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 
13 #include "kdpair.h"
14 
15 #include "include_gunit.h"
16 
17 namespace tesseract {
18 
19 int test_data[] = {8, 1, 2, -4, 7, 9, 65536, 4, 9, 0, -32767, 6, 7};
20 
21 // The fixture for testing GenericHeap and DoublePtr.
22 class NthItemTest : public testing::Test {
23  protected:
24  void SetUp() override {
25  std::locale::global(std::locale(""));
26  }
27 
28  public:
29  virtual ~NthItemTest();
30  // Pushes the test data onto the KDVector.
32  for (size_t i = 0; i < ARRAYSIZE(test_data); ++i) {
33  IntKDPair pair(test_data[i], i);
34  v->push_back(pair);
35  }
36  }
37 };
38 
39 // Destructor.
40 // It is defined here, so the compiler can create a single vtable
41 // instead of a weak vtable (fixes compiler warning).
42 NthItemTest::~NthItemTest() = default;
43 
44 // Tests basic results.
45 TEST_F(NthItemTest, GeneralTest) {
46  KDVector v;
47  // Push the test data onto the KDVector.
48  PushTestData(&v);
49  // Get the min item.
50  int index = v.choose_nth_item(0);
51  // The result is -32767.
52  EXPECT_EQ(-32767, v[index].key);
53  // Get the max item.
54  index = v.choose_nth_item(v.size() - 1);
55  // The result is 65536.
56  EXPECT_EQ(65536, v[index].key);
57  // Invalid items are silently truncated to valid.
58  // Get the min item.
59  index = v.choose_nth_item(-1);
60  // The result is -32767.
61  EXPECT_EQ(-32767, v[index].key);
62  // Get the max item.
63  index = v.choose_nth_item(v.size());
64  // The result is 65536.
65  EXPECT_EQ(65536, v[index].key);
66 }
67 
68 // Tests results on boring data with lots of duplication.
69 TEST_F(NthItemTest, BoringTest) {
70  KDVector v;
71  // Push the test data onto the KDVector.
72  int test_data[] = {8, 8, 8, 8, 8, 7, 7, 7, 7};
73  for (size_t i = 0; i < ARRAYSIZE(test_data); ++i) {
74  IntKDPair pair(test_data[i], i);
75  v.push_back(pair);
76  }
77  // The 3rd item is 7 but the 4th is 8..
78  int index = v.choose_nth_item(3);
79  // The result is 7.
80  EXPECT_EQ(7, v[index].key);
81  index = v.choose_nth_item(4);
82  // The result is 8.
83  EXPECT_EQ(8, v[index].key);
84  // Get the min item.
85  index = v.choose_nth_item(0);
86  // The result is 7.
87  EXPECT_EQ(7, v[index].key);
88  // Get the max item.
89  index = v.choose_nth_item(v.size() - 1);
90  // The result is 8.
91  EXPECT_EQ(8, v[index].key);
92 }
93 
94 // Tests that a unique median in an odd-size array is found correctly.
95 TEST_F(NthItemTest, UniqueTest) {
96  KDVector v;
97  // Push the test data onto the KDVector.
98  PushTestData(&v);
99  // Get the median item.
100  int index = v.choose_nth_item(v.size() / 2);
101  // The result is 6, it started out at index 11.
102  EXPECT_EQ(6, v[index].key);
103  EXPECT_EQ(11, v[index].data);
104 }
105 
106 // Tests that an equal median is found correctly.
107 TEST_F(NthItemTest, EqualTest) {
108  KDVector v;
109  // Push the test data onto the KDVector.
110  PushTestData(&v);
111  // Add an extra 8. This makes the median 7.
112  IntKDPair pair(8, 13);
113  v.push_back(pair);
114  // Get the median item.
115  int index = v.choose_nth_item(v.size() / 2);
116  // The result is 7, it started out at index 4 or 12.
117  EXPECT_EQ(7, v[index].key);
118  EXPECT_TRUE(v[index].data == 4 || v[index].data == 12);
119 }
120 
121 } // namespace tesseract
tesseract::test_data
int test_data[]
Definition: heap_test.cc:24
tesseract::NthItemTest::~NthItemTest
virtual ~NthItemTest()
GenericVector::choose_nth_item
int choose_nth_item(int target_index)
Definition: genericvector.h:289
ARRAYSIZE
#define ARRAYSIZE(arr)
Definition: include_gunit.h:53
include_gunit.h
tesseract::TEST_F
TEST_F(EquationFinderTest, IdentifySpecialText)
Definition: equationdetect_test.cc:181
tesseract::NthItemTest
Definition: nthitem_test.cc:22
genericvector.h
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
tesseract::NthItemTest::SetUp
void SetUp() override
Definition: nthitem_test.cc:24
tesseract::KDVector
Definition: kdpair.h:182
kdpair.h
tesseract
Definition: baseapi.h:65
tesseract::KDPairInc
Definition: kdpair.h:51
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::NthItemTest::PushTestData
void PushTestData(KDVector *v)
Definition: nthitem_test.cc:31