-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more C++ unit tests for String to number conversions #85666
Add more C++ unit tests for String to number conversions #85666
Conversation
tests/core/string/test_string.h
Outdated
static const char *nums[4] = { "1237461283", "- 22", "0", " - 1123412" }; | ||
static const int num[4] = { 1237461283, -22, 0, -1123412 }; | ||
static const char *nums[13] = { "1237461283", "- 22", "0", " - 1123412", "", "10_000_000", "-1_2_3_4", "10__000", " 1 2 34 ", "-0", "007", "0b1011", "0x1012" }; | ||
static const int num[13] = { 1237461283, -22, 0, -1123412, 0, 10000000, -1234, 10000, 1234, 0, 7, 1011, 1012 }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalid suggestion
These values are not aligned, 1011
in decimal is not the same as 1011
in binary, same for hexadecimal 1012
and decimal 1012
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"0b1011"
and "0x1012"
just look like a binary and a hexadecimal number, but to_int()
doesn't handle them as such. to_int()
ignores any non digits, so what it sees is "01011"
and "01012"
and then converts them as base-10, to 1011
and 1012
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, good point, that should be noted I think in the tests to clarify it's correct behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added comments for those cases. I also made some other changes as I found some new cases which hex_to_int()
and bin_to_int()
can't handle. For to_int()
there just don't seem to be any other invalid cases than too small/large integers as all non-digits (except leading minuses) are ignored.
b92229a
to
d782c1b
Compare
tests/core/string/test_string.h
Outdated
// Invalid float strings should return 0. | ||
static const char *invalid_nums[6] = { "qwerty", "qwerty123", "0xffff", "0b1010", "--3.13", "__345" }; | ||
|
||
ERR_PRINT_OFF |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(my bad was meant here)
Is this really required? From what I can see there's no errors on this thrown (only a warning with too high exponent, which you do not test for)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct, only hex_to_int()
prints errors when the string contains illegal characters and 0 is returned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the unnecessary ERR_PRINT_OFF
's are removed and "too high exponent" tests are added.
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The too high exponent case isn't triggered here as it fires with exponent of 512, unsure why it's still infinity here though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed this. There seems to be different levels of "too high exponents". Floats between 1e309 - 1e511 will become INFINITY
, but warning is not printed (hex_to_int
prints errors, btw). When the float gets even larger, 1e512, warning is finally printed. Don't ask me why floats work like this :)
dda7c2a
to
4925025
Compare
4f5e035
to
4a8534d
Compare
tests/core/string/test_string.h
Outdated
} | ||
|
||
// Very large exponents. | ||
CHECK(String("1e308").to_float() == 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CHECK(String("1e308").to_float() == 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0); | |
CHECK(String("1e308").to_float() == 1e308); |
Unless there's some reason that won't work, or it will be very cluttered and error prone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
4a8534d
to
5e40124
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good on my end. Thanks for adding more tests 🙂
Thanks! And congrats on your first merged contribution to the engine! |
This PR adds more C++ units tests to the following string to number conversion functions:
String::to_int()
String::hex_to_int()
String::bin_to_int()
String::to_float()
There are inconsistencies with the conversion functions, but the goal of this PR is not to fix anything, the goal is to document how the conversions currently work using the unit tests.
I'm planning to implement this godotengine/godot-proposals#8546, but I'm not touching conversion functions (even a little bit) without first adding more unit tests.