2018-08-27 00:22:43 +08:00
|
|
|
// (C) Copyright 2017, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2018-08-27 00:22:43 +08:00
|
|
|
#include "kdpair.h"
|
|
|
|
|
|
|
|
#include "include_gunit.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
2018-09-29 15:19:13 +08:00
|
|
|
int test_data[] = {8, 1, 2, -4, 7, 9, 65536, 4, 9, 0, -32767, 6, 7};
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
// The fixture for testing GenericHeap and DoublePtr.
|
|
|
|
class NthItemTest : public testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2019-05-17 00:12:06 +08:00
|
|
|
void SetUp() override {
|
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
public:
|
2021-03-22 15:26:05 +08:00
|
|
|
~NthItemTest() override;
|
2018-08-24 21:07:48 +08:00
|
|
|
// Pushes the test data onto the KDVector.
|
2021-03-13 05:06:34 +08:00
|
|
|
void PushTestData(KDVector *v) {
|
2021-02-24 03:19:52 +08:00
|
|
|
for (size_t i = 0; i < countof(test_data); ++i) {
|
2018-08-24 21:07:48 +08:00
|
|
|
IntKDPair pair(test_data[i], i);
|
|
|
|
v->push_back(pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-04 14:07:20 +08:00
|
|
|
// Destructor.
|
|
|
|
// It is defined here, so the compiler can create a single vtable
|
|
|
|
// instead of a weak vtable (fixes compiler warning).
|
|
|
|
NthItemTest::~NthItemTest() = default;
|
|
|
|
|
2018-08-24 21:07:48 +08:00
|
|
|
// Tests basic results.
|
|
|
|
TEST_F(NthItemTest, GeneralTest) {
|
|
|
|
KDVector v;
|
|
|
|
// Push the test data onto the KDVector.
|
|
|
|
PushTestData(&v);
|
|
|
|
// Get the min item.
|
2021-03-18 03:10:21 +08:00
|
|
|
size_t index = 0;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is -32767.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(-32767, v[index].key());
|
2018-08-24 21:07:48 +08:00
|
|
|
// Get the max item.
|
2021-03-18 03:10:21 +08:00
|
|
|
index = v.size() - 1;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 65536.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(65536, v[index].key());
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests results on boring data with lots of duplication.
|
|
|
|
TEST_F(NthItemTest, BoringTest) {
|
|
|
|
KDVector v;
|
|
|
|
// Push the test data onto the KDVector.
|
2018-09-29 15:19:13 +08:00
|
|
|
int test_data[] = {8, 8, 8, 8, 8, 7, 7, 7, 7};
|
2021-02-24 03:19:52 +08:00
|
|
|
for (size_t i = 0; i < countof(test_data); ++i) {
|
2018-08-24 21:07:48 +08:00
|
|
|
IntKDPair pair(test_data[i], i);
|
|
|
|
v.push_back(pair);
|
|
|
|
}
|
|
|
|
// The 3rd item is 7 but the 4th is 8..
|
2021-03-18 03:10:21 +08:00
|
|
|
size_t index = 3;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 7.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(7, v[index].key());
|
2021-03-18 03:10:21 +08:00
|
|
|
index = 4;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 8.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(8, v[index].key());
|
2018-08-24 21:07:48 +08:00
|
|
|
// Get the min item.
|
2021-03-18 03:10:21 +08:00
|
|
|
index = 0;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 7.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(7, v[index].key());
|
2018-08-24 21:07:48 +08:00
|
|
|
// Get the max item.
|
2021-03-18 03:10:21 +08:00
|
|
|
index = v.size() - 1;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 8.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(8, v[index].key());
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a unique median in an odd-size array is found correctly.
|
|
|
|
TEST_F(NthItemTest, UniqueTest) {
|
|
|
|
KDVector v;
|
|
|
|
// Push the test data onto the KDVector.
|
|
|
|
PushTestData(&v);
|
|
|
|
// Get the median item.
|
2021-03-18 03:10:21 +08:00
|
|
|
size_t index = v.size() / 2;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 6, it started out at index 11.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(6, v[index].key());
|
|
|
|
EXPECT_EQ(11, v[index].data());
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that an equal median is found correctly.
|
|
|
|
TEST_F(NthItemTest, EqualTest) {
|
|
|
|
KDVector v;
|
|
|
|
// Push the test data onto the KDVector.
|
|
|
|
PushTestData(&v);
|
|
|
|
// Add an extra 8. This makes the median 7.
|
|
|
|
IntKDPair pair(8, 13);
|
|
|
|
v.push_back(pair);
|
|
|
|
// Get the median item.
|
2021-03-18 03:10:21 +08:00
|
|
|
size_t index = v.size() / 2;
|
|
|
|
std::nth_element(v.begin(), v.begin() + index, v.end());
|
2018-08-24 21:07:48 +08:00
|
|
|
// The result is 7, it started out at index 4 or 12.
|
2020-12-27 17:41:48 +08:00
|
|
|
EXPECT_EQ(7, v[index].key());
|
|
|
|
EXPECT_TRUE(v[index].data() == 4 || v[index].data() == 12);
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|