utest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Brian Gerkey */
31 
32 #include <stdexcept> // for std::runtime_error
33 #include <gtest/gtest.h>
35 #include "test_constants.h"
36 
37 /* Try to load a valid PNG file. Succeeds if no exception is thrown, and if
38  * the loaded image matches the known dimensions and content of the file.
39  *
40  * This test can fail on OS X, due to an apparent limitation of the
41  * underlying SDL_Image library. */
42 TEST(MapServer, loadValidPNG)
43 {
44  try
45  {
46  nav_msgs::GetMap::Response map_resp;
47  double origin[3] = { 0.0, 0.0, 0.0 };
48  map_server::loadMapFromFile(&map_resp, g_valid_png_file, g_valid_image_res, false, 0.65, 0.1, origin);
49  EXPECT_FLOAT_EQ(map_resp.map.info.resolution, g_valid_image_res);
50  EXPECT_EQ(map_resp.map.info.width, g_valid_image_width);
51  EXPECT_EQ(map_resp.map.info.height, g_valid_image_height);
52  for(unsigned int i=0; i < map_resp.map.info.width * map_resp.map.info.height; i++)
53  EXPECT_EQ(g_valid_image_content[i], map_resp.map.data[i]);
54  }
55  catch(...)
56  {
57  ADD_FAILURE() << "Uncaught exception : " << "This is OK on OS X";
58  }
59 }
60 
61 /* Try to load a valid BMP file. Succeeds if no exception is thrown, and if
62  * the loaded image matches the known dimensions and content of the file. */
63 TEST(MapServer, loadValidBMP)
64 {
65  try
66  {
67  nav_msgs::GetMap::Response map_resp;
68  double origin[3] = { 0.0, 0.0, 0.0 };
69  map_server::loadMapFromFile(&map_resp, g_valid_bmp_file, g_valid_image_res, false, 0.65, 0.1, origin);
70  EXPECT_FLOAT_EQ(map_resp.map.info.resolution, g_valid_image_res);
71  EXPECT_EQ(map_resp.map.info.width, g_valid_image_width);
72  EXPECT_EQ(map_resp.map.info.height, g_valid_image_height);
73  for(unsigned int i=0; i < map_resp.map.info.width * map_resp.map.info.height; i++)
74  EXPECT_EQ(g_valid_image_content[i], map_resp.map.data[i]);
75  }
76  catch(...)
77  {
78  ADD_FAILURE() << "Uncaught exception";
79  }
80 }
81 
82 /* Try to load an invalid file. Succeeds if a std::runtime_error exception
83  * is thrown. */
84 TEST(MapServer, loadInvalidFile)
85 {
86  try
87  {
88  nav_msgs::GetMap::Response map_resp;
89  double origin[3] = { 0.0, 0.0, 0.0 };
90  map_server::loadMapFromFile(&map_resp, "foo", 0.1, false, 0.65, 0.1, origin);
91  }
92  catch(std::runtime_error &e)
93  {
94  SUCCEED();
95  return;
96  }
97  catch(...)
98  {
99  FAIL() << "Uncaught exception";
100  }
101  ADD_FAILURE() << "Didn't throw exception as expected";
102 }
103 
104 std::vector<unsigned int> countValues(const nav_msgs::GetMap::Response& map_resp)
105 {
106  std::vector<unsigned int> counts(256, 0);
107  for (unsigned int i = 0; i < map_resp.map.data.size(); i++)
108  {
109  unsigned char value = static_cast<unsigned char>(map_resp.map.data[i]);
110  counts[value]++;
111  }
112  return counts;
113 }
114 
115 TEST(MapServer, testMapMode)
116 {
117  nav_msgs::GetMap::Response map_resp;
118  double origin[3] = { 0.0, 0.0, 0.0 };
119 
120  map_server::loadMapFromFile(&map_resp, g_spectrum_png_file, 0.1, false, 0.65, 0.1, origin, TRINARY);
121  std::vector<unsigned int> trinary_counts = countValues(map_resp);
122  EXPECT_EQ(90u, trinary_counts[100]);
123  EXPECT_EQ(26u, trinary_counts[0]);
124  EXPECT_EQ(140u, trinary_counts[255]);
125 
126  map_server::loadMapFromFile(&map_resp, g_spectrum_png_file, 0.1, false, 0.65, 0.1, origin, SCALE);
127  std::vector<unsigned int> scale_counts = countValues(map_resp);
128  EXPECT_EQ(90u, scale_counts[100]);
129  EXPECT_EQ(26u, scale_counts[0]);
130  unsigned int scaled_values = 0;
131  for (unsigned int i = 1; i < 100; i++)
132  {
133  scaled_values += scale_counts[i];
134  }
135  EXPECT_EQ(140u, scaled_values);
136 
137  map_server::loadMapFromFile(&map_resp, g_spectrum_png_file, 0.1, false, 0.65, 0.1, origin, RAW);
138  std::vector<unsigned int> raw_counts = countValues(map_resp);
139  for (unsigned int i = 0; i < raw_counts.size(); i++)
140  {
141  EXPECT_EQ(1u, raw_counts[i]) << i;
142  }
143 }
144 
145 int main(int argc, char **argv)
146 {
147  testing::InitGoogleTest(&argc, argv);
148  return RUN_ALL_TESTS();
149 }
main
int main(int argc, char **argv)
Definition: utest.cpp:145
g_valid_image_width
const unsigned int g_valid_image_width
Definition: test_constants.cpp:38
g_spectrum_png_file
const char * g_spectrum_png_file
Definition: test_constants.cpp:60
TEST
TEST(MapServer, loadValidPNG)
Definition: utest.cpp:42
countValues
std::vector< unsigned int > countValues(const nav_msgs::GetMap::Response &map_resp)
Definition: utest.cpp:104
g_valid_bmp_file
const char * g_valid_bmp_file
Definition: test_constants.cpp:59
map_server::loadMapFromFile
void loadMapFromFile(nav_msgs::GetMap::Response *resp, const char *fname, double res, bool negate, double occ_th, double free_th, double *origin, MapMode mode=TRINARY)
Definition: image_loader.cpp:57
TRINARY
@ TRINARY
Definition: image_loader.h:52
test_constants.h
g_valid_image_res
const float g_valid_image_res
Definition: test_constants.cpp:62
MapServer
Definition: main.cpp:60
g_valid_png_file
const char * g_valid_png_file
Definition: test_constants.cpp:58
g_valid_image_height
const unsigned int g_valid_image_height
Definition: test_constants.cpp:39
SCALE
@ SCALE
Definition: image_loader.h:52
RAW
@ RAW
Definition: image_loader.h:52
g_valid_image_content
const char g_valid_image_content[]
Definition: test_constants.cpp:45
image_loader.h


map_server
Author(s): Brian Gerkey, Tony Pratkanis, contradict@gmail.com
autogenerated on Mon Mar 6 2023 03:50:11