BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
stringutils.test.cpp File Reference

Implements class CLASS? More...

Include dependency graph for stringutils.test.cpp:

Go to the source code of this file.

Classes

class  StringUtilsTest
 

Functions

 TEST_F (StringUtilsTest, DoubleToString)
 
 TEST_F (StringUtilsTest, ParseSpaceSeparatedDoubles)
 Testing ParseSpaceSeparatedDoubles. More...
 
 TEST_F (StringUtilsTest, RemoveRepeatedSpaces)
 
 TEST_F (StringUtilsTest, ScientificDoubleToString)
 
 TEST_F (StringUtilsTest, SplitString)
 Testing SplitString method. More...
 
 TEST_F (StringUtilsTest, StringToDouble)
 Testing function StringToDouble. More...
 
 TEST_F (StringUtilsTest, StringToInteger)
 Testing function StringToDouble. More...
 
 TEST_F (StringUtilsTest, TrimWhiteSpace)
 Testing function TrimWhitespace. More...
 

Detailed Description

Implements class CLASS?

Homepage:\n http://www.bornagainproject.org
License:\n GNU General Public License v3 or higher (see COPYING)
Authors
Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)

Definition in file stringutils.test.cpp.

Function Documentation

◆ TEST_F() [1/8]

TEST_F ( StringUtilsTest  ,
DoubleToString   
)

Definition at line 40 of file stringutils.test.cpp.

41 {
43  const int precision = 4;
44  EXPECT_EQ(DoubleToString(0.0, precision), "0.0");
45  EXPECT_EQ(DoubleToString(1.001, precision), "1.001");
46  EXPECT_EQ(DoubleToString(1.0001, precision), "1.0");
47 }
MVVM_MODEL_EXPORT std::string DoubleToString(double input, int precision=12)
Returns string representation of double with given precision.

References ModelView ::Utils::DoubleToString().

Here is the call graph for this function:

◆ TEST_F() [2/8]

TEST_F ( StringUtilsTest  ,
ParseSpaceSeparatedDoubles   
)

Testing ParseSpaceSeparatedDoubles.

Definition at line 159 of file stringutils.test.cpp.

160 {
162  std::vector<double> data;
163 
164  EXPECT_TRUE(ParseSpaceSeparatedDoubles("").empty());
165  EXPECT_TRUE(ParseSpaceSeparatedDoubles(" ").empty());
166  EXPECT_TRUE(ParseSpaceSeparatedDoubles("a").empty());
167  EXPECT_TRUE(ParseSpaceSeparatedDoubles("a b").empty());
168 
169  ASSERT_EQ(ParseSpaceSeparatedDoubles("4.02").size(), 1u);
170  EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42")[0], 42.0);
171 
172  // this tests failing under MacOS
173  // ASSERT_EQ(ParseSpaceSeparatedDoubles("42aaa").size(), 1u);
174  // EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42aaa")[0], 42.0);
175 
176  EXPECT_EQ(ParseSpaceSeparatedDoubles("42,").size(), 1u);
177  EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42,")[0], 42.0);
178 
179  EXPECT_EQ(ParseSpaceSeparatedDoubles("42,43").size(), 1u);
180  EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42,43")[0], 42.0);
181 
182  EXPECT_EQ(ParseSpaceSeparatedDoubles("42 ,43").size(), 1u);
183  EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42 ,43")[0], 42.0);
184 
185  EXPECT_EQ(ParseSpaceSeparatedDoubles("42 43").size(), 2u);
186  EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42 43")[0], 42.0);
187  EXPECT_DOUBLE_EQ(ParseSpaceSeparatedDoubles("42 43")[1], 43.0);
188 }
MVVM_MODEL_EXPORT std::vector< double > ParseSpaceSeparatedDoubles(const std::string &str)
Parses string for double values and returns result as a vector.

References ModelView ::Utils::ParseSpaceSeparatedDoubles().

Here is the call graph for this function:

◆ TEST_F() [3/8]

TEST_F ( StringUtilsTest  ,
RemoveRepeatedSpaces   
)

Definition at line 60 of file stringutils.test.cpp.

61 {
63  EXPECT_EQ(RemoveRepeatedSpaces(std::string{}), std::string{});
64  EXPECT_EQ(RemoveRepeatedSpaces(" "), std::string{" "});
65  EXPECT_EQ(RemoveRepeatedSpaces("a"), std::string{"a"});
66  EXPECT_EQ(RemoveRepeatedSpaces(" a "), std::string{" a "});
67  EXPECT_EQ(RemoveRepeatedSpaces(" a "), std::string{" a "});
68  EXPECT_EQ(RemoveRepeatedSpaces("a bb ccc "), std::string{"a bb ccc "});
69 }
MVVM_MODEL_EXPORT std::string RemoveRepeatedSpaces(std::string str)
Removes repeating spaces for a string.

References ModelView ::Utils::RemoveRepeatedSpaces().

Here is the call graph for this function:

◆ TEST_F() [4/8]

TEST_F ( StringUtilsTest  ,
ScientificDoubleToString   
)

Definition at line 29 of file stringutils.test.cpp.

30 {
32  const int precision = 6;
33  EXPECT_EQ(ScientificDoubleToString(0.0, precision), "0.0e+00");
34  EXPECT_EQ(ScientificDoubleToString(1.0, precision), "1.0e+00");
35  EXPECT_EQ(ScientificDoubleToString(3.0 / 4.0, precision), "7.5e-01");
36  EXPECT_EQ(ScientificDoubleToString(4.0 / 3.0, precision), "1.333333e+00");
37  EXPECT_EQ(ScientificDoubleToString(1000000., precision), "1.0e+06");
38 }
MVVM_MODEL_EXPORT std::string ScientificDoubleToString(double input, int precision=6)
Returns string representation of scientific double.

References ModelView ::Utils::ScientificDoubleToString().

Here is the call graph for this function:

◆ TEST_F() [5/8]

TEST_F ( StringUtilsTest  ,
SplitString   
)

Testing SplitString method.

Carefully checking that it is reproduces Python behavior, as promised in comments to the method.

Definition at line 130 of file stringutils.test.cpp.

131 {
132  using Utils::SplitString;
133 
134  EXPECT_THROW(SplitString("", ""), std::runtime_error);
135  EXPECT_EQ(SplitString("", " "), toVector<std::string>());
136  EXPECT_EQ(SplitString("", ","), toVector<std::string>());
137  EXPECT_EQ(SplitString(" ", " "), toVector<std::string>("", ""));
138  EXPECT_EQ(SplitString("a", " "), toVector<std::string>("a"));
139  EXPECT_EQ(SplitString("a ", " "), toVector<std::string>("a", ""));
140 
141  EXPECT_EQ(SplitString("a b", " "), toVector<std::string>("a", "b"));
142  EXPECT_EQ(SplitString("a b", " "), toVector<std::string>("a", "", "b"));
143 
144  EXPECT_EQ(SplitString("a", "-"), toVector<std::string>("a"));
145 
146  EXPECT_EQ(SplitString("aa", "a"), toVector<std::string>("", "", ""));
147 
148  EXPECT_EQ(SplitString("a,b", ","), toVector<std::string>("a", "b"));
149  EXPECT_EQ(SplitString("a, b", ","), toVector<std::string>("a", " b"));
150 
151  EXPECT_EQ(SplitString("a,b,", ","), toVector<std::string>("a", "b", ""));
152  EXPECT_EQ(SplitString(",a,b,", ","), toVector<std::string>("", "a", "b", ""));
153  EXPECT_EQ(SplitString("aabbcc", "bb"), toVector<std::string>("aa", "cc"));
154  EXPECT_EQ(SplitString("aabbcc", "bb"), toVector<std::string>("aa", "cc"));
155 }
MVVM_MODEL_EXPORT std::vector< std::string > SplitString(const std::string &str, const std::string &delimeter)
Split string on substring using given delimeter. Reproduces Python's str.split() behavior.

References ModelView ::Utils::SplitString().

Here is the call graph for this function:

◆ TEST_F() [6/8]

TEST_F ( StringUtilsTest  ,
StringToDouble   
)

Testing function StringToDouble.

Definition at line 73 of file stringutils.test.cpp.

74 {
76 
77  // not a double
78  EXPECT_FALSE(StringToDouble("").has_value());
79  EXPECT_FALSE(StringToDouble(" ").has_value());
80  EXPECT_FALSE(StringToDouble("a").has_value());
81  EXPECT_FALSE(StringToDouble("a b").has_value());
82 
83  // not a double: some mixture present
84  EXPECT_FALSE(StringToDouble("42a").has_value());
85  EXPECT_FALSE(StringToDouble("42.5.5").has_value());
86 
87  // not a double: more than one double
88  EXPECT_FALSE(StringToDouble("42.5 52").has_value());
89 
90  // valid double
91  EXPECT_TRUE(StringToDouble("42").has_value());
92  EXPECT_TRUE(StringToDouble(" 42").has_value());
93  EXPECT_TRUE(StringToDouble(" 42 ").has_value());
94  EXPECT_DOUBLE_EQ(StringToDouble("42").value(), 42.0);
95  EXPECT_TRUE(StringToDouble("42.5").has_value());
96  EXPECT_DOUBLE_EQ(StringToDouble("42.5").value(), 42.5);
97  EXPECT_TRUE(StringToDouble("-1.12e-06").has_value());
98  EXPECT_DOUBLE_EQ(StringToDouble("-1.12e-06").value(), -1.12e-06);
99 }
MVVM_MODEL_EXPORT std::optional< double > StringToDouble(const std::string &str)
Converts string to double value using classc locale and returns it in the form of optional.

References ModelView ::Utils::StringToDouble().

Here is the call graph for this function:

◆ TEST_F() [7/8]

TEST_F ( StringUtilsTest  ,
StringToInteger   
)

Testing function StringToDouble.

Definition at line 103 of file stringutils.test.cpp.

104 {
106 
107  // not an int
108  EXPECT_FALSE(StringToInteger("").has_value());
109  EXPECT_FALSE(StringToInteger(" ").has_value());
110  EXPECT_FALSE(StringToInteger("a").has_value());
111  EXPECT_FALSE(StringToInteger("a b").has_value());
112 
113  // not an int: some mixture present
114  EXPECT_FALSE(StringToInteger("42a").has_value());
115  EXPECT_FALSE(StringToInteger("42.5").has_value());
116 
117  // not an int: more than one number
118  EXPECT_FALSE(StringToInteger("42.5 52").has_value());
119 
120  // valid int
121  EXPECT_TRUE(StringToInteger("42").has_value());
122  EXPECT_TRUE(StringToInteger(" 42").has_value());
123  EXPECT_TRUE(StringToInteger(" 42 ").has_value());
124  EXPECT_EQ(StringToInteger("42").value(), 42);
125 }
MVVM_MODEL_EXPORT std::optional< int > StringToInteger(const std::string &str)
Converts string to integer.

References ModelView ::Utils::StringToInteger().

Here is the call graph for this function:

◆ TEST_F() [8/8]

TEST_F ( StringUtilsTest  ,
TrimWhiteSpace   
)

Testing function TrimWhitespace.

Definition at line 51 of file stringutils.test.cpp.

52 {
54  EXPECT_EQ(TrimWhitespace(""), std::string());
55  EXPECT_EQ(TrimWhitespace(" "), std::string());
56  EXPECT_EQ(TrimWhitespace("abc"), std::string("abc"));
57  EXPECT_EQ(TrimWhitespace(" \t\n abc cde\n"), std::string("abc cde"));
58 }
MVVM_MODEL_EXPORT std::string TrimWhitespace(const std::string &str)
Returns string after trimming whitespace surrounding, including tabs and carriage returns.

References ModelView ::Utils::TrimWhitespace().

Here is the call graph for this function: