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