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

Implicit conversion from integral numbers to byte array #2702

Closed
1 task
scordio opened this issue Sep 1, 2021 · 15 comments
Closed
1 task

Implicit conversion from integral numbers to byte array #2702

scordio opened this issue Sep 1, 2021 · 15 comments

Comments

@scordio
Copy link
Contributor

scordio commented Sep 1, 2021

I'd like to propose the addition of implicit conversion from integral numbers (byte, short, int, long) to byte array. The concrete use case is code dealing with bitwise operations, mostly related to cryptography.

My wish is to write something like this:

  @ParameterizedTest
  @ValueSource(ints = {Integer.MIN_VALUE, 0, 0xFF, 0xFFFF, 0xFFFFFF, Integer.MAX_VALUE})
  void test(byte[] byteArray) {
    assertThat(byteArray).hasSize(4);
  }

where I can leverage binary and hexadecimal literals which are automatically converted to a byte array using a ByteBuffer with Big Endian order.

I put together a small poc with a custom argument converter. If the proposal is accepted, I'm happy to raise a PR about it.

Deliverables

  • A new implicit conversion from byte / short / int / long to byte[]
@sbrannen
Copy link
Member

sbrannen commented Sep 2, 2021

For that use case, you wouldn't use an ArgumentConverter since that can only convert a single argument.

Rather, you would use an ArgumentsAggregator to aggregate all arguments into a single array.

@sbrannen
Copy link
Member

sbrannen commented Sep 2, 2021

In addition, I don't see how we could introduce implicit support for that without breaking existing use cases, but perhaps I'm overlooking something.

@scordio
Copy link
Contributor Author

scordio commented Sep 2, 2021

@sbrannen I meant something different. Probably a more detailed example would've explained it better.

I don't want to aggregate all the arguments in a single array, I want each argument to be converted into a dedicated byte array, with array size depending on the primitive type.

Following my example:

@ValueSource(ints = {Integer.MIN_VALUE, 0, 0xFF, 0xFFFF, 0xFFFFFF, Integer.MAX_VALUE})

each integer would be converted to a separate byte array of size 4, being the size in bytes of the integer type (i.e., Integer.BYTES). In case of byte values, each array would have size 1; shorts would have arrays of size 2, and longs would have arrays of size 8.

The test cases for the custom converter might explain it better than my words 🙂

@scordio
Copy link
Contributor Author

scordio commented Sep 3, 2021

Written in a style similar to the official docs, this is what I'd like to have:

Source type Target type Example
byte / Byte byte[] 0xFByteBuffer.allocate(Byte.BYTES).put((byte) 0xF).array()
short / Short byte[] 0xFFFByteBuffer.allocate(Short.BYTES).putShort((short) 0xFFF).array()
int / Integer byte[] 0xFFFFFFFByteBuffer.allocate(Integer.BYTES).putInt(0xFFFFFFF).array()
long / Long byte[] 0xFFFFFFFFLByteBuffer.allocate(Long.BYTES).putLong(0xFFFFFFFFL).array()

@scordio scordio changed the title Implicit conversion from numbers to byte array Implicit conversion from integral numbers to byte array Sep 7, 2021
scordio added a commit to scordio/junit5 that referenced this issue Sep 7, 2021
Integral types are converted to a byte array with a Big Endian byte
order and size as the number of bytes used to represent the integral
value in two's complement binary form.

Issue: junit-team#2702
@scordio
Copy link
Contributor Author

scordio commented Sep 7, 2021

I raised #2711 to help evaluate this feature.

scordio added a commit to scordio/junit5 that referenced this issue Sep 7, 2021
Integral types are converted to a byte array with a Big Endian byte
order and size as the number of bytes used to represent the integral
value in two's complement binary form.

Issue: junit-team#2702
scordio added a commit to scordio/junit5 that referenced this issue Sep 7, 2021
Integral types are converted to a byte array with a Big Endian byte
order and size as the number of bytes used to represent the integral
value in two's complement binary form.

Issue: junit-team#2702
scordio added a commit to scordio/junit5 that referenced this issue Sep 8, 2021
Integral types are converted to a byte array with a Big Endian byte
order and size as the number of bytes used to represent the integral
value in two's complement binary form.

Issue: junit-team#2702
@marcphilipp
Copy link
Member

I'm not yet convinced how generally useful such a built-in conversion would be. How about making it opt-in via an annotation like @JavaTimeConversionPattern?

@scordio
Copy link
Contributor Author

scordio commented Oct 31, 2021

I think that bitwise operations are not the most common use case for Java development, so I'm probably targeting a niche area. My thoughts were that such an implicit conversion would not introduce side effects for other use cases and test authors could always define their own conversion logic, if there is any.

Having an annotation would also be nice. What I'd like to avoid is defining a custom converter and sharing it with multiple projects, while this could be a generally usable feature. The annotation could also have an optional parameter to change the endianness, which I would keep as big endian by default.

Do you have any idea about naming? Something like @IntegralToBytesConversion?

@scordio
Copy link
Contributor Author

scordio commented Dec 30, 2021

As the stale bot just commented on #2711, I wanted to check if any decisions have been made about how this topic might move forward. Would @IntegralToBytesConversion be the right way to go?

@stale
Copy link

stale bot commented Dec 30, 2022

This issue has been automatically marked as stale because it has not had recent activity. Given the limited bandwidth of the team, it will be automatically closed if no further activity occurs. Thank you for your contribution.

@stale stale bot added the status: stale label Dec 30, 2022
@scordio
Copy link
Contributor Author

scordio commented Dec 30, 2022

As the stale bot just commented on #2711, I wanted to check if any decisions have been made about how this topic might move forward. Would @IntegralToBytesConversion be the right way to go?

Bump

@stale stale bot removed the status: stale label Dec 30, 2022
@marcphilipp
Copy link
Member

@scordio Sorry for the delay! I think @IntegralToBytesConversion or @IntegralToByteArrayConversion sounds fine. Have you considered submitting this to JUnit Pioneer instead?

@scordio
Copy link
Contributor Author

scordio commented Apr 22, 2023

I was actually thinking about it today 😄 maybe combining this topic with the proposal at #3141 (comment).

As far as I remember, there are no argument converters in JUnit Pioneer. If the project is open to having them, that would work for me. I'll raise an issue and let you know.

@stale
Copy link

stale bot commented May 7, 2023

If you would like us to be able to process this issue, please provide the requested information. If the information is not provided within the next 3 weeks, we will be unable to proceed and this issue will be closed.

@stale stale bot added the status: stale label May 7, 2023
@marcphilipp
Copy link
Member

@scordio Can we close this issue in favor of the Pioneer one?

@scordio
Copy link
Contributor Author

scordio commented May 9, 2023

Superseded by junit-pioneer/junit-pioneer#734.

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

Successfully merging a pull request may close this issue.

4 participants