1f92157deSopenharmony_ci// Copyright 2008, Google Inc. 2f92157deSopenharmony_ci// All rights reserved. 3f92157deSopenharmony_ci// 4f92157deSopenharmony_ci// Redistribution and use in source and binary forms, with or without 5f92157deSopenharmony_ci// modification, are permitted provided that the following conditions are 6f92157deSopenharmony_ci// met: 7f92157deSopenharmony_ci// 8f92157deSopenharmony_ci// * Redistributions of source code must retain the above copyright 9f92157deSopenharmony_ci// notice, this list of conditions and the following disclaimer. 10f92157deSopenharmony_ci// * Redistributions in binary form must reproduce the above 11f92157deSopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 12f92157deSopenharmony_ci// in the documentation and/or other materials provided with the 13f92157deSopenharmony_ci// distribution. 14f92157deSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 15f92157deSopenharmony_ci// contributors may be used to endorse or promote products derived from 16f92157deSopenharmony_ci// this software without specific prior written permission. 17f92157deSopenharmony_ci// 18f92157deSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19f92157deSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20f92157deSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21f92157deSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22f92157deSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23f92157deSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24f92157deSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f92157deSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f92157deSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f92157deSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28f92157deSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f92157deSopenharmony_ci 30f92157deSopenharmony_ci// Tests the --gtest_repeat=number flag. 31f92157deSopenharmony_ci 32f92157deSopenharmony_ci#include <stdlib.h> 33f92157deSopenharmony_ci 34f92157deSopenharmony_ci#include <iostream> 35f92157deSopenharmony_ci 36f92157deSopenharmony_ci#include "gtest/gtest.h" 37f92157deSopenharmony_ci#include "src/gtest-internal-inl.h" 38f92157deSopenharmony_ci 39f92157deSopenharmony_cinamespace { 40f92157deSopenharmony_ci 41f92157deSopenharmony_ci// We need this when we are testing Google Test itself and therefore 42f92157deSopenharmony_ci// cannot use Google Test assertions. 43f92157deSopenharmony_ci#define GTEST_CHECK_INT_EQ_(expected, actual) \ 44f92157deSopenharmony_ci do { \ 45f92157deSopenharmony_ci const int expected_val = (expected); \ 46f92157deSopenharmony_ci const int actual_val = (actual); \ 47f92157deSopenharmony_ci if (::testing::internal::IsTrue(expected_val != actual_val)) { \ 48f92157deSopenharmony_ci ::std::cout << "Value of: " #actual "\n" \ 49f92157deSopenharmony_ci << " Actual: " << actual_val << "\n" \ 50f92157deSopenharmony_ci << "Expected: " #expected "\n" \ 51f92157deSopenharmony_ci << "Which is: " << expected_val << "\n"; \ 52f92157deSopenharmony_ci ::testing::internal::posix::Abort(); \ 53f92157deSopenharmony_ci } \ 54f92157deSopenharmony_ci } while (::testing::internal::AlwaysFalse()) 55f92157deSopenharmony_ci 56f92157deSopenharmony_ci// Used for verifying that global environment set-up and tear-down are 57f92157deSopenharmony_ci// inside the --gtest_repeat loop. 58f92157deSopenharmony_ci 59f92157deSopenharmony_ciint g_environment_set_up_count = 0; 60f92157deSopenharmony_ciint g_environment_tear_down_count = 0; 61f92157deSopenharmony_ci 62f92157deSopenharmony_ciclass MyEnvironment : public testing::Environment { 63f92157deSopenharmony_ci public: 64f92157deSopenharmony_ci MyEnvironment() {} 65f92157deSopenharmony_ci void SetUp() override { g_environment_set_up_count++; } 66f92157deSopenharmony_ci void TearDown() override { g_environment_tear_down_count++; } 67f92157deSopenharmony_ci}; 68f92157deSopenharmony_ci 69f92157deSopenharmony_ci// A test that should fail. 70f92157deSopenharmony_ci 71f92157deSopenharmony_ciint g_should_fail_count = 0; 72f92157deSopenharmony_ci 73f92157deSopenharmony_ciTEST(FooTest, ShouldFail) { 74f92157deSopenharmony_ci g_should_fail_count++; 75f92157deSopenharmony_ci EXPECT_EQ(0, 1) << "Expected failure."; 76f92157deSopenharmony_ci} 77f92157deSopenharmony_ci 78f92157deSopenharmony_ci// A test that should pass. 79f92157deSopenharmony_ci 80f92157deSopenharmony_ciint g_should_pass_count = 0; 81f92157deSopenharmony_ci 82f92157deSopenharmony_ciTEST(FooTest, ShouldPass) { g_should_pass_count++; } 83f92157deSopenharmony_ci 84f92157deSopenharmony_ci// A test that contains a thread-safe death test and a fast death 85f92157deSopenharmony_ci// test. It should pass. 86f92157deSopenharmony_ci 87f92157deSopenharmony_ciint g_death_test_count = 0; 88f92157deSopenharmony_ci 89f92157deSopenharmony_ciTEST(BarDeathTest, ThreadSafeAndFast) { 90f92157deSopenharmony_ci g_death_test_count++; 91f92157deSopenharmony_ci 92f92157deSopenharmony_ci GTEST_FLAG_SET(death_test_style, "threadsafe"); 93f92157deSopenharmony_ci EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), ""); 94f92157deSopenharmony_ci 95f92157deSopenharmony_ci GTEST_FLAG_SET(death_test_style, "fast"); 96f92157deSopenharmony_ci EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), ""); 97f92157deSopenharmony_ci} 98f92157deSopenharmony_ci 99f92157deSopenharmony_ciint g_param_test_count = 0; 100f92157deSopenharmony_ci 101f92157deSopenharmony_ciconst int kNumberOfParamTests = 10; 102f92157deSopenharmony_ci 103f92157deSopenharmony_ciclass MyParamTest : public testing::TestWithParam<int> {}; 104f92157deSopenharmony_ci 105f92157deSopenharmony_ciTEST_P(MyParamTest, ShouldPass) { 106f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam()); 107f92157deSopenharmony_ci g_param_test_count++; 108f92157deSopenharmony_ci} 109f92157deSopenharmony_ciINSTANTIATE_TEST_SUITE_P(MyParamSequence, MyParamTest, 110f92157deSopenharmony_ci testing::Range(0, kNumberOfParamTests)); 111f92157deSopenharmony_ci 112f92157deSopenharmony_ci// Resets the count for each test. 113f92157deSopenharmony_civoid ResetCounts() { 114f92157deSopenharmony_ci g_environment_set_up_count = 0; 115f92157deSopenharmony_ci g_environment_tear_down_count = 0; 116f92157deSopenharmony_ci g_should_fail_count = 0; 117f92157deSopenharmony_ci g_should_pass_count = 0; 118f92157deSopenharmony_ci g_death_test_count = 0; 119f92157deSopenharmony_ci g_param_test_count = 0; 120f92157deSopenharmony_ci} 121f92157deSopenharmony_ci 122f92157deSopenharmony_ci// Checks that the count for each test is expected. 123f92157deSopenharmony_civoid CheckCounts(int expected) { 124f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count); 125f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count); 126f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(expected, g_should_fail_count); 127f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(expected, g_should_pass_count); 128f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(expected, g_death_test_count); 129f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count); 130f92157deSopenharmony_ci} 131f92157deSopenharmony_ci 132f92157deSopenharmony_ci// Tests the behavior of Google Test when --gtest_repeat is not specified. 133f92157deSopenharmony_civoid TestRepeatUnspecified() { 134f92157deSopenharmony_ci ResetCounts(); 135f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS()); 136f92157deSopenharmony_ci CheckCounts(1); 137f92157deSopenharmony_ci} 138f92157deSopenharmony_ci 139f92157deSopenharmony_ci// Tests the behavior of Google Test when --gtest_repeat has the given value. 140f92157deSopenharmony_civoid TestRepeat(int repeat) { 141f92157deSopenharmony_ci GTEST_FLAG_SET(repeat, repeat); 142f92157deSopenharmony_ci GTEST_FLAG_SET(recreate_environments_when_repeating, true); 143f92157deSopenharmony_ci 144f92157deSopenharmony_ci ResetCounts(); 145f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS()); 146f92157deSopenharmony_ci CheckCounts(repeat); 147f92157deSopenharmony_ci} 148f92157deSopenharmony_ci 149f92157deSopenharmony_ci// Tests using --gtest_repeat when --gtest_filter specifies an empty 150f92157deSopenharmony_ci// set of tests. 151f92157deSopenharmony_civoid TestRepeatWithEmptyFilter(int repeat) { 152f92157deSopenharmony_ci GTEST_FLAG_SET(repeat, repeat); 153f92157deSopenharmony_ci GTEST_FLAG_SET(recreate_environments_when_repeating, true); 154f92157deSopenharmony_ci GTEST_FLAG_SET(filter, "None"); 155f92157deSopenharmony_ci 156f92157deSopenharmony_ci ResetCounts(); 157f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS()); 158f92157deSopenharmony_ci CheckCounts(0); 159f92157deSopenharmony_ci} 160f92157deSopenharmony_ci 161f92157deSopenharmony_ci// Tests using --gtest_repeat when --gtest_filter specifies a set of 162f92157deSopenharmony_ci// successful tests. 163f92157deSopenharmony_civoid TestRepeatWithFilterForSuccessfulTests(int repeat) { 164f92157deSopenharmony_ci GTEST_FLAG_SET(repeat, repeat); 165f92157deSopenharmony_ci GTEST_FLAG_SET(recreate_environments_when_repeating, true); 166f92157deSopenharmony_ci GTEST_FLAG_SET(filter, "*-*ShouldFail"); 167f92157deSopenharmony_ci 168f92157deSopenharmony_ci ResetCounts(); 169f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS()); 170f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count); 171f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count); 172f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(0, g_should_fail_count); 173f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count); 174f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_death_test_count); 175f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count); 176f92157deSopenharmony_ci} 177f92157deSopenharmony_ci 178f92157deSopenharmony_ci// Tests using --gtest_repeat when --gtest_filter specifies a set of 179f92157deSopenharmony_ci// failed tests. 180f92157deSopenharmony_civoid TestRepeatWithFilterForFailedTests(int repeat) { 181f92157deSopenharmony_ci GTEST_FLAG_SET(repeat, repeat); 182f92157deSopenharmony_ci GTEST_FLAG_SET(recreate_environments_when_repeating, true); 183f92157deSopenharmony_ci GTEST_FLAG_SET(filter, "*ShouldFail"); 184f92157deSopenharmony_ci 185f92157deSopenharmony_ci ResetCounts(); 186f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS()); 187f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count); 188f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count); 189f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count); 190f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(0, g_should_pass_count); 191f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(0, g_death_test_count); 192f92157deSopenharmony_ci GTEST_CHECK_INT_EQ_(0, g_param_test_count); 193f92157deSopenharmony_ci} 194f92157deSopenharmony_ci 195f92157deSopenharmony_ci} // namespace 196f92157deSopenharmony_ci 197f92157deSopenharmony_ciint main(int argc, char **argv) { 198f92157deSopenharmony_ci testing::InitGoogleTest(&argc, argv); 199f92157deSopenharmony_ci 200f92157deSopenharmony_ci testing::AddGlobalTestEnvironment(new MyEnvironment); 201f92157deSopenharmony_ci 202f92157deSopenharmony_ci TestRepeatUnspecified(); 203f92157deSopenharmony_ci TestRepeat(0); 204f92157deSopenharmony_ci TestRepeat(1); 205f92157deSopenharmony_ci TestRepeat(5); 206f92157deSopenharmony_ci 207f92157deSopenharmony_ci TestRepeatWithEmptyFilter(2); 208f92157deSopenharmony_ci TestRepeatWithEmptyFilter(3); 209f92157deSopenharmony_ci 210f92157deSopenharmony_ci TestRepeatWithFilterForSuccessfulTests(3); 211f92157deSopenharmony_ci 212f92157deSopenharmony_ci TestRepeatWithFilterForFailedTests(4); 213f92157deSopenharmony_ci 214f92157deSopenharmony_ci // It would be nice to verify that the tests indeed loop forever 215f92157deSopenharmony_ci // when GTEST_FLAG(repeat) is negative, but this test will be quite 216f92157deSopenharmony_ci // complicated to write. Since this flag is for interactive 217f92157deSopenharmony_ci // debugging only and doesn't affect the normal test result, such a 218f92157deSopenharmony_ci // test would be an overkill. 219f92157deSopenharmony_ci 220f92157deSopenharmony_ci printf("PASS\n"); 221f92157deSopenharmony_ci return 0; 222f92157deSopenharmony_ci} 223