stl_logging_unittest.cc 5.3 KB

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