src
test
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>
33
#include <
rviz/uniform_string_stream.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.2
f
);
46
EXPECT_EQ(b, 3.4
f
);
47
EXPECT_EQ(c, 560.0
f
);
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
char
do_decimal_point
()
const override
71
{
72
return
'p'
;
73
}
74
};
75
76
TEST
(
UniformStringStream
, print_floats)
77
{
78
UniformStringStream
uss;
79
uss << 1.2f;
80
EXPECT_EQ(uss.str(),
"1.2"
);
81
82
CommaFloat
* comma_float_facet =
new
CommaFloat
;
83
std::locale new_locale(std::locale::classic(), comma_float_facet);
84
85
std::locale old_locale = std::locale::global(new_locale);
86
87
// Make sure the comma_float_facet is working.
88
std::stringstream ss;
89
ss << 3.4f;
90
EXPECT_EQ(ss.str(),
"3p4"
);
91
92
// Make sure the float facet gets clobbered within UniformStringStream.
93
UniformStringStream
uss2;
94
uss2 << 3.4f;
95
EXPECT_EQ(uss2.str(),
"3.4"
);
96
97
// Put things back to normal so other tests don't break.
98
std::locale::global(old_locale);
99
}
100
101
int
main
(
int
argc,
char
** argv)
102
{
103
testing::InitGoogleTest(&argc, argv);
104
return
RUN_ALL_TESTS();
105
}
rviz::UniformStringStream::parseFloat
void parseFloat(float &f)
Parse a float, supporting both period- and comma- style floats (1,2 and 1.2).
Definition:
uniform_string_stream.cpp:46
CommaFloat::do_decimal_point
char do_decimal_point() const override
Definition:
uniform_string_stream_test.cpp:70
TEST
TEST(UniformStringStream, parse_floats)
Definition:
uniform_string_stream_test.cpp:37
main
int main(int argc, char **argv)
Definition:
uniform_string_stream_test.cpp:101
rviz::UniformStringStream
std::stringstream subclass which defaults to the "C" locale, so serialization of numbers is uniform a...
Definition:
uniform_string_stream.h:44
f
f
rviz
Definition:
add_display_dialog.cpp:54
CommaFloat
Definition:
uniform_string_stream_test.cpp:67
uniform_string_stream.h
rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10