tesseract v5.3.3.20231005
stats_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 "kdpair.h"
13#include "statistc.h"
14
15#include "include_gunit.h"
16
17namespace tesseract {
18
19const int kTestData[] = {2, 0, 12, 1, 1, 2, 10, 1, 0, 0, 0, 2, 0, 4, 1, 1};
20
21class STATSTest : public testing::Test {
22public:
23 void SetUp() override {
24 std::locale::global(std::locale(""));
25 stats_.set_range(0, 15);
26 for (size_t i = 0; i < countof(kTestData); ++i) {
28 }
29 }
30
31 void TearDown() override {}
32
34};
35
36// Tests some basic numbers from the stats_.
37TEST_F(STATSTest, BasicStats) {
38 EXPECT_EQ(37, stats_.get_total());
39 EXPECT_EQ(2, stats_.mode());
40 EXPECT_EQ(12, stats_.pile_count(2));
41}
42
43TEST_F(STATSTest, InitStats) {
44 STATS stats;
45 EXPECT_EQ(0, stats.get_total());
46 EXPECT_EQ(0, stats.mode());
47 EXPECT_EQ(0, stats.pile_count(2));
48}
49
50// Tests the top_n_modes function.
51TEST_F(STATSTest, TopNModes) {
52 std::vector<tesseract::KDPairInc<float, int> > modes;
53 int num_modes = stats_.top_n_modes(3, modes);
54 EXPECT_EQ(3, num_modes);
55 // Mode0 is 12 1 1 = 14 total count with a mean of 2 3/14.
56 EXPECT_FLOAT_EQ(2.0f + 3.0f / 14, modes[0].key());
57 EXPECT_EQ(14, modes[0].data());
58 // Mode 1 is 2 10 1 = 13 total count with a mean of 5 12/13.
59 EXPECT_FLOAT_EQ(5.0f + 12.0f / 13, modes[1].key());
60 EXPECT_EQ(13, modes[1].data());
61 // Mode 2 is 4 1 1 = 6 total count with a mean of 13.5.
62 EXPECT_FLOAT_EQ(13.5f, modes[2].key());
63 EXPECT_EQ(6, modes[2].data());
64}
65
66} // namespace tesseract
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2043
#define EXPECT_FLOAT_EQ(val1, val2)
Definition: gtest.h:2144
const int kTestData[]
Definition: stats_test.cc:19
constexpr size_t countof(T const (&)[N]) noexcept
Definition: serialis.h:34
TEST_F(EuroText, FastLatinOCR)
void add(int32_t value, int32_t count)
Definition: statistc.cpp:99
int32_t pile_count(int32_t value) const
Definition: statistc.h:72
int32_t get_total() const
Definition: statistc.h:85
int32_t mode() const
Definition: statistc.cpp:112
bool set_range(int32_t min_bucket_value, int32_t max_bucket_value)
Definition: statistc.cpp:59
void TearDown() override
Definition: stats_test.cc:31
void SetUp() override
Definition: stats_test.cc:23