Add OS_NETBSD macro.
[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 "utilities.h"
31 #include "config.h"
32
33 #ifdef HAVE_USING_OPERATOR
34
35 #include "glog/stl_logging.h"
36
37 #include <iostream>
38 #include <map>
39 #include <string>
40 #include <strstream>
41 #include <vector>
42
43 #ifdef __GNUC__
44 # include <ext/hash_map>
45 # include <ext/hash_set>
46 #endif
47
48 #include "glog/logging.h"
49 #include "googletest.h"
50
51 using namespace std;
52 #ifdef __GNUC__
53 using namespace __gnu_cxx;
54 #endif
55
56 struct user_hash {
57   size_t operator()(int x) const { return x; }
58 };
59
60 void TestSTLLogging() {
61   {
62     // Test a sequence.
63     vector<int> v;
64     v.push_back(10);
65     v.push_back(20);
66     v.push_back(30);
67     char ss_buf[1000];
68     ostrstream ss(ss_buf, sizeof(ss_buf));
69     // Just ostrstream s1; leaks heap.
70     ss << v << ends;
71     CHECK_STREQ(ss.str(), "10 20 30");
72     vector<int> copied_v(v);
73     CHECK_EQ(v, copied_v);  // This must compile.
74   }
75
76   {
77     // Test a sorted pair associative container.
78     map< int, string > m;
79     m[20] = "twenty";
80     m[10] = "ten";
81     m[30] = "thirty";
82     char ss_buf[1000];
83     ostrstream ss(ss_buf, sizeof(ss_buf));
84     ss << m << ends;
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.
88   }
89
90 #ifdef __GNUC__
91   {
92     // Test a hashed simple associative container.
93     hash_set<int> hs;
94     hs.insert(10);
95     hs.insert(20);
96     hs.insert(30);
97     char ss_buf[1000];
98     ostrstream ss(ss_buf, sizeof(ss_buf));
99     ss << hs << ends;
100     CHECK_STREQ(ss.str(), "10 20 30");
101     hash_set<int> copied_hs(hs);
102     CHECK_EQ(hs, copied_hs);  // This must compile.
103   }
104 #endif
105
106 #ifdef __GNUC__
107   {
108     // Test a hashed pair associative container.
109     hash_map<int, string> hm;
110     hm[10] = "ten";
111     hm[20] = "twenty";
112     hm[30] = "thirty";
113     char ss_buf[1000];
114     ostrstream ss(ss_buf, sizeof(ss_buf));
115     ss << hm << ends;
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
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     char ss_buf[1000];
136     ostrstream ss(ss_buf, sizeof(ss_buf));
137     ss << v << ends;
138     CHECK_STREQ(ss.str(), expected.c_str());
139   }
140
141   {
142     // Test a sorted pair associative container.
143     // Use a non-default comparison functor.
144     map< int, string, greater<int> > m;
145     m[20] = "twenty";
146     m[10] = "ten";
147     m[30] = "thirty";
148     char ss_buf[1000];
149     ostrstream ss(ss_buf, sizeof(ss_buf));
150     ss << m << ends;
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.
154   }
155
156 #ifdef __GNUC__
157   {
158     // Test a hashed simple associative container.
159     // Use a user defined hash function.
160     hash_set<int, user_hash> hs;
161     hs.insert(10);
162     hs.insert(20);
163     hs.insert(30);
164     char ss_buf[1000];
165     ostrstream ss(ss_buf, sizeof(ss_buf));
166     ss << hs << ends;
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.
170   }
171 #endif
172 }
173
174 int main(int argc, char** argv) {
175   TestSTLLogging();
176   std::cout << "PASS\n";
177   return 0;
178 }
179
180 #else
181
182 #include <iostream>
183
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";
188   return 0;
189 }
190
191 #endif  // HAVE_USING_OPERATOR