Skip to content
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

Support binary literal format for String to Number conversion #3605

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

Support binary literal format for String to Number conversion #3605

MightyEagle82 opened this issue Dec 16, 2023 · 3 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.

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

3 participants