|
18 | 18 | */ |
19 | 19 | package org.apache.parquet.variant; |
20 | 20 |
|
| 21 | +import java.lang.reflect.Field; |
21 | 22 | import java.math.BigDecimal; |
22 | 23 | import java.nio.ByteBuffer; |
23 | 24 | import java.nio.ByteOrder; |
@@ -181,6 +182,43 @@ public void testFloatBuilder() { |
181 | 182 | }); |
182 | 183 | } |
183 | 184 |
|
| 185 | + @Test |
| 186 | + public void testFloatBuilderDoesNotWriteTooManyBytes() throws Exception { |
| 187 | + VariantBuilder vb = new VariantBuilder(); |
| 188 | + |
| 189 | + Field writeBufferField = VariantBuilder.class.getDeclaredField("writeBuffer"); |
| 190 | + writeBufferField.setAccessible(true); |
| 191 | + Field writePosField = VariantBuilder.class.getDeclaredField("writePos"); |
| 192 | + writePosField.setAccessible(true); |
| 193 | + |
| 194 | + byte[] buffer = (byte[]) writeBufferField.get(vb); |
| 195 | + for (int i = 0; i < 20; i++) { |
| 196 | + buffer[i] = (byte) 0xFF; |
| 197 | + } |
| 198 | + |
| 199 | + float testFloat = 1.23456f; |
| 200 | + vb.appendFloat(testFloat); |
| 201 | + |
| 202 | + int writePos = (Integer) writePosField.get(vb); |
| 203 | + Assert.assertEquals("writePos should be exactly 5 after appendFloat", 5, writePos); |
| 204 | + |
| 205 | + int modifiedBytes = 0; |
| 206 | + for (int i = 0; i < 10; i++) { |
| 207 | + if (buffer[i] != (byte) 0xFF) { |
| 208 | + modifiedBytes++; |
| 209 | + } |
| 210 | + } |
| 211 | + Assert.assertEquals("appendFloat should write exactly 5 bytes (1 header + 4 data)", 5, modifiedBytes); |
| 212 | + |
| 213 | + for (int i = 5; i < 10; i++) { |
| 214 | + Assert.assertEquals( |
| 215 | + "Byte at position " + i + " should not be modified by appendFloat", (byte) 0xFF, buffer[i]); |
| 216 | + } |
| 217 | + |
| 218 | + Variant variant = vb.build(); |
| 219 | + Assert.assertEquals("Float value should be preserved correctly", testFloat, variant.getFloat(), 0.0f); |
| 220 | + } |
| 221 | + |
184 | 222 | @Test |
185 | 223 | public void testDoubleBuilder() { |
186 | 224 | Arrays.asList(Double.MIN_VALUE, 0d, -0d, Double.MAX_VALUE).forEach(d -> { |
|
0 commit comments