diff --git a/src/utils.cpp b/src/utils.cpp index 6c288002b9..bd553d974a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -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 ------------------------------------------------------------------------- */ -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; @@ -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); std::transform(buf.begin(), buf.end(), buf.begin(), tolower); - bool rv = false; + int rv = 0; if ((buf == "yes") || (buf == "on") || (buf == "true") || (buf == "1")) { - rv = true; + rv = 1; } else if ((buf == "no") || (buf == "off") || (buf == "false") || (buf == "0")) { - rv = false; + rv = 0; } else { std::string msg("Expected boolean parameter instead of '"); msg += buf + "' in input script or data file"; diff --git a/src/utils.h b/src/utils.h index 65a1462716..53077a3d5a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -162,16 +162,16 @@ namespace utils { /*! 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 - * 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 line line number in source file for error message * \param str string to be converted to logical * \param do_abort determines whether to call Error::one() or Error::all() * \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 * if it is a valid floating point or integer number diff --git a/unittest/formats/test_input_convert.cpp b/unittest/formats/test_input_convert.cpp index ea7712b36c..020e86a65b 100644 --- a/unittest/formats/test_input_convert.cpp +++ b/unittest/formats/test_input_convert.cpp @@ -40,26 +40,26 @@ protected: TEST_F(InputConvertTest, logical) { - EXPECT_TRUE(utils::logical(FLERR, "yes", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "Yes", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "YES", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "yEs", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "true", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "True", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "TRUE", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "on", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "On", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "ON", false, lmp)); - EXPECT_TRUE(utils::logical(FLERR, "1", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "no", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "No", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "NO", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "nO", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "off", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "Off", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "OFF", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "OfF", false, lmp)); - EXPECT_FALSE(utils::logical(FLERR, "0", false, lmp)); + EXPECT_EQ(utils::logical(FLERR, "yes", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "Yes", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "YES", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "yEs", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "true", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "True", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "TRUE", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "on", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "On", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "ON", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "1", false, lmp), 1); + EXPECT_EQ(utils::logical(FLERR, "no", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "No", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "NO", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "nO", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "off", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "Off", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "OFF", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "OfF", false, lmp), 0); + EXPECT_EQ(utils::logical(FLERR, "0", false, lmp), 0); TEST_FAILURE(".*ERROR: Expected boolean parameter instead of.*", 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, "", false, lmp);); - TEST_FAILURE(".*ERROR: Expected floating point.*", - utils::numeric(FLERR, nullptr, false, lmp);); + TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, nullptr, false, lmp);); TEST_FAILURE(".*ERROR: Expected floating point.*", utils::numeric(FLERR, "2.56D+3", false, lmp);); }