From e6a591b99f33555f0a5b6c0c634c0910e336aa9c Mon Sep 17 00:00:00 2001 From: Teemu Murtola Date: Sun, 28 Dec 2014 14:36:07 +0200 Subject: [PATCH] Split unit test init from testoptions.cpp There is now much more functionality than just handling the options, so it is clearer that the initialization is in its own file. Change-Id: Ibd2857cb33578b80ad4cbcbdde4839b5d10eb3f7 --- src/testutils/testinit.cpp | 149 ++++++++++++++++++++++++++++++++ src/testutils/testinit.h | 82 ++++++++++++++++++ src/testutils/testoptions.cpp | 91 +------------------ src/testutils/testoptions.h | 32 +++---- src/testutils/unittest_main.cpp | 4 +- 5 files changed, 247 insertions(+), 111 deletions(-) create mode 100644 src/testutils/testinit.cpp create mode 100644 src/testutils/testinit.h diff --git a/src/testutils/testinit.cpp b/src/testutils/testinit.cpp new file mode 100644 index 0000000000..85464087b5 --- /dev/null +++ b/src/testutils/testinit.cpp @@ -0,0 +1,149 @@ +/* + * This file is part of the GROMACS molecular simulation package. + * + * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by + * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, + * and including many others, as listed in the AUTHORS file in the + * top-level source directory and at http://www.gromacs.org. + * + * GROMACS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * GROMACS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GROMACS; if not, see + * http://www.gnu.org/licenses, or write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * If you want to redistribute modifications to GROMACS, please + * consider that scientific software is very special. Version + * control is crucial - bugs must be traceable. We will be happy to + * consider code for inclusion in the official distribution, but + * derived work must not be called official GROMACS. Details are found + * in the README & COPYING files - if they are missing, get the + * official version at http://www.gromacs.org. + * + * To help us fund GROMACS development, we humbly ask that you cite + * the research papers on the package. Check out http://www.gromacs.org. + */ +/*! \internal \file + * \brief + * Implements functions in testinit.h. + * + * \author Teemu Murtola + * \ingroup module_testutils + */ +#include "gmxpre.h" + +#include "testinit.h" + +#include +#include + +#include + +#include "gromacs/commandline/cmdlinehelpcontext.h" +#include "gromacs/commandline/cmdlinehelpwriter.h" +#include "gromacs/commandline/cmdlineinit.h" +#include "gromacs/commandline/cmdlineparser.h" +#include "gromacs/math/utilities.h" +#include "gromacs/options/basicoptions.h" +#include "gromacs/options/options.h" +#include "gromacs/utility/errorcodes.h" +#include "gromacs/utility/exceptions.h" +#include "gromacs/utility/file.h" +#include "gromacs/utility/programcontext.h" + +#include "testutils/mpi-printer.h" +#include "testutils/refdata.h" +#include "testutils/testfilemanager.h" +#include "testutils/testoptions.h" + +namespace gmx +{ +namespace test +{ + +namespace +{ +//! Prints the command-line options for the unit test binary. +void printHelp(const Options &options) +{ + std::fprintf(stderr, + "\nYou can use the following GROMACS-specific command-line flags\n" + "to control the behavior of the tests:\n\n"); + CommandLineHelpContext context(&File::standardError(), + eHelpOutputFormat_Console, NULL); + context.setModuleDisplayName(getProgramContext().displayName()); + CommandLineHelpWriter(options).writeHelp(context); +} +} // namespace + +//! \cond internal +void initTestUtils(const char *dataPath, const char *tempPath, int *argc, char ***argv) +{ +#ifndef NDEBUG + gmx_feenableexcept(); +#endif + gmx::initForCommandLine(argc, argv); + try + { + ::testing::InitGoogleMock(argc, *argv); + if (dataPath != NULL) + { + TestFileManager::setInputDataDirectory(dataPath); + } + if (tempPath != NULL) + { + TestFileManager::setGlobalOutputTempDirectory(tempPath); + } + bool bHelp = false; + Options options(NULL, NULL); + // TODO: A single option that accepts multiple names would be nicer. + // Also, we recognize -help, but GTest doesn't, which leads to a bit + // unintuitive behavior. + options.addOption(BooleanOption("h").store(&bHelp) + .description("Print GROMACS-specific unit test options")); + options.addOption(BooleanOption("help").store(&bHelp).hidden()); + options.addOption(BooleanOption("?").store(&bHelp).hidden()); + // TODO: Consider removing this option from test binaries that do not need it. + initReferenceData(&options); + initTestOptions(&options); + try + { + CommandLineParser(&options).parse(argc, *argv); + options.finish(); + } + catch (const UserInputError &) + { + printHelp(options); + throw; + } + if (bHelp) + { + printHelp(options); + } + setFatalErrorHandler(NULL); + initMPIOutput(); + } + catch (const std::exception &ex) + { + printFatalErrorMessage(stderr, ex); + std::exit(processExceptionAtExitForCommandLine(ex)); + } +} + +void finalizeTestUtils() +{ + gmx::finalizeForCommandLine(); +} +//! \endcond + +} // namespace test +} // namespace gmx diff --git a/src/testutils/testinit.h b/src/testutils/testinit.h new file mode 100644 index 0000000000..ce65602c13 --- /dev/null +++ b/src/testutils/testinit.h @@ -0,0 +1,82 @@ +/* + * This file is part of the GROMACS molecular simulation package. + * + * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by + * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, + * and including many others, as listed in the AUTHORS file in the + * top-level source directory and at http://www.gromacs.org. + * + * GROMACS is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 + * of the License, or (at your option) any later version. + * + * GROMACS is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GROMACS; if not, see + * http://www.gnu.org/licenses, or write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * If you want to redistribute modifications to GROMACS, please + * consider that scientific software is very special. Version + * control is crucial - bugs must be traceable. We will be happy to + * consider code for inclusion in the official distribution, but + * derived work must not be called official GROMACS. Details are found + * in the README & COPYING files - if they are missing, get the + * official version at http://www.gromacs.org. + * + * To help us fund GROMACS development, we humbly ask that you cite + * the research papers on the package. Check out http://www.gromacs.org. + */ +/*! \internal \file + * \brief + * Functions for initialing \Gromacs unit test executables. + * + * \author Teemu Murtola + * \ingroup module_testutils + */ +#ifndef GMX_TESTUTILS_TESTINIT_H +#define GMX_TESTUTILS_TESTINIT_H + +namespace gmx +{ + +namespace test +{ + +//! \cond internal +/*! \internal + * \brief + * Initializes the test utilities library. + * + * Does not throw. Terminates the program with a non-zero error code if an + * error occurs. + * + * This function is automatically called by unittest_main.cpp. + * + * \ingroup module_testutils + */ +void initTestUtils(const char *dataPath, const char *tempPath, int *argc, char ***argv); + +/*! \internal + * \brief + * Finalizes the test utilities library. + * + * Does not throw. Terminates the program with a non-zero error code if an + * error occurs. + * + * This function is automatically called by unittest_main.cpp. + * + * \ingroup module_testutils + */ +void finalizeTestUtils(); +//! \endcond + +} // namespace test +} // namespace gmx + +#endif diff --git a/src/testutils/testoptions.cpp b/src/testutils/testoptions.cpp index c94395ab14..9bc036f51d 100644 --- a/src/testutils/testoptions.cpp +++ b/src/testutils/testoptions.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -43,31 +43,11 @@ #include "testoptions.h" -#include -#include - #include -#include - #include "thread_mpi/mutex.h" -#include "gromacs/commandline/cmdlinehelpcontext.h" -#include "gromacs/commandline/cmdlinehelpwriter.h" -#include "gromacs/commandline/cmdlineinit.h" -#include "gromacs/commandline/cmdlineparser.h" -#include "gromacs/math/utilities.h" -#include "gromacs/options/basicoptions.h" -#include "gromacs/options/options.h" #include "gromacs/utility/classhelpers.h" -#include "gromacs/utility/errorcodes.h" -#include "gromacs/utility/exceptions.h" -#include "gromacs/utility/file.h" -#include "gromacs/utility/programcontext.h" - -#include "testutils/mpi-printer.h" -#include "testutils/refdata.h" -#include "testutils/testfilemanager.h" namespace gmx { @@ -125,18 +105,6 @@ void TestOptionsRegistry::initOptions(Options *options) } } -//! Prints the command-line options for the unit test binary. -void printHelp(const Options &options) -{ - std::fprintf(stderr, - "\nYou can use the following GROMACS-specific command-line flags\n" - "to control the behavior of the tests:\n\n"); - CommandLineHelpContext context(&File::standardError(), - eHelpOutputFormat_Console, NULL); - context.setModuleDisplayName(getProgramContext().displayName()); - CommandLineHelpWriter(options).writeHelp(context); -} - } // namespace void registerTestOptions(const char *name, TestOptionsProvider *provider) @@ -144,62 +112,9 @@ void registerTestOptions(const char *name, TestOptionsProvider *provider) TestOptionsRegistry::getInstance().add(name, provider); } -void initTestUtils(const char *dataPath, const char *tempPath, int *argc, char ***argv) -{ -#ifndef NDEBUG - gmx_feenableexcept(); -#endif - gmx::initForCommandLine(argc, argv); - try - { - ::testing::InitGoogleMock(argc, *argv); - if (dataPath != NULL) - { - TestFileManager::setInputDataDirectory(dataPath); - } - if (tempPath != NULL) - { - TestFileManager::setGlobalOutputTempDirectory(tempPath); - } - bool bHelp = false; - Options options(NULL, NULL); - // TODO: A single option that accepts multiple names would be nicer. - // Also, we recognize -help, but GTest doesn't, which leads to a bit - // unintuitive behavior. - options.addOption(BooleanOption("h").store(&bHelp) - .description("Print GROMACS-specific unit test options")); - options.addOption(BooleanOption("help").store(&bHelp).hidden()); - options.addOption(BooleanOption("?").store(&bHelp).hidden()); - // TODO: Consider removing this option from test binaries that do not need it. - initReferenceData(&options); - TestOptionsRegistry::getInstance().initOptions(&options); - try - { - CommandLineParser(&options).parse(argc, *argv); - options.finish(); - } - catch (const UserInputError &) - { - printHelp(options); - throw; - } - if (bHelp) - { - printHelp(options); - } - setFatalErrorHandler(NULL); - initMPIOutput(); - } - catch (const std::exception &ex) - { - printFatalErrorMessage(stderr, ex); - std::exit(processExceptionAtExitForCommandLine(ex)); - } -} - -void finalizeTestUtils() +void initTestOptions(Options *options) { - gmx::finalizeForCommandLine(); + TestOptionsRegistry::getInstance().initOptions(options); } } // namespace test diff --git a/src/testutils/testoptions.h b/src/testutils/testoptions.h index b835190b45..bdc980504c 100644 --- a/src/testutils/testoptions.h +++ b/src/testutils/testoptions.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -97,6 +97,16 @@ class TestOptionsProvider * \ingroup module_testutils */ void registerTestOptions(const char *name, TestOptionsProvider *provider); +/*! \libinternal \brief + * Initializes the options from all registered test providers. + * + * \param options The options are added here. + * + * This is called automatically by initTestUtils(). + * + * \ingroup module_testutils + */ +void initTestOptions(Options *options); // Uncrustify screws up the indentation for the example otherwise. /* *INDENT-OFF* */ @@ -167,26 +177,6 @@ void registerTestOptions(const char *name, TestOptionsProvider *provider); \ void name::initOptions(::gmx::Options *options) -/*! \libinternal \brief - * Initializes the test utilities library. - * - * Does not throw. Terminates the program with a non-zero error code if an - * error occurs. - * - * This function is automatically called by unittest_main.cpp. - */ -void initTestUtils(const char *dataPath, const char *tempPath, int *argc, char ***argv); - -/*! \libinternal \brief - * Finalizes the test utilities library. - * - * Does not throw. Terminates the program with a non-zero error code if an - * error occurs. - * - * This function is automatically called by unittest_main.cpp. - */ -void finalizeTestUtils(); - } // namespace test } // namespace gmx diff --git a/src/testutils/unittest_main.cpp b/src/testutils/unittest_main.cpp index f9acf247bf..4510f29a2e 100644 --- a/src/testutils/unittest_main.cpp +++ b/src/testutils/unittest_main.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2010,2011,2012,2013,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -43,7 +43,7 @@ #include -#include "testutils/testoptions.h" +#include "testutils/testinit.h" #ifndef TEST_DATA_PATH //! Path to test input data directory (needs to be set by the build system). -- 2.22.0