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