config_test.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <gtest/gtest.h>
00031 #include <rviz/config.h>
00032 
00033 #include <ros/package.h> // For ros::package::getPath()
00034 
00035 TEST( Config, comparison )
00036 {
00037   std::string lhs = "b/c/d";
00038   std::string rhs = "b/c_d";
00039   rviz::Config::DirectoryCompare dc;
00040   EXPECT_FALSE( dc( lhs, rhs ));
00041 }
00042 
00043 TEST( Config, set_then_get )
00044 {
00045   rviz::Config c;
00046   c.set( "a", "1" );
00047   int a;
00048   EXPECT_TRUE( c.get( "a", &a, 2 ));
00049   EXPECT_EQ( a, 1 );
00050   float aa;
00051   EXPECT_TRUE( c.get( "a", &aa, 2.0 ));
00052   EXPECT_EQ( aa, 1.0 );
00053   std::string aaa;
00054   EXPECT_TRUE( c.get( "a", &aaa, "two" ));
00055   EXPECT_EQ( aaa, "1" );
00056 }
00057 
00058 TEST( Config, parse_floats )
00059 {
00060   rviz::Config c;
00061   c.set( "f", "1.1" );
00062   float f;
00063   EXPECT_TRUE( c.get( "f", &f, 2.0f ));
00064   EXPECT_EQ( f, 1.1f );
00065 
00066   // In Europe they use "," for a decimal point.
00067   c.set( "f", "1,2" );
00068   EXPECT_TRUE( c.get( "f", &f, 2.0f ));
00069   EXPECT_EQ( f, 1.2f );
00070 
00071   // This test should pass in all locales because Config uses
00072   // UniformStringStream which forces output in the "C" locale.
00073   std::string str;
00074   c.set( "f", 1.2f );
00075   c.get( "f", &str, "chub" );
00076   EXPECT_EQ( str, "1.2" );
00077 }
00078 
00079 TEST( Config, default_values )
00080 {
00081   rviz::Config c;
00082   int a;
00083   EXPECT_FALSE( c.get( "a", &a, 2 ));
00084   EXPECT_EQ( a, 2 );
00085   EXPECT_FALSE( c.get( "a", &a ));
00086   EXPECT_EQ( a, 0 );
00087   float aa;
00088   EXPECT_FALSE( c.get( "a", &aa, 2.0 ));
00089   EXPECT_EQ( aa, 2.0 );
00090   EXPECT_FALSE( c.get( "a", &aa ));
00091   EXPECT_EQ( aa, 0.0 );
00092   std::string aaa;
00093   EXPECT_FALSE( c.get( "a", &aaa, "two" ));
00094   EXPECT_EQ( aaa, "two" );
00095   EXPECT_FALSE( c.get( "a", &aaa ));
00096   EXPECT_EQ( aaa, "" );
00097 }
00098 
00099 TEST( Config, write_empty_string_value )
00100 {
00101   rviz::Config c;
00102   c.set( "key", "" );
00103 
00104   std::stringstream ss;
00105   c.write( ss );
00106 
00107   EXPECT_EQ( "key=\n", ss.str() );
00108 }
00109 
00110 TEST( Config, read_empty_string_value )
00111 {
00112   std::istringstream ss( "key=\n" );
00113 
00114   rviz::Config c;
00115   c.read( ss );
00116 
00117   std::string s;
00118   EXPECT_TRUE( c.get( "key", &s, "default" ));
00119   EXPECT_EQ( "", s );
00120 }
00121 
00122 TEST( Config, set_get_empty_value )
00123 {
00124   rviz::Config c;
00125   c.set( "key", "" );
00126 
00127   std::string s;
00128   EXPECT_TRUE( c.get( "key", &s, "default" ));
00129   EXPECT_EQ( "", s );
00130 }
00131 
00132 TEST( Config, write )
00133 {
00134   rviz::Config c;
00135   c.set( "/b/c_d", 1 );
00136   c.set( "b/alpha", 2 );
00137   c.set( "z", 6 );
00138   c.set( "/a/z", 3 );
00139   c.set( "d", 7 );
00140   c.set( "/b/c/d", 4);
00141   c.set( "/a", 5 );
00142   c.set( "/art/for/arts/sake", 8 );
00143   std::stringstream ss;
00144   c.write( ss );
00145   EXPECT_EQ( 
00146 "a=5\n\
00147 d=7\n\
00148 z=6\n\
00149 [a]\n\
00150 z=3\n\
00151 [art]\n\
00152 [art/for]\n\
00153 [art/for/arts]\n\
00154 sake=8\n\
00155 [b]\n\
00156 alpha=2\n\
00157 c_d=1\n\
00158 [b/c]\n\
00159 d=4\n\
00160 ", ss.str() );
00161 }
00162 
00163 TEST( Config, read )
00164 {
00165   std::string input =
00166 "a=1\n\
00167 foo_bar=biff=1; bazz=17\n\
00168 [chub]\n\
00169 sand=wich\n\
00170 [chub/town]\n\
00171 belly=big\n\
00172 tummy=large\n\
00173 ";
00174   std::istringstream iss;
00175   iss.str( input );
00176   rviz::Config c;
00177   c.read( iss );
00178 
00179   std::string s;
00180   EXPECT_TRUE( c.get( "/chub/town/belly", &s ));
00181   EXPECT_EQ( "big", s );
00182 
00183   EXPECT_TRUE( c.get( "/chub/sand", &s ));
00184   EXPECT_EQ( "wich", s );
00185 
00186   EXPECT_TRUE( c.get( "/foo_bar", &s ));
00187   EXPECT_EQ( "biff=1; bazz=17", s );
00188 
00189   int i;
00190   EXPECT_TRUE( c.get( "/a", &i ));
00191   EXPECT_EQ( 1, i );
00192 }
00193 
00194 TEST( Config, escape_space )
00195 {
00196   rviz::Config c;
00197   c.set( "a b", "c d" );
00198   std::string s;
00199   EXPECT_TRUE( c.get( "a b", &s ));
00200   EXPECT_EQ( "c d", s );
00201 
00202   std::stringstream ss;
00203   c.write( ss );
00204   EXPECT_EQ( "a\\ b=c d\n", ss.str() );
00205 }
00206 
00207 TEST( Config, escape_colon )
00208 {
00209   rviz::Config c;
00210   c.set( "a:b", "c:d" );
00211   std::string s;
00212   EXPECT_TRUE( c.get( "a:b", &s ));
00213   EXPECT_EQ( "c:d", s );
00214 
00215   std::stringstream ss;
00216   c.write( ss );
00217   EXPECT_EQ( "a\\:b=c:d\n", ss.str() );
00218 }
00219 
00220 TEST( Config, first_slash_is_optional )
00221 {
00222   rviz::Config c;
00223   c.set( "a", 1 );
00224   int i;
00225   EXPECT_TRUE( c.get( "/a", &i ));
00226   EXPECT_EQ( 1, i );
00227 
00228   c.set( "/b", 2 );
00229   EXPECT_TRUE( c.get( "b", &i ));
00230   EXPECT_EQ( 2, i );
00231 }
00232 
00233 TEST( Config, end_to_end )
00234 {
00235   rviz::Config c;
00236   c.set( "/a: b/foo/bar", 1.25f );
00237   c.set( "/a: b/foo/biff", 17 );
00238   c.set( "a", "a/b=c d" );
00239 
00240   std::stringstream ss;
00241   c.write( ss );
00242 
00243   c.clear();
00244 
00245   c.read( ss );
00246   float f;
00247   EXPECT_TRUE( c.get( "/a: b/foo/bar", &f ));
00248   EXPECT_EQ( 1.25, f ); // I don't need approximately-equal because I chose a floating-point-friendly number.
00249   int i;
00250   EXPECT_TRUE( c.get( "/a: b/foo/biff", &i ));
00251   EXPECT_EQ( 17, i );
00252   std::string s;
00253   EXPECT_TRUE( c.get( "a", &s ));
00254   EXPECT_EQ( "a/b=c d", s );
00255 }
00256 
00257 TEST( Config, file_io )
00258 {
00259   std::string package_path = ros::package::getPath(ROS_PACKAGE_NAME);
00260 
00261   rviz::Config c;
00262   EXPECT_TRUE( c.readFromFile( package_path + "/src/test/test_in.vcg" ));
00263   EXPECT_TRUE( c.writeToFile( package_path + "/build/test_out.vcg" ));
00264 
00265   // This test would be better if it sorted the input and output files
00266   // and checked that the results were the same.  I don't care enough
00267   // to learn how to do that with gtest right now.
00268 }
00269 
00270 int main(int argc, char **argv){
00271   testing::InitGoogleTest(&argc, argv);
00272   return RUN_ALL_TESTS();
00273 }
00274 


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35