3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Tests for string utility functions and classes.
35 * For development, the tests can be run with a '-stdout' command-line option
36 * to print out the help to stdout instead of using the XML reference
39 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
40 * \ingroup module_utility
45 #include <gtest/gtest.h>
47 #include "gromacs/utility/stringutil.h"
49 #include "testutils/refdata.h"
50 #include "testutils/stringtest.h"
55 /********************************************************************
56 * Tests for simple string utilities
59 TEST(StringUtilityTest, StartsWithWorks)
61 EXPECT_TRUE(gmx::startsWith("foobar", "foo"));
62 EXPECT_TRUE(gmx::startsWith("foobar", ""));
63 EXPECT_TRUE(gmx::startsWith("", ""));
64 EXPECT_FALSE(gmx::startsWith("", "foobar"));
65 EXPECT_FALSE(gmx::startsWith("foo", "foobar"));
66 EXPECT_FALSE(gmx::startsWith("foobar", "oob"));
67 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), "foo"));
68 EXPECT_TRUE(gmx::startsWith(std::string("foobar"), ""));
69 EXPECT_TRUE(gmx::startsWith(std::string(""), ""));
70 EXPECT_FALSE(gmx::startsWith(std::string(""), "foobar"));
71 EXPECT_FALSE(gmx::startsWith(std::string("foo"), "foobar"));
72 EXPECT_FALSE(gmx::startsWith(std::string("foobar"), "oob"));
75 TEST(StringUtilityTest, EndsWithWorks)
77 EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
78 EXPECT_TRUE(gmx::endsWith("foobar", NULL));
79 EXPECT_TRUE(gmx::endsWith("foobar", ""));
80 EXPECT_TRUE(gmx::endsWith("", ""));
81 EXPECT_FALSE(gmx::endsWith("", "foobar"));
82 EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
83 EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
84 EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
87 TEST(StringUtilityTest, StripSuffixIfPresent)
89 EXPECT_EQ("foo", gmx::stripSuffixIfPresent("foobar", "bar"));
90 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", NULL));
91 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", ""));
92 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "bbar"));
93 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "barr"));
94 EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
97 /********************************************************************
98 * Tests for formatString()
101 TEST(FormatStringTest, HandlesBasicFormatting)
103 EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
106 TEST(FormatStringTest, HandlesLongStrings)
108 std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
109 EXPECT_EQ(2002U, longString.length());
110 EXPECT_EQ("x10", longString.substr(1999));
113 /********************************************************************
114 * Tests for concatenateStrings()
117 //! Test fixture for gmx::concatenateStrings().
118 typedef gmx::test::StringTestBase ConcatenateStringsTest;
120 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
122 static const char * const strings[] = {
129 checkText(gmx::concatenateStrings(strings), "CombinedStrings");
132 /********************************************************************
133 * Tests for replaceAll() and replaceAllWords()
136 TEST(ReplaceAllTest, HandlesEmptyStrings)
138 EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
139 EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
142 TEST(ReplaceAllTest, HandlesNoMatches)
144 const std::string text("Text with no matches");
145 EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
146 EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
149 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
151 EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
152 EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
153 EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
154 EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
157 TEST(ReplaceAllTest, HandlesMultipleMatches)
159 const std::string text("Text aaa with multiple aaa matches");
160 EXPECT_EQ("Text bbbb with multiple bbbb matches",
161 gmx::replaceAll(text, "aaa", "bbbb"));
162 EXPECT_EQ("Text bbbb with multiple bbbb matches",
163 gmx::replaceAllWords(text, "aaa", "bbbb"));
166 TEST(ReplaceAllTest, HandlesWordBoundaries)
168 const std::string text("Text aaax with one word aaa match");
169 EXPECT_EQ("Text aaax with one word bbbb match",
170 gmx::replaceAllWords(text, "aaa", "bbbb"));
173 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
175 const std::string text("Text with recursive aaabbbbbb matches");
176 EXPECT_EQ("Text with recursive aaaaaabbb matches",
177 gmx::replaceAll(text, "aaabbb", "aaaaaa"));
180 /********************************************************************
181 * Tests for TextLineWrapper
184 //! Simple test string for wrapping.
185 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
186 //! Test string for wrapping with embedded line breaks.
187 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
188 //! Test string for wrapping with a long word.
189 const char g_wrapTextLongWord[]
190 = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
191 //! Test string for wrapping with extra whitespace.
192 const char g_wrapTextWhitespace[] = " A quick brown fox jumps \n over the lazy dog";
194 //! Test fixture for gmx::TextLineWrapper.
195 typedef gmx::test::StringTestBase TextLineWrapperTest;
197 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
199 gmx::TextLineWrapper wrapper;
201 EXPECT_EQ("", wrapper.wrapToString(""));
202 EXPECT_EQ("", wrapper.wrapToString(" "));
203 EXPECT_TRUE(wrapper.wrapToVector("").empty());
204 EXPECT_TRUE(wrapper.wrapToString(" ").empty());
207 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
209 gmx::TextLineWrapper wrapper;
211 EXPECT_EQ("line", wrapper.wrapToString("line"));
212 EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
213 EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
214 EXPECT_EQ("\n", wrapper.wrapToString("\n"));
215 EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
217 std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
218 ASSERT_EQ(1U, wrapped.size());
219 EXPECT_EQ("line", wrapped[0]);
222 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
223 ASSERT_EQ(1U, wrapped.size());
224 EXPECT_EQ("line", wrapped[0]);
227 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
228 ASSERT_EQ(2U, wrapped.size());
229 EXPECT_EQ("line", wrapped[0]);
230 EXPECT_EQ("", wrapped[1]);
233 std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
234 ASSERT_EQ(1U, wrapped.size());
235 EXPECT_EQ("", wrapped[0]);
238 std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
239 ASSERT_EQ(2U, wrapped.size());
240 EXPECT_EQ("", wrapped[0]);
241 EXPECT_EQ("", wrapped[1]);
245 TEST_F(TextLineWrapperTest, WrapsCorrectly)
247 gmx::TextLineWrapper wrapper;
249 wrapper.settings().setLineLength(10);
250 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
251 std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
252 checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
253 wrapper.settings().setLineLength(13);
254 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
255 wrapper.settings().setLineLength(14);
256 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
257 checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
260 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
262 gmx::TextLineWrapper wrapper;
264 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
265 wrapper.settings().setLineLength(10);
266 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
267 wrapper.settings().setLineLength(14);
268 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
271 TEST_F(TextLineWrapperTest, HandlesIndent)
273 gmx::TextLineWrapper wrapper;
274 wrapper.settings().setIndent(2);
276 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
277 wrapper.settings().setLineLength(16);
278 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
281 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
283 gmx::TextLineWrapper wrapper;
284 wrapper.settings().setFirstLineIndent(2);
285 wrapper.settings().setIndent(4);
287 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
288 wrapper.settings().setLineLength(16);
289 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
292 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
294 gmx::TextLineWrapper wrapper;
295 wrapper.settings().setFirstLineIndent(2);
296 wrapper.settings().setIndent(4);
297 wrapper.settings().setContinuationChar('\\');
299 wrapper.settings().setLineLength(16);
300 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
303 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
305 gmx::TextLineWrapper wrapper;
307 wrapper.settings().setLineLength(14);
308 wrapper.settings().setStripLeadingWhitespace(true);
309 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
310 "WrappedAt14StripLeading");
311 wrapper.settings().setStripLeadingWhitespace(false);
312 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
313 "WrappedAt14PreserveLeading");