sample2_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // A sample program demonstrating using Google C++ testing framework.
  30. // This sample shows how to write a more complex unit test for a class
  31. // that has multiple member functions.
  32. //
  33. // Usually, it's a good idea to have one test for each method in your
  34. // class. You don't have to do that exactly, but it helps to keep
  35. // your tests organized. You may also throw in additional tests as
  36. // needed.
  37. #include "sample2.h"
  38. #include "gtest/gtest.h"
  39. namespace {
  40. // In this example, we test the MyString class (a simple string).
  41. // Tests the default c'tor.
  42. TEST(MyString, DefaultConstructor) {
  43. const MyString s;
  44. // Asserts that s.c_string() returns NULL.
  45. //
  46. // <TechnicalDetails>
  47. //
  48. // If we write NULL instead of
  49. //
  50. // static_cast<const char *>(NULL)
  51. //
  52. // in this assertion, it will generate a warning on gcc 3.4. The
  53. // reason is that EXPECT_EQ needs to know the types of its
  54. // arguments in order to print them when it fails. Since NULL is
  55. // #defined as 0, the compiler will use the formatter function for
  56. // int to print it. However, gcc thinks that NULL should be used as
  57. // a pointer, not an int, and therefore complains.
  58. //
  59. // The root of the problem is C++'s lack of distinction between the
  60. // integer number 0 and the null pointer constant. Unfortunately,
  61. // we have to live with this fact.
  62. //
  63. // </TechnicalDetails>
  64. EXPECT_STREQ(NULL, s.c_string());
  65. EXPECT_EQ(0u, s.Length());
  66. }
  67. const char kHelloString[] = "Hello, world!";
  68. // Tests the c'tor that accepts a C string.
  69. TEST(MyString, ConstructorFromCString) {
  70. const MyString s(kHelloString);
  71. EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
  72. EXPECT_EQ(sizeof(kHelloString)/sizeof(kHelloString[0]) - 1,
  73. s.Length());
  74. }
  75. // Tests the copy c'tor.
  76. TEST(MyString, CopyConstructor) {
  77. const MyString s1(kHelloString);
  78. const MyString s2 = s1;
  79. EXPECT_EQ(0, strcmp(s2.c_string(), kHelloString));
  80. }
  81. // Tests the Set method.
  82. TEST(MyString, Set) {
  83. MyString s;
  84. s.Set(kHelloString);
  85. EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
  86. // Set should work when the input pointer is the same as the one
  87. // already in the MyString object.
  88. s.Set(s.c_string());
  89. EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
  90. // Can we set the MyString to NULL?
  91. s.Set(NULL);
  92. EXPECT_STREQ(NULL, s.c_string());
  93. }
  94. } // namespace