Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / utility / tests / stringutil.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Tests for string utility functions and classes.
38  *
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
41  * framework.
42  *
43  * \author Teemu Murtola <teemu.murtola@gmail.com>
44  * \ingroup module_utility
45  */
46 #include <string>
47 #include <vector>
48
49 #include <gtest/gtest.h>
50
51 #include "gromacs/utility/stringutil.h"
52
53 #include "testutils/refdata.h"
54 #include "testutils/stringtest.h"
55
56 namespace
57 {
58
59 /********************************************************************
60  * Tests for simple string utilities
61  */
62
63 TEST(StringUtilityTest, StartsWithWorks)
64 {
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"));
77 }
78
79 TEST(StringUtilityTest, EndsWithWorks)
80 {
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"));
89 }
90
91 TEST(StringUtilityTest, StripSuffixIfPresent)
92 {
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"));
99 }
100
101 /********************************************************************
102  * Tests for formatString()
103  */
104
105 TEST(FormatStringTest, HandlesBasicFormatting)
106 {
107     EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
108 }
109
110 TEST(FormatStringTest, HandlesLongStrings)
111 {
112     std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
113     EXPECT_EQ(2002U, longString.length());
114     EXPECT_EQ("x10", longString.substr(1999));
115 }
116
117 /********************************************************************
118  * Tests for concatenateStrings()
119  */
120
121 //! Test fixture for gmx::concatenateStrings().
122 typedef gmx::test::StringTestBase ConcatenateStringsTest;
123
124 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
125 {
126     static const char * const strings[] = {
127         "First string",
128         "Second string ",
129         "Third string\n",
130         "Fourth string",
131         ""
132     };
133     checkText(gmx::concatenateStrings(strings), "CombinedStrings");
134 }
135
136 /********************************************************************
137  * Tests for replaceAll() and replaceAllWords()
138  */
139
140 TEST(ReplaceAllTest, HandlesEmptyStrings)
141 {
142     EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
143     EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
144 }
145
146 TEST(ReplaceAllTest, HandlesNoMatches)
147 {
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"));
151 }
152
153 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
154 {
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"));
159 }
160
161 TEST(ReplaceAllTest, HandlesMultipleMatches)
162 {
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"));
168 }
169
170 TEST(ReplaceAllTest, HandlesWordBoundaries)
171 {
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"));
175 }
176
177 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
178 {
179     const std::string text("Text with recursive aaabbbbbb matches");
180     EXPECT_EQ("Text with recursive aaaaaabbb matches",
181               gmx::replaceAll(text, "aaabbb", "aaaaaa"));
182 }
183
184 /********************************************************************
185  * Tests for TextLineWrapper
186  */
187
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";
197
198 //! Test fixture for gmx::TextLineWrapper.
199 typedef gmx::test::StringTestBase TextLineWrapperTest;
200
201 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
202 {
203     gmx::TextLineWrapper wrapper;
204
205     EXPECT_EQ("", wrapper.wrapToString(""));
206     EXPECT_EQ("", wrapper.wrapToString("   "));
207     EXPECT_TRUE(wrapper.wrapToVector("").empty());
208     EXPECT_TRUE(wrapper.wrapToString("   ").empty());
209 }
210
211 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
212 {
213     gmx::TextLineWrapper wrapper;
214
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"));
220     {
221         std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
222         ASSERT_EQ(1U, wrapped.size());
223         EXPECT_EQ("line", wrapped[0]);
224     }
225     {
226         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
227         ASSERT_EQ(1U, wrapped.size());
228         EXPECT_EQ("line", wrapped[0]);
229     }
230     {
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]);
235     }
236     {
237         std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
238         ASSERT_EQ(1U, wrapped.size());
239         EXPECT_EQ("", wrapped[0]);
240     }
241     {
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]);
246     }
247 }
248
249 TEST_F(TextLineWrapperTest, WrapsCorrectly)
250 {
251     gmx::TextLineWrapper wrapper;
252
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");
262 }
263
264 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
265 {
266     gmx::TextLineWrapper wrapper;
267
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");
273 }
274
275 TEST_F(TextLineWrapperTest, HandlesIndent)
276 {
277     gmx::TextLineWrapper wrapper;
278     wrapper.settings().setIndent(2);
279
280     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
281     wrapper.settings().setLineLength(16);
282     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
283 }
284
285 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
286 {
287     gmx::TextLineWrapper wrapper;
288     wrapper.settings().setFirstLineIndent(2);
289     wrapper.settings().setIndent(4);
290
291     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
292     wrapper.settings().setLineLength(16);
293     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
294 }
295
296 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
297 {
298     gmx::TextLineWrapper wrapper;
299     wrapper.settings().setFirstLineIndent(2);
300     wrapper.settings().setIndent(4);
301     wrapper.settings().setContinuationChar('\\');
302
303     wrapper.settings().setLineLength(16);
304     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
305 }
306
307 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
308 {
309     gmx::TextLineWrapper wrapper;
310
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");
318 }
319
320 } // namespace