uniform_string_stream_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 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 #include <locale>
31 
32 #include <gtest/gtest.h>
34 
35 using namespace rviz;
36 
37 TEST( UniformStringStream, parse_floats )
38 {
39  UniformStringStream uss( "1,2 3.4 5,6e2" );
40  float a, b, c;
41  uss.parseFloat( a );
42  uss.parseFloat( b );
43  uss.parseFloat( c );
44  EXPECT_TRUE( !!uss );
45  EXPECT_EQ( a, 1.2f );
46  EXPECT_EQ( b, 3.4f );
47  EXPECT_EQ( c, 560.0f );
48  uss.parseFloat( a );
49  EXPECT_FALSE( !!uss );
50 }
51 
52 TEST( UniformStringStream, parse_ints )
53 {
54  UniformStringStream uss( "1 2 -3" );
55  int a, b, c;
56  uss >> a;
57  uss >> b;
58  uss >> c;
59  EXPECT_TRUE( !!uss );
60  EXPECT_EQ( a, 1 );
61  EXPECT_EQ( b, 2 );
62  EXPECT_EQ( c, -3 );
63  uss >> a;
64  EXPECT_FALSE( !!uss );
65 }
66 
67 class CommaFloat: public std::numpunct<char>
68 {
69 protected:
70  virtual char do_decimal_point() const { return 'p'; }
71 };
72 
73 TEST( UniformStringStream, print_floats )
74 {
76  uss << 1.2f;
77  EXPECT_EQ( uss.str(), "1.2" );
78 
79  CommaFloat* comma_float_facet = new CommaFloat;
80  std::locale new_locale( std::locale::classic(), comma_float_facet );
81 
82  std::locale old_locale = std::locale::global( new_locale );
83 
84  // Make sure the comma_float_facet is working.
85  std::stringstream ss;
86  ss << 3.4f;
87  EXPECT_EQ( ss.str(), "3p4" );
88 
89  // Make sure the float facet gets clobbered within UniformStringStream.
91  uss2 << 3.4f;
92  EXPECT_EQ( uss2.str(), "3.4" );
93 
94  // Put things back to normal so other tests don't break.
95  std::locale::global( old_locale );
96 }
97 
98 int main(int argc, char **argv){
99  testing::InitGoogleTest(&argc, argv);
100  return RUN_ALL_TESTS();
101 }
void parseFloat(float &f)
Parse a float, supporting both period- and comma- style floats (1,2 and 1.2).
f
std::stringstream subclass which defaults to the "C" locale, so serialization of numbers is uniform a...
int main(int argc, char **argv)
virtual char do_decimal_point() const
TEST(UniformStringStream, parse_floats)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51