1 // Copyright (c) 2003, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
32 #ifdef HAVE_USING_OPERATOR
34 #include "glog/stl_logging.h"
43 # include <ext/hash_map>
44 # include <ext/hash_set>
47 #include "glog/logging.h"
48 #include "googletest.h"
52 using namespace __gnu_cxx;
56 size_t operator()(int x) const { return x; }
59 void TestSTLLogging() {
67 ostrstream ss(ss_buf, sizeof(ss_buf));
68 // Just ostrstream s1; leaks heap.
70 CHECK_STREQ(ss.str(), "10 20 30");
71 vector<int> copied_v(v);
72 CHECK_EQ(v, copied_v); // This must compile.
76 // Test a sorted pair associative container.
82 ostrstream ss(ss_buf, sizeof(ss_buf));
84 CHECK_STREQ(ss.str(), "(10, ten) (20, twenty) (30, thirty)");
85 map< int, string > copied_m(m);
86 CHECK_EQ(m, copied_m); // This must compile.
91 // Test a hashed simple associative container.
97 ostrstream ss(ss_buf, sizeof(ss_buf));
99 CHECK_STREQ(ss.str(), "10 20 30");
100 hash_set<int> copied_hs(hs);
101 CHECK_EQ(hs, copied_hs); // This must compile.
107 // Test a hashed pair associative container.
108 hash_map<int, string> hm;
113 ostrstream ss(ss_buf, sizeof(ss_buf));
115 CHECK_STREQ(ss.str(), "(10, ten) (20, twenty) (30, thirty)");
116 hash_map<int, string> copied_hm(hm);
117 CHECK_EQ(hm, copied_hm); // this must compile
122 // Test a long sequence.
125 for (int i = 0; i < 100; i++) {
127 if (i > 0) expected += ' ';
129 sprintf(buf, "%d", i);
135 ostrstream ss(ss_buf, sizeof(ss_buf));
137 CHECK_STREQ(ss.str(), expected.c_str());
141 // Test a sorted pair associative container.
142 // Use a non-default comparison functor.
143 map< int, string, greater<int> > m;
148 ostrstream ss(ss_buf, sizeof(ss_buf));
150 CHECK_STREQ(ss.str(), "(30, thirty) (20, twenty) (10, ten)");
151 map< int, string, greater<int> > copied_m(m);
152 CHECK_EQ(m, copied_m); // This must compile.
157 // Test a hashed simple associative container.
158 // Use a user defined hash function.
159 hash_set<int, user_hash> hs;
164 ostrstream ss(ss_buf, sizeof(ss_buf));
166 CHECK_STREQ(ss.str(), "10 20 30");
167 hash_set<int, user_hash> copied_hs(hs);
168 CHECK_EQ(hs, copied_hs); // This must compile.
173 int main(int argc, char** argv) {
175 std::cout << "PASS\n";
183 int main(int argc, char** argv) {
184 std::cout << "We don't support stl_logging for this compiler.\n"
185 << "(we need compiler support of 'using ::operator<<' "
186 << "for this feature.)\n";
190 #endif // HAVE_USING_OPERATOR