Split unit test init from testoptions.cpp
authorTeemu Murtola <teemu.murtola@gmail.com>
Sun, 28 Dec 2014 12:36:07 +0000 (14:36 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Wed, 7 Jan 2015 06:32:39 +0000 (07:32 +0100)
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 [new file with mode: 0644]
src/testutils/testinit.h [new file with mode: 0644]
src/testutils/testoptions.cpp
src/testutils/testoptions.h
src/testutils/unittest_main.cpp

diff --git a/src/testutils/testinit.cpp b/src/testutils/testinit.cpp
new file mode 100644 (file)
index 0000000..8546408
--- /dev/null
@@ -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 <teemu.murtola@gmail.com>
+ * \ingroup module_testutils
+ */
+#include "gmxpre.h"
+
+#include "testinit.h"
+
+#include <cstdio>
+#include <cstdlib>
+
+#include <gmock/gmock.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/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 (file)
index 0000000..ce65602
--- /dev/null
@@ -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 <teemu.murtola@gmail.com>
+ * \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
index c94395ab140ea409244ca65859c7ed596e8177cc..9bc036f51dd9cd3463a9a4bade3bec64e62579f1 100644 (file)
@@ -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.
 
 #include "testoptions.h"
 
-#include <cstdio>
-#include <cstdlib>
-
 #include <list>
 
-#include <gmock/gmock.h>
-
 #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
index b835190b4528471c7bc97b42b083fc6c25206419..bdc980504c0bc84c9c2023586d93994554192e2a 100644 (file)
@@ -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
 
index f9acf247bf906dc287885af944798d9cdb247b8c..4510f29a2ef9f884a6b098d194e71dc6254f6df7 100644 (file)
@@ -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 <gtest/gtest.h>
 
-#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).