|
18 | 18 |
|
19 | 19 | package org.apache.avro.io; |
20 | 20 |
|
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
21 | 23 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
22 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals; |
23 | | - |
24 | | -import org.junit.jupiter.api.Test; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
25 | 26 |
|
26 | 27 | public class TestBinaryData { |
27 | 28 |
|
@@ -60,4 +61,35 @@ void testIntLongVleEquality() { |
60 | 61 | BinaryData.encodeLong(Integer.MIN_VALUE, longResult, 0); |
61 | 62 | assertArrayEquals(intResult, longResult); |
62 | 63 | } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testCompareBytesUnsigned() |
| 67 | + { |
| 68 | + // Test case: byte value 0xFF (-1 as signed, 255 as unsigned) |
| 69 | + // should be greater than 0x7F (127) |
| 70 | + byte[] b1 = new byte[] {(byte) 0xFF}; |
| 71 | + byte[] b2 = new byte[] {(byte) 0x7F}; |
| 72 | + int result = BinaryData.compareBytes(b1, 0, 1, b2, 0, 1); |
| 73 | + assertTrue(result > 0, "0xFF (255 unsigned) should be greater than 0x7F (127)"); |
| 74 | + result = BinaryData.compareBytes(b2, 0, 1, b1, 0, 1); |
| 75 | + assertTrue(result < 0, "0x7F (127) should be less than 0xFF (255 unsigned)"); |
| 76 | + result = BinaryData.compareBytes(b1, 0, 1, b1, 0, 1); |
| 77 | + assertEquals(0, result, "Equal byte arrays should return 0"); |
| 78 | + |
| 79 | + // Test with multiple bytes: {0x00, 0xFF} vs {0x00, 0x7F} |
| 80 | + byte[] b3 = new byte[] {0x00, (byte) 0xFF}; |
| 81 | + byte[] b4 = new byte[] {0x00, (byte) 0x7F}; |
| 82 | + byte[] b5 = new byte[] {(byte) 0xFF, 0x00}; |
| 83 | + byte[] b6 = new byte[] {(byte) 0x7F, 0x00}; |
| 84 | + result = BinaryData.compareBytes(b3, 0, 2, b4, 0, 2); |
| 85 | + assertTrue(result > 1, "{0x00, 0xFF} should be greater than {0x00, 0x7F}"); |
| 86 | + result = BinaryData.compareBytes(b5, 0, 2, b6, 0, 2); |
| 87 | + assertTrue(result > 1, "{0xFF, 0x00} should be greater than {0x7F, 0x00}"); |
| 88 | + |
| 89 | + // Test with negative byte values: -1 (0xFF) should be greater than -128 (0x80) |
| 90 | + byte[] b7 = new byte[] {(byte) -1}; |
| 91 | + byte[] b8 = new byte[] {(byte) -128}; |
| 92 | + result = BinaryData.compareBytes(b7, 0, 1, b8, 0, 1); |
| 93 | + assertTrue(result > 0, "-1 (0xFF=255 unsigned) should be greater than -128 (0x80=128 unsigned)"); |
| 94 | + } |
63 | 95 | } |
0 commit comments