Add some more tests for config's attribute handling.

This commit is contained in:
Pentarctagon 2022-11-01 13:28:21 -05:00
parent 29a2add33f
commit d98d5bea2f
2 changed files with 72 additions and 2 deletions

View file

@ -135,8 +135,9 @@ namespace
{
/**
* Attempts to convert @a source to the template type.
* This is to avoid "overzealous reinterpretations of certain WML strings as
* numeric types" (c.f. bug #19201).
* This is to avoid "overzealous reinterpretations of certain WML strings as numeric types".
* For example: the version "2.1" and "2.10" are not the same.
* Another example: the string "0001" given to [message] should not be displayed to the player as just "1".
* @returns true if the conversion was successful and the source string
* can be reobtained by streaming the result.
*/

View file

@ -29,12 +29,71 @@ BOOST_AUTO_TEST_SUITE(test_config)
BOOST_AUTO_TEST_CASE(test_config_attribute_value)
{
config c;
config c1;
config c2;
const config& cc = c;
int x_int;
std::string x_str;
long long x_sll;
double x_dbl;
// compare identical assigned int vs string
c1["x"] = 6;
c2["x"] = "6";
BOOST_CHECK_EQUAL(c1["x"], c2["x"]);
// compare identical assigned int vs floating point
c1["x"] = 6;
c2["x"] = 6.0;
BOOST_CHECK_EQUAL(c1["x"], c2["x"]);
// compare identical assigned int-string vs floating point
c1["x"] = "6";
c2["x"] = 6.0;
BOOST_CHECK_EQUAL(c1["x"], c2["x"]);
// compare identical assigned floating point-string vs int
c1["x"] = 6;
c2["x"] = "6.0";
BOOST_CHECK_NE(c1["x"], c2["x"]);
// compare identical assigned floating point vs string
c1["x"] = 6.0;
c2["x"] = "6.0";
BOOST_CHECK_NE(c1["x"], c2["x"]);
// check what happens when trying to get a numeric result from a non-numeric value
c["x"] = "1aaaa";
x_str = c["x"].str();
BOOST_CHECK_EQUAL(x_str, "1aaaa");
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 1);
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 1ll);
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1.0);
c["x"] = "1.7aaaa";
x_str = c["x"].str();
BOOST_CHECK_EQUAL(x_str, "1.7aaaa");
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 1);
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 1ll);
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1.7);
c["x"] = "aaaa1";
x_str = c["x"].str();
BOOST_CHECK_EQUAL(x_str, "aaaa1");
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 0);
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 0ll);
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 0.0);
// check type conversion when assigned as int
c["x"] = 1;
x_str = c["x"].str();
BOOST_CHECK_EQUAL(x_str, "1");
@ -45,6 +104,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1.0);
// check type conversion when assigned as int (again)
c["x"] = 10000000;
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 10000000);
@ -55,6 +115,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1e7);
// check type conversion when assigned aan empty string
c["x"] = "";
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 0ll);
@ -65,6 +126,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 0.0);
// check type conversion when assigned as a hex string
c["x"] = "0x11";
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 0);
@ -75,6 +137,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 0.0);
// check type conversion when assigned as a hex string (again)
c["x"] = "0xab";
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 0);
@ -85,6 +148,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 0.0);
// check type conversion when assigned as a string with leading zeroes
c["x"] = "00001111";
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 1111);
@ -95,6 +159,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1.111e3);
// check type conversion when assigned as a string with only zeroes
c["x"] = "000000";
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, 0);
@ -105,6 +170,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 0.0);
// check type conversion when assigned as a string with leading zeroes and is too large to fit in an int
c["x"] = "01234567890123456789";
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 1234567890123456789ll);
@ -115,6 +181,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1.23456789012345678e18);
// check type conversion when assigned as a string with no leading zeroes and is too large to fit in an int
c["x"] = "99999999999999999999";
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 0ll);
@ -125,6 +192,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK_EQUAL(x_dbl, 1e20);
// check type conversion when assigned as a floating point
c["x"] = 1.499;
x_sll = c["x"].to_long_long();
BOOST_CHECK_EQUAL(x_sll, 1ll);
@ -135,6 +203,7 @@ BOOST_AUTO_TEST_CASE(test_config_attribute_value)
x_dbl = c["x"].to_double();
BOOST_CHECK(std::abs(x_dbl - 1.499) < 1e-6);
// check type conversion when assigned as a long long (int overflows)
c["x"] = 123456789123ll;
x_int = c["x"].to_int();
BOOST_CHECK_EQUAL(x_int, -1097262461);