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.
30 #include "utilities.h"
33 #ifdef HAVE_USING_OPERATOR
35 #include "glog/stl_logging.h"
44 # include <ext/hash_map>
45 # include <ext/hash_set>
48 #include "glog/logging.h"
49 #include "googletest.h"
53 using namespace __gnu_cxx;
57 size_t operator()(int x) const { return x; }
60 void TestSTLLogging() {
68 ostrstream ss(ss_buf, sizeof(ss_buf));
69 // Just ostrstream s1; leaks heap.
71 CHECK_STREQ(ss.str(), "10 20 30");
72 vector<int> copied_v(v);
73 CHECK_EQ(v, copied_v); // This must compile.
77 // Test a sorted pair associative container.
83 ostrstream ss(ss_buf, sizeof(ss_buf));
85 CHECK_STREQ(ss.str(), "(10, ten) (20, twenty) (30, thirty)");
86 map< int, string > copied_m(m);
87 CHECK_EQ(m, copied_m); // This must compile.
92 // Test a hashed simple associative container.
98 ostrstream ss(ss_buf, sizeof(ss_buf));
100 CHECK_STREQ(ss.str(), "10 20 30");
101 hash_set<int> copied_hs(hs);
102 CHECK_EQ(hs, copied_hs); // This must compile.
108 // Test a hashed pair associative container.
109 hash_map<int, string> hm;
114 ostrstream ss(ss_buf, sizeof(ss_buf));
116 CHECK_STREQ(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
123 // Test a long sequence.
126 for (int i = 0; i < 100; i++) {
128 if (i > 0) expected += ' ';
130 sprintf(buf, "%d", i);
136 ostrstream ss(ss_buf, sizeof(ss_buf));
138 CHECK_STREQ(ss.str(), expected.c_str());
142 // Test a sorted pair associative container.
143 // Use a non-default comparison functor.
144 map< int, string, greater<int> > m;
149 ostrstream ss(ss_buf, sizeof(ss_buf));
151 CHECK_STREQ(ss.str(), "(30, thirty) (20, twenty) (10, ten)");
152 map< int, string, greater<int> > copied_m(m);
153 CHECK_EQ(m, copied_m); // This must compile.
158 // Test a hashed simple associative container.
159 // Use a user defined hash function.
160 hash_set<int, user_hash> hs;
165 ostrstream ss(ss_buf, sizeof(ss_buf));
167 CHECK_STREQ(ss.str(), "10 20 30");
168 hash_set<int, user_hash> copied_hs(hs);
169 CHECK_EQ(hs, copied_hs); // This must compile.
174 int main(int argc, char** argv) {
176 std::cout << "PASS\n";
184 int main(int argc, char** argv) {
185 std::cout << "We don't support stl_logging for this compiler.\n"
186 << "(we need compiler support of 'using ::operator<<' "
187 << "for this feature.)\n";
191 #endif // HAVE_USING_OPERATOR