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