2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012, 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 <gtest/gtest.h>
51 #include "gromacs/utility/stringutil.h"
53 #include "testutils/refdata.h"
54 #include "testutils/stringtest.h"
59 /********************************************************************
60 * Tests for simple string utilities
63 TEST(StringUtilityTest, StartsWithWorks)
65 EXPECT_TRUE(gmx::startsWith("foobar", "foo"));
66 EXPECT_TRUE(gmx::startsWith("foobar", ""));
67 EXPECT_TRUE(gmx::startsWith("", ""));
68 EXPECT_FALSE(gmx::startsWith("", "foobar"));
69 EXPECT_FALSE(gmx::startsWith("foo", "foobar"));
70 EXPECT_FALSE(gmx::startsWith("foobar", "oob"));
71 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), "foo"));
72 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), ""));
73 EXPECT_TRUE(gmx::startsWith(std::string(""), ""));
74 EXPECT_FALSE(gmx::startsWith(std::string(""), "foobar"));
75 EXPECT_FALSE(gmx::startsWith(std::string("foo"), "foobar"));
76 EXPECT_FALSE(gmx::startsWith(std::string("foobar"), "oob"));
79 TEST(StringUtilityTest, EndsWithWorks)
81 EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
82 EXPECT_TRUE(gmx::endsWith("foobar", NULL));
83 EXPECT_TRUE(gmx::endsWith("foobar", ""));
84 EXPECT_TRUE(gmx::endsWith("", ""));
85 EXPECT_FALSE(gmx::endsWith("", "foobar"));
86 EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
87 EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
88 EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
91 TEST(StringUtilityTest, StripSuffixIfPresent)
93 EXPECT_EQ("foo", gmx::stripSuffixIfPresent("foobar", "bar"));
94 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", NULL));
95 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", ""));
96 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "bbar"));
97 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "barr"));
98 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
101 /********************************************************************
102 * Tests for formatString()
105 TEST(FormatStringTest, HandlesBasicFormatting)
107 EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
110 TEST(FormatStringTest, HandlesLongStrings)
112 std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
113 EXPECT_EQ(2002U, longString.length());
114 EXPECT_EQ("x10", longString.substr(1999));
117 /********************************************************************
118 * Tests for concatenateStrings()
121 //! Test fixture for gmx::concatenateStrings().
122 typedef gmx::test::StringTestBase ConcatenateStringsTest;
124 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
126 static const char * const strings[] = {
133 checkText(gmx::concatenateStrings(strings), "CombinedStrings");
136 /********************************************************************
137 * Tests for replaceAll() and replaceAllWords()
140 TEST(ReplaceAllTest, HandlesEmptyStrings)
142 EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
143 EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
146 TEST(ReplaceAllTest, HandlesNoMatches)
148 const std::string text("Text with no matches");
149 EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
150 EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
153 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
155 EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
156 EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
157 EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
158 EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
161 TEST(ReplaceAllTest, HandlesMultipleMatches)
163 const std::string text("Text aaa with multiple aaa matches");
164 EXPECT_EQ("Text bbbb with multiple bbbb matches",
165 gmx::replaceAll(text, "aaa", "bbbb"));
166 EXPECT_EQ("Text bbbb with multiple bbbb matches",
167 gmx::replaceAllWords(text, "aaa", "bbbb"));
170 TEST(ReplaceAllTest, HandlesWordBoundaries)
172 const std::string text("Text aaax with one word aaa match");
173 EXPECT_EQ("Text aaax with one word bbbb match",
174 gmx::replaceAllWords(text, "aaa", "bbbb"));
177 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
179 const std::string text("Text with recursive aaabbbbbb matches");
180 EXPECT_EQ("Text with recursive aaaaaabbb matches",
181 gmx::replaceAll(text, "aaabbb", "aaaaaa"));
184 /********************************************************************
185 * Tests for TextLineWrapper
188 //! Simple test string for wrapping.
189 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
190 //! Test string for wrapping with embedded line breaks.
191 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
192 //! Test string for wrapping with a long word.
193 const char g_wrapTextLongWord[]
194 = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
195 //! Test string for wrapping with extra whitespace.
196 const char g_wrapTextWhitespace[] = " A quick brown fox jumps \n over the lazy dog";
198 //! Test fixture for gmx::TextLineWrapper.
199 typedef gmx::test::StringTestBase TextLineWrapperTest;
201 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
203 gmx::TextLineWrapper wrapper;
205 EXPECT_EQ("", wrapper.wrapToString(""));
206 EXPECT_EQ("", wrapper.wrapToString(" "));
207 EXPECT_TRUE(wrapper.wrapToVector("").empty());
208 EXPECT_TRUE(wrapper.wrapToString(" ").empty());
211 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
213 gmx::TextLineWrapper wrapper;
215 EXPECT_EQ("line", wrapper.wrapToString("line"));
216 EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
217 EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
218 EXPECT_EQ("\n", wrapper.wrapToString("\n"));
219 EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
221 std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
222 ASSERT_EQ(1U, wrapped.size());
223 EXPECT_EQ("line", wrapped[0]);
226 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
227 ASSERT_EQ(1U, wrapped.size());
228 EXPECT_EQ("line", wrapped[0]);
231 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
232 ASSERT_EQ(2U, wrapped.size());
233 EXPECT_EQ("line", wrapped[0]);
234 EXPECT_EQ("", wrapped[1]);
237 std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
238 ASSERT_EQ(1U, wrapped.size());
239 EXPECT_EQ("", wrapped[0]);
242 std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
243 ASSERT_EQ(2U, wrapped.size());
244 EXPECT_EQ("", wrapped[0]);
245 EXPECT_EQ("", wrapped[1]);
249 TEST_F(TextLineWrapperTest, WrapsCorrectly)
251 gmx::TextLineWrapper wrapper;
253 wrapper.settings().setLineLength(10);
254 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
255 std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
256 checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
257 wrapper.settings().setLineLength(13);
258 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
259 wrapper.settings().setLineLength(14);
260 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
261 checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
264 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
266 gmx::TextLineWrapper wrapper;
268 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
269 wrapper.settings().setLineLength(10);
270 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
271 wrapper.settings().setLineLength(14);
272 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
275 TEST_F(TextLineWrapperTest, HandlesIndent)
277 gmx::TextLineWrapper wrapper;
278 wrapper.settings().setIndent(2);
280 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
281 wrapper.settings().setLineLength(16);
282 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
285 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
287 gmx::TextLineWrapper wrapper;
288 wrapper.settings().setFirstLineIndent(2);
289 wrapper.settings().setIndent(4);
291 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
292 wrapper.settings().setLineLength(16);
293 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
296 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
298 gmx::TextLineWrapper wrapper;
299 wrapper.settings().setFirstLineIndent(2);
300 wrapper.settings().setIndent(4);
301 wrapper.settings().setContinuationChar('\\');
303 wrapper.settings().setLineLength(16);
304 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
307 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
309 gmx::TextLineWrapper wrapper;
311 wrapper.settings().setLineLength(14);
312 wrapper.settings().setStripLeadingWhitespace(true);
313 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
314 "WrappedAt14StripLeading");
315 wrapper.settings().setStripLeadingWhitespace(false);
316 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
317 "WrappedAt14PreserveLeading");