Merge remote-tracking branch 'gerrit/release-4-6'
[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, EndsWithWorks)
60 {
61     EXPECT_TRUE(gmx::endsWith("foobar", "bar"));
62     EXPECT_TRUE(gmx::endsWith("foobar", NULL));
63     EXPECT_TRUE(gmx::endsWith("foobar", ""));
64     EXPECT_FALSE(gmx::endsWith("foobar", "bbar"));
65     EXPECT_FALSE(gmx::endsWith("foobar", "barr"));
66     EXPECT_FALSE(gmx::endsWith("foobar", "foofoobar"));
67 }
68
69 TEST(StringUtilityTest, StripSuffixIfPresent)
70 {
71     EXPECT_EQ("foo", gmx::stripSuffixIfPresent("foobar", "bar"));
72     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", NULL));
73     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", ""));
74     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "bbar"));
75     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "barr"));
76     EXPECT_EQ("foobar", gmx::stripSuffixIfPresent("foobar", "foofoobar"));
77 }
78
79 /********************************************************************
80  * Tests for formatString()
81  */
82
83 TEST(FormatStringTest, HandlesBasicFormatting)
84 {
85     EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
86 }
87
88 TEST(FormatStringTest, HandlesLongStrings)
89 {
90     std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
91     EXPECT_EQ(2002U, longString.length());
92     EXPECT_EQ("x10", longString.substr(1999));
93 }
94
95 /********************************************************************
96  * Tests for concatenateStrings()
97  */
98
99 typedef gmx::test::StringTestBase ConcatenateStringsTest;
100
101 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
102 {
103     static const char * const strings[] = {
104         "First string",
105         "Second string ",
106         "Third string\n",
107         "Fourth string",
108         ""
109     };
110     checkText(gmx::concatenateStrings(strings), "CombinedStrings");
111 }
112
113 /********************************************************************
114  * Tests for replaceAll() and replaceAllWords()
115  */
116
117 TEST(ReplaceAllTest, HandlesEmptyStrings)
118 {
119     EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
120     EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
121 }
122
123 TEST(ReplaceAllTest, HandlesNoMatches)
124 {
125     const std::string text("Text with no matches");
126     EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
127     EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
128 }
129
130 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
131 {
132     EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
133     EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
134     EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
135     EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
136 }
137
138 TEST(ReplaceAllTest, HandlesMultipleMatches)
139 {
140     const std::string text("Text aaa with multiple aaa matches");
141     EXPECT_EQ("Text bbbb with multiple bbbb matches",
142               gmx::replaceAll(text, "aaa", "bbbb"));
143     EXPECT_EQ("Text bbbb with multiple bbbb matches",
144               gmx::replaceAllWords(text, "aaa", "bbbb"));
145 }
146
147 TEST(ReplaceAllTest, HandlesWordBoundaries)
148 {
149     const std::string text("Text aaax with one word aaa match");
150     EXPECT_EQ("Text aaax with one word bbbb match",
151               gmx::replaceAllWords(text, "aaa", "bbbb"));
152 }
153
154 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
155 {
156     const std::string text("Text with recursive aaabbbbbb matches");
157     EXPECT_EQ("Text with recursive aaaaaabbb matches",
158               gmx::replaceAll(text, "aaabbb", "aaaaaa"));
159 }
160
161 /********************************************************************
162  * Tests for TextLineWrapper
163  */
164
165 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
166 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
167 const char g_wrapTextLongWord[]
168     = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
169 const char g_wrapTextWhitespace[] = " A quick brown   fox jumps  \n over the lazy dog";
170
171 typedef gmx::test::StringTestBase TextLineWrapperTest;
172
173 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
174 {
175     gmx::TextLineWrapper wrapper;
176
177     EXPECT_EQ("", wrapper.wrapToString(""));
178     EXPECT_EQ("", wrapper.wrapToString("   "));
179     EXPECT_TRUE(wrapper.wrapToVector("").empty());
180     EXPECT_TRUE(wrapper.wrapToString("   ").empty());
181 }
182
183 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
184 {
185     gmx::TextLineWrapper wrapper;
186
187     EXPECT_EQ("line", wrapper.wrapToString("line"));
188     EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
189     EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
190     EXPECT_EQ("\n", wrapper.wrapToString("\n"));
191     EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
192     {
193         std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
194         ASSERT_EQ(1U, wrapped.size());
195         EXPECT_EQ("line", wrapped[0]);
196     }
197     {
198         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
199         ASSERT_EQ(1U, wrapped.size());
200         EXPECT_EQ("line", wrapped[0]);
201     }
202     {
203         std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
204         ASSERT_EQ(2U, wrapped.size());
205         EXPECT_EQ("line", wrapped[0]);
206         EXPECT_EQ("", wrapped[1]);
207     }
208     {
209         std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
210         ASSERT_EQ(1U, wrapped.size());
211         EXPECT_EQ("", wrapped[0]);
212     }
213     {
214         std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
215         ASSERT_EQ(2U, wrapped.size());
216         EXPECT_EQ("", wrapped[0]);
217         EXPECT_EQ("", wrapped[1]);
218     }
219 }
220
221 TEST_F(TextLineWrapperTest, WrapsCorrectly)
222 {
223     gmx::TextLineWrapper wrapper;
224
225     wrapper.setLineLength(10);
226     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
227     std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
228     checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
229     wrapper.setLineLength(13);
230     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
231     wrapper.setLineLength(14);
232     checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
233     checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
234 }
235
236 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
237 {
238     gmx::TextLineWrapper wrapper;
239
240     checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
241     wrapper.setLineLength(10);
242     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
243     wrapper.setLineLength(14);
244     checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
245 }
246
247 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
248 {
249     gmx::TextLineWrapper wrapper;
250
251     wrapper.setLineLength(14);
252     checkText(wrapper.wrapToString(g_wrapTextWhitespace), "WrappedAt14");
253 }
254
255 } // namespace