Merge release-4-6 into master
[alexxy/gromacs.git] / src / gromacs / utility / tests / stringutil.cpp
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
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.
13
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.
18  *
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.
25  *
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.
28  *
29  * For more info, check our website at http://www.gromacs.org
30  */
31 /*! \internal \file
32  * \brief
33  * Tests for string utility functions and classes.
34  *
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
37  * framework.
38  *
39  * \author Teemu Murtola <teemu.murtola@cbr.su.se>
40  * \ingroup module_utility
41  */
42 #include <string>
43 #include <vector>
44
45 #include <gtest/gtest.h>
46
47 #include "gromacs/utility/stringutil.h"
48
49 #include "testutils/refdata.h"
50 #include "testutils/stringtest.h"
51
52 namespace
53 {
54
55 /********************************************************************
56  * Tests for simple string utilities
57  */
58
59 TEST(StringUtilityTest, StartsWithWorks)
60 {
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"));
73 }
74
75 TEST(StringUtilityTest, EndsWithWorks)
76 {
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"));
85 }
86
87 TEST(StringUtilityTest, StripSuffixIfPresent)
88 {
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"));
95 }
96
97 /********************************************************************
98  * Tests for formatString()
99  */
100
101 TEST(FormatStringTest, HandlesBasicFormatting)
102 {
103     EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
104 }
105
106 TEST(FormatStringTest, HandlesLongStrings)
107 {
108     std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
109     EXPECT_EQ(2002U, longString.length());
110     EXPECT_EQ("x10", longString.substr(1999));
111 }
112
113 /********************************************************************
114  * Tests for concatenateStrings()
115  */
116
117 typedef gmx::test::StringTestBase ConcatenateStringsTest;
118
119 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
120 {
121     static const char * const strings[] = {
122         "First string",
123         "Second string ",
124         "Third string\n",
125         "Fourth string",
126         ""
127     };
128     checkText(gmx::concatenateStrings(strings), "CombinedStrings");
129 }
130
131 /********************************************************************
132  * Tests for replaceAll() and replaceAllWords()
133  */
134
135 TEST(ReplaceAllTest, HandlesEmptyStrings)
136 {
137     EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
138     EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
139 }
140
141 TEST(ReplaceAllTest, HandlesNoMatches)
142 {
143     const std::string text("Text with no matches");
144     EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
145     EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
146 }
147
148 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
149 {
150     EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
151     EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
152     EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
153     EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
154 }
155
156 TEST(ReplaceAllTest, HandlesMultipleMatches)
157 {
158     const std::string text("Text aaa with multiple aaa matches");
159     EXPECT_EQ("Text bbbb with multiple bbbb matches",
160               gmx::replaceAll(text, "aaa", "bbbb"));
161     EXPECT_EQ("Text bbbb with multiple bbbb matches",
162               gmx::replaceAllWords(text, "aaa", "bbbb"));
163 }
164
165 TEST(ReplaceAllTest, HandlesWordBoundaries)
166 {
167     const std::string text("Text aaax with one word aaa match");
168     EXPECT_EQ("Text aaax with one word bbbb match",
169               gmx::replaceAllWords(text, "aaa", "bbbb"));
170 }
171
172 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
173 {
174     const std::string text("Text with recursive aaabbbbbb matches");
175     EXPECT_EQ("Text with recursive aaaaaabbb matches",
176               gmx::replaceAll(text, "aaabbb", "aaaaaa"));
177 }
178
179 /********************************************************************
180  * Tests for TextLineWrapper
181  */
182
183 //! Simple test string for wrapping.
184 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
185 //! Test string for wrapping with embedded line breaks.
186 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
187 //! Test string for wrapping with a long word.
188 const char g_wrapTextLongWord[]
189     = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
190 //! Test string for wrapping with extra whitespace.
191 const char g_wrapTextWhitespace[] = " A quick brown   fox jumps  \n over the lazy dog";
192
193 typedef gmx::test::StringTestBase TextLineWrapperTest;
194
195 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
196 {
197     gmx::TextLineWrapper wrapper;
198
199     EXPECT_EQ("", wrapper.wrapToString(""));
200     EXPECT_EQ("", wrapper.wrapToString("   "));
201     EXPECT_TRUE(wrapper.wrapToVector("").empty());
202     EXPECT_TRUE(wrapper.wrapToString("   ").empty());
203 }
204
205 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
206 {
207     gmx::TextLineWrapper wrapper;
208
209     EXPECT_EQ("line", wrapper.wrapToString("line"));
210     EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
211     EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
212     EXPECT_EQ("\n", wrapper.wrapToString("\n"));
213     EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
214     {
215         std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
216         ASSERT_EQ(1U, wrapped.size());
217         EXPECT_EQ("line", wrapped[0]);
218     }
219     {
220         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
221         ASSERT_EQ(1U, wrapped.size());
222         EXPECT_EQ("line", wrapped[0]);
223     }
224     {
225         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
226         ASSERT_EQ(2U, wrapped.size());
227         EXPECT_EQ("line", wrapped[0]);
228         EXPECT_EQ("", wrapped[1]);
229     }
230     {
231         std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
232         ASSERT_EQ(1U, wrapped.size());
233         EXPECT_EQ("", wrapped[0]);
234     }
235     {
236         std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
237         ASSERT_EQ(2U, wrapped.size());
238         EXPECT_EQ("", wrapped[0]);
239         EXPECT_EQ("", wrapped[1]);
240     }
241 }
242
243 TEST_F(TextLineWrapperTest, WrapsCorrectly)
244 {
245     gmx::TextLineWrapper wrapper;
246
247     wrapper.settings().setLineLength(10);
248     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
249     std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
250     checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
251     wrapper.settings().setLineLength(13);
252     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
253     wrapper.settings().setLineLength(14);
254     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
255     checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
256 }
257
258 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
259 {
260     gmx::TextLineWrapper wrapper;
261
262     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
263     wrapper.settings().setLineLength(10);
264     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
265     wrapper.settings().setLineLength(14);
266     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
267 }
268
269 TEST_F(TextLineWrapperTest, HandlesIndent)
270 {
271     gmx::TextLineWrapper wrapper;
272     wrapper.settings().setIndent(2);
273
274     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
275     wrapper.settings().setLineLength(16);
276     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
277 }
278
279 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
280 {
281     gmx::TextLineWrapper wrapper;
282     wrapper.settings().setFirstLineIndent(2);
283     wrapper.settings().setIndent(4);
284
285     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
286     wrapper.settings().setLineLength(16);
287     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
288 }
289
290 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
291 {
292     gmx::TextLineWrapper wrapper;
293     wrapper.settings().setFirstLineIndent(2);
294     wrapper.settings().setIndent(4);
295     wrapper.settings().setContinuationChar('\\');
296
297     wrapper.settings().setLineLength(16);
298     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
299 }
300
301 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
302 {
303     gmx::TextLineWrapper wrapper;
304
305     wrapper.settings().setLineLength(14);
306     wrapper.settings().setStripLeadingWhitespace(true);
307     checkText(wrapper.wrapToString(g_wrapTextWhitespace),
308               "WrappedAt14StripLeading");
309     wrapper.settings().setStripLeadingWhitespace(false);
310     checkText(wrapper.wrapToString(g_wrapTextWhitespace),
311               "WrappedAt14PreserveLeading");
312 }
313
314 } // namespace