MolSSI Integral Reference Project
test_common.hpp
Go to the documentation of this file.
1 /*! \file
2  *
3  * \brief Miscellaneous testing functions
4  */
5 
6 #pragma once
7 
8 #include <string>
9 #include <vector>
10 #include <istream>
11 #include <limits>
12 
13 namespace mirp {
14 
15 //! \brief Maximum length of a line
16 static const std::streamsize max_length = std::numeric_limits<std::streamsize>::max();
17 
18 /*! \brief Compare two double-precision numbers for equality within a tolerance
19  *
20  * This tests the relative difference between two double-precision values.
21  *
22  * The relative difference is calculated as
23  *
24  * abs(a - b)/max(abs(a), abs(b))
25  *
26  * \param [in] a,b Values to compare
27  * \param [in] tol Tolerance to compare to
28  * \return True if the relative difference is less than \p tol
29  */
30 bool almost_equal(double a, double b, double tol);
31 
32 
33 /*! \brief Print the results of a test
34  *
35  * \param [in] nfailed Number of failed tests
36  * \param [in] ntests Total number of tests run
37  */
38 void print_results(unsigned long nfailed, unsigned long ntests);
39 
40 
41 /*! \brief Convert a character representing an angular momentum to an integer
42  *
43  * Converts s,p,d,f,... to 0,1,2,3,...
44  *
45  * The character is case insensitive
46  *
47  * \throw std::runtime_error if the character cannot be converted to an integer
48  */
49 int amchar_to_int(char am);
50 
51 
52 /*! \brief Convert a string representing an element to its atomic Z number
53  *
54  * Converts H to 1, He to 2, etc
55  *
56  * The element string is case insensitive
57  *
58  * \throw std::runtime_error if the character cannot be converted to an integer
59  */
60 int element_to_z(const std::string & element);
61 
62 
63 /*! \brief Converts a string to lower case, returning a copy */
64 std::string str_tolower(const std::string & s);
65 
66 
67 /*! \brief Trims spaces and tabs from both ends of a string, returning a copy */
68 std::string trim(const std::string & s);
69 
70 
71 /*! \brief Splits a string into components
72  *
73  * The components must be separated by the character \p sep
74  */
75 std::vector<std::string> split(const std::string & s, char sep = ' ');
76 
77 
78 /*! \brief Advances the stream past any comment and blank lines
79  *
80  * The stream will be advanced past the lines that were read.
81  *
82  * If an EOF is encountered, false is returned
83  *
84  * \param [in] fs The stream to read from
85  * \param [in] commentchar Lines beginning with the character will be skipped
86  * \return True if there is additional data in the file, false on EOF
87  */
88 bool file_skip(std::istream & fs, char commentchar);
89 
90 } // close namespace mirp
91 
bool file_skip(std::istream &fs, char commentchar)
Advances the stream past any comment and blank lines.
Definition: test_common.cpp:170
bool almost_equal(double a, double b, double tol)
Compare two double-precision numbers for equality within a tolerance.
Definition: test_common.cpp:64
void print_results(unsigned long nfailed, unsigned long ntests)
Print the results of a test.
Definition: test_common.cpp:84
std::string str_tolower(const std::string &s)
Converts a string to lower case, returning a copy.
Definition: test_common.cpp:123
static const std::streamsize max_length
Maximum length of a line.
Definition: test_common.hpp:16
int element_to_z(const std::string &element)
Convert a string representing an element to its atomic Z number.
Definition: test_common.cpp:109
int amchar_to_int(char am)
Convert a character representing an angular momentum to an integer.
Definition: test_common.cpp:94
std::string trim(const std::string &s)
Trims spaces and tabs from both ends of a string, returning a copy.
Definition: test_common.cpp:132
std::vector< std::string > split(const std::string &s, char sep)
Splits a string into components.
Definition: test_common.cpp:146