Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
googletest-message-test.cc
Go to the documentation of this file.
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // 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
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 //
31 // Tests for the Message class.
32 
33 #include "gtest/gtest-message.h"
34 
35 #include "gtest/gtest.h"
36 
37 namespace {
38 
39 using ::testing::Message;
40 
41 // Tests the testing::Message class
42 
43 // Tests the default constructor.
44 TEST(MessageTest, DefaultConstructor) {
45  const Message msg;
46  EXPECT_EQ("", msg.GetString());
47 }
48 
49 // Tests the copy constructor.
50 TEST(MessageTest, CopyConstructor) {
51  const Message msg1("Hello");
52  const Message msg2(msg1);
53  EXPECT_EQ("Hello", msg2.GetString());
54 }
55 
56 // Tests constructing a Message from a C-string.
57 TEST(MessageTest, ConstructsFromCString) {
58  Message msg("Hello");
59  EXPECT_EQ("Hello", msg.GetString());
60 }
61 
62 // Tests streaming a float.
63 TEST(MessageTest, StreamsFloat) {
64  const std::string s = (Message() << 1.23456F << " " << 2.34567F).GetString();
65  // Both numbers should be printed with enough precision.
66  EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s.c_str());
67  EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s.c_str());
68 }
69 
70 // Tests streaming a double.
71 TEST(MessageTest, StreamsDouble) {
72  const std::string s = (Message() << 1260570880.4555497 << " "
73  << 1260572265.1954534).GetString();
74  // Both numbers should be printed with enough precision.
75  EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str());
76  EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str());
77 }
78 
79 // Tests streaming a non-char pointer.
80 TEST(MessageTest, StreamsPointer) {
81  int n = 0;
82  int* p = &n;
83  EXPECT_NE("(null)", (Message() << p).GetString());
84 }
85 
86 // Tests streaming a NULL non-char pointer.
87 TEST(MessageTest, StreamsNullPointer) {
88  int* p = nullptr;
89  EXPECT_EQ("(null)", (Message() << p).GetString());
90 }
91 
92 // Tests streaming a C string.
93 TEST(MessageTest, StreamsCString) {
94  EXPECT_EQ("Foo", (Message() << "Foo").GetString());
95 }
96 
97 // Tests streaming a NULL C string.
98 TEST(MessageTest, StreamsNullCString) {
99  char* p = nullptr;
100  EXPECT_EQ("(null)", (Message() << p).GetString());
101 }
102 
103 // Tests streaming std::string.
104 TEST(MessageTest, StreamsString) {
105  const ::std::string str("Hello");
106  EXPECT_EQ("Hello", (Message() << str).GetString());
107 }
108 
109 // Tests that we can output strings containing embedded NULs.
110 TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
111  const char char_array_with_nul[] =
112  "Here's a NUL\0 and some more string";
113  const ::std::string string_with_nul(char_array_with_nul,
114  sizeof(char_array_with_nul) - 1);
115  EXPECT_EQ("Here's a NUL\\0 and some more string",
116  (Message() << string_with_nul).GetString());
117 }
118 
119 // Tests streaming a NUL char.
120 TEST(MessageTest, StreamsNULChar) {
121  EXPECT_EQ("\\0", (Message() << '\0').GetString());
122 }
123 
124 // Tests streaming int.
125 TEST(MessageTest, StreamsInt) {
126  EXPECT_EQ("123", (Message() << 123).GetString());
127 }
128 
129 // Tests that basic IO manipulators (endl, ends, and flush) can be
130 // streamed to Message.
131 TEST(MessageTest, StreamsBasicIoManip) {
132  EXPECT_EQ("Line 1.\nA NUL char \\0 in line 2.",
133  (Message() << "Line 1." << std::endl
134  << "A NUL char " << std::ends << std::flush
135  << " in line 2.").GetString());
136 }
137 
138 // Tests Message::GetString()
139 TEST(MessageTest, GetString) {
140  Message msg;
141  msg << 1 << " lamb";
142  EXPECT_EQ("1 lamb", msg.GetString());
143 }
144 
145 // Tests streaming a Message object to an ostream.
146 TEST(MessageTest, StreamsToOStream) {
147  Message msg("Hello");
148  ::std::stringstream ss;
149  ss << msg;
151 }
152 
153 // Tests that a Message object doesn't take up too much stack space.
154 TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
155  EXPECT_LE(sizeof(Message), 16U);
156 }
157 
158 } // namespace
const char * p
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
#define EXPECT_LE(val1, val2)
Definition: gtest.h:2042
#define EXPECT_NE(val1, val2)
Definition: gtest.h:2040
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2348
GTEST_API_ AssertionResult IsSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
Definition: gtest.cc:1789
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2038
GTEST_API_ std::string StringStreamToString(::std::stringstream *stream)
Definition: gtest.cc:2171
int n