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