return int instead of bool to minimize code changes

This commit is contained in:
Axel Kohlmeyer
2021-09-19 18:07:56 -04:00
parent bfa2ea1fba
commit 61c71c6605
3 changed files with 28 additions and 29 deletions

View File

@ -353,7 +353,7 @@ std::string utils::check_packages_for_style(const std::string &style, const std:
called by various commands to check validity of their arguments called by various commands to check validity of their arguments
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
bool utils::logical(const char *file, int line, const char *str, bool do_abort, LAMMPS *lmp) int utils::logical(const char *file, int line, const char *str, bool do_abort, LAMMPS *lmp)
{ {
int n = 0; int n = 0;
@ -372,11 +372,11 @@ bool utils::logical(const char *file, int line, const char *str, bool do_abort,
if (has_utf8(buf)) buf = utf8_subst(buf); if (has_utf8(buf)) buf = utf8_subst(buf);
std::transform(buf.begin(), buf.end(), buf.begin(), tolower); std::transform(buf.begin(), buf.end(), buf.begin(), tolower);
bool rv = false; int rv = 0;
if ((buf == "yes") || (buf == "on") || (buf == "true") || (buf == "1")) { if ((buf == "yes") || (buf == "on") || (buf == "true") || (buf == "1")) {
rv = true; rv = 1;
} else if ((buf == "no") || (buf == "off") || (buf == "false") || (buf == "0")) { } else if ((buf == "no") || (buf == "off") || (buf == "false") || (buf == "0")) {
rv = false; rv = 0;
} else { } else {
std::string msg("Expected boolean parameter instead of '"); std::string msg("Expected boolean parameter instead of '");
msg += buf + "' in input script or data file"; msg += buf + "' in input script or data file";

View File

@ -162,16 +162,16 @@ namespace utils {
/*! Convert a string to a boolean while checking whether it is a valid boolean term. /*! Convert a string to a boolean while checking whether it is a valid boolean term.
* Valid terms are 'yes', 'no', 'true', 'false', 'on', 'off', '1', '0'. Capilatization * Valid terms are 'yes', 'no', 'true', 'false', 'on', 'off', '1', '0'. Capilatization
* will be ignored (thus 'yEs' or 'nO' are valid, 'yay' or 'nay' are not). * will be ignored (thus 'yEs' or 'nO' are valid, 'yeah' or 'nope' are not).
* *
* \param file name of source file for error message * \param file name of source file for error message
* \param line line number in source file for error message * \param line line number in source file for error message
* \param str string to be converted to logical * \param str string to be converted to logical
* \param do_abort determines whether to call Error::one() or Error::all() * \param do_abort determines whether to call Error::one() or Error::all()
* \param lmp pointer to top-level LAMMPS class instance * \param lmp pointer to top-level LAMMPS class instance
* \return boolean */ * \return 1 if string resolves to "true", otherwise 0 */
bool logical(const char *file, int line, const char *str, bool do_abort, LAMMPS *lmp); int logical(const char *file, int line, const char *str, bool do_abort, LAMMPS *lmp);
/*! Convert a string to a floating point number while checking /*! Convert a string to a floating point number while checking
* if it is a valid floating point or integer number * if it is a valid floating point or integer number

View File

@ -40,26 +40,26 @@ protected:
TEST_F(InputConvertTest, logical) TEST_F(InputConvertTest, logical)
{ {
EXPECT_TRUE(utils::logical(FLERR, "yes", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "yes", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "Yes", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "Yes", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "YES", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "YES", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "yEs", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "yEs", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "true", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "true", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "True", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "True", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "TRUE", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "TRUE", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "on", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "on", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "On", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "On", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "ON", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "ON", false, lmp), 1);
EXPECT_TRUE(utils::logical(FLERR, "1", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "1", false, lmp), 1);
EXPECT_FALSE(utils::logical(FLERR, "no", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "no", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "No", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "No", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "NO", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "NO", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "nO", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "nO", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "off", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "off", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "Off", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "Off", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "OFF", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "OFF", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "OfF", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "OfF", false, lmp), 0);
EXPECT_FALSE(utils::logical(FLERR, "0", false, lmp)); EXPECT_EQ(utils::logical(FLERR, "0", false, lmp), 0);
TEST_FAILURE(".*ERROR: Expected boolean parameter instead of.*", TEST_FAILURE(".*ERROR: Expected boolean parameter instead of.*",
utils::logical(FLERR, "yay", false, lmp);); utils::logical(FLERR, "yay", false, lmp););
@ -84,8 +84,7 @@ TEST_F(InputConvertTest, numeric)
TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, "yay", false, lmp);); TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, "yay", false, lmp););
TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, "", false, lmp);); TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, "", false, lmp););
TEST_FAILURE(".*ERROR: Expected floating point.*", TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, nullptr, false, lmp););
utils::numeric(FLERR, nullptr, false, lmp););
TEST_FAILURE(".*ERROR: Expected floating point.*", TEST_FAILURE(".*ERROR: Expected floating point.*",
utils::numeric(FLERR, "2.56D+3", false, lmp);); utils::numeric(FLERR, "2.56D+3", false, lmp););
} }