You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.0 KiB

  1. // Copyright 2008, 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. // Tests the --gtest_repeat=number flag.
  32. #include <stdlib.h>
  33. #include <iostream>
  34. #include "gtest/gtest.h"
  35. // Indicates that this translation unit is part of Google Test's
  36. // implementation. It must come before gtest-internal-inl.h is
  37. // included, or there will be a compiler error. This trick is to
  38. // prevent a user from accidentally including gtest-internal-inl.h in
  39. // his code.
  40. #define GTEST_IMPLEMENTATION_ 1
  41. #include "src/gtest-internal-inl.h"
  42. #undef GTEST_IMPLEMENTATION_
  43. namespace testing {
  44. GTEST_DECLARE_string_(death_test_style);
  45. GTEST_DECLARE_string_(filter);
  46. GTEST_DECLARE_int32_(repeat);
  47. } // namespace testing
  48. using testing::GTEST_FLAG(death_test_style);
  49. using testing::GTEST_FLAG(filter);
  50. using testing::GTEST_FLAG(repeat);
  51. namespace {
  52. // We need this when we are testing Google Test itself and therefore
  53. // cannot use Google Test assertions.
  54. #define GTEST_CHECK_INT_EQ_(expected, actual) \
  55. do {\
  56. const int expected_val = (expected);\
  57. const int actual_val = (actual);\
  58. if (::testing::internal::IsTrue(expected_val != actual_val)) {\
  59. ::std::cout << "Value of: " #actual "\n"\
  60. << " Actual: " << actual_val << "\n"\
  61. << "Expected: " #expected "\n"\
  62. << "Which is: " << expected_val << "\n";\
  63. ::testing::internal::posix::Abort();\
  64. }\
  65. } while(::testing::internal::AlwaysFalse())
  66. // Used for verifying that global environment set-up and tear-down are
  67. // inside the gtest_repeat loop.
  68. int g_environment_set_up_count = 0;
  69. int g_environment_tear_down_count = 0;
  70. class MyEnvironment : public testing::Environment {
  71. public:
  72. MyEnvironment() {}
  73. virtual void SetUp() { g_environment_set_up_count++; }
  74. virtual void TearDown() { g_environment_tear_down_count++; }
  75. };
  76. // A test that should fail.
  77. int g_should_fail_count = 0;
  78. TEST(FooTest, ShouldFail) {
  79. g_should_fail_count++;
  80. EXPECT_EQ(0, 1) << "Expected failure.";
  81. }
  82. // A test that should pass.
  83. int g_should_pass_count = 0;
  84. TEST(FooTest, ShouldPass) {
  85. g_should_pass_count++;
  86. }
  87. // A test that contains a thread-safe death test and a fast death
  88. // test. It should pass.
  89. int g_death_test_count = 0;
  90. TEST(BarDeathTest, ThreadSafeAndFast) {
  91. g_death_test_count++;
  92. GTEST_FLAG(death_test_style) = "threadsafe";
  93. EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
  94. GTEST_FLAG(death_test_style) = "fast";
  95. EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
  96. }
  97. #if GTEST_HAS_PARAM_TEST
  98. int g_param_test_count = 0;
  99. const int kNumberOfParamTests = 10;
  100. class MyParamTest : public testing::TestWithParam<int> {};
  101. TEST_P(MyParamTest, ShouldPass) {
  102. // TODO(vladl@google.com): Make parameter value checking robust
  103. // WRT order of tests.
  104. GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
  105. g_param_test_count++;
  106. }
  107. INSTANTIATE_TEST_CASE_P(MyParamSequence,
  108. MyParamTest,
  109. testing::Range(0, kNumberOfParamTests));
  110. #endif // GTEST_HAS_PARAM_TEST
  111. // Resets the count for each test.
  112. void ResetCounts() {
  113. g_environment_set_up_count = 0;
  114. g_environment_tear_down_count = 0;
  115. g_should_fail_count = 0;
  116. g_should_pass_count = 0;
  117. g_death_test_count = 0;
  118. #if GTEST_HAS_PARAM_TEST
  119. g_param_test_count = 0;
  120. #endif // GTEST_HAS_PARAM_TEST
  121. }
  122. // Checks that the count for each test is expected.
  123. void CheckCounts(int expected) {
  124. GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
  125. GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
  126. GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
  127. GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
  128. GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
  129. #if GTEST_HAS_PARAM_TEST
  130. GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
  131. #endif // GTEST_HAS_PARAM_TEST
  132. }
  133. // Tests the behavior of Google Test when --gtest_repeat is not specified.
  134. void TestRepeatUnspecified() {
  135. ResetCounts();
  136. GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  137. CheckCounts(1);
  138. }
  139. // Tests the behavior of Google Test when --gtest_repeat has the given value.
  140. void TestRepeat(int repeat) {
  141. GTEST_FLAG(repeat) = repeat;
  142. ResetCounts();
  143. GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
  144. CheckCounts(repeat);
  145. }
  146. // Tests using --gtest_repeat when --gtest_filter specifies an empty
  147. // set of tests.
  148. void TestRepeatWithEmptyFilter(int repeat) {
  149. GTEST_FLAG(repeat) = repeat;
  150. GTEST_FLAG(filter) = "None";
  151. ResetCounts();
  152. GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  153. CheckCounts(0);
  154. }
  155. // Tests using --gtest_repeat when --gtest_filter specifies a set of
  156. // successful tests.
  157. void TestRepeatWithFilterForSuccessfulTests(int repeat) {
  158. GTEST_FLAG(repeat) = repeat;
  159. GTEST_FLAG(filter) = "*-*ShouldFail";
  160. ResetCounts();
  161. GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
  162. GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  163. GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  164. GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
  165. GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
  166. GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
  167. #if GTEST_HAS_PARAM_TEST
  168. GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
  169. #endif // GTEST_HAS_PARAM_TEST
  170. }
  171. // Tests using --gtest_repeat when --gtest_filter specifies a set of
  172. // failed tests.
  173. void TestRepeatWithFilterForFailedTests(int repeat) {
  174. GTEST_FLAG(repeat) = repeat;
  175. GTEST_FLAG(filter) = "*ShouldFail";
  176. ResetCounts();
  177. GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
  178. GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
  179. GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
  180. GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
  181. GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
  182. GTEST_CHECK_INT_EQ_(0, g_death_test_count);
  183. #if GTEST_HAS_PARAM_TEST
  184. GTEST_CHECK_INT_EQ_(0, g_param_test_count);
  185. #endif // GTEST_HAS_PARAM_TEST
  186. }
  187. } // namespace
  188. int main(int argc, char **argv) {
  189. testing::InitGoogleTest(&argc, argv);
  190. testing::AddGlobalTestEnvironment(new MyEnvironment);
  191. TestRepeatUnspecified();
  192. TestRepeat(0);
  193. TestRepeat(1);
  194. TestRepeat(5);
  195. TestRepeatWithEmptyFilter(2);
  196. TestRepeatWithEmptyFilter(3);
  197. TestRepeatWithFilterForSuccessfulTests(3);
  198. TestRepeatWithFilterForFailedTests(4);
  199. // It would be nice to verify that the tests indeed loop forever
  200. // when GTEST_FLAG(repeat) is negative, but this test will be quite
  201. // complicated to write. Since this flag is for interactive
  202. // debugging only and doesn't affect the normal test result, such a
  203. // test would be an overkill.
  204. printf("PASS\n");
  205. return 0;
  206. }