From 40d4314670d1bb2667a93e3a3b263fd50bd77ff1 Mon Sep 17 00:00:00 2001 From: "Dunajski, Bartosz" Date: Tue, 27 Aug 2019 15:58:53 +0200 Subject: [PATCH] Templated SetUp and TearDown in fixtures Change-Id: I86b0e88db1ed52966ed5f0a6474deda09a415768 Signed-off-by: Dunajski, Bartosz --- unit_tests/fixtures/CMakeLists.txt | 1 + .../fixtures/templated_fixture_tests.cpp | 93 ++++++++ unit_tests/gen_common/test.h | 213 ++++++++---------- 3 files changed, 193 insertions(+), 114 deletions(-) create mode 100644 unit_tests/fixtures/templated_fixture_tests.cpp diff --git a/unit_tests/fixtures/CMakeLists.txt b/unit_tests/fixtures/CMakeLists.txt index c5a8abb5a0..675100aa10 100644 --- a/unit_tests/fixtures/CMakeLists.txt +++ b/unit_tests/fixtures/CMakeLists.txt @@ -40,6 +40,7 @@ set(IGDRCL_SRCS_tests_fixtures ${CMAKE_CURRENT_SOURCE_DIR}/scenario_test_fixture.h ${CMAKE_CURRENT_SOURCE_DIR}/simple_arg_fixture.h ${CMAKE_CURRENT_SOURCE_DIR}/simple_arg_kernel_fixture.h + ${CMAKE_CURRENT_SOURCE_DIR}/templated_fixture_tests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/two_walker_fixture.h ${CMAKE_CURRENT_SOURCE_DIR}/ult_command_stream_receiver_fixture.h ) diff --git a/unit_tests/fixtures/templated_fixture_tests.cpp b/unit_tests/fixtures/templated_fixture_tests.cpp new file mode 100644 index 0000000000..cef5e24d6b --- /dev/null +++ b/unit_tests/fixtures/templated_fixture_tests.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2019 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#include "runtime/helpers/hardware_commands_helper.h" +#include "test.h" + +namespace NEO { +struct TemplatedFixtureTests : public ::testing::Test { + void SetUp() override { + baseSetUpCallId = callsOrder++; + } + + void TearDown() override { + EXPECT_EQ(idForBaseTearDown, callsOrder); + EXPECT_EQ((idForBaseTearDown - 1), templateBaseTearDownCallId); + } + + template + void SetUpT() { + templateBaseSetUpCallId = callsOrder++; + } + + template + void TearDownT() { + templateBaseTearDownCallId = callsOrder++; + } + + uint32_t callsOrder = 0; + uint32_t baseSetUpCallId = -1; + uint32_t templateBaseSetUpCallId = -1; + uint32_t templateBaseTearDownCallId = -1; + + uint32_t idForBaseTearDown = -1; +}; + +HWTEST_F_T(TemplatedFixtureTests, whenExecutingTemplatedTestThenCallTemplatedSetupAndTeardown) { + EXPECT_EQ(2u, callsOrder); + + EXPECT_EQ(0u, baseSetUpCallId); + EXPECT_EQ(1u, templateBaseSetUpCallId); + + idForBaseTearDown = callsOrder + 1; +} + +struct DerivedTemplatedFixtureTests : public TemplatedFixtureTests { + template + void SetUpT() { + TemplatedFixtureTests::SetUpT(); + templateDerivedSetUpCallId = callsOrder++; + } + + template + void TearDownT() { + templateDerivedTearDownCallId = callsOrder++; + TemplatedFixtureTests::TearDownT(); + } + + uint32_t templateDerivedSetUpCallId = -1; + uint32_t templateDerivedTearDownCallId = -1; +}; + +HWTEST_F_T(DerivedTemplatedFixtureTests, whenExecutingTemplatedTestThenCallTemplatedSetupAndTeardown) { + EXPECT_EQ(3u, callsOrder); + + EXPECT_EQ(0u, baseSetUpCallId); + EXPECT_EQ(1u, templateBaseSetUpCallId); + EXPECT_EQ(2u, templateDerivedSetUpCallId); + + idForBaseTearDown = callsOrder + 2; +} + +struct TemplatedFixtureBaseTests : public ::testing::Test { + template + void SetUpT() { + capturedPipeControlWaRequiredInSetUp = HardwareCommandsHelper::isPipeControlWArequired(**platformDevices); + } + + template + void TearDownT() {} + + bool capturedPipeControlWaRequiredInSetUp = false; +}; + +HWTEST_F_T(TemplatedFixtureBaseTests, whenExecutingTemplatedSetupThenTemplateTargetsCorrectPlatform) { + bool capturedPipeControlWaRequiredInTestBody = HardwareCommandsHelper::isPipeControlWArequired(**platformDevices); + + EXPECT_EQ(capturedPipeControlWaRequiredInTestBody, capturedPipeControlWaRequiredInSetUp); +} +} // namespace NEO diff --git a/unit_tests/gen_common/test.h b/unit_tests/gen_common/test.h index 1120320b96..72d541fac6 100644 --- a/unit_tests/gen_common/test.h +++ b/unit_tests/gen_common/test.h @@ -21,33 +21,49 @@ extern PRODUCT_FAMILY productFamily; extern GFXCORE_FAMILY renderCoreFamily; #ifdef TESTS_GEN8 -#define BDW_TYPED_TEST_BODY testBodyHw::GfxFamily>(); +#define BDW_TYPED_TEST(method) method::GfxFamily>(); #define BDW_TYPED_CMDTEST_BODY runCmdTestHwIfSupported::GfxFamily>(); #define BDW_SUPPORTED_TEST(cmdSetBase) NEO::GfxFamilyMapper::GfxFamily::supportsCmdSet(cmdSetBase) #else -#define BDW_TYPED_TEST_BODY +#define BDW_TYPED_TEST(method) #define BDW_TYPED_CMDTEST_BODY #define BDW_SUPPORTED_TEST(cmdSetBase) false #endif #ifdef TESTS_GEN9 -#define SKL_TYPED_TEST_BODY testBodyHw::GfxFamily>(); +#define SKL_TYPED_TEST(method) method::GfxFamily>(); #define SKL_TYPED_CMDTEST_BODY runCmdTestHwIfSupported::GfxFamily>(); #define SKL_SUPPORTED_TEST(cmdSetBase) NEO::GfxFamilyMapper::GfxFamily::supportsCmdSet(cmdSetBase) #else -#define SKL_TYPED_TEST_BODY +#define SKL_TYPED_TEST(method) #define SKL_TYPED_CMDTEST_BODY #define SKL_SUPPORTED_TEST(cmdSetBase) false #endif #ifdef TESTS_GEN11 -#define ICL_TYPED_TEST_BODY testBodyHw::GfxFamily>(); +#define ICL_TYPED_TEST(method) method::GfxFamily>(); #define ICL_TYPED_CMDTEST_BODY runCmdTestHwIfSupported::GfxFamily>(); #define ICL_SUPPORTED_TEST(cmdSetBase) NEO::GfxFamilyMapper::GfxFamily::supportsCmdSet(cmdSetBase) #else -#define ICL_TYPED_TEST_BODY +#define ICL_TYPED_TEST(method) #define ICL_TYPED_CMDTEST_BODY #define ICL_SUPPORTED_TEST(cmdSetBase) false #endif +#define FAMILY_SELECTOR(family, methodName) \ + switch (family) { \ + case IGFX_GEN8_CORE: \ + BDW_TYPED_TEST(methodName) \ + break; \ + case IGFX_GEN9_CORE: \ + SKL_TYPED_TEST(methodName) \ + break; \ + case IGFX_GEN11_CORE: \ + ICL_TYPED_TEST(methodName) \ + break; \ + default: \ + ASSERT_TRUE((false && "Unknown hardware family")); \ + break; \ + } + #define TO_STR2(x) #x #define TO_STR(x) TO_STR2(x) @@ -103,90 +119,85 @@ extern GFXCORE_FAMILY renderCoreFamily; // Macros to provide template based testing. // Test can use FamilyType in the test -- equivalent to SKLFamily -#define HWTEST_TEST_(test_suite_name, test_name, parent_class, parent_id) \ - CHECK_TEST_NAME_LENGTH(test_suite_name, test_name) \ - class PLATFORM_EXCLUDES_CLASS_NAME(test_suite_name, test_name) { \ - public: \ - static std::unique_ptr> &getExcludes() { \ - static std::unique_ptr> excludes; \ - return excludes; \ - } \ - static void addExclude(uint32_t product) { \ - auto &excludes = getExcludes(); \ - if (excludes == nullptr) { \ - excludes = std::make_unique>(); \ - } \ - excludes->insert(product); \ - } \ - }; \ - \ - class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) : public parent_class { \ - \ - public: \ - GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ - () {} \ - \ - private: \ - template \ - void testBodyHw(); \ - bool notExcluded() const { \ - using ExcludesT = PLATFORM_EXCLUDES_CLASS_NAME(test_suite_name, test_name); \ - auto &excludes = ExcludesT::getExcludes(); \ - if (excludes == nullptr) { \ - return true; \ - } \ - return excludes->count(::productFamily) == 0; \ - } \ - void SetUp() override { \ - if (notExcluded()) { \ - parent_class::SetUp(); \ - } \ - } \ - void TearDown() override { \ - if (notExcluded()) { \ - parent_class::TearDown(); \ - } \ - } \ - \ - void TestBody() override { \ - if (notExcluded()) { \ - switch (::renderCoreFamily) { \ - case IGFX_GEN8_CORE: \ - BDW_TYPED_TEST_BODY \ - break; \ - case IGFX_GEN9_CORE: \ - SKL_TYPED_TEST_BODY \ - break; \ - case IGFX_GEN11_CORE: \ - ICL_TYPED_TEST_BODY \ - break; \ - default: \ - ASSERT_TRUE((false && "Unknown hardware family")); \ - break; \ - } \ - } \ - } \ - static ::testing::TestInfo *const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ - GTEST_DISALLOW_COPY_AND_ASSIGN_( \ - GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)); \ - }; \ - \ - ::testing::TestInfo *const GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::test_info_ = \ - ::testing::internal::MakeAndRegisterTestInfo( \ - #test_suite_name, #test_name, nullptr, nullptr, \ - ::testing::internal::CodeLocation(__FILE__, __LINE__), (parent_id), \ - ::testing::internal::SuiteApiResolver< \ - parent_class>::GetSetUpCaseOrSuite(), \ - ::testing::internal::SuiteApiResolver< \ - parent_class>::GetTearDownCaseOrSuite(), \ - new ::testing::internal::TestFactoryImpl); \ - template \ +#define HWTEST_TEST_(test_suite_name, test_name, parent_class, parent_id, SetUpT_name, TearDownT_name) \ + CHECK_TEST_NAME_LENGTH(test_suite_name, test_name) \ + class PLATFORM_EXCLUDES_CLASS_NAME(test_suite_name, test_name) { \ + public: \ + static std::unique_ptr> &getExcludes() { \ + static std::unique_ptr> excludes; \ + return excludes; \ + } \ + static void addExclude(uint32_t product) { \ + auto &excludes = getExcludes(); \ + if (excludes == nullptr) { \ + excludes = std::make_unique>(); \ + } \ + excludes->insert(product); \ + } \ + }; \ + \ + class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) : public parent_class { \ + \ + public: \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \ + () {} \ + \ + private: \ + template \ + void testBodyHw(); \ + bool notExcluded() const { \ + using ExcludesT = PLATFORM_EXCLUDES_CLASS_NAME(test_suite_name, test_name); \ + auto &excludes = ExcludesT::getExcludes(); \ + if (excludes == nullptr) { \ + return true; \ + } \ + return excludes->count(::productFamily) == 0; \ + } \ + template \ + void emptyFcn() {} \ + void SetUp() override { \ + if (notExcluded()) { \ + parent_class::SetUp(); \ + FAMILY_SELECTOR(::renderCoreFamily, SetUpT_name) \ + } \ + } \ + void TearDown() override { \ + if (notExcluded()) { \ + FAMILY_SELECTOR(::renderCoreFamily, TearDownT_name) \ + parent_class::TearDown(); \ + } \ + } \ + \ + void TestBody() override { \ + if (notExcluded()) { \ + FAMILY_SELECTOR(::renderCoreFamily, testBodyHw) \ + } \ + } \ + static ::testing::TestInfo *const test_info_ GTEST_ATTRIBUTE_UNUSED_; \ + GTEST_DISALLOW_COPY_AND_ASSIGN_( \ + GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)); \ + }; \ + \ + ::testing::TestInfo *const GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::test_info_ = \ + ::testing::internal::MakeAndRegisterTestInfo( \ + #test_suite_name, #test_name, nullptr, nullptr, \ + ::testing::internal::CodeLocation(__FILE__, __LINE__), (parent_id), \ + ::testing::internal::SuiteApiResolver< \ + parent_class>::GetSetUpCaseOrSuite(), \ + ::testing::internal::SuiteApiResolver< \ + parent_class>::GetTearDownCaseOrSuite(), \ + new ::testing::internal::TestFactoryImpl); \ + template \ void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::testBodyHw() #define HWTEST_F(test_fixture, test_name) \ HWTEST_TEST_(test_fixture, test_name, test_fixture, \ - ::testing::internal::GetTypeId()) + ::testing::internal::GetTypeId(), emptyFcn, emptyFcn) + +#define HWTEST_F_T(test_fixture, test_name) \ + HWTEST_TEST_(test_fixture, test_name, test_fixture, \ + ::testing::internal::GetTypeId(), SetUpT, TearDownT) #define PLATFORM_EXCLUDES_CLASS_NAME(test_suite_name, test_name) \ PLATFORM_EXCLUDES_##test_suite_name##test_name @@ -392,20 +403,7 @@ extern GFXCORE_FAMILY renderCoreFamily; void testBodyHw(); \ \ virtual void TestBody() override { \ - switch (::renderCoreFamily) { \ - case IGFX_GEN8_CORE: \ - BDW_TYPED_TEST_BODY \ - break; \ - case IGFX_GEN9_CORE: \ - SKL_TYPED_TEST_BODY \ - break; \ - case IGFX_GEN11_CORE: \ - ICL_TYPED_TEST_BODY \ - break; \ - default: \ - ASSERT_TRUE((false && "Unknown hardware family")); \ - break; \ - } \ + FAMILY_SELECTOR(::renderCoreFamily, testBodyHw) \ } \ \ private: \ @@ -651,20 +649,7 @@ extern GFXCORE_FAMILY renderCoreFamily; void testBodyHw(); \ \ void TestBody() override { \ - switch (::renderCoreFamily) { \ - case IGFX_GEN8_CORE: \ - BDW_TYPED_TEST_BODY \ - break; \ - case IGFX_GEN9_CORE: \ - SKL_TYPED_TEST_BODY \ - break; \ - case IGFX_GEN11_CORE: \ - ICL_TYPED_TEST_BODY \ - break; \ - default: \ - ASSERT_TRUE((false && "Unknown hardware family")); \ - break; \ - } \ + FAMILY_SELECTOR(::renderCoreFamily, testBodyHw) \ } \ }; \ bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \