Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static byte[] intToBytes(int value, int size) {
int shift = 0;
int finalPosition = size > Integer.BYTES ? size - Integer.BYTES : 0;
for (int i = size - 1; i >= finalPosition; i--) {
result[i] = (byte) (value >>> shift);
result[i] = (byte) ((value >>> shift) & 0xFF);
shift += 8;
}

Expand All @@ -192,7 +192,7 @@ public static byte[] longToBytes(long value, int size) {
int shift = 0;
int finalPosition = size > Long.BYTES ? size - Long.BYTES : 0;
for (int i = size - 1; i >= finalPosition; i--) {
result[i] = (byte) (value >>> shift);
result[i] = (byte) ((value >>> shift) & 0xFF);
shift += 8;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,77 @@ void testIntegerReconversion() {
}
}

@Test
void testIntToBytesWithNegativeValues() {
// Test negative int values to ensure unsigned right shift is handled correctly
int negativeValue = -1;
byte[] result = DataConverter.intToBytes(negativeValue, 4);
assertArrayEquals(
new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF},
result,
"Negative -1 should produce all FF bytes");

// Test another negative value
negativeValue = -256; // 0xFFFFFF00
result = DataConverter.intToBytes(negativeValue, 4);
assertArrayEquals(
new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00},
result,
"Negative -256 should produce correct bytes");

// Test with size larger than int
negativeValue = -1;
result = DataConverter.intToBytes(negativeValue, 6);
assertArrayEquals(
new byte[] {0x00, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF},
result,
"Negative -1 with size 6 should be padded with zeros");
}

@Test
void testLongToBytesWithNegativeValues() {
// Test negative long values to ensure unsigned right shift is handled correctly
long negativeValue = -1L;
byte[] result = DataConverter.longToBytes(negativeValue, 8);
assertArrayEquals(
new byte[] {
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
},
result,
"Negative -1L should produce all FF bytes");

// Test another negative value
negativeValue = -256L; // 0xFFFFFFFFFFFFFF00
result = DataConverter.longToBytes(negativeValue, 8);
assertArrayEquals(
new byte[] {
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00
},
result,
"Negative -256L should produce correct bytes");

// Test with size larger than long
negativeValue = -1L;
result = DataConverter.longToBytes(negativeValue, 10);
assertArrayEquals(
new byte[] {
0x00,
0x00,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF
},
result,
"Negative -1L with size 10 should be padded with zeros");
}

/** Test of indexOf method, of class DataConverter. */
@Test
void testIndexOf() {
Expand Down