test_lastseen.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 simple unit test for a function,
  31. // using Google C++ testing framework.
  32. //
  33. // Writing a unit test using Google C++ testing framework is easy as 1-2-3:
  34. // Step 1. Include necessary header files such that the stuff your
  35. // test logic needs is declared.
  36. //
  37. // Don't forget gtest.h, which declares the testing framework.
  38. #include "gtest/gtest.h"
  39. #include "lastseen.h"
  40. namespace {
  41. // Step 2. Use the TEST macro to define your tests.
  42. //
  43. // TEST has two parameters: the test case name and the test name.
  44. // After using the macro, you should define your test logic between a
  45. // pair of braces. You can use a bunch of macros to indicate the
  46. // success or failure of a test. EXPECT_TRUE and EXPECT_EQ are
  47. // examples of such macros. For a complete list, see gtest.h.
  48. //
  49. // <TechnicalDetails>
  50. //
  51. // In Google Test, tests are grouped into test cases. This is how we
  52. // keep test code organized. You should put logically related tests
  53. // into the same test case.
  54. //
  55. // The test case name and the test name should both be valid C++
  56. // identifiers. And you should not use underscore (_) in the names.
  57. //
  58. // Google Test guarantees that each test you define is run exactly
  59. // once, but it makes no guarantee on the order the tests are
  60. // executed. Therefore, you should write your tests in such a way
  61. // that their results don't depend on their order.
  62. //
  63. // </TechnicalDetails>
  64. TEST(LastSeenTest, basic) {
  65. LastSeen one(1);
  66. EXPECT_FALSE(one.seen_before(0));
  67. EXPECT_TRUE(one.seen_before(0));
  68. EXPECT_TRUE(one.seen_before(0));
  69. EXPECT_TRUE(one.seen_before(0));
  70. EXPECT_FALSE(one.seen_before(1));
  71. EXPECT_TRUE(one.seen_before(1));
  72. }
  73. TEST(LastSeenTest, Deep2) {
  74. LastSeen two(2);
  75. EXPECT_FALSE(two.seen_before(0));
  76. EXPECT_FALSE(two.seen_before(1));
  77. EXPECT_TRUE(two.seen_before(0));
  78. EXPECT_TRUE(two.seen_before(1));
  79. }
  80. } // namespace
  81. // Step 3. Call RUN_ALL_TESTS() in main().
  82. //
  83. // We do this by linking in src/gtest_main.cc file, which consists of
  84. // a main() function which calls RUN_ALL_TESTS() for us.
  85. //
  86. // This runs all the tests you've defined, prints the result, and
  87. // returns 0 if successful, or 1 otherwise.
  88. //
  89. // Did you notice that we didn't register the tests? The
  90. // RUN_ALL_TESTS() macro magically knows about all the tests we
  91. // defined. Isn't this convenient?