File: test_basic.cpp

package info (click to toggle)
mdds 3.2.0-4
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 6,372 kB
  • sloc: cpp: 21,777; sh: 1,369; makefile: 692; python: 602
file content (128 lines) | stat: -rw-r--r-- 3,802 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

// SPDX-FileCopyrightText: 2021 - 2025 Kohei Yoshida
//
// SPDX-License-Identifier: MIT

#include "test_global.hpp" // This must be the first header to be included.
#include "test_global_rtree.hpp"

using mdds::rtree;
using std::cout;
using std::endl;

void rtree_test_basic_search()
{
    MDDS_TEST_FUNC_SCOPE;

    using rt_type = rtree<int16_t, std::string>;
    using search_type = rt_type::search_type;
    rt_type::integrity_check_properties check_props;
    check_props.throw_on_first_error = true;

    rt_type tree;
    const rt_type& ctree = tree;
    rt_type::extent_type expected_bb;

    tree.insert({{0, 0}, {2, 2}}, "test");
    expected_bb = {{0, 0}, {2, 2}};
    TEST_ASSERT(tree.extent() == expected_bb);
    TEST_ASSERT(tree.size() == 1);

    tree.insert({{3, 3}, {5, 5}}, "test again");
    expected_bb = {{0, 0}, {5, 5}};
    TEST_ASSERT(tree.extent() == expected_bb);
    TEST_ASSERT(tree.size() == 2);

    tree.insert({{-2, 1}, {3, 6}}, "more test");
    expected_bb = {{-2, 0}, {5, 6}};
    TEST_ASSERT(tree.extent() == expected_bb);
    TEST_ASSERT(tree.size() == 3);

    tree.check_integrity(check_props);

    // Verify the search method works.

    rt_type::const_search_results res = ctree.search({1, 1}, search_type::overlap);

    auto it = res.cbegin(), it_end = res.cend();

    size_t n = std::distance(it, it_end);
    TEST_ASSERT(n == 2);

    std::unordered_map<std::string, rt_type::extent_type> expected_values = {
        {"test", {{0, 0}, {2, 2}}},
        {"more test", {{-2, 1}, {3, 6}}},
    };

    for (; it != it_end; ++it)
    {
        cout << "bounding box: " << it.extent().to_string() << "; value: " << *it << "; depth: " << it.depth() << endl;
        auto itv = expected_values.find(*it);
        TEST_ASSERT(itv != expected_values.end());
        TEST_ASSERT(itv->second == it.extent());
        TEST_ASSERT(it.depth() == 1);
    }

    // Perform an out-of-bound search by point.
    std::vector<rt_type::point_type> pts = {
        {-10, -10},
        {1, 7},
        {6, 3},
    };

    for (const rt_type::point_type& pt : pts)
    {
        res = ctree.search(pt, search_type::overlap);
        TEST_ASSERT(res.cbegin() == res.cend());
    }
}

void rtree_test_basic_erase()
{
    MDDS_TEST_FUNC_SCOPE;

    using rt_type = rtree<int16_t, std::string>;
    using search_type = rt_type::search_type;
    rt_type::integrity_check_properties check_props;
    check_props.throw_on_first_error = true;

    rt_type tree;
    const rt_type& ctree = tree;
    tree.insert({{-2, -2}, {2, 2}}, "erase me");
    TEST_ASSERT(!tree.empty());
    TEST_ASSERT(tree.size() == 1);

    rt_type::const_search_results res = ctree.search({0, 0}, search_type::overlap);

    size_t n = std::distance(res.begin(), res.end());
    TEST_ASSERT(n == 1);

    rt_type::const_iterator it = res.begin();
    TEST_ASSERT(it != res.end());

    tree.erase(it);
    TEST_ASSERT(tree.empty());
    TEST_ASSERT(tree.size() == 0);
    TEST_ASSERT(rt_type::extent_type() == tree.extent());

    tree.insert({{0, 0}, {2, 2}}, "erase me");
    tree.insert({{-10, -4}, {0, 0}}, "erase me");
    rt_type::extent_type expected_bb({-10, -4}, {2, 2});
    TEST_ASSERT(tree.extent() == expected_bb);
    TEST_ASSERT(tree.size() == 2);

    res = ctree.search({-5, -2}, search_type::overlap);
    n = std::distance(res.begin(), res.end());
    TEST_ASSERT(n == 1);
    it = res.begin();
    tree.erase(it);
    TEST_ASSERT(!tree.empty()); // there should be one value stored in the tree.
    TEST_ASSERT(tree.size() == 1);
    expected_bb = {{0, 0}, {2, 2}};
    TEST_ASSERT(tree.extent() == expected_bb);

    tree.check_integrity(check_props);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */