tesseract  5.0.0
list_test.cc
Go to the documentation of this file.
1 // (C) Copyright 2020, 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 "include_gunit.h"
13 #include "clst.h"
14 #include "elst.h"
15 #include "elst2.h"
16 
17 namespace tesseract {
18 
19 class ListTest : public ::testing::Test {
20 protected:
21  void SetUp() override {
22  static std::locale system_locale("");
23  std::locale::global(system_locale);
24  }
25  const size_t ListSize = 5;
26 };
27 
28 class Clst : public CLIST_LINK {
29 public:
30  Clst(unsigned n) : value(n) {}
31  unsigned value;
32 };
33 
34 class Elst : public ELIST_LINK {
35 public:
36  Elst(unsigned n) : value(n) {}
37  unsigned value;
38 };
39 
40 class Elst2 : public ELIST2_LINK {
41 public:
42  Elst2(unsigned n) : value(n) {}
43  unsigned value;
44 };
45 
49 
50 TEST_F(ListTest, TestCLIST) {
51  Clst_CLIST list;
52  EXPECT_TRUE(list.empty());
53  EXPECT_EQ(list.length(), 0);
54  auto it = CLIST_ITERATOR(&list);
55  for (unsigned i = 0; i < ListSize; i++) {
56  auto *lst = new Clst(i);
57  it.add_to_end(lst);
58  }
59  EXPECT_TRUE(!list.empty());
60  EXPECT_EQ(list.length(), ListSize);
61  it.move_to_first();
62  unsigned n = 0;
63  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
64  EXPECT_TRUE(n == 0 || !it.at_first());
65  auto *lst = reinterpret_cast<Clst *>(it.data());
66  EXPECT_EQ(lst->value, n);
67  n++;
68  EXPECT_TRUE(n != ListSize || it.at_last());
69  }
70  it.forward();
71  n++;
72  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
73  auto *lst = reinterpret_cast<Clst *>(it.extract());
74  EXPECT_EQ(lst->value, n % ListSize);
75  n++;
76  delete lst;
77  }
78  // TODO: add more tests for CLIST
79 }
80 
81 TEST_F(ListTest, TestELIST) {
82  Elst_LIST list;
83  EXPECT_TRUE(list.empty());
84  EXPECT_EQ(list.length(), 0);
85  auto it = ELIST_ITERATOR(&list);
86  for (unsigned i = 0; i < ListSize; i++) {
87  auto *elst = new Elst(i);
88  it.add_to_end(elst);
89  }
90  EXPECT_TRUE(!list.empty());
91  EXPECT_EQ(list.length(), ListSize);
92  it.move_to_first();
93  unsigned n = 0;
94  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
95  EXPECT_TRUE(n == 0 || !it.at_first());
96  auto *elst = reinterpret_cast<Elst *>(it.data());
97  EXPECT_EQ(elst->value, n);
98  n++;
99  EXPECT_TRUE(n != ListSize || it.at_last());
100  }
101  it.forward();
102  n++;
103  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
104  auto *elst = reinterpret_cast<Elst *>(it.extract());
105  EXPECT_EQ(elst->value, n % ListSize);
106  n++;
107  delete elst;
108  }
109  // TODO: add more tests for ELIST
110 }
111 
112 TEST_F(ListTest, TestELIST2) {
113  Elst2_LIST list;
114  EXPECT_TRUE(list.empty());
115  EXPECT_EQ(list.length(), 0);
116  auto it = ELIST2_ITERATOR(&list);
117  for (unsigned i = 0; i < ListSize; i++) {
118  auto *lst = new Elst2(i);
119  it.add_to_end(lst);
120  }
121  EXPECT_TRUE(!list.empty());
122  EXPECT_EQ(list.length(), ListSize);
123  it.move_to_first();
124  unsigned n = 0;
125  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
126  EXPECT_TRUE(n == 0 || !it.at_first());
127  auto *lst = reinterpret_cast<Elst2 *>(it.data());
128  EXPECT_EQ(lst->value, n);
129  n++;
130  EXPECT_TRUE(n != ListSize || it.at_last());
131  }
132  it.backward();
133  n--;
134  for (it.mark_cycle_pt(); !it.cycled_list(); it.backward()) {
135  auto *lst = reinterpret_cast<Elst2 *>(it.data());
136  EXPECT_EQ(lst->value, n);
137  n--;
138  }
139  it.forward();
140  n++;
141  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
142  auto *lst = reinterpret_cast<Elst2 *>(it.extract());
143  EXPECT_EQ(lst->value, n % ListSize);
144  n++;
145  delete lst;
146  }
147  // TODO: add more tests for ELIST2
148 }
149 
150 } // namespace tesseract.
#define CLISTIZEH(CLASSNAME)
Definition: clst.h:705
#define ELISTIZEH(CLASSNAME)
Definition: elst.h:803
#define ELIST2IZEH(CLASSNAME)
Definition: elst2.h:822
TEST_F(EuroText, FastLatinOCR)
void SetUp() override
Definition: list_test.cc:21
const size_t ListSize
Definition: list_test.cc:25
unsigned value
Definition: list_test.cc:31
Clst(unsigned n)
Definition: list_test.cc:30
Elst(unsigned n)
Definition: list_test.cc:36
unsigned value
Definition: list_test.cc:37
unsigned value
Definition: list_test.cc:43
Elst2(unsigned n)
Definition: list_test.cc:42