/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/test/gtest-message_test.cc

http://github.com/tomahawk-player/tomahawk · C++ · 167 lines · 89 code · 24 blank · 54 comment · 0 complexity · b8b72f22a0e3ecef6dfe2c6d09c2fc6a MD5 · raw file

  1. // Copyright 2005, 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. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // Tests for the Message class.
  33. #include <gtest/gtest-message.h>
  34. #include <gtest/gtest.h>
  35. namespace {
  36. using ::testing::Message;
  37. using ::testing::internal::StrStream;
  38. // A helper function that turns a Message into a C string.
  39. const char* ToCString(const Message& msg) {
  40. static testing::internal::String result;
  41. result = msg.GetString();
  42. return result.c_str();
  43. }
  44. // Tests the testing::Message class
  45. // Tests the default constructor.
  46. TEST(MessageTest, DefaultConstructor) {
  47. const Message msg;
  48. EXPECT_STREQ("", ToCString(msg));
  49. }
  50. // Tests the copy constructor.
  51. TEST(MessageTest, CopyConstructor) {
  52. const Message msg1("Hello");
  53. const Message msg2(msg1);
  54. EXPECT_STREQ("Hello", ToCString(msg2));
  55. }
  56. // Tests constructing a Message from a C-string.
  57. TEST(MessageTest, ConstructsFromCString) {
  58. Message msg("Hello");
  59. EXPECT_STREQ("Hello", ToCString(msg));
  60. }
  61. // Tests streaming a float.
  62. TEST(MessageTest, StreamsFloat) {
  63. const char* const s = ToCString(Message() << 1.23456F << " " << 2.34567F);
  64. // Both numbers should be printed with enough precision.
  65. EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s);
  66. EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s);
  67. }
  68. // Tests streaming a double.
  69. TEST(MessageTest, StreamsDouble) {
  70. const char* const s = ToCString(Message() << 1260570880.4555497 << " "
  71. << 1260572265.1954534);
  72. // Both numbers should be printed with enough precision.
  73. EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s);
  74. EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s);
  75. }
  76. // Tests streaming a non-char pointer.
  77. TEST(MessageTest, StreamsPointer) {
  78. int n = 0;
  79. int* p = &n;
  80. EXPECT_STRNE("(null)", ToCString(Message() << p));
  81. }
  82. // Tests streaming a NULL non-char pointer.
  83. TEST(MessageTest, StreamsNullPointer) {
  84. int* p = NULL;
  85. EXPECT_STREQ("(null)", ToCString(Message() << p));
  86. }
  87. // Tests streaming a C string.
  88. TEST(MessageTest, StreamsCString) {
  89. EXPECT_STREQ("Foo", ToCString(Message() << "Foo"));
  90. }
  91. // Tests streaming a NULL C string.
  92. TEST(MessageTest, StreamsNullCString) {
  93. char* p = NULL;
  94. EXPECT_STREQ("(null)", ToCString(Message() << p));
  95. }
  96. // Tests streaming std::string.
  97. TEST(MessageTest, StreamsString) {
  98. const ::std::string str("Hello");
  99. EXPECT_STREQ("Hello", ToCString(Message() << str));
  100. }
  101. // Tests that we can output strings containing embedded NULs.
  102. TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
  103. const char char_array_with_nul[] =
  104. "Here's a NUL\0 and some more string";
  105. const ::std::string string_with_nul(char_array_with_nul,
  106. sizeof(char_array_with_nul) - 1);
  107. EXPECT_STREQ("Here's a NUL\\0 and some more string",
  108. ToCString(Message() << string_with_nul));
  109. }
  110. // Tests streaming a NUL char.
  111. TEST(MessageTest, StreamsNULChar) {
  112. EXPECT_STREQ("\\0", ToCString(Message() << '\0'));
  113. }
  114. // Tests streaming int.
  115. TEST(MessageTest, StreamsInt) {
  116. EXPECT_STREQ("123", ToCString(Message() << 123));
  117. }
  118. // Tests that basic IO manipulators (endl, ends, and flush) can be
  119. // streamed to Message.
  120. TEST(MessageTest, StreamsBasicIoManip) {
  121. EXPECT_STREQ("Line 1.\nA NUL char \\0 in line 2.",
  122. ToCString(Message() << "Line 1." << std::endl
  123. << "A NUL char " << std::ends << std::flush
  124. << " in line 2."));
  125. }
  126. // Tests Message::GetString()
  127. TEST(MessageTest, GetString) {
  128. Message msg;
  129. msg << 1 << " lamb";
  130. EXPECT_STREQ("1 lamb", msg.GetString().c_str());
  131. }
  132. // Tests streaming a Message object to an ostream.
  133. TEST(MessageTest, StreamsToOStream) {
  134. Message msg("Hello");
  135. StrStream ss;
  136. ss << msg;
  137. EXPECT_STREQ("Hello", testing::internal::StrStreamToString(&ss).c_str());
  138. }
  139. // Tests that a Message object doesn't take up too much stack space.
  140. TEST(MessageTest, DoesNotTakeUpMuchStackSpace) {
  141. EXPECT_LE(sizeof(Message), 16U);
  142. }
  143. } // namespace