Expanded the GetIrTest behavior to enable efficient testing
authorBerk Hess <hess@kth.se>
Tue, 21 Sep 2021 10:39:28 +0000 (10:39 +0000)
committerJoe Jordan <ejjordan12@gmail.com>
Tue, 21 Sep 2021 10:39:28 +0000 (10:39 +0000)
src/gromacs/gmxpreprocess/readir.cpp
src/gromacs/gmxpreprocess/tests/readir.cpp

index 28b4184f14c761463635096a9dfe92cbcfd03e95..1286c79bf79dc772b26fb7da38d5243e187dd0dc 100644 (file)
@@ -2482,6 +2482,7 @@ void get_ir(const char*     mdparin,
     ir->userreal4 = get_ereal(&inp, "userreal4", 0, wi);
 #undef CTYPE
 
+    if (mdparout)
     {
         gmx::TextOutputFile stream(mdparout);
         write_inpfile(&stream, mdparout, &inp, FALSE, writeMdpHeader, wi);
index af1b9b967a6a5b0230afd5b6112c42be6d777075..de21db7179f34cf84051af8ef9743f8dfa00fe58 100644 (file)
@@ -82,30 +82,58 @@ public:
         sfree(opts_.include);
         sfree(opts_.define);
     }
+
+    //! Tells whether warnings and/or errors are expected from inputrec parsing and checking, and whether we should compare the output
+    enum class TestBehavior
+    {
+        NoErrorAndCompareOutput,   //!< Expect no warnings/error and compare output
+        ErrorAndCompareOutput,     //!< Expect at least one warning/error and compare output
+        ErrorAndDoNotCompareOutput //!< Expect at least one warning/error and do not compare output
+    };
+
     /*! \brief Test mdp reading and writing
      *
      * \todo Modernize read_inp and write_inp to use streams,
      * which will make these tests run faster, because they don't
      * use disk files. */
-    void runTest(const std::string& inputMdpFileContents)
+    void runTest(const std::string& inputMdpFileContents,
+                 const TestBehavior testBehavior = TestBehavior::NoErrorAndCompareOutput)
     {
-        auto inputMdpFilename  = fileManager_.getTemporaryFilePath("input.mdp");
-        auto outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
+        const bool expectError   = testBehavior != TestBehavior::NoErrorAndCompareOutput;
+        const bool compareOutput = testBehavior != TestBehavior::ErrorAndDoNotCompareOutput;
+
+        std::string inputMdpFilename = fileManager_.getTemporaryFilePath("input.mdp");
+        std::string outputMdpFilename;
+        if (compareOutput)
+        {
+            outputMdpFilename = fileManager_.getTemporaryFilePath("output.mdp");
+        }
 
         TextWriter::writeFileFromString(inputMdpFilename, inputMdpFileContents);
 
-        get_ir(inputMdpFilename.c_str(), outputMdpFilename.c_str(), &mdModules_, &ir_, &opts_, WriteMdpHeader::no, wi_);
+        get_ir(inputMdpFilename.c_str(),
+               outputMdpFilename.empty() ? nullptr : outputMdpFilename.c_str(),
+               &mdModules_,
+               &ir_,
+               &opts_,
+               WriteMdpHeader::no,
+               wi_);
 
         check_ir(inputMdpFilename.c_str(), mdModules_.notifiers(), &ir_, &opts_, wi_);
         // Now check
-        bool                 failure = warning_errors_exist(wi_);
-        TestReferenceData    data;
-        TestReferenceChecker checker(data.rootChecker());
-        checker.checkBoolean(failure, "Error parsing mdp file");
-        warning_reset(wi_);
-
-        auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
-        checker.checkString(outputMdpContents, "OutputMdpFile");
+        bool failure = warning_errors_exist(wi_);
+        EXPECT_EQ(failure, expectError);
+
+        if (compareOutput)
+        {
+            TestReferenceData    data;
+            TestReferenceChecker checker(data.rootChecker());
+            checker.checkBoolean(failure, "Error parsing mdp file");
+            warning_reset(wi_);
+
+            auto outputMdpContents = TextReader::readFileToString(outputMdpFilename);
+            checker.checkString(outputMdpContents, "OutputMdpFile");
+        }
     }
 
     TestFileManager                    fileManager_;
@@ -174,6 +202,40 @@ TEST_F(GetIrTest, AcceptsEmptyLines)
     runTest(inputMdpFile);
 }
 
+TEST_F(GetIrTest, MtsCheckNstcalcenergy)
+{
+    const char* inputMdpFile[] = {
+        "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstcalcenergy = 5"
+    };
+    runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
+}
+
+TEST_F(GetIrTest, MtsCheckNstenergy)
+{
+    const char* inputMdpFile[] = {
+        "mts = yes", "mts-levels = 2", "mts-level2-factor = 2", "nstenergy = 5"
+    };
+    runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
+}
+
+TEST_F(GetIrTest, MtsCheckNstpcouple)
+{
+    const char* inputMdpFile[] = { "mts = yes",
+                                   "mts-levels = 2",
+                                   "mts-level2-factor = 2",
+                                   "pcoupl = Berendsen",
+                                   "nstpcouple = 5" };
+    runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
+}
+
+TEST_F(GetIrTest, MtsCheckNstdhdl)
+{
+    const char* inputMdpFile[] = {
+        "mts = yes", "mts-level2-factor = 2", "free-energy = yes", "nstdhdl = 5"
+    };
+    runTest(joinStrings(inputMdpFile, "\n"), TestBehavior::ErrorAndDoNotCompareOutput);
+}
+
 // These tests observe how the electric-field keys behave, since they
 // are currently the only ones using the new Options-style handling.
 TEST_F(GetIrTest, AcceptsElectricField)