2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * Tests for string utility functions and classes.
39 * For development, the tests can be run with a '-stdout' command-line option
40 * to print out the help to stdout instead of using the XML reference
43 * \author Teemu Murtola <teemu.murtola@gmail.com>
44 * \ingroup module_utility
51 #include <gmock/gmock.h>
52 #include <gtest/gtest.h>
54 #include "gromacs/utility/stringutil.h"
56 #include "testutils/refdata.h"
57 #include "testutils/stringtest.h"
62 /********************************************************************
63 * Tests for simple string utilities
66 TEST(StringUtilityTest, StartsWith)
68 EXPECT_TRUE(gmx::startsWith("foobar", "foo"));
69 EXPECT_TRUE(gmx::startsWith("foobar", ""));
70 EXPECT_TRUE(gmx::startsWith("", ""));
71 EXPECT_FALSE(gmx::startsWith("", "foobar"));
72 EXPECT_FALSE(gmx::startsWith("foo", "foobar"));
73 EXPECT_FALSE(gmx::startsWith("foobar", "oob"));
74 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), "foo"));
75 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), ""));
76 EXPECT_TRUE(gmx::startsWith(std::string(""), ""));
77 EXPECT_FALSE(gmx::startsWith(std::string(""), "foobar"));
78 EXPECT_FALSE(gmx::startsWith(std::string("foo"), "foobar"));
79 EXPECT_FALSE(gmx::startsWith(std::string("foobar"), "oob"));
82 TEST(StringUtilityTest, EndsWith)
84 EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
85 EXPECT_TRUE(gmx::endsWith("foobar", NULL));
86 EXPECT_TRUE(gmx::endsWith("foobar", ""));
87 EXPECT_TRUE(gmx::endsWith("", ""));
88 EXPECT_FALSE(gmx::endsWith("", "foobar"));
89 EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
90 EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
91 EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
94 TEST(StringUtilityTest, StripSuffixIfPresent)
96 EXPECT_EQ("foo", gmx::stripSuffixIfPresent("foobar", "bar"));
97 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", NULL));
98 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", ""));
99 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "bbar"));
100 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "barr"));
101 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
104 TEST(StringUtilityTest, StripString)
106 EXPECT_EQ("", gmx::stripString(""));
107 EXPECT_EQ("foo", gmx::stripString("foo"));
108 EXPECT_EQ("foo", gmx::stripString(" foo"));
109 EXPECT_EQ("foo", gmx::stripString("foo "));
110 EXPECT_EQ("f o o", gmx::stripString(" f o o "));
113 TEST(StringUtilityTest, SplitString)
115 using ::testing::ElementsAre;
116 using ::testing::IsEmpty;
117 using ::testing::Matcher;
118 Matcher<std::vector<std::string> > matcher = ElementsAre("foo", "bar");
119 EXPECT_THAT(gmx::splitString("foo bar"), matcher);
120 EXPECT_THAT(gmx::splitString(" foo bar"), matcher);
121 EXPECT_THAT(gmx::splitString("foo bar "), matcher);
122 EXPECT_THAT(gmx::splitString(" foo \t bar "), matcher);
123 EXPECT_THAT(gmx::splitString(""), IsEmpty());
124 EXPECT_THAT(gmx::splitString(" "), IsEmpty());
127 /********************************************************************
128 * Tests for formatString()
131 TEST(FormatStringTest, HandlesBasicFormatting)
133 EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
136 TEST(FormatStringTest, HandlesLongStrings)
138 std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
139 EXPECT_EQ(2002U, longString.length());
140 EXPECT_EQ("x10", longString.substr(1999));
143 /********************************************************************
144 * Tests for concatenateStrings()
147 //! Test fixture for gmx::concatenateStrings().
148 typedef gmx::test::StringTestBase ConcatenateStringsTest;
150 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
152 static const char * const strings[] = {
159 checkText(gmx::concatenateStrings(strings), "CombinedStrings");
162 /********************************************************************
163 * Tests for replaceAll() and replaceAllWords()
166 TEST(ReplaceAllTest, HandlesEmptyStrings)
168 EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
169 EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
172 TEST(ReplaceAllTest, HandlesNoMatches)
174 const std::string text("Text with no matches");
175 EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
176 EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
179 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
181 EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
182 EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
183 EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
184 EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
187 TEST(ReplaceAllTest, HandlesMultipleMatches)
189 const std::string text("Text aaa with multiple aaa matches");
190 EXPECT_EQ("Text bbbb with multiple bbbb matches",
191 gmx::replaceAll(text, "aaa", "bbbb"));
192 EXPECT_EQ("Text bbbb with multiple bbbb matches",
193 gmx::replaceAllWords(text, "aaa", "bbbb"));
196 TEST(ReplaceAllTest, HandlesWordBoundaries)
198 const std::string text("Text aaax with one word aaa match");
199 EXPECT_EQ("Text aaax with one word bbbb match",
200 gmx::replaceAllWords(text, "aaa", "bbbb"));
203 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
205 const std::string text("Text with recursive aaabbbbbb matches");
206 EXPECT_EQ("Text with recursive aaaaaabbb matches",
207 gmx::replaceAll(text, "aaabbb", "aaaaaa"));
210 /********************************************************************
211 * Tests for TextLineWrapper
214 //! Simple test string for wrapping.
215 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
216 //! Test string for wrapping with embedded line breaks.
217 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
218 //! Test string for wrapping with a long word.
219 const char g_wrapTextLongWord[]
220 = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
221 //! Test string for wrapping with extra whitespace.
222 const char g_wrapTextWhitespace[] = " A quick brown fox jumps \n over the lazy dog";
224 //! Test fixture for gmx::TextLineWrapper.
225 typedef gmx::test::StringTestBase TextLineWrapperTest;
227 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
229 gmx::TextLineWrapper wrapper;
231 EXPECT_EQ("", wrapper.wrapToString(""));
232 EXPECT_EQ("", wrapper.wrapToString(" "));
233 EXPECT_TRUE(wrapper.wrapToVector("").empty());
234 EXPECT_TRUE(wrapper.wrapToString(" ").empty());
237 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
239 gmx::TextLineWrapper wrapper;
241 EXPECT_EQ("line", wrapper.wrapToString("line"));
242 EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
243 EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
244 EXPECT_EQ("\n", wrapper.wrapToString("\n"));
245 EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
247 std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
248 ASSERT_EQ(1U, wrapped.size());
249 EXPECT_EQ("line", wrapped[0]);
252 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
253 ASSERT_EQ(1U, wrapped.size());
254 EXPECT_EQ("line", wrapped[0]);
257 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
258 ASSERT_EQ(2U, wrapped.size());
259 EXPECT_EQ("line", wrapped[0]);
260 EXPECT_EQ("", wrapped[1]);
263 std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
264 ASSERT_EQ(1U, wrapped.size());
265 EXPECT_EQ("", wrapped[0]);
268 std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
269 ASSERT_EQ(2U, wrapped.size());
270 EXPECT_EQ("", wrapped[0]);
271 EXPECT_EQ("", wrapped[1]);
275 TEST_F(TextLineWrapperTest, WrapsCorrectly)
277 gmx::TextLineWrapper wrapper;
279 wrapper.settings().setLineLength(10);
280 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
281 std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
282 checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
283 wrapper.settings().setLineLength(13);
284 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
285 wrapper.settings().setLineLength(14);
286 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
287 checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
290 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
292 gmx::TextLineWrapper wrapper;
294 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
295 wrapper.settings().setLineLength(10);
296 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
297 wrapper.settings().setLineLength(14);
298 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
301 TEST_F(TextLineWrapperTest, HandlesIndent)
303 gmx::TextLineWrapper wrapper;
304 wrapper.settings().setIndent(2);
306 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
307 wrapper.settings().setLineLength(16);
308 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
311 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
313 gmx::TextLineWrapper wrapper;
314 wrapper.settings().setFirstLineIndent(2);
315 wrapper.settings().setIndent(4);
317 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
318 wrapper.settings().setLineLength(16);
319 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
322 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
324 gmx::TextLineWrapper wrapper;
325 wrapper.settings().setFirstLineIndent(2);
326 wrapper.settings().setIndent(4);
327 wrapper.settings().setContinuationChar('\\');
329 wrapper.settings().setLineLength(16);
330 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
333 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
335 gmx::TextLineWrapper wrapper;
337 wrapper.settings().setLineLength(14);
338 wrapper.settings().setStripLeadingWhitespace(true);
339 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
340 "WrappedAt14StripLeading");
341 wrapper.settings().setStripLeadingWhitespace(false);
342 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
343 "WrappedAt14PreserveLeading");