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