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