GRINS-0.8.0
string_utils.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // GRINS - General Reacting Incompressible Navier-Stokes
5 //
6 // Copyright (C) 2014-2017 Paul T. Bauman, Roy H. Stogner
7 // Copyright (C) 2010-2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include "grins_config.h"
26 
27 #ifdef GRINS_HAVE_CPPUNIT
28 
29 #include <libmesh/ignore_warnings.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/TestCase.h>
32 #include <libmesh/restore_warnings.h>
33 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 #include <limits>
38 
39 #include "grins/string_utils.h"
40 
41 // Ignore warnings from auto_ptr in CPPUNIT_TEST_SUITE_END()
42 #include <libmesh/ignore_warnings.h>
43 
44 namespace GRINSTesting
45 {
46  class StringUtilitiesTest : public CppUnit::TestCase
47  {
48  public:
50 
54 
56 
57  public:
58 
60  {
61  {
62  std::string str_1("N->N2");
63  std::vector<std::string> test_1_split_exact(2);
64  test_1_split_exact[0] = std::string("N");
65  test_1_split_exact[1] = std::string("N2");
66 
67  std::vector<std::string> str_1_split;
68  GRINS::StringUtilities::split_string( str_1, "->", str_1_split);
69  this->test_string( str_1_split, test_1_split_exact );
70  }
71 
72  {
73  std::string str_2("N+C(s)->CN");
74  std::vector<std::string> test_2_split_exact(2);
75  test_2_split_exact[0] = std::string("N+C(s)");
76  test_2_split_exact[1] = std::string("CN");
77 
78  std::vector<std::string> str_2_split;
79  GRINS::StringUtilities::split_string( str_2, "->", str_2_split);
80  this->test_string( str_2_split, test_2_split_exact );
81  }
82 
83  {
84  std::string str_3("u:v:w:T:p:w_N:w_N2:p0");
85  std::vector<std::string> test_3_split_exact(8);
86  test_3_split_exact[0] = std::string("u");
87  test_3_split_exact[1] = std::string("v");
88  test_3_split_exact[2] = std::string("w");
89  test_3_split_exact[3] = std::string("T");
90  test_3_split_exact[4] = std::string("p");
91  test_3_split_exact[5] = std::string("w_N");
92  test_3_split_exact[6] = std::string("w_N2");
93  test_3_split_exact[7] = std::string("p0");
94 
95  std::vector<std::string> str_3_split;
96  GRINS::StringUtilities::split_string( str_3, ":", str_3_split);
97  this->test_string( str_3_split, test_3_split_exact );
98  }
99 
100  {
101  std::string str_4("u v w T p w_N w_N2 p0");
102  std::vector<std::string> test_4_split_exact(8);
103  test_4_split_exact[0] = std::string("u");
104  test_4_split_exact[1] = std::string("v");
105  test_4_split_exact[2] = std::string("w");
106  test_4_split_exact[3] = std::string("T");
107  test_4_split_exact[4] = std::string("p");
108  test_4_split_exact[5] = std::string("w_N");
109  test_4_split_exact[6] = std::string("w_N2");
110  test_4_split_exact[7] = std::string("p0");
111 
112  std::vector<std::string> str_4_split;
113  GRINS::StringUtilities::split_string( str_4, " ", str_4_split);
114  this->test_string( str_4_split, test_4_split_exact );
115  }
116  }
117 
119  {
120  std::string one = "1";
121  int ione = GRINS::StringUtilities::string_to_T<int>(one);
122  unsigned int uione = GRINS::StringUtilities::string_to_T<unsigned int>(one);
123  CPPUNIT_ASSERT_EQUAL(1,ione);
124  CPPUNIT_ASSERT_EQUAL((unsigned int)1,uione);
125 
126  std::string tenp1 = "10.1";
127  double dtenp1 = GRINS::StringUtilities::string_to_T<double>(tenp1);
128  CPPUNIT_ASSERT_DOUBLES_EQUAL(10.1,
129  dtenp1,
130  std::numeric_limits<double>::epsilon());
131  }
132 
134  {
135  std::string one_exact = "1";
136  std::string tenp1_exact = "10.1";
137 
138  {
139  std::string one_test = GRINS::StringUtilities::T_to_string<int>(1);
140  CPPUNIT_ASSERT_EQUAL(one_exact,one_test);
141  }
142 
143  {
144  std::string one_test = GRINS::StringUtilities::T_to_string<unsigned int>(1);
145  CPPUNIT_ASSERT_EQUAL(one_exact,one_test);
146  }
147 
148  {
149  std::string tenp1_test = GRINS::StringUtilities::T_to_string<double>(10.1);
150  CPPUNIT_ASSERT_EQUAL(tenp1_exact,tenp1_test);
151  }
152  }
153 
154  private:
155 
156  void test_string( const std::vector<std::string>& test,
157  const std::vector<std::string>& exact )
158  {
159  CPPUNIT_ASSERT_EQUAL(test.size(), exact.size() );
160 
161  for( unsigned int s = 0; s < test.size(); s++ )
162  CPPUNIT_ASSERT_EQUAL( test[s], exact[s] );
163  }
164 
165  };
166 
167  CPPUNIT_TEST_SUITE_REGISTRATION( StringUtilitiesTest );
168 
169 } // end namespace GRINSTesting
170 
171 #endif // GRINS_HAVE_CPPUNIT
CPPUNIT_TEST_SUITE_REGISTRATION(AntiochAirNASA9ThermoTest)
int test(ChemicalMixture &chem_mixture)
void test_string(const std::vector< std::string > &test, const std::vector< std::string > &exact)
Definition: string_utils.C:156
CPPUNIT_TEST_SUITE(StringUtilitiesTest)
void split_string(const std::string &input, const std::string &delimiter, std::vector< std::string > &results)
Definition: string_utils.C:31

Generated on Tue Dec 19 2017 12:47:28 for GRINS-0.8.0 by  doxygen 1.8.9.1