blob: 269094c07bfa4a0170970a8a72614eafb2d80074 [file] [log] [blame]
Austin Schuh906616c2019-01-21 20:25:11 -08001// 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
65using namespace std;
66#ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
67using namespace __gnu_cxx;
68#endif
69
70struct user_hash {
71 size_t operator()(int x) const { return x; }
72};
73
74static 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(), "10 20 30");
111 hash_set<int> copied_hs(hs);
112 CHECK_EQ(hs, copied_hs); // This must compile.
113 }
114#endif
115
116#ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
117 {
118 // Test a hashed pair associative container.
119 hash_map<int, string> hm;
120 hm[10] = "ten";
121 hm[20] = "twenty";
122 hm[30] = "thirty";
123 ostringstream ss;
124 ss << hm;
125 EXPECT_EQ(ss.str(), "(10, ten) (20, twenty) (30, thirty)");
126 hash_map<int, string> copied_hm(hm);
127 CHECK_EQ(hm, copied_hm); // this must compile
128 }
129#endif
130
131 {
132 // Test a long sequence.
133 vector<int> v;
134 string expected;
135 for (int i = 0; i < 100; i++) {
136 v.push_back(i);
137 if (i > 0) expected += ' ';
138 char buf[256];
139 sprintf(buf, "%d", i);
140 expected += buf;
141 }
142 v.push_back(100);
143 expected += " ...";
144 ostringstream ss;
145 ss << v;
146 CHECK_EQ(ss.str(), expected.c_str());
147 }
148
149 {
150 // Test a sorted pair associative container.
151 // Use a non-default comparison functor.
152 map< int, string, greater<int> > m;
153 m[20] = "twenty";
154 m[10] = "ten";
155 m[30] = "thirty";
156 ostringstream ss;
157 ss << m;
158 EXPECT_EQ(ss.str(), "(30, thirty) (20, twenty) (10, ten)");
159 map< int, string, greater<int> > copied_m(m);
160 CHECK_EQ(m, copied_m); // This must compile.
161 }
162
163#ifdef GLOG_STL_LOGGING_FOR_EXT_HASH
164 {
165 // Test a hashed simple associative container.
166 // Use a user defined hash function.
167 hash_set<int, user_hash> hs;
168 hs.insert(10);
169 hs.insert(20);
170 hs.insert(30);
171 ostringstream ss;
172 ss << hs;
173 EXPECT_EQ(ss.str(), "10 20 30");
174 hash_set<int, user_hash> copied_hs(hs);
175 CHECK_EQ(hs, copied_hs); // This must compile.
176 }
177#endif
178}
179
180int main(int, char**) {
181 TestSTLLogging();
182 std::cout << "PASS\n";
183 return 0;
184}
185
186#else
187
188#include <iostream>
189
190int main(int, char**) {
191 std::cout << "We don't support stl_logging for this compiler.\n"
192 << "(we need compiler support of 'using ::operator<<' "
193 << "for this feature.)\n";
194 return 0;
195}
196
197#endif // HAVE_USING_OPERATOR