3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
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.
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.
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.
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.
29 * For more info, check our website at http://www.gromacs.org
33 * Tests for string utility functions and classes.
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
39 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
40 * \ingroup module_utility
45 #include <gtest/gtest.h>
47 #include "gromacs/utility/stringutil.h"
49 #include "testutils/refdata.h"
50 #include "testutils/stringtest.h"
55 /********************************************************************
56 * Tests for formatString()
59 TEST(FormatStringTest, HandlesBasicFormatting)
61 EXPECT_EQ("12 abc", gmx::formatString("%d %s", 12, "abc"));
64 TEST(FormatStringTest, HandlesLongStrings)
66 std::string longString = gmx::formatString("%*c%d", 2000, 'x', 10);
67 EXPECT_EQ(2002U, longString.length());
68 EXPECT_EQ("x10", longString.substr(1999));
71 /********************************************************************
72 * Tests for concatenateStrings()
75 typedef gmx::test::StringTestBase ConcatenateStringsTest;
77 TEST_F(ConcatenateStringsTest, HandlesDifferentStringEndings)
79 static const char * const strings[] = {
86 checkText(gmx::concatenateStrings(strings), "CombinedStrings");
89 /********************************************************************
90 * Tests for replaceAll() and replaceAllWords()
93 TEST(ReplaceAllTest, HandlesEmptyStrings)
95 EXPECT_EQ("", gmx::replaceAll("", "aaa", "bbbb"));
96 EXPECT_EQ("", gmx::replaceAllWords("", "aaa", "bbbb"));
99 TEST(ReplaceAllTest, HandlesNoMatches)
101 const std::string text("Text with no matches");
102 EXPECT_EQ(text, gmx::replaceAll(text, "aaa", "bbbb"));
103 EXPECT_EQ(text, gmx::replaceAllWords(text, "aaa", "bbbb"));
106 TEST(ReplaceAllTest, HandlesMatchesAtEnds)
108 EXPECT_EQ("bbbbtext", gmx::replaceAll("aaatext", "aaa", "bbbb"));
109 EXPECT_EQ("textbbbb", gmx::replaceAll("textaaa", "aaa", "bbbb"));
110 EXPECT_EQ("bbbb text", gmx::replaceAllWords("aaa text", "aaa", "bbbb"));
111 EXPECT_EQ("text bbbb", gmx::replaceAllWords("text aaa", "aaa", "bbbb"));
114 TEST(ReplaceAllTest, HandlesMultipleMatches)
116 const std::string text("Text aaa with multiple aaa matches");
117 EXPECT_EQ("Text bbbb with multiple bbbb matches",
118 gmx::replaceAll(text, "aaa", "bbbb"));
119 EXPECT_EQ("Text bbbb with multiple bbbb matches",
120 gmx::replaceAllWords(text, "aaa", "bbbb"));
123 TEST(ReplaceAllTest, HandlesWordBoundaries)
125 const std::string text("Text aaax with one word aaa match");
126 EXPECT_EQ("Text aaax with one word bbbb match",
127 gmx::replaceAllWords(text, "aaa", "bbbb"));
130 TEST(ReplaceAllTest, HandlesPossibleRecursiveMatches)
132 const std::string text("Text with recursive aaabbbbbb matches");
133 EXPECT_EQ("Text with recursive aaaaaabbb matches",
134 gmx::replaceAll(text, "aaabbb", "aaaaaa"));
137 /********************************************************************
138 * Tests for TextLineWrapper
141 const char g_wrapText[] = "A quick brown fox jumps over the lazy dog";
142 const char g_wrapText2[] = "A quick brown fox jumps\nover the lazy dog";
143 const char g_wrapTextLongWord[]
144 = "A quick brown fox jumps awordthatoverflowsaline over the lazy dog";
145 const char g_wrapTextWhitespace[] = " A quick brown fox jumps \n over the lazy dog";
147 typedef gmx::test::StringTestBase TextLineWrapperTest;
149 TEST_F(TextLineWrapperTest, HandlesEmptyStrings)
151 gmx::TextLineWrapper wrapper;
153 EXPECT_EQ("", wrapper.wrapToString(""));
154 EXPECT_EQ("", wrapper.wrapToString(" "));
155 EXPECT_TRUE(wrapper.wrapToVector("").empty());
156 EXPECT_TRUE(wrapper.wrapToString(" ").empty());
159 TEST_F(TextLineWrapperTest, HandlesTrailingNewlines)
161 gmx::TextLineWrapper wrapper;
163 EXPECT_EQ("line", wrapper.wrapToString("line"));
164 EXPECT_EQ("line\n", wrapper.wrapToString("line\n"));
165 EXPECT_EQ("line\n\n", wrapper.wrapToString("line\n\n"));
166 EXPECT_EQ("\n", wrapper.wrapToString("\n"));
167 EXPECT_EQ("\n\n", wrapper.wrapToString("\n\n"));
169 std::vector<std::string> wrapped(wrapper.wrapToVector("line"));
170 ASSERT_EQ(1U, wrapped.size());
171 EXPECT_EQ("line", wrapped[0]);
174 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n"));
175 ASSERT_EQ(1U, wrapped.size());
176 EXPECT_EQ("line", wrapped[0]);
179 std::vector<std::string> wrapped(wrapper.wrapToVector("line\n\n"));
180 ASSERT_EQ(2U, wrapped.size());
181 EXPECT_EQ("line", wrapped[0]);
182 EXPECT_EQ("", wrapped[1]);
185 std::vector<std::string> wrapped(wrapper.wrapToVector("\n"));
186 ASSERT_EQ(1U, wrapped.size());
187 EXPECT_EQ("", wrapped[0]);
190 std::vector<std::string> wrapped(wrapper.wrapToVector("\n\n"));
191 ASSERT_EQ(2U, wrapped.size());
192 EXPECT_EQ("", wrapped[0]);
193 EXPECT_EQ("", wrapped[1]);
197 TEST_F(TextLineWrapperTest, WrapsCorrectly)
199 gmx::TextLineWrapper wrapper;
201 wrapper.setLineLength(10);
202 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt10");
203 std::vector<std::string> wrapped(wrapper.wrapToVector(g_wrapText));
204 checker().checkSequence(wrapped.begin(), wrapped.end(), "WrappedToVector");
205 wrapper.setLineLength(13);
206 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt13");
207 wrapper.setLineLength(14);
208 checkText(wrapper.wrapToString(g_wrapText), "WrappedAt14");
209 checkText(wrapper.wrapToString(g_wrapTextLongWord), "WrappedWithLongWord");
212 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExistingBreaks)
214 gmx::TextLineWrapper wrapper;
216 checkText(wrapper.wrapToString(g_wrapText2), "WrappedWithNoLimit");
217 wrapper.setLineLength(10);
218 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt10");
219 wrapper.setLineLength(14);
220 checkText(wrapper.wrapToString(g_wrapText2), "WrappedAt14");
223 TEST_F(TextLineWrapperTest, WrapsCorrectlyWithExtraWhitespace)
225 gmx::TextLineWrapper wrapper;
227 wrapper.setLineLength(14);
228 checkText(wrapper.wrapToString(g_wrapTextWhitespace), "WrappedAt14");