Blame nss/external_tests/google_test/gtest/test/gtest_repeat_test.cc

Packit 40b132
// Copyright 2008, Google Inc.
Packit 40b132
// All rights reserved.
Packit 40b132
//
Packit 40b132
// Redistribution and use in source and binary forms, with or without
Packit 40b132
// modification, are permitted provided that the following conditions are
Packit 40b132
// met:
Packit 40b132
//
Packit 40b132
//     * Redistributions of source code must retain the above copyright
Packit 40b132
// notice, this list of conditions and the following disclaimer.
Packit 40b132
//     * Redistributions in binary form must reproduce the above
Packit 40b132
// copyright notice, this list of conditions and the following disclaimer
Packit 40b132
// in the documentation and/or other materials provided with the
Packit 40b132
// distribution.
Packit 40b132
//     * Neither the name of Google Inc. nor the names of its
Packit 40b132
// contributors may be used to endorse or promote products derived from
Packit 40b132
// this software without specific prior written permission.
Packit 40b132
//
Packit 40b132
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 40b132
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 40b132
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 40b132
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 40b132
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 40b132
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 40b132
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 40b132
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 40b132
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 40b132
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 40b132
//
Packit 40b132
// Author: wan@google.com (Zhanyong Wan)
Packit 40b132
Packit 40b132
// Tests the --gtest_repeat=number flag.
Packit 40b132
Packit 40b132
#include <stdlib.h>
Packit 40b132
#include <iostream>
Packit 40b132
#include "gtest/gtest.h"
Packit 40b132
Packit 40b132
// Indicates that this translation unit is part of Google Test's
Packit 40b132
// implementation.  It must come before gtest-internal-inl.h is
Packit 40b132
// included, or there will be a compiler error.  This trick is to
Packit 40b132
// prevent a user from accidentally including gtest-internal-inl.h in
Packit 40b132
// his code.
Packit 40b132
#define GTEST_IMPLEMENTATION_ 1
Packit 40b132
#include "src/gtest-internal-inl.h"
Packit 40b132
#undef GTEST_IMPLEMENTATION_
Packit 40b132
Packit 40b132
namespace testing {
Packit 40b132
Packit 40b132
GTEST_DECLARE_string_(death_test_style);
Packit 40b132
GTEST_DECLARE_string_(filter);
Packit 40b132
GTEST_DECLARE_int32_(repeat);
Packit 40b132
Packit 40b132
}  // namespace testing
Packit 40b132
Packit 40b132
using testing::GTEST_FLAG(death_test_style);
Packit 40b132
using testing::GTEST_FLAG(filter);
Packit 40b132
using testing::GTEST_FLAG(repeat);
Packit 40b132
Packit 40b132
namespace {
Packit 40b132
Packit 40b132
// We need this when we are testing Google Test itself and therefore
Packit 40b132
// cannot use Google Test assertions.
Packit 40b132
#define GTEST_CHECK_INT_EQ_(expected, actual) \
Packit 40b132
  do {\
Packit 40b132
    const int expected_val = (expected);\
Packit 40b132
    const int actual_val = (actual);\
Packit 40b132
    if (::testing::internal::IsTrue(expected_val != actual_val)) {\
Packit 40b132
      ::std::cout << "Value of: " #actual "\n"\
Packit 40b132
                  << "  Actual: " << actual_val << "\n"\
Packit 40b132
                  << "Expected: " #expected "\n"\
Packit 40b132
                  << "Which is: " << expected_val << "\n";\
Packit 40b132
      ::testing::internal::posix::Abort();\
Packit 40b132
    }\
Packit 40b132
  } while (::testing::internal::AlwaysFalse())
Packit 40b132
Packit 40b132
Packit 40b132
// Used for verifying that global environment set-up and tear-down are
Packit 40b132
// inside the gtest_repeat loop.
Packit 40b132
Packit 40b132
int g_environment_set_up_count = 0;
Packit 40b132
int g_environment_tear_down_count = 0;
Packit 40b132
Packit 40b132
class MyEnvironment : public testing::Environment {
Packit 40b132
 public:
Packit 40b132
  MyEnvironment() {}
Packit 40b132
  virtual void SetUp() { g_environment_set_up_count++; }
Packit 40b132
  virtual void TearDown() { g_environment_tear_down_count++; }
Packit 40b132
};
Packit 40b132
Packit 40b132
// A test that should fail.
Packit 40b132
Packit 40b132
int g_should_fail_count = 0;
Packit 40b132
Packit 40b132
TEST(FooTest, ShouldFail) {
Packit 40b132
  g_should_fail_count++;
Packit 40b132
  EXPECT_EQ(0, 1) << "Expected failure.";
Packit 40b132
}
Packit 40b132
Packit 40b132
// A test that should pass.
Packit 40b132
Packit 40b132
int g_should_pass_count = 0;
Packit 40b132
Packit 40b132
TEST(FooTest, ShouldPass) {
Packit 40b132
  g_should_pass_count++;
Packit 40b132
}
Packit 40b132
Packit 40b132
// A test that contains a thread-safe death test and a fast death
Packit 40b132
// test.  It should pass.
Packit 40b132
Packit 40b132
int g_death_test_count = 0;
Packit 40b132
Packit 40b132
TEST(BarDeathTest, ThreadSafeAndFast) {
Packit 40b132
  g_death_test_count++;
Packit 40b132
Packit 40b132
  GTEST_FLAG(death_test_style) = "threadsafe";
Packit 40b132
  EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
Packit 40b132
Packit 40b132
  GTEST_FLAG(death_test_style) = "fast";
Packit 40b132
  EXPECT_DEATH_IF_SUPPORTED(::testing::internal::posix::Abort(), "");
Packit 40b132
}
Packit 40b132
Packit 40b132
#if GTEST_HAS_PARAM_TEST
Packit 40b132
int g_param_test_count = 0;
Packit 40b132
Packit 40b132
const int kNumberOfParamTests = 10;
Packit 40b132
Packit 40b132
class MyParamTest : public testing::TestWithParam<int> {};
Packit 40b132
Packit 40b132
TEST_P(MyParamTest, ShouldPass) {
Packit 40b132
  // TODO(vladl@google.com): Make parameter value checking robust
Packit 40b132
  //                         WRT order of tests.
Packit 40b132
  GTEST_CHECK_INT_EQ_(g_param_test_count % kNumberOfParamTests, GetParam());
Packit 40b132
  g_param_test_count++;
Packit 40b132
}
Packit 40b132
INSTANTIATE_TEST_CASE_P(MyParamSequence,
Packit 40b132
                        MyParamTest,
Packit 40b132
                        testing::Range(0, kNumberOfParamTests));
Packit 40b132
#endif  // GTEST_HAS_PARAM_TEST
Packit 40b132
Packit 40b132
// Resets the count for each test.
Packit 40b132
void ResetCounts() {
Packit 40b132
  g_environment_set_up_count = 0;
Packit 40b132
  g_environment_tear_down_count = 0;
Packit 40b132
  g_should_fail_count = 0;
Packit 40b132
  g_should_pass_count = 0;
Packit 40b132
  g_death_test_count = 0;
Packit 40b132
#if GTEST_HAS_PARAM_TEST
Packit 40b132
  g_param_test_count = 0;
Packit 40b132
#endif  // GTEST_HAS_PARAM_TEST
Packit 40b132
}
Packit 40b132
Packit 40b132
// Checks that the count for each test is expected.
Packit 40b132
void CheckCounts(int expected) {
Packit 40b132
  GTEST_CHECK_INT_EQ_(expected, g_environment_set_up_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(expected, g_environment_tear_down_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(expected, g_should_fail_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(expected, g_should_pass_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(expected, g_death_test_count);
Packit 40b132
#if GTEST_HAS_PARAM_TEST
Packit 40b132
  GTEST_CHECK_INT_EQ_(expected * kNumberOfParamTests, g_param_test_count);
Packit 40b132
#endif  // GTEST_HAS_PARAM_TEST
Packit 40b132
}
Packit 40b132
Packit 40b132
// Tests the behavior of Google Test when --gtest_repeat is not specified.
Packit 40b132
void TestRepeatUnspecified() {
Packit 40b132
  ResetCounts();
Packit 40b132
  GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
Packit 40b132
  CheckCounts(1);
Packit 40b132
}
Packit 40b132
Packit 40b132
// Tests the behavior of Google Test when --gtest_repeat has the given value.
Packit 40b132
void TestRepeat(int repeat) {
Packit 40b132
  GTEST_FLAG(repeat) = repeat;
Packit 40b132
Packit 40b132
  ResetCounts();
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat > 0 ? 1 : 0, RUN_ALL_TESTS());
Packit 40b132
  CheckCounts(repeat);
Packit 40b132
}
Packit 40b132
Packit 40b132
// Tests using --gtest_repeat when --gtest_filter specifies an empty
Packit 40b132
// set of tests.
Packit 40b132
void TestRepeatWithEmptyFilter(int repeat) {
Packit 40b132
  GTEST_FLAG(repeat) = repeat;
Packit 40b132
  GTEST_FLAG(filter) = "None";
Packit 40b132
Packit 40b132
  ResetCounts();
Packit 40b132
  GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
Packit 40b132
  CheckCounts(0);
Packit 40b132
}
Packit 40b132
Packit 40b132
// Tests using --gtest_repeat when --gtest_filter specifies a set of
Packit 40b132
// successful tests.
Packit 40b132
void TestRepeatWithFilterForSuccessfulTests(int repeat) {
Packit 40b132
  GTEST_FLAG(repeat) = repeat;
Packit 40b132
  GTEST_FLAG(filter) = "*-*ShouldFail";
Packit 40b132
Packit 40b132
  ResetCounts();
Packit 40b132
  GTEST_CHECK_INT_EQ_(0, RUN_ALL_TESTS());
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(0, g_should_fail_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_should_pass_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_death_test_count);
Packit 40b132
#if GTEST_HAS_PARAM_TEST
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat * kNumberOfParamTests, g_param_test_count);
Packit 40b132
#endif  // GTEST_HAS_PARAM_TEST
Packit 40b132
}
Packit 40b132
Packit 40b132
// Tests using --gtest_repeat when --gtest_filter specifies a set of
Packit 40b132
// failed tests.
Packit 40b132
void TestRepeatWithFilterForFailedTests(int repeat) {
Packit 40b132
  GTEST_FLAG(repeat) = repeat;
Packit 40b132
  GTEST_FLAG(filter) = "*ShouldFail";
Packit 40b132
Packit 40b132
  ResetCounts();
Packit 40b132
  GTEST_CHECK_INT_EQ_(1, RUN_ALL_TESTS());
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_environment_set_up_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_environment_tear_down_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(repeat, g_should_fail_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(0, g_should_pass_count);
Packit 40b132
  GTEST_CHECK_INT_EQ_(0, g_death_test_count);
Packit 40b132
#if GTEST_HAS_PARAM_TEST
Packit 40b132
  GTEST_CHECK_INT_EQ_(0, g_param_test_count);
Packit 40b132
#endif  // GTEST_HAS_PARAM_TEST
Packit 40b132
}
Packit 40b132
Packit 40b132
}  // namespace
Packit 40b132
Packit 40b132
int main(int argc, char **argv) {
Packit 40b132
  testing::InitGoogleTest(&argc, argv);
Packit 40b132
  testing::AddGlobalTestEnvironment(new MyEnvironment);
Packit 40b132
Packit 40b132
  TestRepeatUnspecified();
Packit 40b132
  TestRepeat(0);
Packit 40b132
  TestRepeat(1);
Packit 40b132
  TestRepeat(5);
Packit 40b132
Packit 40b132
  TestRepeatWithEmptyFilter(2);
Packit 40b132
  TestRepeatWithEmptyFilter(3);
Packit 40b132
Packit 40b132
  TestRepeatWithFilterForSuccessfulTests(3);
Packit 40b132
Packit 40b132
  TestRepeatWithFilterForFailedTests(4);
Packit 40b132
Packit 40b132
  // It would be nice to verify that the tests indeed loop forever
Packit 40b132
  // when GTEST_FLAG(repeat) is negative, but this test will be quite
Packit 40b132
  // complicated to write.  Since this flag is for interactive
Packit 40b132
  // debugging only and doesn't affect the normal test result, such a
Packit 40b132
  // test would be an overkill.
Packit 40b132
Packit 40b132
  printf("PASS\n");
Packit 40b132
  return 0;
Packit 40b132
}