PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/kdtree_tests.cc

https://bitbucket.org/tetonedge/libsrvf
C++ | 87 lines | 70 code | 17 blank | 0 comment | 8 complexity | fc0d18821571156b7a4de5e3ec4ab3eb MD5 | raw file
Possible License(s): GPL-3.0
 1#include <boost/test/unit_test.hpp>
 2
 3#include <srvf/matrix.h>
 4#include <srvf/kdtree.h>
 5#include <srvf/numeric.h>
 6
 7#include <cstdlib>
 8#include <vector>
 9#include <algorithm>
10
11#define MY_CHECK_CLOSE(a,b) \
12 BOOST_CHECK_EQUAL(srvf::numeric::almost_equal((a),(b)), true)
13
14#define MY_REQUIRE_CLOSE(a,b) \
15 BOOST_REQUIRE_EQUAL(srvf::numeric::almost_equal((a),(b)), true)
16
17
18static srvf::Matrix randmat_(size_t r, size_t c, int modulus)
19{
20  srvf::Matrix result(r,c);
21  for (size_t i=0; i<result.size(); ++i)
22    result[i] = (double)(rand() % modulus);
23  return result;
24}
25
26
27BOOST_AUTO_TEST_SUITE(kdtree_tests)
28
29BOOST_AUTO_TEST_CASE(basic_test1)
30{
31  srvf::KdTree<srvf::Matrix> tree;
32  std::vector<srvf::Matrix> mats;
33  for (size_t i=0; i<5000; ++i)
34  {
35    mats.push_back(randmat_(1,8,2000));
36    tree.insert(mats.back());
37  }
38
39  for (size_t i=0; i<100; ++i)
40  {
41    srvf::Matrix query = randmat_(1,8,2000);
42    double expected_min_dist=1e9;
43    for (size_t j=0; j<mats.size(); ++j)
44    {
45      double cur_dist = query.distance_to(mats[j]);
46      expected_min_dist = std::min(expected_min_dist, cur_dist);
47    }
48
49    double actual_min_dist=1e9;
50    srvf::Matrix actual_nbr=tree.get_nearest_neighbor(query, &actual_min_dist);
51    MY_CHECK_CLOSE(actual_min_dist, expected_min_dist);
52  }
53}
54
55BOOST_AUTO_TEST_CASE(basic_test2)
56{
57  srvf::KdTree<srvf::Matrix> tree;
58  std::vector<srvf::Matrix> mats;
59  std::vector<bool> intree;
60  for (size_t i=0; i<100; ++i)
61  {
62    mats.push_back(randmat_(1,3,100));
63    bool inserted = tree.insert_cond(mats.back(), 10.0);
64    intree.push_back(inserted);
65
66    double min_dist=1e9;
67    for (size_t j=0; j+1<i; ++j)
68    {
69      if (intree[j])
70        min_dist = std::min(min_dist, mats[i].distance_to(mats[j]));
71    }
72
73    BOOST_CHECK_EQUAL( (min_dist >= 10.0), inserted );
74  }
75}
76
77BOOST_AUTO_TEST_CASE(timing_test1)
78{
79  srvf::KdTree<srvf::Matrix> tree;
80
81  for (size_t i=0; i<10000; ++i)
82  {
83    tree.insert_cond(randmat_(1,3,10), 0.05);
84  }
85}
86
87BOOST_AUTO_TEST_SUITE_END()