c63a9c4559680ca7f02ec9b188a531a97c5a4b4f
[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 "gmxpre.h"
47
48 #include <string>
49 #include <vector>
50
51 #include <gmock/gmock.h>
52 #include <gtest/gtest.h>
53
54 #include "gromacs/utility/stringutil.h"
55
56 #include "testutils/refdata.h"
57 #include "testutils/stringtest.h"
58
59 namespace
60 {
61
62 /********************************************************************
63  * Tests for simple string utilities
64  */
65
66 TEST(StringUtilityTest, StartsWith)
67 {
68     EXPECT_TRUE(gmx::startsWith("foobar", "foo"));
69     EXPECT_TRUE(gmx::startsWith("foobar", ""));
70     EXPECT_TRUE(gmx::startsWith("", ""));
71     EXPECT_FALSE(gmx::startsWith("", "foobar"));
72     EXPECT_FALSE(gmx::startsWith("foo", "foobar"));
73     EXPECT_FALSE(gmx::startsWith("foobar", "oob"));
74     EXPECT_TRUE(gmx::startsWith(std::string("foobar"), "foo"));
75     EXPECT_TRUE(gmx::startsWith(std::string("foobar"), ""));
76     EXPECT_TRUE(gmx::startsWith(std::string(""), ""));
77     EXPECT_FALSE(gmx::startsWith(std::string(""), "foobar"));
78     EXPECT_FALSE(gmx::startsWith(std::string("foo"), "foobar"));
79     EXPECT_FALSE(gmx::startsWith(std::string("foobar"), "oob"));
80 }
81
82 TEST(StringUtilityTest, EndsWith)
83 {
84     EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
85     EXPECT_TRUE(gmx::endsWith("foobar", NULL));
86     EXPECT_TRUE(gmx::endsWith("foobar", ""));
87     EXPECT_TRUE(gmx::endsWith("", ""));
88     EXPECT_FALSE(gmx::endsWith("", "foobar"));
89     EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
90     EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
91     EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
92 }
93
94 TEST(StringUtilityTest, StripSuffixIfPresent)
95 {
96     EXPECT_EQ("foo", gmx::stripSuffixIfPresent("foobar", "bar"));
97     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", NULL));
98     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", ""));
99     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "bbar"));
100     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "barr"));
101     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
102 }
103
104 TEST(StringUtilityTest, StripString)
105 {
106     EXPECT_EQ("", gmx::stripString(""));
107     EXPECT_EQ("foo", gmx::stripString("foo"));
108     EXPECT_EQ("foo", gmx::stripString("  foo"));
109     EXPECT_EQ("foo", gmx::stripString("foo "));
110     EXPECT_EQ("f o o", gmx::stripString(" f o o  "));
111 }
112
113 TEST(StringUtilityTest, SplitString)
114 {
115     using ::testing::ElementsAre;
116     using ::testing::IsEmpty;
117     using ::testing::Matcher;
118     Matcher<std::vector<std::string> > matcher = ElementsAre("foo", "bar");
119     EXPECT_THAT(gmx::splitString("foo bar"), matcher);
120     EXPECT_THAT(gmx::splitString("  foo bar"), matcher);
121     EXPECT_THAT(gmx::splitString("foo bar  "), matcher);
122     EXPECT_THAT(gmx::splitString(" foo \t bar  "), matcher);
123     EXPECT_THAT(gmx::splitString(""), IsEmpty());
124     EXPECT_THAT(gmx::splitString("   "), IsEmpty());
125 }
126
127 /********************************************************************
128  * Tests for formatString()
129  */
130
131 TEST(FormatStringTest, HandlesBasicFormatting)
132 {
133     EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
134 }
135
136 TEST(FormatStringTest, HandlesLongStrings)
137 {
138     std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
139     EXPECT_EQ(2002U, longString.length());
140     EXPECT_EQ("x10", longString.substr(1999));
141 }
142
143 /********************************************************************
144  * Tests for concatenateStrings()
145  */
146
147 //! Test fixture for gmx::concatenateStrings().
148 typedef gmx::test::StringTestBase ConcatenateStringsTest;
149
150 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
151 {
152     static const char * const strings[] = {
153         "First string",
154         "Second string ",
155         "Third string\n",
156         "Fourth string",
157         ""
158     };
159     checkText(gmx::concatenateStrings(strings), "CombinedStrings");
160 }
161
162 /********************************************************************
163  * Tests for replaceAll() and replaceAllWords()
164  */
165
166 TEST(ReplaceAllTest, HandlesEmptyStrings)
167 {
168     EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
169     EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
170 }
171
172 TEST(ReplaceAllTest, HandlesNoMatches)
173 {
174     const std::string text("Text with no matches");
175     EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
176     EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
177 }
178
179 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
180 {
181     EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
182     EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
183     EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
184     EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
185 }
186
187 TEST(ReplaceAllTest, HandlesMultipleMatches)
188 {
189     const std::string text("Text aaa with multiple aaa matches");
190     EXPECT_EQ("Text bbbb with multiple bbbb matches",
191               gmx::replaceAll(text, "aaa", "bbbb"));
192     EXPECT_EQ("Text bbbb with multiple bbbb matches",
193               gmx::replaceAllWords(text, "aaa", "bbbb"));
194 }
195
196 TEST(ReplaceAllTest, HandlesWordBoundaries)
197 {
198     const std::string text("Text aaax with one word aaa match");
199     EXPECT_EQ("Text aaax with one word bbbb match",
200               gmx::replaceAllWords(text, "aaa", "bbbb"));
201 }
202
203 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
204 {
205     const std::string text("Text with recursive aaabbbbbb matches");
206     EXPECT_EQ("Text with recursive aaaaaabbb matches",
207               gmx::replaceAll(text, "aaabbb", "aaaaaa"));
208 }
209
210 /********************************************************************
211  * Tests for TextLineWrapper
212  */
213
214 //! Simple test string for wrapping.
215 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
216 //! Test string for wrapping with embedded line breaks.
217 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
218 //! Test string for wrapping with a long word.
219 const char g_wrapTextLongWord[]
220     = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
221 //! Test string for wrapping with extra whitespace.
222 const char g_wrapTextWhitespace[] = " A quick brown   fox jumps  \n over the lazy dog";
223
224 //! Test fixture for gmx::TextLineWrapper.
225 typedef gmx::test::StringTestBase TextLineWrapperTest;
226
227 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
228 {
229     gmx::TextLineWrapper wrapper;
230
231     EXPECT_EQ("", wrapper.wrapToString(""));
232     EXPECT_EQ("", wrapper.wrapToString("   "));
233     EXPECT_TRUE(wrapper.wrapToVector("").empty());
234     EXPECT_TRUE(wrapper.wrapToString("   ").empty());
235 }
236
237 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
238 {
239     gmx::TextLineWrapper wrapper;
240
241     EXPECT_EQ("line", wrapper.wrapToString("line"));
242     EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
243     EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
244     EXPECT_EQ("\n", wrapper.wrapToString("\n"));
245     EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
246     {
247         std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
248         ASSERT_EQ(1U, wrapped.size());
249         EXPECT_EQ("line", wrapped[0]);
250     }
251     {
252         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
253         ASSERT_EQ(1U, wrapped.size());
254         EXPECT_EQ("line", wrapped[0]);
255     }
256     {
257         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
258         ASSERT_EQ(2U, wrapped.size());
259         EXPECT_EQ("line", wrapped[0]);
260         EXPECT_EQ("", wrapped[1]);
261     }
262     {
263         std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
264         ASSERT_EQ(1U, wrapped.size());
265         EXPECT_EQ("", wrapped[0]);
266     }
267     {
268         std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
269         ASSERT_EQ(2U, wrapped.size());
270         EXPECT_EQ("", wrapped[0]);
271         EXPECT_EQ("", wrapped[1]);
272     }
273 }
274
275 TEST_F(TextLineWrapperTest, WrapsCorrectly)
276 {
277     gmx::TextLineWrapper wrapper;
278
279     wrapper.settings().setLineLength(10);
280     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
281     std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
282     checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
283     wrapper.settings().setLineLength(13);
284     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
285     wrapper.settings().setLineLength(14);
286     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
287     checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
288 }
289
290 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
291 {
292     gmx::TextLineWrapper wrapper;
293
294     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
295     wrapper.settings().setLineLength(10);
296     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
297     wrapper.settings().setLineLength(14);
298     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
299 }
300
301 TEST_F(TextLineWrapperTest, HandlesIndent)
302 {
303     gmx::TextLineWrapper wrapper;
304     wrapper.settings().setIndent(2);
305
306     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
307     wrapper.settings().setLineLength(16);
308     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
309 }
310
311 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
312 {
313     gmx::TextLineWrapper wrapper;
314     wrapper.settings().setFirstLineIndent(2);
315     wrapper.settings().setIndent(4);
316
317     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
318     wrapper.settings().setLineLength(16);
319     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
320 }
321
322 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
323 {
324     gmx::TextLineWrapper wrapper;
325     wrapper.settings().setFirstLineIndent(2);
326     wrapper.settings().setIndent(4);
327     wrapper.settings().setContinuationChar('\\');
328
329     wrapper.settings().setLineLength(16);
330     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
331 }
332
333 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
334 {
335     gmx::TextLineWrapper wrapper;
336
337     wrapper.settings().setLineLength(14);
338     wrapper.settings().setStripLeadingWhitespace(true);
339     checkText(wrapper.wrapToString(g_wrapTextWhitespace),
340               "WrappedAt14StripLeading");
341     wrapper.settings().setStripLeadingWhitespace(false);
342     checkText(wrapper.wrapToString(g_wrapTextWhitespace),
343               "WrappedAt14PreserveLeading");
344 }
345
346 } // namespace