media/webrtc/trunk/testing/gtest/test/gtest-unittest-api_test.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest-unittest-api_test.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,341 @@
     1.4 +// Copyright 2009 Google Inc.  All rights reserved.
     1.5 +//
     1.6 +// Redistribution and use in source and binary forms, with or without
     1.7 +// modification, are permitted provided that the following conditions are
     1.8 +// met:
     1.9 +//
    1.10 +//     * Redistributions of source code must retain the above copyright
    1.11 +// notice, this list of conditions and the following disclaimer.
    1.12 +//     * Redistributions in binary form must reproduce the above
    1.13 +// copyright notice, this list of conditions and the following disclaimer
    1.14 +// in the documentation and/or other materials provided with the
    1.15 +// distribution.
    1.16 +//     * Neither the name of Google Inc. nor the names of its
    1.17 +// contributors may be used to endorse or promote products derived from
    1.18 +// this software without specific prior written permission.
    1.19 +//
    1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.31 +//
    1.32 +// Author: vladl@google.com (Vlad Losev)
    1.33 +//
    1.34 +// The Google C++ Testing Framework (Google Test)
    1.35 +//
    1.36 +// This file contains tests verifying correctness of data provided via
    1.37 +// UnitTest's public methods.
    1.38 +
    1.39 +#include "gtest/gtest.h"
    1.40 +
    1.41 +#include <string.h>  // For strcmp.
    1.42 +#include <algorithm>
    1.43 +
    1.44 +using ::testing::InitGoogleTest;
    1.45 +
    1.46 +namespace testing {
    1.47 +namespace internal {
    1.48 +
    1.49 +template <typename T>
    1.50 +struct LessByName {
    1.51 +  bool operator()(const T* a, const T* b) {
    1.52 +    return strcmp(a->name(), b->name()) < 0;
    1.53 +  }
    1.54 +};
    1.55 +
    1.56 +class UnitTestHelper {
    1.57 + public:
    1.58 +  // Returns the array of pointers to all test cases sorted by the test case
    1.59 +  // name.  The caller is responsible for deleting the array.
    1.60 +  static TestCase const** const GetSortedTestCases() {
    1.61 +    UnitTest& unit_test = *UnitTest::GetInstance();
    1.62 +    TestCase const** const test_cases =
    1.63 +        new const TestCase*[unit_test.total_test_case_count()];
    1.64 +
    1.65 +    for (int i = 0; i < unit_test.total_test_case_count(); ++i)
    1.66 +      test_cases[i] = unit_test.GetTestCase(i);
    1.67 +
    1.68 +    std::sort(test_cases,
    1.69 +              test_cases + unit_test.total_test_case_count(),
    1.70 +              LessByName<TestCase>());
    1.71 +    return test_cases;
    1.72 +  }
    1.73 +
    1.74 +  // Returns the test case by its name.  The caller doesn't own the returned
    1.75 +  // pointer.
    1.76 +  static const TestCase* FindTestCase(const char* name) {
    1.77 +    UnitTest& unit_test = *UnitTest::GetInstance();
    1.78 +    for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
    1.79 +      const TestCase* test_case = unit_test.GetTestCase(i);
    1.80 +      if (0 == strcmp(test_case->name(), name))
    1.81 +        return test_case;
    1.82 +    }
    1.83 +    return NULL;
    1.84 +  }
    1.85 +
    1.86 +  // Returns the array of pointers to all tests in a particular test case
    1.87 +  // sorted by the test name.  The caller is responsible for deleting the
    1.88 +  // array.
    1.89 +  static TestInfo const** const GetSortedTests(const TestCase* test_case) {
    1.90 +    TestInfo const** const tests =
    1.91 +        new const TestInfo*[test_case->total_test_count()];
    1.92 +
    1.93 +    for (int i = 0; i < test_case->total_test_count(); ++i)
    1.94 +      tests[i] = test_case->GetTestInfo(i);
    1.95 +
    1.96 +    std::sort(tests, tests + test_case->total_test_count(),
    1.97 +              LessByName<TestInfo>());
    1.98 +    return tests;
    1.99 +  }
   1.100 +};
   1.101 +
   1.102 +#if GTEST_HAS_TYPED_TEST
   1.103 +template <typename T> class TestCaseWithCommentTest : public Test {};
   1.104 +TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
   1.105 +TYPED_TEST(TestCaseWithCommentTest, Dummy) {}
   1.106 +
   1.107 +const int kTypedTestCases = 1;
   1.108 +const int kTypedTests = 1;
   1.109 +#else
   1.110 +const int kTypedTestCases = 0;
   1.111 +const int kTypedTests = 0;
   1.112 +#endif  // GTEST_HAS_TYPED_TEST
   1.113 +
   1.114 +// We can only test the accessors that do not change value while tests run.
   1.115 +// Since tests can be run in any order, the values the accessors that track
   1.116 +// test execution (such as failed_test_count) can not be predicted.
   1.117 +TEST(ApiTest, UnitTestImmutableAccessorsWork) {
   1.118 +  UnitTest* unit_test = UnitTest::GetInstance();
   1.119 +
   1.120 +  ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
   1.121 +  EXPECT_EQ(1 + kTypedTestCases, unit_test->test_case_to_run_count());
   1.122 +  EXPECT_EQ(2, unit_test->disabled_test_count());
   1.123 +  EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
   1.124 +  EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
   1.125 +
   1.126 +  const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
   1.127 +
   1.128 +  EXPECT_STREQ("ApiTest", test_cases[0]->name());
   1.129 +  EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
   1.130 +#if GTEST_HAS_TYPED_TEST
   1.131 +  EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
   1.132 +#endif  // GTEST_HAS_TYPED_TEST
   1.133 +
   1.134 +  delete[] test_cases;
   1.135 +
   1.136 +  // The following lines initiate actions to verify certain methods in
   1.137 +  // FinalSuccessChecker::TearDown.
   1.138 +
   1.139 +  // Records a test property to verify TestResult::GetTestProperty().
   1.140 +  RecordProperty("key", "value");
   1.141 +}
   1.142 +
   1.143 +AssertionResult IsNull(const char* str) {
   1.144 +  if (str != NULL) {
   1.145 +    return testing::AssertionFailure() << "argument is " << str;
   1.146 +  }
   1.147 +  return AssertionSuccess();
   1.148 +}
   1.149 +
   1.150 +TEST(ApiTest, TestCaseImmutableAccessorsWork) {
   1.151 +  const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
   1.152 +  ASSERT_TRUE(test_case != NULL);
   1.153 +
   1.154 +  EXPECT_STREQ("ApiTest", test_case->name());
   1.155 +  EXPECT_TRUE(IsNull(test_case->type_param()));
   1.156 +  EXPECT_TRUE(test_case->should_run());
   1.157 +  EXPECT_EQ(1, test_case->disabled_test_count());
   1.158 +  EXPECT_EQ(3, test_case->test_to_run_count());
   1.159 +  ASSERT_EQ(4, test_case->total_test_count());
   1.160 +
   1.161 +  const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
   1.162 +
   1.163 +  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
   1.164 +  EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
   1.165 +  EXPECT_TRUE(IsNull(tests[0]->value_param()));
   1.166 +  EXPECT_TRUE(IsNull(tests[0]->type_param()));
   1.167 +  EXPECT_FALSE(tests[0]->should_run());
   1.168 +
   1.169 +  EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
   1.170 +  EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
   1.171 +  EXPECT_TRUE(IsNull(tests[1]->value_param()));
   1.172 +  EXPECT_TRUE(IsNull(tests[1]->type_param()));
   1.173 +  EXPECT_TRUE(tests[1]->should_run());
   1.174 +
   1.175 +  EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
   1.176 +  EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
   1.177 +  EXPECT_TRUE(IsNull(tests[2]->value_param()));
   1.178 +  EXPECT_TRUE(IsNull(tests[2]->type_param()));
   1.179 +  EXPECT_TRUE(tests[2]->should_run());
   1.180 +
   1.181 +  EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
   1.182 +  EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
   1.183 +  EXPECT_TRUE(IsNull(tests[3]->value_param()));
   1.184 +  EXPECT_TRUE(IsNull(tests[3]->type_param()));
   1.185 +  EXPECT_TRUE(tests[3]->should_run());
   1.186 +
   1.187 +  delete[] tests;
   1.188 +  tests = NULL;
   1.189 +
   1.190 +#if GTEST_HAS_TYPED_TEST
   1.191 +  test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
   1.192 +  ASSERT_TRUE(test_case != NULL);
   1.193 +
   1.194 +  EXPECT_STREQ("TestCaseWithCommentTest/0", test_case->name());
   1.195 +  EXPECT_STREQ(GetTypeName<int>().c_str(), test_case->type_param());
   1.196 +  EXPECT_TRUE(test_case->should_run());
   1.197 +  EXPECT_EQ(0, test_case->disabled_test_count());
   1.198 +  EXPECT_EQ(1, test_case->test_to_run_count());
   1.199 +  ASSERT_EQ(1, test_case->total_test_count());
   1.200 +
   1.201 +  tests = UnitTestHelper::GetSortedTests(test_case);
   1.202 +
   1.203 +  EXPECT_STREQ("Dummy", tests[0]->name());
   1.204 +  EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
   1.205 +  EXPECT_TRUE(IsNull(tests[0]->value_param()));
   1.206 +  EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
   1.207 +  EXPECT_TRUE(tests[0]->should_run());
   1.208 +
   1.209 +  delete[] tests;
   1.210 +#endif  // GTEST_HAS_TYPED_TEST
   1.211 +}
   1.212 +
   1.213 +TEST(ApiTest, TestCaseDisabledAccessorsWork) {
   1.214 +  const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
   1.215 +  ASSERT_TRUE(test_case != NULL);
   1.216 +
   1.217 +  EXPECT_STREQ("DISABLED_Test", test_case->name());
   1.218 +  EXPECT_TRUE(IsNull(test_case->type_param()));
   1.219 +  EXPECT_FALSE(test_case->should_run());
   1.220 +  EXPECT_EQ(1, test_case->disabled_test_count());
   1.221 +  EXPECT_EQ(0, test_case->test_to_run_count());
   1.222 +  ASSERT_EQ(1, test_case->total_test_count());
   1.223 +
   1.224 +  const TestInfo* const test_info = test_case->GetTestInfo(0);
   1.225 +  EXPECT_STREQ("Dummy2", test_info->name());
   1.226 +  EXPECT_STREQ("DISABLED_Test", test_info->test_case_name());
   1.227 +  EXPECT_TRUE(IsNull(test_info->value_param()));
   1.228 +  EXPECT_TRUE(IsNull(test_info->type_param()));
   1.229 +  EXPECT_FALSE(test_info->should_run());
   1.230 +}
   1.231 +
   1.232 +// These two tests are here to provide support for testing
   1.233 +// test_case_to_run_count, disabled_test_count, and test_to_run_count.
   1.234 +TEST(ApiTest, DISABLED_Dummy1) {}
   1.235 +TEST(DISABLED_Test, Dummy2) {}
   1.236 +
   1.237 +class FinalSuccessChecker : public Environment {
   1.238 + protected:
   1.239 +  virtual void TearDown() {
   1.240 +    UnitTest* unit_test = UnitTest::GetInstance();
   1.241 +
   1.242 +    EXPECT_EQ(1 + kTypedTestCases, unit_test->successful_test_case_count());
   1.243 +    EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
   1.244 +    EXPECT_EQ(0, unit_test->failed_test_case_count());
   1.245 +    EXPECT_EQ(0, unit_test->failed_test_count());
   1.246 +    EXPECT_TRUE(unit_test->Passed());
   1.247 +    EXPECT_FALSE(unit_test->Failed());
   1.248 +    ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
   1.249 +
   1.250 +    const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
   1.251 +
   1.252 +    EXPECT_STREQ("ApiTest", test_cases[0]->name());
   1.253 +    EXPECT_TRUE(IsNull(test_cases[0]->type_param()));
   1.254 +    EXPECT_TRUE(test_cases[0]->should_run());
   1.255 +    EXPECT_EQ(1, test_cases[0]->disabled_test_count());
   1.256 +    ASSERT_EQ(4, test_cases[0]->total_test_count());
   1.257 +    EXPECT_EQ(3, test_cases[0]->successful_test_count());
   1.258 +    EXPECT_EQ(0, test_cases[0]->failed_test_count());
   1.259 +    EXPECT_TRUE(test_cases[0]->Passed());
   1.260 +    EXPECT_FALSE(test_cases[0]->Failed());
   1.261 +
   1.262 +    EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
   1.263 +    EXPECT_TRUE(IsNull(test_cases[1]->type_param()));
   1.264 +    EXPECT_FALSE(test_cases[1]->should_run());
   1.265 +    EXPECT_EQ(1, test_cases[1]->disabled_test_count());
   1.266 +    ASSERT_EQ(1, test_cases[1]->total_test_count());
   1.267 +    EXPECT_EQ(0, test_cases[1]->successful_test_count());
   1.268 +    EXPECT_EQ(0, test_cases[1]->failed_test_count());
   1.269 +
   1.270 +#if GTEST_HAS_TYPED_TEST
   1.271 +    EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
   1.272 +    EXPECT_STREQ(GetTypeName<int>().c_str(), test_cases[2]->type_param());
   1.273 +    EXPECT_TRUE(test_cases[2]->should_run());
   1.274 +    EXPECT_EQ(0, test_cases[2]->disabled_test_count());
   1.275 +    ASSERT_EQ(1, test_cases[2]->total_test_count());
   1.276 +    EXPECT_EQ(1, test_cases[2]->successful_test_count());
   1.277 +    EXPECT_EQ(0, test_cases[2]->failed_test_count());
   1.278 +    EXPECT_TRUE(test_cases[2]->Passed());
   1.279 +    EXPECT_FALSE(test_cases[2]->Failed());
   1.280 +#endif  // GTEST_HAS_TYPED_TEST
   1.281 +
   1.282 +    const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
   1.283 +    const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
   1.284 +    EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
   1.285 +    EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
   1.286 +    EXPECT_FALSE(tests[0]->should_run());
   1.287 +
   1.288 +    EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
   1.289 +    EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
   1.290 +    EXPECT_TRUE(IsNull(tests[1]->value_param()));
   1.291 +    EXPECT_TRUE(IsNull(tests[1]->type_param()));
   1.292 +    EXPECT_TRUE(tests[1]->should_run());
   1.293 +    EXPECT_TRUE(tests[1]->result()->Passed());
   1.294 +    EXPECT_EQ(0, tests[1]->result()->test_property_count());
   1.295 +
   1.296 +    EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
   1.297 +    EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
   1.298 +    EXPECT_TRUE(IsNull(tests[2]->value_param()));
   1.299 +    EXPECT_TRUE(IsNull(tests[2]->type_param()));
   1.300 +    EXPECT_TRUE(tests[2]->should_run());
   1.301 +    EXPECT_TRUE(tests[2]->result()->Passed());
   1.302 +    EXPECT_EQ(0, tests[2]->result()->test_property_count());
   1.303 +
   1.304 +    EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
   1.305 +    EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
   1.306 +    EXPECT_TRUE(IsNull(tests[3]->value_param()));
   1.307 +    EXPECT_TRUE(IsNull(tests[3]->type_param()));
   1.308 +    EXPECT_TRUE(tests[3]->should_run());
   1.309 +    EXPECT_TRUE(tests[3]->result()->Passed());
   1.310 +    EXPECT_EQ(1, tests[3]->result()->test_property_count());
   1.311 +    const TestProperty& property = tests[3]->result()->GetTestProperty(0);
   1.312 +    EXPECT_STREQ("key", property.key());
   1.313 +    EXPECT_STREQ("value", property.value());
   1.314 +
   1.315 +    delete[] tests;
   1.316 +
   1.317 +#if GTEST_HAS_TYPED_TEST
   1.318 +    test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
   1.319 +    tests = UnitTestHelper::GetSortedTests(test_case);
   1.320 +
   1.321 +    EXPECT_STREQ("Dummy", tests[0]->name());
   1.322 +    EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
   1.323 +    EXPECT_TRUE(IsNull(tests[0]->value_param()));
   1.324 +    EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
   1.325 +    EXPECT_TRUE(tests[0]->should_run());
   1.326 +    EXPECT_TRUE(tests[0]->result()->Passed());
   1.327 +    EXPECT_EQ(0, tests[0]->result()->test_property_count());
   1.328 +
   1.329 +    delete[] tests;
   1.330 +#endif  // GTEST_HAS_TYPED_TEST
   1.331 +    delete[] test_cases;
   1.332 +  }
   1.333 +};
   1.334 +
   1.335 +}  // namespace internal
   1.336 +}  // namespace testing
   1.337 +
   1.338 +int main(int argc, char **argv) {
   1.339 +  InitGoogleTest(&argc, argv);
   1.340 +
   1.341 +  AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
   1.342 +
   1.343 +  return RUN_ALL_TESTS();
   1.344 +}

mercurial