Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / tests / stringutil.cpp
index 433306268f06e5b28732ec162911c6eb79841f43..dcdc5cba0c7553cf641afc1faf01f724e97c482f 100644 (file)
@@ -1,32 +1,36 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2012,2014, 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.
  *
- *                 G   R   O   M   A   C   S
+ * 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.
  *
- *          GROningen MAchine for Chemical Simulations
+ * 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.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * 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, 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 www.gromacs.org.
+ * 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 papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
  * \brief
  * to print out the help to stdout instead of using the XML reference
  * framework.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_utility
  */
+#include "gmxpre.h"
+
+#include "gromacs/utility/stringutil.h"
+
 #include <string>
 #include <vector>
 
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
-#include "gromacs/utility/stringutil.h"
-
 #include "testutils/refdata.h"
 #include "testutils/stringtest.h"
 
@@ -56,11 +63,29 @@ namespace
  * Tests for simple string utilities
  */
 
-TEST(StringUtilityTest, EndsWithWorks)
+TEST(StringUtilityTest, StartsWith)
+{
+    EXPECT_TRUE(gmx::startsWith("foobar", "foo"));
+    EXPECT_TRUE(gmx::startsWith("foobar", ""));
+    EXPECT_TRUE(gmx::startsWith("", ""));
+    EXPECT_FALSE(gmx::startsWith("", "foobar"));
+    EXPECT_FALSE(gmx::startsWith("foo", "foobar"));
+    EXPECT_FALSE(gmx::startsWith("foobar", "oob"));
+    EXPECT_TRUE(gmx::startsWith(std::string("foobar"), "foo"));
+    EXPECT_TRUE(gmx::startsWith(std::string("foobar"), ""));
+    EXPECT_TRUE(gmx::startsWith(std::string(""), ""));
+    EXPECT_FALSE(gmx::startsWith(std::string(""), "foobar"));
+    EXPECT_FALSE(gmx::startsWith(std::string("foo"), "foobar"));
+    EXPECT_FALSE(gmx::startsWith(std::string("foobar"), "oob"));
+}
+
+TEST(StringUtilityTest, EndsWith)
 {
     EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
     EXPECT_TRUE(gmx::endsWith("foobar", NULL));
     EXPECT_TRUE(gmx::endsWith("foobar", ""));
+    EXPECT_TRUE(gmx::endsWith("", ""));
+    EXPECT_FALSE(gmx::endsWith("", "foobar"));
     EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
     EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
     EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
@@ -76,6 +101,29 @@ TEST(StringUtilityTest, StripSuffixIfPresent)
     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
 }
 
+TEST(StringUtilityTest, StripString)
+{
+    EXPECT_EQ("", gmx::stripString(""));
+    EXPECT_EQ("foo", gmx::stripString("foo"));
+    EXPECT_EQ("foo", gmx::stripString("  foo"));
+    EXPECT_EQ("foo", gmx::stripString("foo "));
+    EXPECT_EQ("f o o", gmx::stripString(" f o o  "));
+}
+
+TEST(StringUtilityTest, SplitString)
+{
+    using ::testing::ElementsAre;
+    using ::testing::IsEmpty;
+    using ::testing::Matcher;
+    Matcher<std::vector<std::string> > matcher = ElementsAre("foo", "bar");
+    EXPECT_THAT(gmx::splitString("foo bar"), matcher);
+    EXPECT_THAT(gmx::splitString("  foo bar"), matcher);
+    EXPECT_THAT(gmx::splitString("foo bar  "), matcher);
+    EXPECT_THAT(gmx::splitString(" foo \t bar  "), matcher);
+    EXPECT_THAT(gmx::splitString(""), IsEmpty());
+    EXPECT_THAT(gmx::splitString("   "), IsEmpty());
+}
+
 /********************************************************************
  * Tests for formatString()
  */
@@ -96,6 +144,7 @@ TEST(FormatStringTest, HandlesLongStrings)
  * Tests for concatenateStrings()
  */
 
+//! Test fixture for gmx::concatenateStrings().
 typedef gmx::test::StringTestBase ConcatenateStringsTest;
 
 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
@@ -162,12 +211,17 @@ TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
  * Tests for TextLineWrapper
  */
 
+//! Simple test string for wrapping.
 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
+//! Test string for wrapping with embedded line breaks.
 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
+//! Test string for wrapping with a long word.
 const char g_wrapTextLongWord[]
     = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
+//! Test string for wrapping with extra whitespace.
 const char g_wrapTextWhitespace[] = " A quick brown   fox jumps  \n over the lazy dog";
 
+//! Test fixture for gmx::TextLineWrapper.
 typedef gmx::test::StringTestBase TextLineWrapperTest;
 
 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
@@ -222,13 +276,13 @@ TEST_F(TextLineWrapperTest, WrapsCorrectly)
 {
     gmx::TextLineWrapper wrapper;
 
-    wrapper.setLineLength(10);
+    wrapper.settings().setLineLength(10);
     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
     std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
     checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
-    wrapper.setLineLength(13);
+    wrapper.settings().setLineLength(13);
     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
-    wrapper.setLineLength(14);
+    wrapper.settings().setLineLength(14);
     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
     checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
 }
@@ -238,18 +292,55 @@ TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
     gmx::TextLineWrapper wrapper;
 
     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
-    wrapper.setLineLength(10);
+    wrapper.settings().setLineLength(10);
     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
-    wrapper.setLineLength(14);
+    wrapper.settings().setLineLength(14);
     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
 }
 
+TEST_F(TextLineWrapperTest, HandlesIndent)
+{
+    gmx::TextLineWrapper wrapper;
+    wrapper.settings().setIndent(2);
+
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
+    wrapper.settings().setLineLength(16);
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
+}
+
+TEST_F(TextLineWrapperTest, HandlesHangingIndent)
+{
+    gmx::TextLineWrapper wrapper;
+    wrapper.settings().setFirstLineIndent(2);
+    wrapper.settings().setIndent(4);
+
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
+    wrapper.settings().setLineLength(16);
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
+}
+
+TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
+{
+    gmx::TextLineWrapper wrapper;
+    wrapper.settings().setFirstLineIndent(2);
+    wrapper.settings().setIndent(4);
+    wrapper.settings().setContinuationChar('\\');
+
+    wrapper.settings().setLineLength(16);
+    checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
+}
+
 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
 {
     gmx::TextLineWrapper wrapper;
 
-    wrapper.setLineLength(14);
-    checkText(wrapper.wrapToString(g_wrapTextWhitespace), "WrappedAt14");
+    wrapper.settings().setLineLength(14);
+    wrapper.settings().setStripLeadingWhitespace(true);
+    checkText(wrapper.wrapToString(g_wrapTextWhitespace),
+              "WrappedAt14StripLeading");
+    wrapper.settings().setStripLeadingWhitespace(false);
+    checkText(wrapper.wrapToString(g_wrapTextWhitespace),
+              "WrappedAt14PreserveLeading");
 }
 
 } // namespace