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
49 #include <gmock/gmock.h>
50 #include <gtest/gtest.h>
52 #include "gromacs/utility/stringutil.h"
54 #include "testutils/refdata.h"
55 #include "testutils/stringtest.h"
60 /********************************************************************
61 * Tests for simple string utilities
64 TEST(StringUtilityTest, StartsWith)
66 EXPECT_TRUE(gmx::startsWith("foobar", "foo"));
67 EXPECT_TRUE(gmx::startsWith("foobar", ""));
68 EXPECT_TRUE(gmx::startsWith("", ""));
69 EXPECT_FALSE(gmx::startsWith("", "foobar"));
70 EXPECT_FALSE(gmx::startsWith("foo", "foobar"));
71 EXPECT_FALSE(gmx::startsWith("foobar", "oob"));
72 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), "foo"));
73 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), ""));
74 EXPECT_TRUE(gmx::startsWith(std::string(""), ""));
75 EXPECT_FALSE(gmx::startsWith(std::string(""), "foobar"));
76 EXPECT_FALSE(gmx::startsWith(std::string("foo"), "foobar"));
77 EXPECT_FALSE(gmx::startsWith(std::string("foobar"), "oob"));
80 TEST(StringUtilityTest, EndsWith)
82 EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
83 EXPECT_TRUE(gmx::endsWith("foobar", NULL));
84 EXPECT_TRUE(gmx::endsWith("foobar", ""));
85 EXPECT_TRUE(gmx::endsWith("", ""));
86 EXPECT_FALSE(gmx::endsWith("", "foobar"));
87 EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
88 EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
89 EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
92 TEST(StringUtilityTest, StripSuffixIfPresent)
94 EXPECT_EQ("foo", gmx::stripSuffixIfPresent("foobar", "bar"));
95 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", NULL));
96 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", ""));
97 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "bbar"));
98 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "barr"));
99 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
102 TEST(StringUtilityTest, StripString)
104 EXPECT_EQ("", gmx::stripString(""));
105 EXPECT_EQ("foo", gmx::stripString("foo"));
106 EXPECT_EQ("foo", gmx::stripString(" foo"));
107 EXPECT_EQ("foo", gmx::stripString("foo "));
108 EXPECT_EQ("f o o", gmx::stripString(" f o o "));
111 TEST(StringUtilityTest, SplitString)
113 using ::testing::ElementsAre;
114 using ::testing::IsEmpty;
115 using ::testing::Matcher;
116 Matcher<std::vector<std::string> > matcher = ElementsAre("foo", "bar");
117 EXPECT_THAT(gmx::splitString("foo bar"), matcher);
118 EXPECT_THAT(gmx::splitString(" foo bar"), matcher);
119 EXPECT_THAT(gmx::splitString("foo bar "), matcher);
120 EXPECT_THAT(gmx::splitString(" foo \t bar "), matcher);
121 EXPECT_THAT(gmx::splitString(""), IsEmpty());
122 EXPECT_THAT(gmx::splitString(" "), IsEmpty());
125 /********************************************************************
126 * Tests for formatString()
129 TEST(FormatStringTest, HandlesBasicFormatting)
131 EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
134 TEST(FormatStringTest, HandlesLongStrings)
136 std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
137 EXPECT_EQ(2002U, longString.length());
138 EXPECT_EQ("x10", longString.substr(1999));
141 /********************************************************************
142 * Tests for concatenateStrings()
145 //! Test fixture for gmx::concatenateStrings().
146 typedef gmx::test::StringTestBase ConcatenateStringsTest;
148 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
150 static const char * const strings[] = {
157 checkText(gmx::concatenateStrings(strings), "CombinedStrings");
160 /********************************************************************
161 * Tests for replaceAll() and replaceAllWords()
164 TEST(ReplaceAllTest, HandlesEmptyStrings)
166 EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
167 EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
170 TEST(ReplaceAllTest, HandlesNoMatches)
172 const std::string text("Text with no matches");
173 EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
174 EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
177 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
179 EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
180 EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
181 EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
182 EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
185 TEST(ReplaceAllTest, HandlesMultipleMatches)
187 const std::string text("Text aaa with multiple aaa matches");
188 EXPECT_EQ("Text bbbb with multiple bbbb matches",
189 gmx::replaceAll(text, "aaa", "bbbb"));
190 EXPECT_EQ("Text bbbb with multiple bbbb matches",
191 gmx::replaceAllWords(text, "aaa", "bbbb"));
194 TEST(ReplaceAllTest, HandlesWordBoundaries)
196 const std::string text("Text aaax with one word aaa match");
197 EXPECT_EQ("Text aaax with one word bbbb match",
198 gmx::replaceAllWords(text, "aaa", "bbbb"));
201 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
203 const std::string text("Text with recursive aaabbbbbb matches");
204 EXPECT_EQ("Text with recursive aaaaaabbb matches",
205 gmx::replaceAll(text, "aaabbb", "aaaaaa"));
208 /********************************************************************
209 * Tests for TextLineWrapper
212 //! Simple test string for wrapping.
213 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
214 //! Test string for wrapping with embedded line breaks.
215 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
216 //! Test string for wrapping with a long word.
217 const char g_wrapTextLongWord[]
218 = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
219 //! Test string for wrapping with extra whitespace.
220 const char g_wrapTextWhitespace[] = " A quick brown fox jumps \n over the lazy dog";
222 //! Test fixture for gmx::TextLineWrapper.
223 typedef gmx::test::StringTestBase TextLineWrapperTest;
225 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
227 gmx::TextLineWrapper wrapper;
229 EXPECT_EQ("", wrapper.wrapToString(""));
230 EXPECT_EQ("", wrapper.wrapToString(" "));
231 EXPECT_TRUE(wrapper.wrapToVector("").empty());
232 EXPECT_TRUE(wrapper.wrapToString(" ").empty());
235 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
237 gmx::TextLineWrapper wrapper;
239 EXPECT_EQ("line", wrapper.wrapToString("line"));
240 EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
241 EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
242 EXPECT_EQ("\n", wrapper.wrapToString("\n"));
243 EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
245 std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
246 ASSERT_EQ(1U, wrapped.size());
247 EXPECT_EQ("line", wrapped[0]);
250 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
251 ASSERT_EQ(1U, wrapped.size());
252 EXPECT_EQ("line", wrapped[0]);
255 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
256 ASSERT_EQ(2U, wrapped.size());
257 EXPECT_EQ("line", wrapped[0]);
258 EXPECT_EQ("", wrapped[1]);
261 std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
262 ASSERT_EQ(1U, wrapped.size());
263 EXPECT_EQ("", wrapped[0]);
266 std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
267 ASSERT_EQ(2U, wrapped.size());
268 EXPECT_EQ("", wrapped[0]);
269 EXPECT_EQ("", wrapped[1]);
273 TEST_F(TextLineWrapperTest, WrapsCorrectly)
275 gmx::TextLineWrapper wrapper;
277 wrapper.settings().setLineLength(10);
278 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
279 std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
280 checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
281 wrapper.settings().setLineLength(13);
282 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
283 wrapper.settings().setLineLength(14);
284 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
285 checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
288 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
290 gmx::TextLineWrapper wrapper;
292 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
293 wrapper.settings().setLineLength(10);
294 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
295 wrapper.settings().setLineLength(14);
296 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
299 TEST_F(TextLineWrapperTest, HandlesIndent)
301 gmx::TextLineWrapper wrapper;
302 wrapper.settings().setIndent(2);
304 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
305 wrapper.settings().setLineLength(16);
306 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
309 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
311 gmx::TextLineWrapper wrapper;
312 wrapper.settings().setFirstLineIndent(2);
313 wrapper.settings().setIndent(4);
315 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
316 wrapper.settings().setLineLength(16);
317 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
320 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
322 gmx::TextLineWrapper wrapper;
323 wrapper.settings().setFirstLineIndent(2);
324 wrapper.settings().setIndent(4);
325 wrapper.settings().setContinuationChar('\\');
327 wrapper.settings().setLineLength(16);
328 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
331 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
333 gmx::TextLineWrapper wrapper;
335 wrapper.settings().setLineLength(14);
336 wrapper.settings().setStripLeadingWhitespace(true);
337 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
338 "WrappedAt14StripLeading");
339 wrapper.settings().setStripLeadingWhitespace(false);
340 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
341 "WrappedAt14PreserveLeading");