From: Artem Zhmurov Date: Thu, 13 May 2021 08:06:19 +0000 (+0300) Subject: Add appendIf(..) method and tests for MessageStringCollector X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=aa04952d9466cb493573af9312dfbfc02cb66e14;p=alexxy%2Fgromacs.git Add appendIf(..) method and tests for MessageStringCollector The messages are usually added conditionally, which makes it more natural to use msc.appendIf(conditional, message) as compared to if(conditional) { msc.append(message); }. Also adds tests for basic functionality of the class and renames the files according to conventions. Refs #3886 --- diff --git a/src/gromacs/mdlib/updategroups.cpp b/src/gromacs/mdlib/updategroups.cpp index 64a0a724dd..700276ea34 100644 --- a/src/gromacs/mdlib/updategroups.cpp +++ b/src/gromacs/mdlib/updategroups.cpp @@ -59,7 +59,7 @@ #include "gromacs/topology/topology.h" #include "gromacs/utility/listoflists.h" #include "gromacs/utility/logger.h" -#include "gromacs/utility/messagestringcollector.h" +#include "gromacs/utility/message_string_collector.h" namespace gmx { diff --git a/src/gromacs/selection/selectionoption.cpp b/src/gromacs/selection/selectionoption.cpp index 76612b3ea3..fb2717662f 100644 --- a/src/gromacs/selection/selectionoption.cpp +++ b/src/gromacs/selection/selectionoption.cpp @@ -2,7 +2,7 @@ * This file is part of the GROMACS molecular simulation package. * * Copyright (c) 2010-2018, The GROMACS development team. - * Copyright (c) 2019, by the GROMACS development team, led by + * Copyright (c) 2019,2021, 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. @@ -52,7 +52,7 @@ #include "gromacs/selection/selectionoptionmanager.h" #include "gromacs/utility/exceptions.h" #include "gromacs/utility/gmxassert.h" -#include "gromacs/utility/messagestringcollector.h" +#include "gromacs/utility/message_string_collector.h" #include "selectionfileoptionstorage.h" #include "selectionoptionstorage.h" diff --git a/src/gromacs/utility.h b/src/gromacs/utility.h index f04c1c515e..fce177d218 100644 --- a/src/gromacs/utility.h +++ b/src/gromacs/utility.h @@ -139,7 +139,7 @@ * Similarly, gmxomp.h removes the need to use conditional compilation for code * that needs to include omp.h for OpenMP functions. * - * The header messagestringcollector.h declares a gmx::MessageStringCollector + * The header message_string_collector.h declares a gmx::MessageStringCollector * class for composing messages with context information. * * The header sysinfo.h declares gmx_getpid() for getting the current process diff --git a/src/gromacs/utility/messagestringcollector.cpp b/src/gromacs/utility/message_string_collector.cpp similarity index 90% rename from src/gromacs/utility/messagestringcollector.cpp rename to src/gromacs/utility/message_string_collector.cpp index 306838627d..a956bd9048 100644 --- a/src/gromacs/utility/messagestringcollector.cpp +++ b/src/gromacs/utility/message_string_collector.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2011,2012,2014,2016,2019, by the GROMACS development team, led by + * Copyright (c) 2011,2012,2014,2016,2019,2021, 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. @@ -41,7 +41,7 @@ */ #include "gmxpre.h" -#include "messagestringcollector.h" +#include "message_string_collector.h" #include @@ -101,6 +101,22 @@ void MessageStringCollector::append(const std::string& message) } } +void MessageStringCollector::appendIf(bool condition, const char* message) +{ + if (condition) + { + append(std::string(message)); + } +} + +void MessageStringCollector::appendIf(bool condition, const std::string& message) +{ + if (condition) + { + append(message); + } +} + void MessageStringCollector::finishContext() { GMX_RELEASE_ASSERT(!impl_->contexts_.empty(), "finishContext() called without context"); diff --git a/src/gromacs/utility/messagestringcollector.h b/src/gromacs/utility/message_string_collector.h similarity index 92% rename from src/gromacs/utility/messagestringcollector.h rename to src/gromacs/utility/message_string_collector.h index 8b1595fd43..a75851d7aa 100644 --- a/src/gromacs/utility/messagestringcollector.h +++ b/src/gromacs/utility/message_string_collector.h @@ -40,8 +40,8 @@ * \inlibraryapi * \ingroup module_utility */ -#ifndef GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H -#define GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H +#ifndef GMX_UTILITY_MESSAGE_STRING_COLLECTOR_H +#define GMX_UTILITY_MESSAGE_STRING_COLLECTOR_H #include #include @@ -87,6 +87,14 @@ public: * Adds a new message. */ void append(const std::string& message); + /*! \brief + * Adds a new message if the condition is satisfied.. + */ + void appendIf(bool condition, const char* message); + /*! \brief + * Adds a new message if the condition is satisfied. + */ + void appendIf(bool condition, const std::string& message); /*! \brief * Ends a context started with startContext(). * @@ -177,4 +185,4 @@ private: } // namespace gmx -#endif +#endif // GMX_UTILITY_MESSAGE_STRING_COLLECTOR_H diff --git a/src/gromacs/utility/tests/CMakeLists.txt b/src/gromacs/utility/tests/CMakeLists.txt index 7532e3c008..142d4c1984 100644 --- a/src/gromacs/utility/tests/CMakeLists.txt +++ b/src/gromacs/utility/tests/CMakeLists.txt @@ -48,6 +48,7 @@ gmx_add_unit_test(UtilityUnitTests utility-test listoflists.cpp logger.cpp mdmodulesnotifier.cpp + message_string_collector.cpp path.cpp physicalnodecommunicator.cpp range.cpp diff --git a/src/gromacs/utility/tests/message_string_collector.cpp b/src/gromacs/utility/tests/message_string_collector.cpp new file mode 100644 index 0000000000..fd7acefec7 --- /dev/null +++ b/src/gromacs/utility/tests/message_string_collector.cpp @@ -0,0 +1,180 @@ +/* + * This file is part of the GROMACS molecular simulation package. + * + * Copyright (c) 2021, 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 Tests for MessageStringCollector. + * + * \author Artem Zhmurov + * \ingroup module_utility + */ +#include "gmxpre.h" + +#include "gromacs/utility/message_string_collector.h" + +#include + +namespace gmx +{ + +namespace +{ + +TEST(MessageStringCollectorTest, CanAddAndClearMessagesNoContext) +{ + MessageStringCollector messages; + + EXPECT_TRUE(messages.isEmpty()); + + messages.append("Message1"); + + EXPECT_FALSE(messages.isEmpty()); + + messages.append("Message2"); + + EXPECT_FALSE(messages.isEmpty()); + + messages.clear(); + + EXPECT_TRUE(messages.isEmpty()); +} + +TEST(MessageStringCollectorTest, CanAddAndClearMessagesWithContext) +{ + MessageStringCollector messages; + + messages.startContext("Context1"); + + EXPECT_TRUE(messages.isEmpty()); + + messages.append("Message1"); + + EXPECT_FALSE(messages.isEmpty()); + + messages.append("Message1"); + + EXPECT_FALSE(messages.isEmpty()); + + messages.finishContext(); + + messages.clear(); + + EXPECT_TRUE(messages.isEmpty()); +} + +TEST(MessageStringCollectorTest, CanAddStringMessages) +{ + std::string context1 = "Context1"; + std::string message1 = "Message1"; + + MessageStringCollector messagesChar; + MessageStringCollector messagesString; + + messagesChar.startContext(context1.c_str()); + messagesChar.append(message1.c_str()); + messagesChar.finishContext(); + + messagesString.startContext(context1); + messagesString.append(message1); + messagesString.finishContext(); + + EXPECT_EQ(messagesChar.toString(), messagesString.toString()); +} + +TEST(MessageStringCollectorTest, CanAddCharMessagesConditionally) +{ + std::string context1 = "Context1"; + std::string message1 = "Message1"; + std::string message2 = "Message2"; + bool conditional1 = true; + bool conditional2 = false; + + MessageStringCollector messagesDirect; + MessageStringCollector messagesConditional; + + messagesDirect.startContext(context1); + if (conditional1) + { + messagesDirect.append(message1.c_str()); + } + + if (conditional2) + { + messagesDirect.append(message2.c_str()); + } + + messagesDirect.finishContext(); + + messagesConditional.startContext(context1); + messagesConditional.appendIf(conditional1, message1.c_str()); + messagesConditional.appendIf(conditional2, message2.c_str()); + messagesConditional.finishContext(); + + EXPECT_EQ(messagesDirect.toString(), messagesConditional.toString()); +} + +TEST(MessageStringCollectorTest, CanAddStringMessagesConditionally) +{ + std::string context1 = "Context1"; + std::string message1 = "Message1"; + std::string message2 = "Message2"; + bool conditional1 = true; + bool conditional2 = false; + + MessageStringCollector messagesDirect; + MessageStringCollector messagesConditional; + + messagesDirect.startContext(context1); + if (conditional1) + { + messagesDirect.append(message1); + } + + if (conditional2) + { + messagesDirect.append(message2); + } + + messagesDirect.finishContext(); + + messagesConditional.startContext(context1); + messagesConditional.appendIf(conditional1, message1); + messagesConditional.appendIf(conditional2, message2); + messagesConditional.finishContext(); + + EXPECT_EQ(messagesDirect.toString(), messagesConditional.toString()); +} + +} // namespace + +} // namespace gmx