diff --git a/epochX/cudacpp/gg_tt.mad/SubProcesses/MadgraphTest.h b/epochX/cudacpp/gg_tt.mad/SubProcesses/MadgraphTest.h index b707ddd1a7..4860136c9d 100644 --- a/epochX/cudacpp/gg_tt.mad/SubProcesses/MadgraphTest.h +++ b/epochX/cudacpp/gg_tt.mad/SubProcesses/MadgraphTest.h @@ -97,6 +97,29 @@ namespace * Users need to implement: * - Functions to retrieve matrix element and 4-momenta. These are used in the tests. * - Driver functions that run the madgraph workflow. + * + * Usage: + * ``` + * class TestImplementation : public TestDriverBase { + * + * } + * + * class TestImplementation2 : public TestDriverBase { + * + * } + * + * INSTANTIATE_TEST_SUITE_P( TestName, + * MadgraphTest, + * testing::Values( new TestImplementation, new TestImplementation2, ... ) ); + *``` + * + * For adapting the test workflow, see the .cc and adapt + * TEST_P(MadgraphTest, CompareMomentaAndME) + * + * To add a test that should be runnable with all test implementations that derive from TestDriverBase, add a new + * TEST_P(MadgraphTest, ) { + * + * } */ class TestDriverBase { @@ -161,18 +184,34 @@ class TestDriverBase /** * Test class that's defining all tests to run with a Madgraph workflow. + * The tests are defined below using TEST_P. + * Instantiate them using: + * ``` + * INSTANTIATE_TEST_SUITE_P( TestName, + * MadgraphTest, + * testing::Values( new TestImplementation, new TestImplementation2, ... ) ); + * ``` */ -class MadgraphTest +class MadgraphTest : public testing::TestWithParam { -public: - MadgraphTest( TestDriverBase& testDriverRef ) : testDriver( &testDriverRef ) {} - ~MadgraphTest() {} - void CompareMomentaAndME() const; -private: - TestDriverBase* testDriver; // non-owning pointer +protected: + std::unique_ptr testDriver; + + MadgraphTest() + : TestWithParam(), testDriver( GetParam() ) + { + } }; -void MadgraphTest::CompareMomentaAndME() const +// WARNING: before the split of C++ and CUDA builds, both CPU and GPU tests were linked together into the same executable; +// it was therefore necessary to prevent multiply-defined symbols by only compiling this when "#ifndef MGONGPUCPP_GPUIMPL"; +// now that runTest.exe only contains either CPU or GPU tests, this is no longer necessary! +//#ifndef MGONGPUCPP_GPUIMPL + +/// Compare momenta and matrix elements. +/// This uses an implementation of TestDriverBase to run a madgraph workflow, +/// and compares momenta and matrix elements with a reference file. +TEST_P( MadgraphTest, CompareMomentaAndME ) { const fptype toleranceMomenta = std::is_same::value ? 1.E-10 : 4.E-2; // see #735 #ifdef __APPLE__ @@ -205,7 +244,7 @@ void MadgraphTest::CompareMomentaAndME() const { referenceData = readReferenceData( refFileName ); } - //ASSERT_FALSE( HasFailure() ); // It doesn't make any sense to continue if we couldn't read the reference file. + ASSERT_FALSE( HasFailure() ); // It doesn't make any sense to continue if we couldn't read the reference file. // ************************************** // *** START MAIN LOOP ON #ITERATIONS *** // ************************************** @@ -215,8 +254,7 @@ void MadgraphTest::CompareMomentaAndME() const testDriver->prepareMomenta( energy ); testDriver->runSigmaKin( iiter ); // --- Run checks on all events produced in this iteration - //for( std::size_t ievt = 0; ievt < testDriver->nevt && !HasFailure(); ++ievt ) - for( std::size_t ievt = 0; ievt < testDriver->nevt; ++ievt ) + for( std::size_t ievt = 0; ievt < testDriver->nevt && !HasFailure(); ++ievt ) { if( dumpEvents ) { @@ -283,4 +321,9 @@ void MadgraphTest::CompareMomentaAndME() const } } +// WARNING: before the split of C++ and CUDA builds, both CPU and GPU tests were linked together into the same executable; +// it was therefore necessary to prevent multiply-defined symbols by only compiling this when "#ifndef MGONGPUCPP_GPUIMPL"; +// now that runTest.exe only contains either CPU or GPU tests, this is no longer necessary! +//#endif // MGONGPUCPP_GPUIMPL + #endif /* MADGRAPHTEST_H_ */ diff --git a/epochX/cudacpp/gg_tt.mad/SubProcesses/runTest.cc b/epochX/cudacpp/gg_tt.mad/SubProcesses/runTest.cc index ff45e2338c..dd940af4a8 100644 --- a/epochX/cudacpp/gg_tt.mad/SubProcesses/runTest.cc +++ b/epochX/cudacpp/gg_tt.mad/SubProcesses/runTest.cc @@ -242,37 +242,38 @@ struct CUDATest : public CUDA_CPU_TestBase }; #endif /* clang-format off */ -// AV July 2024 much simpler class structure without the presently-unnecessary googletest templates -// This is meant as a workaround to prevent not-understood segfault #907 when adding a second test +// Use two levels of macros to force stringification at the right level +// (see https://gcc.gnu.org/onlinedocs/gcc-3.0.1/cpp_3.html#SEC17 and https://stackoverflow.com/a/3419392) +// Google macro is in https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest-param-test.h +#define TESTID_CPU1( s ) s##_CPU1 +#define XTESTID_CPU1( s ) TESTID_CPU1( s ) +#define MG_INSTANTIATE_TEST_SUITE_CPU1( prefix, test_suite_name ) \ +INSTANTIATE_TEST_SUITE_P( prefix, \ + test_suite_name, \ + testing::Values( new CPUTest( MG_EPOCH_REFERENCE_FILE_NAME ) ) ); +#define TESTID_CPU2( s ) s##_CPU2 +#define XTESTID_CPU2( s ) TESTID_CPU2( s ) +#define MG_INSTANTIATE_TEST_SUITE_CPU2( prefix, test_suite_name ) \ +INSTANTIATE_TEST_SUITE_P( prefix, \ + test_suite_name, \ + testing::Values( new CPUTest( MG_EPOCH_REFERENCE_FILE_NAME ) ) ); +#define TESTID_GPU1( s ) s##_GPU1 +#define XTESTID_GPU1( s ) TESTID_GPU1( s ) +#define MG_INSTANTIATE_TEST_SUITE_GPU1( prefix, test_suite_name ) \ +INSTANTIATE_TEST_SUITE_P( prefix, \ + test_suite_name, \ + testing::Values( new CUDATest( MG_EPOCH_REFERENCE_FILE_NAME ) ) ); +#define TESTID_GPU2( s ) s##_GPU2 +#define XTESTID_GPU2( s ) TESTID_GPU2( s ) +#define MG_INSTANTIATE_TEST_SUITE_GPU2( prefix, test_suite_name ) \ +INSTANTIATE_TEST_SUITE_P( prefix, \ + test_suite_name, \ + testing::Values( new CUDATest( MG_EPOCH_REFERENCE_FILE_NAME ) ) ); + #ifdef MGONGPUCPP_GPUIMPL -// CUDA tests -CUDATest cudaDriver1( MG_EPOCH_REFERENCE_FILE_NAME ); -CUDATest cudaDriver2( MG_EPOCH_REFERENCE_FILE_NAME ); -MadgraphTest mgTest1( cudaDriver1 ); -MadgraphTest mgTest2( cudaDriver2 ); -#define TESTID1( s ) s##_GPU_MADGRAPH1 -#define XTESTID1( s ) TESTID1( s ) -#define TESTID2( s ) s##_GPU_MADGRAPH2 -#define XTESTID2( s ) TESTID2( s ) +MG_INSTANTIATE_TEST_SUITE_GPU1( XTESTID_GPU1( MG_EPOCH_PROCESS_ID ), MadgraphTest ); +MG_INSTANTIATE_TEST_SUITE_GPU2( XTESTID_GPU2( MG_EPOCH_PROCESS_ID ), MadgraphTest ); #else -// CPU tests -CPUTest cppDriver1( MG_EPOCH_REFERENCE_FILE_NAME ); -CPUTest cppDriver2( MG_EPOCH_REFERENCE_FILE_NAME ); -MadgraphTest mgTest1( cppDriver1 ); -MadgraphTest mgTest2( cppDriver2 ); -#define TESTID1( s ) s##_CPU_MADGRAPH1 -#define XTESTID1( s ) TESTID1( s ) -#define TESTID2( s ) s##_CPU_MADGRAPH2 -#define XTESTID2( s ) TESTID2( s ) -#endif - -// Instantiate Google tests -TEST( XTESTID1( MG_EPOCH_PROCESS_ID ), compareMomAndME ) -{ - mgTest1.CompareMomentaAndME(); -} -TEST( XTESTID2( MG_EPOCH_PROCESS_ID ), compareMomAndME ) -{ - mgTest2.CompareMomentaAndME(); -} -/* clang-format on */ +MG_INSTANTIATE_TEST_SUITE_CPU1( XTESTID_CPU1( MG_EPOCH_PROCESS_ID ), MadgraphTest ); +MG_INSTANTIATE_TEST_SUITE_CPU2( XTESTID_CPU2( MG_EPOCH_PROCESS_ID ), MadgraphTest ); +#endif /* clang-format on */