SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / utility / tests / textreader.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2017,2018,2019,2021, 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 gmx::TextReader.
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  * \ingroup module_utility
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/utility/textreader.h"
45
46 #include <functional>
47 #include <string>
48 #include <vector>
49
50 #include <gmock/gmock.h>
51 #include <gtest/gtest.h>
52
53 #include "gromacs/utility/basedefinitions.h"
54 #include "gromacs/utility/stringstream.h"
55 #include "gromacs/utility/stringutil.h"
56
57 #include "testutils/refdata.h"
58 #include "testutils/stringtest.h"
59
60 namespace gmx
61 {
62 namespace test
63 {
64 namespace
65 {
66
67 //! Convenience name.
68 using Container = std::vector<std::string>;
69 //! Convenience type for callbacks.
70 using TestCallbackFunc = void (*)(TextReader&);
71
72 //! Helper struct.
73 struct TextReaderTestParams
74 {
75     //! Input data.
76     const Container input;
77     //! Callback to configure the reader with the behaviour being tested.
78     TestCallbackFunc callback;
79     //! Output to expect from the configured reader acting on the \c input.
80     const Container expectedOutput;
81 };
82
83 //! Test fixture.
84 class TextReaderTest : public ::testing::TestWithParam<TextReaderTestParams>
85 {
86 };
87
88 TEST_P(TextReaderTest, UsingDifferentConfigurations)
89 {
90     const auto& params = GetParam();
91
92     // Prepare the reader with the input lines.
93     StringInputStream stream(params.input);
94     TextReader        reader(&stream);
95     // Configure the intended reading behaviour.
96     params.callback(reader);
97     // Read the input and store what is read.
98     Container   readLines;
99     std::string line;
100     while (reader.readLine(&line))
101     {
102         readLines.push_back(line);
103     }
104     // Check the results
105     EXPECT_THAT(readLines, ::testing::ElementsAreArray(params.expectedOutput));
106 }
107
108 //! Test input data. Some configurations will remove comments delimited by '#'.
109 const Container g_inputs = {
110     "",
111     " \t ",
112     "expected text",
113     " expected text ",
114     "expected text \t",
115     " \t expected text",
116     " \t expected text \t",
117     "expected text#",
118     "expected text\t #",
119     "expected text# ",
120     "expected text   # not expected ",
121     "#",
122     "\t #",
123     "   # not expected ",
124 };
125
126 /*! \brief A case of expected output data that is produced from two
127  * different configurations.
128  *
129  * Note that the implementation of StringInputStream joins the input
130  * container with "\n", so the inputs are always changed before being
131  * read. The name of this variable reflects that TextReader does not
132  * change them during reading. */
133 const Container g_unchangedOutputs = {
134     "\n",
135     " \t \n",
136     "expected text\n",
137     " expected text \n",
138     "expected text \t\n",
139     " \t expected text\n",
140     " \t expected text \t\n",
141     "expected text#\n",
142     "expected text\t #\n",
143     "expected text# \n",
144     "expected text   # not expected \n",
145     "#\n",
146     "\t #\n",
147     "   # not expected \n",
148 };
149 INSTANTIATE_TEST_SUITE_P(
150         ParsesLinesDifferently,
151         TextReaderTest,
152         ::testing::Values(
153                 TextReaderTestParams{ g_inputs, [](TextReader& r) { GMX_UNUSED_VALUE(r); }, g_unchangedOutputs },
154                 TextReaderTestParams{ g_inputs,
155                                       [](TextReader& r) { r.setTrimLeadingWhiteSpace(true); },
156                                       {
157                                               "",
158                                               "",
159                                               "expected text\n",
160                                               "expected text \n",
161                                               "expected text \t\n",
162                                               "expected text\n",
163                                               "expected text \t\n",
164                                               "expected text#\n",
165                                               "expected text\t #\n",
166                                               "expected text# \n",
167                                               "expected text   # not expected \n",
168                                               "#\n",
169                                               "#\n",
170                                               "# not expected \n",
171                                       } },
172                 TextReaderTestParams{ g_inputs,
173                                       [](TextReader& r) { r.setTrimTrailingWhiteSpace(true); },
174                                       {
175                                               "",
176                                               "",
177                                               "expected text",
178                                               " expected text",
179                                               "expected text",
180                                               " \t expected text",
181                                               " \t expected text",
182                                               "expected text#",
183                                               "expected text\t #",
184                                               "expected text#",
185                                               "expected text   # not expected",
186                                               "#",
187                                               "\t #",
188                                               "   # not expected",
189                                       } },
190                 TextReaderTestParams{ g_inputs,
191                                       [](TextReader& r) {
192                                           r.setTrimTrailingWhiteSpace(true);
193                                           r.setTrimLeadingWhiteSpace(true);
194                                       },
195                                       {
196                                               "",
197                                               "",
198                                               "expected text",
199                                               "expected text",
200                                               "expected text",
201                                               "expected text",
202                                               "expected text",
203                                               "expected text#",
204                                               "expected text\t #",
205                                               "expected text#",
206                                               "expected text   # not expected",
207                                               "#",
208                                               "#",
209                                               "# not expected",
210                                       } },
211                 TextReaderTestParams{ g_inputs,
212                                       [](TextReader& r) { r.setTrimTrailingComment(true, '#'); },
213                                       {
214                                               "\n",
215                                               " \t \n",
216                                               "expected text\n",
217                                               " expected text \n",
218                                               "expected text \t\n",
219                                               " \t expected text\n",
220                                               " \t expected text \t\n",
221                                               "expected text",
222                                               "expected text\t ",
223                                               "expected text",
224                                               "expected text   ",
225                                               "",
226                                               "\t ",
227                                               "   ",
228                                       } },
229                 TextReaderTestParams{ g_inputs,
230                                       [](TextReader& r) {
231                                           r.setTrimTrailingComment(true, '#');
232                                           r.setTrimTrailingComment(false, 0);
233                                       },
234                                       g_unchangedOutputs },
235                 TextReaderTestParams{ g_inputs,
236                                       [](TextReader& r) {
237                                           r.setTrimTrailingComment(true, '#');
238                                           r.setTrimTrailingWhiteSpace(true);
239                                       },
240                                       {
241                                               "",
242                                               "",
243                                               "expected text",
244                                               " expected text",
245                                               "expected text",
246                                               " \t expected text",
247                                               " \t expected text",
248                                               "expected text",
249                                               "expected text",
250                                               "expected text",
251                                               "expected text",
252                                               "",
253                                               "",
254                                               "",
255                                       } }));
256
257 } // namespace
258 } // namespace test
259 } // namespace gmx