utest.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018, Locus Robotics
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 #include <gtest/gtest.h>
00035 #include <nav_grid_iterators/iterators.h>
00036 #include <algorithm>
00037 #include <vector>
00038 
00039 using nav_grid::Index;
00040 
00041 template<class iterator_type>
00042 int countIterations(iterator_type it, int max_iterations = 1000)
00043 {
00044   int count = 0;
00045   iterator_type end = it.end();
00046   for ( ; it != end; ++it)
00047   {
00048     ++count;
00049     if (count >= max_iterations) break;
00050   }
00051   return count;
00052 }
00053 
00054 TEST(WholeGrid, whole_grid)
00055 {
00056   nav_grid::NavGridInfo info;
00057   info.width = 5;
00058   info.height = 8;
00059   int count = 0;
00060   for (nav_grid_iterators::WholeGrid it(info); it != it.end(); ++it)
00061   {
00062     Index i = *it;
00063     ASSERT_EQ(i.x, count % info.width);
00064     ASSERT_EQ(i.y, count / info.width);
00065     ++count;
00066   }
00067   ASSERT_EQ(count, 40);
00068 }
00069 
00070 TEST(WholeGrid, whole_grid_range)
00071 {
00072   nav_grid::NavGridInfo info;
00073   info.width = 3;
00074   info.height = 6;
00075   int count = 0;
00076   for (Index i : nav_grid_iterators::WholeGrid(info))
00077   {
00078     ASSERT_EQ(i.x, count % info.width);
00079     ASSERT_EQ(i.y, count / info.width);
00080     ++count;
00081   }
00082   ASSERT_EQ(count, 18);
00083 }
00084 
00085 TEST(WholeGrid, std_stuff)
00086 {
00087   nav_grid::NavGridInfo info;
00088   info.width = 8;
00089   info.height = 2;
00090   nav_grid_iterators::WholeGrid wg(info);
00091 
00092   std::vector<Index> vec;
00093   std::copy(wg.begin(), wg.end(), std::back_inserter(vec));
00094   for (int count = 0; count < 16; ++count)
00095   {
00096     Index& i = vec[count];
00097     ASSERT_EQ(i.x, count % info.width);
00098     ASSERT_EQ(i.y, count / info.width);
00099   }
00100 }
00101 
00102 TEST(SubGrid, sub_grid)
00103 {
00104   nav_grid::NavGridInfo info;
00105   info.width = 5;
00106   info.height = 8;
00107   int count = 0;
00108   for (Index i : nav_grid_iterators::SubGrid(&info, 1, 2, 2, 3))
00109   {
00110     ASSERT_EQ(i.x, static_cast<unsigned int>(1 + count % 2));
00111     ASSERT_EQ(i.y, static_cast<unsigned int>(2 + count / 2));
00112     ++count;
00113   }
00114   ASSERT_EQ(count, 6);
00115 
00116   ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, 1, 3, 4, 1)), 4);
00117   ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, 1, 3, 2, 2)), 4);
00118   nav_core2::UIntBounds bounds(1, 3, 4, 3);
00119   ASSERT_EQ(countIterations(nav_grid_iterators::SubGrid(&info, bounds)), 4);
00120 }
00121 
00122 TEST(SubGrid, equality)
00123 {
00124   nav_grid::NavGridInfo info;
00125   info.width = 5;
00126   info.height = 8;
00127   nav_grid_iterators::SubGrid it1(&info, 1, 2, 2, 3);
00128   nav_grid_iterators::SubGrid it2(&info, 1, 2, 1, 1);
00129   ASSERT_FALSE(it1 == it2);
00130 }
00131 
00132 TEST(CircleFill, circle)
00133 {
00134   nav_grid::NavGridInfo info;
00135   info.width = 8;
00136   info.height = 8;
00137   info.resolution = 1.0;
00138 
00139   ASSERT_EQ(countIterations(nav_grid_iterators::CircleFill(&info, 4.0, 4.0, 3.0)), 32);
00140   ASSERT_EQ(countIterations(nav_grid_iterators::CircleFill(&info, 4.3, 4.0, 3.0)), 28);
00141   ASSERT_EQ(countIterations(nav_grid_iterators::CircleFill(&info, 4.0, 4.0, 8.0)), 64);
00142   ASSERT_EQ(countIterations(nav_grid_iterators::CircleFill(&info, 14.0, 4.0, 1.0)), 0);
00143   ASSERT_EQ(countIterations(nav_grid_iterators::CircleFill(&info, 0.0, 4.0, 4.0)), 26);
00144   ASSERT_EQ(countIterations(nav_grid_iterators::CircleFill(&info, 4.0, 4.0, 3.0).begin()), 32);
00145 }
00146 
00147 TEST(CircleFill, equality)
00148 {
00149   nav_grid::NavGridInfo info;
00150   info.width = 5;
00151   info.height = 8;
00152   nav_grid_iterators::CircleFill it1(&info, 4.0, 4.0, 8.0);
00153   nav_grid_iterators::CircleFill it2(&info, 1.0, 1.0, 100.0);
00154   ASSERT_FALSE(it1 == it2);
00155 }
00156 
00157 TEST(CircleOutline, circle_outline)
00158 {
00159   nav_grid::NavGridInfo info;
00160   info.width = 8;
00161   info.height = 8;
00162   info.resolution = 1.0;
00163 
00164   unsigned int size = 0;
00165   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 1);
00166   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 8);
00167   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 16);
00168   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 20);
00169   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 14);
00170   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 5);
00171   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 0);
00172   EXPECT_EQ(countIterations(nav_grid_iterators::CircleOutline(&info, 4.0, 4.0, size++)), 0);
00173 }
00174 
00175 TEST(CircleOutline, equality)
00176 {
00177   nav_grid::NavGridInfo info;
00178   info.width = 5;
00179   info.height = 8;
00180   nav_grid_iterators::CircleOutline it1(&info, 3.0, 1.0, 1.0);
00181   nav_grid_iterators::CircleOutline it2(&info, 1.0, 1.0, 3.0);
00182   ASSERT_FALSE(it1 == it2);
00183 }
00184 
00185 TEST(Spiral, spiral)
00186 {
00187   nav_grid::NavGridInfo info;
00188   info.width = 8;
00189   info.height = 8;
00190   info.resolution = 1.0;
00191   info.origin_x = 0.0;
00192   info.origin_y = 0.0;
00193   ASSERT_EQ(countIterations(nav_grid_iterators::Spiral(&info, 4.0, 4.0, 3.0)), 32);
00194   ASSERT_EQ(countIterations(nav_grid_iterators::Spiral(&info, 4.3, 4.0, 3.0)), 28);
00195   ASSERT_EQ(countIterations(nav_grid_iterators::Spiral(&info, 4.0, 4.0, 8.0)), 64);
00196   ASSERT_EQ(countIterations(nav_grid_iterators::Spiral(&info, 14.0, 4.0, 1.0)), 0);
00197   ASSERT_EQ(countIterations(nav_grid_iterators::Spiral(&info, 0.0, 4.0, 4.0)), 26);
00198 }
00199 
00200 TEST(Spiral, equality)
00201 {
00202   nav_grid::NavGridInfo info;
00203   info.width = 5;
00204   info.height = 8;
00205   nav_grid_iterators::Spiral it1(&info, 1.0, 1.0, 1.0);
00206   nav_grid_iterators::Spiral it2(&info, 1.0, 1.0, 3.0);
00207   ASSERT_FALSE(it1 == it2);
00208 }
00209 
00210 TEST(Line, signed_line)
00211 {
00212   nav_grid::NavGridInfo info;
00213   info.width = 10;
00214   info.height = 10;
00215   info.resolution = 1.0;
00216   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0, 0)), 1);
00217   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 3, 0)), 4);
00218   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, -3, 0)), 1);
00219   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 3, 0, 0, 0)), 4);
00220   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -3, 0, 0, 0)), 1);
00221   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -3, 0, 0, 0, false)), 0);
00222   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0, 3)), 4);
00223   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0, -3)), 1);
00224   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 3, 0, 0)), 4);
00225   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, -3, 0, 0)), 1);
00226   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 1, 9)), 10);
00227   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 1, 9, 0, 0)), 10);
00228   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 9, 1, 0, 0)), 10);
00229   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 9, 1)), 10);
00230 
00231   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -5, 5, 15, 5)), 10);
00232   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 15, 5, -5, 5)), 10);
00233   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 15, 5, 15, 7)), 0);
00234 
00235   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -1, -5, -1, -7)), 0);
00236 }
00237 
00238 TEST(Line, signed_line_diff_res)
00239 {
00240   // This is the same test as above with a reduced resolution. Most of the coordinates are divided by 10
00241   // (with an additional 0.05 in some places to avoid floating point errors)
00242   nav_grid::NavGridInfo info;
00243   info.width = 10;
00244   info.height = 10;
00245   info.resolution = 0.1;
00246   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0, 0)), 1);
00247   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0.35, 0)), 4);
00248   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, -0.35, 0)), 1);
00249   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0.35, 0, 0, 0)), 4);
00250   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -0.35, 0, 0, 0)), 1);
00251   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -0.35, 0, 0, 0, false)), 0);
00252   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0, 0.35)), 4);
00253   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0, -0.35)), 1);
00254   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0.35, 0, 0)), 4);
00255   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, -0.35, 0, 0)), 1);
00256   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0.15, 0.95)), 10);
00257   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0.15, 0.95, 0, 0)), 10);
00258   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0.95, 0.15, 0, 0)), 10);
00259   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 0, 0, 0.95, 0.15)), 10);
00260 
00261   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -0.5, 0.5, 1.5, 0.5)), 10);
00262   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 1.5, 0.5, -0.55, 0.55)), 10);
00263   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 1.5, 0.5, 1.5, 0.75)), 0);
00264 
00265   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, -0.1, -0.5, -0.1, -0.7)), 0);
00266 }
00267 
00268 TEST(Line, random_test_case)
00269 {
00270   nav_grid::NavGridInfo info;
00271   info.width = 795;
00272   info.height = 925;
00273   info.resolution = 0.05;
00274   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 6.2402, 30.651832, 2.805347, 22.8941, false, false)), 224);
00275   EXPECT_EQ(countIterations(nav_grid_iterators::Line(&info, 6.2402, 30.651832, 2.805347, 22.8941, true, false)), 225);
00276 }
00277 
00278 TEST(Line, equality)
00279 {
00280   nav_grid::NavGridInfo info;
00281   info.width = 5;
00282   info.height = 8;
00283   nav_grid_iterators::Line it1(&info, 0, 0, 5, 5);
00284   nav_grid_iterators::Line it2(&info, 0, 0, 1, 1);
00285   ASSERT_FALSE(it1 == it2);
00286 }
00287 
00288 nav_2d_msgs::Point2D make_point(double x, double y)
00289 {
00290   nav_2d_msgs::Point2D pt;
00291   pt.x = x;
00292   pt.y = y;
00293   return pt;
00294 }
00295 
00296 TEST(Polygon, polygon)
00297 {
00298   nav_grid::NavGridInfo info;
00299   // First check to make sure it works when the polygon is completely on the grid
00300   info.width = 10;
00301   info.height = 10;
00302   info.resolution = 1.0;
00303   nav_2d_msgs::Polygon2D simple_square;
00304   simple_square.points.push_back(make_point(1.4, 1.4));
00305   simple_square.points.push_back(make_point(1.4, 3.6));
00306   simple_square.points.push_back(make_point(3.6, 3.6));
00307   simple_square.points.push_back(make_point(3.6, 1.4));
00308 
00309   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonOutline(&info, simple_square)), 8);
00310   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonFill(&info, simple_square)), 9);
00311 
00312   // Then do it again when it is only partially on the grid
00313   info.height = 3;
00314   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonOutline(&info, simple_square)), 5);
00315   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonFill(&info, simple_square)), 6);
00316 
00317   // Then check when it is completely off the grid
00318   info.resolution = 0.1;
00319   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonOutline(&info, simple_square)), 0);
00320   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonFill(&info, simple_square)), 0);
00321 }
00322 
00323 TEST(Polygon, empty_polygon)
00324 {
00325   nav_grid::NavGridInfo info;
00326   info.width = 10;
00327   info.height = 10;
00328   info.resolution = 1.0;
00329   nav_2d_msgs::Polygon2D empty_polygon;
00330 
00331   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonOutline(&info, empty_polygon)), 0);
00332   EXPECT_EQ(countIterations(nav_grid_iterators::PolygonFill(&info, empty_polygon)), 0);
00333 }
00334 
00335 TEST(Polygon, equality)
00336 {
00337   nav_grid::NavGridInfo info;
00338   info.width = 10;
00339   info.height = 10;
00340   info.resolution = 1.0;
00341   nav_2d_msgs::Polygon2D simple_square;
00342   simple_square.points.push_back(make_point(1.4, 1.4));
00343   simple_square.points.push_back(make_point(1.4, 3.6));
00344   simple_square.points.push_back(make_point(3.6, 3.6));
00345   simple_square.points.push_back(make_point(3.6, 1.4));
00346 
00347   nav_2d_msgs::Polygon2D triangle;
00348   triangle.points.push_back(make_point(1.4, 1.4));
00349   triangle.points.push_back(make_point(1.4, 3.6));
00350   triangle.points.push_back(make_point(3.6, 3.6));
00351 
00352 
00353   nav_grid_iterators::PolygonOutline it1(&info, simple_square);
00354   nav_grid_iterators::PolygonOutline it2(&info, triangle);
00355   ASSERT_FALSE(it1 == it2);
00356 
00357   nav_grid_iterators::PolygonFill it3(&info, simple_square);
00358   nav_grid_iterators::PolygonFill it4(&info, triangle);
00359   ASSERT_FALSE(it3 == it4);
00360 }
00361 
00362 TEST(Iterators, test_copy)
00363 {
00364   // This test will fail to compile if you cannot use the copy operator
00365   nav_grid::NavGridInfo info;
00366   info.width = 10;
00367   info.height = 10;
00368   info.resolution = 1.0;
00369 
00370   nav_2d_msgs::Polygon2D simple_square;
00371   simple_square.points.push_back(make_point(1.4, 1.4));
00372   simple_square.points.push_back(make_point(1.4, 3.6));
00373   simple_square.points.push_back(make_point(3.6, 3.6));
00374   simple_square.points.push_back(make_point(3.6, 1.4));
00375 
00376   nav_grid_iterators::WholeGrid whole_grid(info);
00377   whole_grid = whole_grid.begin();
00378   nav_grid_iterators::SubGrid sub_grid(&info, 1, 2, 2, 3);
00379   sub_grid = sub_grid.begin();
00380   nav_grid_iterators::CircleFill cf(&info, 4.0, 4.0, 3.0);
00381   cf = cf.begin();
00382   nav_grid_iterators::CircleOutline co(&info, 4.0, 4.0, 3.0);
00383   co = co.begin();
00384   nav_grid_iterators::Spiral spiral(&info, 4.0, 4.0, 3.0);
00385   spiral = spiral.begin();
00386   nav_grid_iterators::Line line(&info, 0, 0, 0, 0);
00387   line = line.begin();
00388   nav_grid_iterators::PolygonOutline po(&info, simple_square);
00389   po = po.begin();
00390   nav_grid_iterators::PolygonFill pf(&info, simple_square);
00391   pf = pf.begin();
00392 }
00393 
00394 TEST(Iterators, test_assignment)
00395 {
00396   nav_grid::NavGridInfo info;
00397   info.width = 10;
00398   info.height = 10;
00399   info.resolution = 1.0;
00400   nav_grid_iterators::CircleFill iter1(&info, 3.0, 3.0, 1.0);
00401   // Sequence should be (2, 2) (3, 2) (2, 3) (3, 3)
00402   EXPECT_EQ((*iter1).x, 2U);
00403   EXPECT_EQ((*iter1).y, 2U);
00404 
00405   nav_grid_iterators::CircleFill iter2 = iter1;
00406 
00407   // Effective Copy
00408   EXPECT_EQ((*iter1).x, 2U);
00409   EXPECT_EQ((*iter1).y, 2U);
00410   EXPECT_EQ((*iter2).x, 2U);
00411   EXPECT_EQ((*iter2).y, 2U);
00412 
00413   // Increment only iter2
00414   ++iter2;
00415   EXPECT_EQ((*iter1).x, 2U);
00416   EXPECT_EQ((*iter1).y, 2U);
00417   EXPECT_EQ((*iter2).x, 3U);
00418   EXPECT_EQ((*iter2).y, 2U);
00419 
00420   // Increment first to match
00421   ++iter1;
00422   EXPECT_EQ((*iter1).x, 3U);
00423   EXPECT_EQ((*iter1).y, 2U);
00424   EXPECT_EQ((*iter2).x, 3U);
00425   EXPECT_EQ((*iter2).y, 2U);
00426 
00427   // Increment only iter2
00428   ++iter2;
00429   EXPECT_EQ((*iter1).x, 3U);
00430   EXPECT_EQ((*iter1).y, 2U);
00431   EXPECT_EQ((*iter2).x, 2U);
00432   EXPECT_EQ((*iter2).y, 3U);
00433 
00434   // Check copy when not at the start
00435   nav_grid_iterators::CircleFill iter3 = iter1;
00436   EXPECT_EQ((*iter1).x, 3U);
00437   EXPECT_EQ((*iter1).y, 2U);
00438   EXPECT_EQ((*iter2).x, 2U);
00439   EXPECT_EQ((*iter2).y, 3U);
00440   EXPECT_EQ((*iter3).x, 3U);
00441   EXPECT_EQ((*iter3).y, 2U);
00442 }
00443 
00444 int main(int argc, char **argv)
00445 {
00446   testing::InitGoogleTest(&argc, argv);
00447   return RUN_ALL_TESTS();
00448 }


nav_grid_iterators
Author(s):
autogenerated on Wed Jun 26 2019 20:09:45