Skip to content

Support binary literal format for String to Number conversion #3605

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

Open
MightyEagle82 opened this issue Dec 16, 2023 · 7 comments
Open

Support binary literal format for String to Number conversion #3605

MightyEagle82 opened this issue Dec 16, 2023 · 7 comments

Comments

@MightyEagle82
Copy link

At the moment there is support for hexadecimal literals but not for binary:

  • 0xF0 - allowed
  • 0b11110000 - disallowed
@sbrannen
Copy link
Member

sbrannen commented Dec 17, 2023

We actually provide support for decimal, hexadecimal, and octal numbers simply because that's what java.lang.Integer.decode(String) and related decode() methods support.

I had forgotten about binary literals, since I never use them personally, but...

Starting in J2SE 7.0, as binary numbers, using the form 0b (or 0B) followed by one or more binary digits (0 or 1). For example, 0b101010 is the integer 42. Like octal and hex numbers, binary literals may represent negative numbers.

So, although we could potentially introduce explicit support for binary literals in org.junit.platform.commons.support.conversion.StringToNumberConverter, I think it may also be a good idea to request direct support in the JDK (in Byte::decode, Short::decode, Integer::decode, and Long::decode).

@sormuras, thoughts?

@sbrannen sbrannen changed the title Support for binary literals in parameterized test arguments Support binary literal format for String to Number conversion Dec 17, 2023
@sbrannen sbrannen changed the title Support binary literal format for String to Number conversion Support binary literal format for String to Number conversion Dec 17, 2023
@sormuras
Copy link
Member

https://bugs.openjdk.org/browse/JDK-7181749 "Integer.decode("0b1010101") fails" has:

Duplicate of functionality requested in comments of 6863378; closing accordingly.

Which in turn https://bugs.openjdk.org/browse/JDK-6863378 "Project Coin: Consider library support for underscores in numbers and binary literals" has:

"Not essential functionality; closing as will not fix."

So, supporting such conversions is an option for JUnit - if there's enough interest from users and we are willing to maintain it.

@MightyEagle82
Copy link
Author

MightyEagle82 commented Dec 17, 2023

Guys, thank you for the replies anyway.

It was the first time when I decide to use CsvSource instead of MethodSource to provide binary values for test arguments. I haven't faced with decode methods until now. After posting this issue I found that they didn't support binary literals and was very surprised.

I understand that JUnit relies on JDK API. So it won't be a problem if you just close this issue with won't be fixed status.

@young0264
Copy link

Hi @sbrannen @sormuras

Thanks for the detailed discussion above.
I understand that 'Integer.decode()' and related JDK methods do not currently support binary literals ('Ob..'), and that this has been discussed in OpenJDK with a "won't fix" resolution.

I'd be happy to work on a PR that adds support for binary literals(e.g, Ob1010) to 'StringToNumberconverter' within JUnit, handling it explicitly in cases where the JDK doesn't.

Would the team be open to such a contribution, assuming it comes with tests and minimal maintenance overhead?

Thanks again for your work on JUnit.!

@marcphilipp
Copy link
Member

Let's hold off on this issue until #853 is resolved.

@young0264
Copy link

Thanks for the clarification, @marcphilipp.
Understood - I'll wait until #853 is resolved before proceeding.
If there's anything I can help with regarding #853 or related discussion in the meantime, feel free to let me know.

Looking forward to contributing when the time is right.

@scordio
Copy link
Contributor

scordio commented May 16, 2025

If there's anything I can help with regarding #853 or related discussion in the meantime, feel free to let me know.

Hi @young0264, you could provide your feedback on #4219, once it's no longer a draft or after it's been merged.

For a preview of its usage, see #4514 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants