clang-tidy: google tests applicable
[gromacs.git] / src / gromacs / utility / tests / stringutil.cpp
blob0fea8f8fd9d40f1bd35603364001d9e4c0e36660
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \internal \file
36 * \brief
37 * Tests for string utility functions and classes.
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.
43 * \author Teemu Murtola <teemu.murtola@gmail.com>
44 * \ingroup module_utility
46 #include "gmxpre.h"
48 #include "gromacs/utility/stringutil.h"
50 #include <string>
51 #include <vector>
53 #include <gmock/gmock.h>
54 #include <gtest/gtest.h>
56 #include "gromacs/utility/arrayref.h"
57 #include "gromacs/utility/exceptions.h"
59 #include "testutils/refdata.h"
60 #include "testutils/stringtest.h"
62 namespace gmx
64 namespace test
66 namespace
69 /********************************************************************
70 * Tests for simple string utilities
73 TEST(StringUtilityTest, StartsWith)
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"));
89 TEST(StringUtilityTest, EndsWith)
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"));
101 TEST(StringUtilityTest, StripSuffixIfPresent)
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"));
111 TEST(StringUtilityTest, StripString)
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 "));
120 TEST(StringUtilityTest, SplitString)
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());
134 TEST(StringUtilityTest, SplitDelimitedString)
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());
146 TEST(StringUtilityTest, SplitAndTrimDelimitedString)
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"));
166 /********************************************************************
167 * Tests for formatString()
170 TEST(FormatStringTest, HandlesBasicFormatting)
172 EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
175 TEST(FormatStringTest, HandlesLongStrings)
177 std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
178 EXPECT_EQ(2002U, longString.length());
179 EXPECT_EQ("x10", longString.substr(1999));
182 /********************************************************************
183 * Tests for StringFormatter
186 TEST(StringFormatterTest, HandlesBasicFormatting)
188 int value = 103;
189 EXPECT_EQ("103", gmx::StringFormatter("%d") (value));
190 EXPECT_EQ("null", gmx::StringFormatter("null") (value));
193 /********************************************************************
194 * Tests for formatAndJoin
197 TEST(formatAndJoinTest, Works)
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")));
204 const int values[] = { 0, 1, 4 };
205 EXPECT_EQ("0,1,4", gmx::formatAndJoin(gmx::ArrayRef<const int>(values), ",",
206 gmx::StringFormatter("%d")));
209 /********************************************************************
210 * Tests for joinStrings
213 TEST(JoinStringsTest, Works)
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, "-"));
222 /********************************************************************
223 * Tests for replaceAll() and replaceAllWords()
226 TEST(ReplaceAllTest, HandlesEmptyStrings)
228 EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
229 EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
232 TEST(ReplaceAllTest, HandlesNoMatches)
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"));
239 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
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"));
247 TEST(ReplaceAllTest, HandlesMultipleMatches)
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"));
256 TEST(ReplaceAllTest, HandlesWordBoundaries)
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"));
263 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
265 const std::string text("Text with recursive aaabbbbbb matches");
266 EXPECT_EQ("Text with recursive aaaaaabbb matches",
267 gmx::replaceAll(text, "aaabbb", "aaaaaa"));
270 /********************************************************************
271 * Tests for TextLineWrapper
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";
286 //! Test fixture for gmx::TextLineWrapper.
287 typedef gmx::test::StringTestBase TextLineWrapperTest;
289 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
291 gmx::TextLineWrapper wrapper;
293 EXPECT_EQ("", wrapper.wrapToString(""));
294 EXPECT_EQ("", wrapper.wrapToString(" "));
295 EXPECT_TRUE(wrapper.wrapToVector("").empty());
297 std::vector<std::string> wrapped(wrapper.wrapToVector(" "));
298 ASSERT_EQ(1U, wrapped.size());
299 EXPECT_EQ("", wrapped[0]);
303 TEST_F(TextLineWrapperTest, HandlesTrailingWhitespace)
305 gmx::TextLineWrapper wrapper;
307 EXPECT_EQ("line", wrapper.wrapToString("line "));
308 EXPECT_EQ("line\n", wrapper.wrapToString("line \n"));
310 wrapper.settings().setKeepFinalSpaces(true);
311 EXPECT_EQ("line ", wrapper.wrapToString("line "));
312 EXPECT_EQ("line \n", wrapper.wrapToString("line \n"));
315 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
317 gmx::TextLineWrapper wrapper;
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"));
325 std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
326 ASSERT_EQ(1U, wrapped.size());
327 EXPECT_EQ("line", wrapped[0]);
330 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
331 ASSERT_EQ(1U, wrapped.size());
332 EXPECT_EQ("line", wrapped[0]);
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]);
341 std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
342 ASSERT_EQ(1U, wrapped.size());
343 EXPECT_EQ("", wrapped[0]);
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]);
353 TEST_F(TextLineWrapperTest, WrapsCorrectly)
355 gmx::TextLineWrapper wrapper;
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");
368 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
370 gmx::TextLineWrapper wrapper;
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");
379 TEST_F(TextLineWrapperTest, HandlesIndent)
381 gmx::TextLineWrapper wrapper;
382 wrapper.settings().setIndent(2);
384 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
385 wrapper.settings().setLineLength(16);
386 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
389 TEST_F(TextLineWrapperTest, HandlesIndentWithEmptyLines)
391 gmx::TextLineWrapper wrapper;
392 wrapper.settings().setIndent(2);
394 checkText(wrapper.wrapToString(g_wrapText3), "WrappedWithNoLimit");
395 wrapper.settings().setLineLength(16);
396 checkText(wrapper.wrapToString(g_wrapText3), "WrappedAt14");
399 TEST_F(TextLineWrapperTest, HandlesHangingIndent)
401 gmx::TextLineWrapper wrapper;
402 wrapper.settings().setFirstLineIndent(2);
403 wrapper.settings().setIndent(4);
405 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
406 wrapper.settings().setLineLength(16);
407 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
410 TEST_F(TextLineWrapperTest, HandlesContinuationCharacter)
412 gmx::TextLineWrapper wrapper;
413 wrapper.settings().setFirstLineIndent(2);
414 wrapper.settings().setIndent(4);
415 wrapper.settings().setContinuationChar('\\');
417 wrapper.settings().setLineLength(16);
418 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14/12");
421 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
423 gmx::TextLineWrapper wrapper;
424 wrapper.settings().setLineLength(14);
426 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
427 "WrappedAt14");
429 wrapper.settings().setKeepFinalSpaces(true);
430 checkText(wrapper.wrapToString(g_wrapTextWhitespace),
431 "WrappedAt14WithTrailingWhitespace");
434 } // namespace
435 } // namespace test
436 } // namespace gmx