Merge branch release-4-6 into release-5-0
[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,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.
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 <gmock/gmock.h>
50 #include <gtest/gtest.h>
51
52 #include "gromacs/utility/stringutil.h"
53
54 #include "testutils/refdata.h"
55 #include "testutils/stringtest.h"
56
57 namespace
58 {
59
60 /********************************************************************
61  * Tests for simple string utilities
62  */
63
64 TEST(StringUtilityTest, StartsWith)
65 {
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"));
78 }
79
80 TEST(StringUtilityTest, EndsWith)
81 {
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"));
90 }
91
92 TEST(StringUtilityTest, StripSuffixIfPresent)
93 {
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"));
100 }
101
102 TEST(StringUtilityTest, StripString)
103 {
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  "));
109 }
110
111 TEST(StringUtilityTest, SplitString)
112 {
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());
123 }
124
125 /********************************************************************
126  * Tests for formatString()
127  */
128
129 TEST(FormatStringTest, HandlesBasicFormatting)
130 {
131     EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
132 }
133
134 TEST(FormatStringTest, HandlesLongStrings)
135 {
136     std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
137     EXPECT_EQ(2002U, longString.length());
138     EXPECT_EQ("x10", longString.substr(1999));
139 }
140
141 /********************************************************************
142  * Tests for concatenateStrings()
143  */
144
145 //! Test fixture for gmx::concatenateStrings().
146 typedef gmx::test::StringTestBase ConcatenateStringsTest;
147
148 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
149 {
150     static const char * const strings[] = {
151         "First string",
152         "Second string ",
153         "Third string\n",
154         "Fourth string",
155         ""
156     };
157     checkText(gmx::concatenateStrings(strings), "CombinedStrings");
158 }
159
160 /********************************************************************
161  * Tests for replaceAll() and replaceAllWords()
162  */
163
164 TEST(ReplaceAllTest, HandlesEmptyStrings)
165 {
166     EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
167     EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
168 }
169
170 TEST(ReplaceAllTest, HandlesNoMatches)
171 {
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"));
175 }
176
177 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
178 {
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"));
183 }
184
185 TEST(ReplaceAllTest, HandlesMultipleMatches)
186 {
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"));
192 }
193
194 TEST(ReplaceAllTest, HandlesWordBoundaries)
195 {
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"));
199 }
200
201 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
202 {
203     const std::string text("Text with recursive aaabbbbbb matches");
204     EXPECT_EQ("Text with recursive aaaaaabbb matches",
205               gmx::replaceAll(text, "aaabbb", "aaaaaa"));
206 }
207
208 /********************************************************************
209  * Tests for TextLineWrapper
210  */
211
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";
221
222 //! Test fixture for gmx::TextLineWrapper.
223 typedef gmx::test::StringTestBase TextLineWrapperTest;
224
225 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
226 {
227     gmx::TextLineWrapper wrapper;
228
229     EXPECT_EQ("", wrapper.wrapToString(""));
230     EXPECT_EQ("", wrapper.wrapToString("   "));
231     EXPECT_TRUE(wrapper.wrapToVector("").empty());
232     EXPECT_TRUE(wrapper.wrapToString("   ").empty());
233 }
234
235 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
236 {
237     gmx::TextLineWrapper wrapper;
238
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"));
244     {
245         std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
246         ASSERT_EQ(1U, wrapped.size());
247         EXPECT_EQ("line", wrapped[0]);
248     }
249     {
250         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
251         ASSERT_EQ(1U, wrapped.size());
252         EXPECT_EQ("line", wrapped[0]);
253     }
254     {
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]);
259     }
260     {
261         std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
262         ASSERT_EQ(1U, wrapped.size());
263         EXPECT_EQ("", wrapped[0]);
264     }
265     {
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]);
270     }
271 }
272
273 TEST_F(TextLineWrapperTest, WrapsCorrectly)
274 {
275     gmx::TextLineWrapper wrapper;
276
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");
286 }
287
288 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
289 {
290     gmx::TextLineWrapper wrapper;
291
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");
297 }
298
299 TEST_F(TextLineWrapperTest, HandlesIndent)
300 {
301     gmx::TextLineWrapper wrapper;
302     wrapper.settings().setIndent(2);
303
304     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
305     wrapper.settings().setLineLength(16);
306     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
307 }
308
309 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
310 {
311     gmx::TextLineWrapper wrapper;
312     wrapper.settings().setFirstLineIndent(2);
313     wrapper.settings().setIndent(4);
314
315     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
316     wrapper.settings().setLineLength(16);
317     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
318 }
319
320 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
321 {
322     gmx::TextLineWrapper wrapper;
323     wrapper.settings().setFirstLineIndent(2);
324     wrapper.settings().setIndent(4);
325     wrapper.settings().setContinuationChar('\\');
326
327     wrapper.settings().setLineLength(16);
328     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
329 }
330
331 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
332 {
333     gmx::TextLineWrapper wrapper;
334
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");
342 }
343
344 } // namespace