QMCPACK
test_ModernStringUtils.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "catch.hpp"
13 #include "ModernStringUtils.hpp"
14 #include <limits>
15 
16 /** \file
17  */
18 namespace qmcplusplus
19 {
20 
21 TEST_CASE("ModernStringUtils_strToLower", "[utilities]")
22 {
23  std::string test_string("IamAMixedCaseTest_String");
24  std::string lcase_string = lowerCase(test_string);
25  CHECK(lcase_string == "iamamixedcasetest_string");
26  std::string i_am_not_just_ascii{"\xab"
27  "not_Just_ASCII"};
28  // very imcomplete check if we are in a c locale that clobbers char beyond ASII
29  // \todo If you know C locales well is this ever a worry?
30  i_am_not_just_ascii = lowerCase(i_am_not_just_ascii);
31  CHECK(i_am_not_just_ascii ==
32  "\xab"
33  "not_just_ascii");
34 }
35 
36 TEST_CASE("ModernStringUtils_split", "[utilities]")
37 {
39 
40  std::string nothing;
41  auto no_tokens = split(nothing, " ");
42  CHECK(no_tokens.empty());
43  no_tokens = split(nothing, "");
44  CHECK(no_tokens.empty());
45 
46  std::string white_space{" "};
47  auto tokens = split(white_space, ".");
48  CHECK(tokens.size() == 1);
49  CHECK(tokens[0] == " ");
50 
51  tokens = split(white_space, " ");
52  CHECK(tokens.empty());
53 
54  std::string test_line{"hi there 101, random line"};
55  tokens = split(test_line, " ");
56  CHECK(tokens[0].size() == 2);
57  CHECK(tokens[4].size() == 4);
58  CHECK(tokens[3] == "random");
59 
60  tokens = split(test_line, "");
61  CHECK(tokens.size() == 1);
62 
63  tokens = split(test_line, ";");
64  CHECK(tokens.size() == 1);
65 
66  std::string test_lines{R"(
67 this is a multi
68 line
69 token test
70 )"};
71  auto tokens_lines = split(test_lines, "\n");
72  CHECK(tokens_lines[0] == "this is a multi");
73  CHECK(tokens_lines[1] == "line");
74  CHECK(tokens_lines[2] == "token test");
75 
76  std::string test_multidel{"this \t is a multidelimiter \n \n token test"};
77  auto tokens_multidel = split(test_multidel, "\t \n");
78  CHECK(tokens_multidel[0] == "this");
79  CHECK(tokens_multidel[1] == "is");
80  CHECK(tokens_multidel[4] == "token");
81 }
82 
83 TEST_CASE("ModernStringUtils_string2Real", "[utilities]")
84 {
85  std::string_view svalue{"101.52326626"};
86  double value = string2Real<double>(svalue);
87  CHECK(value == Approx(101.52326626));
88 } // namespace qmcplusplus
89 
90 TEST_CASE("ModernStringUtils_string2Int", "[utilities]")
91 {
92  std::string_view svalue{"1003"};
93  auto value = string2Int<int>(svalue);
94  CHECK(value == 1003);
95  long too_large_for_int = std::numeric_limits<int>::max();
96  too_large_for_int += 2;
97  std::ostringstream input;
98  input << too_large_for_int;
99 //Safety pre stdlibcxx 10 doesn't seem worth the effort
100 #if _GLIBCXX_RELEASE > 10
101  CHECK_THROWS_AS(string2Int<int>(input.str()), std::range_error);
102  CHECK_THROWS_AS(string2Int<int>("bad"), std::runtime_error);
103 #endif
104  long big_enough = string2Int<decltype(big_enough)>(input.str());
105  CHECK(big_enough == too_large_for_int);
106 } // namespace qmcplusplus
107 
108 
109 TEST_CASE("ModernStringUtils_strip", "[utilities]")
110 {
111  using modernstrutil::strip;
112 
113  std::string nothing;
114  auto stripped_nothing = strip(nothing);
115  CHECK(stripped_nothing.empty());
116 
117  std::string white_space{" "};
118  auto stripped_white_space = strip(white_space);
119  CHECK(stripped_white_space.empty());
120 
121  std::string test_lines{R"(
122  r1 1 0 0
123  r2 0 1 0
124  r3 0 0 1
125 )"};
126 
127  std::string_view stripped_lines = strip(test_lines);
128 
129  std::string_view ref_stripped{"r1 1 0 0\n r2 0 1 0\n r3 0 0 1"};
130  CHECK(ref_stripped == stripped_lines);
131 
132  std::string_view another{"r1 1 0 0\n r2 0 1 0\n r3 0 0 1\n \0"};
133  std::string_view another_stripped = strip(another);
134  CHECK(another_stripped == "r1 1 0 0\n r2 0 1 0\n r3 0 0 1");
135 
136  std::string_view unneeded{"this needs no stripping"};
137  std::string_view unneeded_stripped = strip(unneeded);
138  CHECK(unneeded_stripped == unneeded);
139 }
140 
141 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
std::string_view strip(const string_view s)
std::string strip(const std::string &s)
Definition: string_utils.h:26
std::vector< std::string > split(const std::string &s)
Definition: string_utils.h:57
std::string lowerCase(const std::string_view s)
++17
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
std::vector< std::string_view > split(const string_view s, const string_view delimiters)
testing::ValidSpinDensityInput input