stl_logging_unittest.cc
Go to the documentation of this file.
1 // Copyright (c) 2003, 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 #include "config.h"
31 
32 #ifdef HAVE_USING_OPERATOR
33 
34 #include <functional>
35 #include <iostream>
36 #include <map>
37 #include <ostream>
38 #include <string>
39 #include <vector>
40 
41 #ifdef __GNUC__
42 // C++0x isn't enabled by default in GCC and libc++ does not have
43 // non-standard ext/* and tr1/unordered_*.
44 # if defined(_LIBCPP_VERSION)
45 # ifndef GLOG_STL_LOGGING_FOR_UNORDERED
46 # define GLOG_STL_LOGGING_FOR_UNORDERED
47 # endif
48 # else
49 # ifndef GLOG_STL_LOGGING_FOR_EXT_HASH
50 # define GLOG_STL_LOGGING_FOR_EXT_HASH
51 # endif
52 # ifndef GLOG_STL_LOGGING_FOR_EXT_SLIST
53 # define GLOG_STL_LOGGING_FOR_EXT_SLIST
54 # endif
55 # ifndef GLOG_STL_LOGGING_FOR_TR1_UNORDERED
56 # define GLOG_STL_LOGGING_FOR_TR1_UNORDERED
57 # endif
58 # endif
59 #endif
60 
61 #include <glog/logging.h>
62 #include <glog/stl_logging.h>
63 #include "googletest.h"
64 
65 using namespace std;
66 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
67 using namespace __gnu_cxx;
68 #endif
69 
70 struct user_hash {
71  size_t operator()(int x) const { return static_cast<size_t>(x); }
72 };
73 
74 static void TestSTLLogging() {
75  {
76  // Test a sequence.
77  vector<int> v;
78  v.push_back(10);
79  v.push_back(20);
80  v.push_back(30);
81  ostringstream ss;
82  ss << v;
83  EXPECT_EQ(ss.str(), "10 20 30");
84  vector<int> copied_v(v);
85  CHECK_EQ(v, copied_v); // This must compile.
86  }
87 
88  {
89  // Test a sorted pair associative container.
90  map< int, string > m;
91  m[20] = "twenty";
92  m[10] = "ten";
93  m[30] = "thirty";
94  ostringstream ss;
95  ss << m;
96  EXPECT_EQ(ss.str(), "(10, ten) (20, twenty) (30, thirty)");
97  map< int, string > copied_m(m);
98  CHECK_EQ(m, copied_m); // This must compile.
99  }
100 
101 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
102  {
103  // Test a hashed simple associative container.
104  hash_set<int> hs;
105  hs.insert(10);
106  hs.insert(20);
107  hs.insert(30);
108  ostringstream ss;
109  ss << hs;
110  EXPECT_EQ(ss.str().size(), 8);
111  EXPECT_TRUE(ss.str().find("10") != string::npos);
112  EXPECT_TRUE(ss.str().find("20") != string::npos);
113  EXPECT_TRUE(ss.str().find("30") != string::npos);
114  hash_set<int> copied_hs(hs);
115  CHECK_EQ(hs, copied_hs); // This must compile.
116  }
117 #endif
118 
119 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
120  {
121  // Test a hashed pair associative container.
122  hash_map<int, string> hm;
123  hm[10] = "ten";
124  hm[20] = "twenty";
125  hm[30] = "thirty";
126  ostringstream ss;
127  ss << hm;
128  EXPECT_EQ(ss.str().size(), 35);
129  EXPECT_TRUE(ss.str().find("(10, ten)") != string::npos);
130  EXPECT_TRUE(ss.str().find("(20, twenty)") != string::npos);
131  EXPECT_TRUE(ss.str().find("(30, thirty)") != string::npos);
132  hash_map<int, string> copied_hm(hm);
133  CHECK_EQ(hm, copied_hm); // this must compile
134  }
135 #endif
136 
137  {
138  // Test a long sequence.
139  vector<int> v;
140  string expected;
141  for (int i = 0; i < 100; i++) {
142  v.push_back(i);
143  if (i > 0) expected += ' ';
144  const size_t buf_size = 256;
145  char buf[buf_size];
146  snprintf(buf, buf_size, "%d", i);
147  expected += buf;
148  }
149  v.push_back(100);
150  expected += " ...";
151  ostringstream ss;
152  ss << v;
153  CHECK_EQ(ss.str(), expected.c_str());
154  }
155 
156  {
157  // Test a sorted pair associative container.
158  // Use a non-default comparison functor.
159  map< int, string, greater<int> > m;
160  m[20] = "twenty";
161  m[10] = "ten";
162  m[30] = "thirty";
163  ostringstream ss;
164  ss << m;
165  EXPECT_EQ(ss.str(), "(30, thirty) (20, twenty) (10, ten)");
166  map< int, string, greater<int> > copied_m(m);
167  CHECK_EQ(m, copied_m); // This must compile.
168  }
169 
170 #ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
171  {
172  // Test a hashed simple associative container.
173  // Use a user defined hash function.
174  hash_set<int, user_hash> hs;
175  hs.insert(10);
176  hs.insert(20);
177  hs.insert(30);
178  ostringstream ss;
179  ss << hs;
180  EXPECT_EQ(ss.str().size(), 8);
181  EXPECT_TRUE(ss.str().find("10") != string::npos);
182  EXPECT_TRUE(ss.str().find("20") != string::npos);
183  EXPECT_TRUE(ss.str().find("30") != string::npos);
184  hash_set<int, user_hash> copied_hs(hs);
185  CHECK_EQ(hs, copied_hs); // This must compile.
186  }
187 #endif
188 }
189 
190 int main(int, char**) {
191  TestSTLLogging();
192  std::cout << "PASS\n";
193  return 0;
194 }
195 
196 #else
197 
198 #include <iostream>
199 
200 int main(int, char**) {
201  std::cout << "We don't support stl_logging for this compiler.\n"
202  << "(we need compiler support of 'using ::operator<<' "
203  << "for this feature.)\n";
204  return 0;
205 }
206 
207 #endif // HAVE_USING_OPERATOR
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
x
GLint GLenum GLint x
Definition: glcorearb.h:2834
snprintf
int snprintf(char *str, size_t size, const char *format,...)
Definition: port.cc:64
main
int main(int, char **)
Definition: stl_logging_unittest.cc:200
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:4175
i
int i
Definition: gmock-matchers_test.cc:764
v
const GLdouble * v
Definition: glcorearb.h:3106
std
m
const upb_json_parsermethod * m
Definition: ruby/ext/google/protobuf_c/upb.h:10501
CHECK_EQ
#define CHECK_EQ(a, b)
Definition: check.h:65


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59