forked from organicmaps/organicmaps-tmp
Add kdtree unit-tests.
This commit is contained in:
parent
74d1fcd371
commit
ae2358e9cf
1 changed files with 60 additions and 21 deletions
|
@ -6,36 +6,38 @@
|
|||
|
||||
namespace
|
||||
{
|
||||
typedef m4::Tree<m2::RectD> tree_t;
|
||||
typedef m2::RectD R;
|
||||
|
||||
bool compare_true(m2::RectD const &, m2::RectD const &) { return true; }
|
||||
bool compare_false(m2::RectD const &, m2::RectD const &) { return false; }
|
||||
typedef m4::Tree<R> tree_t;
|
||||
|
||||
bool compare_true(R const &, R const &) { return true; }
|
||||
bool compare_false(R const &, R const &) { return false; }
|
||||
}
|
||||
|
||||
UNIT_TEST(Tree4D_Smoke)
|
||||
{
|
||||
tree_t theTree;
|
||||
|
||||
m2::RectD arr[] = {
|
||||
m2::RectD(0, 0, 1, 1),
|
||||
m2::RectD(1, 1, 2, 2),
|
||||
m2::RectD(2, 2, 3, 3)
|
||||
R arr[] = {
|
||||
R(0, 0, 1, 1),
|
||||
R(1, 1, 2, 2),
|
||||
R(2, 2, 3, 3)
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(arr); ++i)
|
||||
theTree.ReplaceIf(arr[i], arr[i], &compare_true);
|
||||
|
||||
vector<m2::RectD> test;
|
||||
vector<R> test;
|
||||
theTree.ForEach(MakeBackInsertFunctor(test));
|
||||
TEST_EQUAL(3, test.size(), ());
|
||||
|
||||
test.clear();
|
||||
m2::RectD const searchR(1.5, 1.5, 1.5, 1.5);
|
||||
R const searchR(1.5, 1.5, 1.5, 1.5);
|
||||
theTree.ForEachInRect(searchR, MakeBackInsertFunctor(test));
|
||||
TEST_EQUAL(1, test.size(), ());
|
||||
TEST_EQUAL(test[0], arr[1], ());
|
||||
|
||||
m2::RectD const replaceR(0.5, 0.5, 2.5, 2.5);
|
||||
R const replaceR(0.5, 0.5, 2.5, 2.5);
|
||||
theTree.ReplaceIf(replaceR, replaceR, &compare_true);
|
||||
|
||||
test.clear();
|
||||
|
@ -52,18 +54,18 @@ UNIT_TEST(Tree4D_ReplaceIf)
|
|||
{
|
||||
tree_t theTree;
|
||||
|
||||
m2::RectD arr[] = {
|
||||
m2::RectD(8, 13, 554, 32), m2::RectD(555, 13, 700, 32),
|
||||
m2::RectD(8, 33, 554, 52), m2::RectD(555, 33, 700, 52),
|
||||
m2::RectD(8, 54, 554, 73), m2::RectD(555, 54, 700, 73),
|
||||
m2::RectD(8, 76, 554, 95), m2::RectD(555, 76, 700, 95)
|
||||
R arr[] = {
|
||||
R(8, 13, 554, 32), R(555, 13, 700, 32),
|
||||
R(8, 33, 554, 52), R(555, 33, 700, 52),
|
||||
R(8, 54, 554, 73), R(555, 54, 700, 73),
|
||||
R(8, 76, 554, 95), R(555, 76, 700, 95)
|
||||
};
|
||||
|
||||
m2::RectD arr1[] = {
|
||||
m2::RectD(3, 23, 257, 42), m2::RectD(600, 23, 800, 42),
|
||||
m2::RectD(3, 43, 257, 62), m2::RectD(600, 43, 800, 62),
|
||||
m2::RectD(3, 65, 257, 84), m2::RectD(600, 65, 800, 84),
|
||||
m2::RectD(3, 87, 257, 106), m2::RectD(600, 87, 800, 106)
|
||||
R arr1[] = {
|
||||
R(3, 23, 257, 42), R(600, 23, 800, 42),
|
||||
R(3, 43, 257, 62), R(600, 43, 800, 62),
|
||||
R(3, 65, 257, 84), R(600, 65, 800, 84),
|
||||
R(3, 87, 257, 106), R(600, 87, 800, 106)
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(arr); ++i)
|
||||
|
@ -77,10 +79,47 @@ UNIT_TEST(Tree4D_ReplaceIf)
|
|||
TEST_EQUAL ( theTree.GetSize(), count + 1, () );
|
||||
}
|
||||
|
||||
vector<m2::RectD> test;
|
||||
vector<R> test;
|
||||
theTree.ForEach(MakeBackInsertFunctor(test));
|
||||
|
||||
TEST_EQUAL(ARRAY_SIZE(arr), test.size(), ());
|
||||
for (size_t i = 0; i < test.size(); ++i)
|
||||
TEST_EQUAL(test[i], arr[i], ());
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
void CheckInRect(R const * arr, size_t count, R const & searchR, size_t expected)
|
||||
{
|
||||
tree_t theTree;
|
||||
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
theTree.Add(arr[i], arr[i]);
|
||||
|
||||
vector<R> test;
|
||||
theTree.ForEachInRect(searchR, MakeBackInsertFunctor(test));
|
||||
|
||||
TEST_EQUAL(test.size(), expected, ());
|
||||
}
|
||||
}
|
||||
|
||||
UNIT_TEST(Tree4D_ForEachInRect)
|
||||
{
|
||||
{
|
||||
R arr[] = {
|
||||
R(0, 0, 1, 1), R(5, 5, 10, 10), R(-1, -1, 0, 0), R(-10, -10, -5, -5)
|
||||
};
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(1, 1, 5, 5), 0);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(-5, -5, -1, -1), 0);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(3, 3, 3, 3), 0);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(-3, -3, -3, -3), 0);
|
||||
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(0.5, 0.5, 0.5, 0.5), 1);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(8, 8, 8, 8), 1);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(-0.5, -0.5, -0.5, -0.5), 1);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(-8, -8, -8, -8), 1);
|
||||
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(0.5, 0.5, 5.5, 5.5), 2);
|
||||
CheckInRect(arr, ARRAY_SIZE(arr), R(-5.5, -5.5, -0.5, -0.5), 2);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue