vinooganesh commented on code in PR #3397:
URL: https://github.com/apache/parquet-java/pull/3397#discussion_r3575350215


##########
parquet-column/src/test/java/org/apache/parquet/column/values/alp/AlpEncoderDecoderTest.java:
##########
@@ -0,0 +1,349 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.parquet.column.values.alp;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+/**
+ * Tests for the core ALP encoder/decoder logic.
+ */
+public class AlpEncoderDecoderTest {
+
+  // ========== Float Encoding/Decoding Tests ==========
+
+  @Test
+  public void testFloatRoundTrip() {
+    float[] testValues = {0.0f, 1.0f, -1.0f, 3.14159f, 100.5f, 0.001f, 
1234567.0f};
+
+    for (float value : testValues) {
+      for (int exponent = 0; exponent <= AlpConstants.FLOAT_MAX_EXPONENT; 
exponent++) {
+        for (int factor = 0; factor <= exponent; factor++) {
+          if (!AlpEncoderDecoder.isFloatException(value, exponent, factor)) {
+            int encoded = AlpEncoderDecoder.encodeFloat(value, exponent, 
factor);
+            float decoded = AlpEncoderDecoder.decodeFloat(encoded, exponent, 
factor);
+            assertEquals(
+                "Round-trip failed for value=" + value + ", exponent=" + 
exponent + ", factor="
+                    + factor,
+                Float.floatToRawIntBits(value),
+                Float.floatToRawIntBits(decoded));
+          }
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testFloatExceptionDetection() {
+    assertTrue("NaN should be an exception", 
AlpEncoderDecoder.isFloatException(Float.NaN));
+    assertTrue(
+        "Positive infinity should be an exception",
+        AlpEncoderDecoder.isFloatException(Float.POSITIVE_INFINITY));
+    assertTrue(
+        "Negative infinity should be an exception",
+        AlpEncoderDecoder.isFloatException(Float.NEGATIVE_INFINITY));
+    assertTrue("Negative zero should be an exception", 
AlpEncoderDecoder.isFloatException(-0.0f));
+
+    assertFalse("1.0f should not be a basic exception", 
AlpEncoderDecoder.isFloatException(1.0f));
+    assertFalse("0.0f should not be a basic exception", 
AlpEncoderDecoder.isFloatException(0.0f));
+  }
+
+  @Test
+  public void testFloatEncoding() {
+    assertEquals(123, AlpEncoderDecoder.encodeFloat(1.23f, 2, 0));
+    assertEquals(123, AlpEncoderDecoder.encodeFloat(12.3f, 2, 1));
+    assertEquals(0, AlpEncoderDecoder.encodeFloat(0.0f, 5, 0));
+  }
+
+  @Test
+  public void testFloatDecoding() {
+    assertEquals(1.23f, AlpEncoderDecoder.decodeFloat(123, 2, 0), 1e-6f);
+    assertEquals(12.3f, AlpEncoderDecoder.decodeFloat(123, 2, 1), 1e-6f);
+    assertEquals(0.0f, AlpEncoderDecoder.decodeFloat(0, 5, 0), 0.0f);
+  }
+
+  @Test
+  public void testFloatEncodeRounding() {
+    // Verify rounding behavior (magic number trick rounds to nearest)
+    assertEquals(5, AlpEncoderDecoder.encodeFloat(5.4f, 0, 0));
+    assertEquals(6, AlpEncoderDecoder.encodeFloat(5.6f, 0, 0));
+    assertEquals(-5, AlpEncoderDecoder.encodeFloat(-5.4f, 0, 0));
+    assertEquals(-6, AlpEncoderDecoder.encodeFloat(-5.6f, 0, 0));
+    assertEquals(0, AlpEncoderDecoder.encodeFloat(0.0f, 0, 0));
+  }
+
+  @Test
+  public void testFloatEncodeDecodeWithFactor() {
+    // Verify that encode/decode with non-zero factor works correctly.
+    // The key correctness property: encode uses value * POW10[e] * 
POW10_NEGATIVE[f],
+    // and decode uses encoded * POW10[f] * POW10_NEGATIVE[e].
+    float value = 12.3f;
+    int encoded = AlpEncoderDecoder.encodeFloat(value, 2, 1);
+    assertEquals(123, encoded); // 12.3 * 100 * 0.1 = 123
+    float decoded = AlpEncoderDecoder.decodeFloat(encoded, 2, 1);
+    assertEquals(Float.floatToRawIntBits(value), 
Float.floatToRawIntBits(decoded));
+  }
+
+  // ========== Double Encoding/Decoding Tests ==========
+
+  @Test
+  public void testDoubleRoundTrip() {
+    double[] testValues = {0.0, 1.0, -1.0, 3.14159265358979, 100.5, 0.001, 
12345678901234.0};
+
+    for (double value : testValues) {
+      for (int exponent = 0; exponent <= 
Math.min(AlpConstants.DOUBLE_MAX_EXPONENT, 10); exponent++) {
+        for (int factor = 0; factor <= exponent; factor++) {
+          if (!AlpEncoderDecoder.isDoubleException(value, exponent, factor)) {
+            long encoded = AlpEncoderDecoder.encodeDouble(value, exponent, 
factor);
+            double decoded = AlpEncoderDecoder.decodeDouble(encoded, exponent, 
factor);
+            assertEquals(
+                "Round-trip failed for value=" + value + ", exponent=" + 
exponent + ", factor="
+                    + factor,
+                Double.doubleToRawLongBits(value),
+                Double.doubleToRawLongBits(decoded));
+          }
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testDoubleExceptionDetection() {
+    assertTrue("NaN should be an exception", 
AlpEncoderDecoder.isDoubleException(Double.NaN));
+    assertTrue(
+        "Positive infinity should be an exception",
+        AlpEncoderDecoder.isDoubleException(Double.POSITIVE_INFINITY));
+    assertTrue(
+        "Negative infinity should be an exception",
+        AlpEncoderDecoder.isDoubleException(Double.NEGATIVE_INFINITY));
+    assertTrue("Negative zero should be an exception", 
AlpEncoderDecoder.isDoubleException(-0.0));
+
+    assertFalse("1.0 should not be a basic exception", 
AlpEncoderDecoder.isDoubleException(1.0));
+    assertFalse("0.0 should not be a basic exception", 
AlpEncoderDecoder.isDoubleException(0.0));
+  }
+
+  @Test
+  public void testDoubleEncoding() {
+    assertEquals(123L, AlpEncoderDecoder.encodeDouble(1.23, 2, 0));
+    assertEquals(123L, AlpEncoderDecoder.encodeDouble(12.3, 2, 1));
+    assertEquals(0L, AlpEncoderDecoder.encodeDouble(0.0, 5, 0));
+  }
+
+  @Test
+  public void testDoubleDecoding() {
+    assertEquals(1.23, AlpEncoderDecoder.decodeDouble(123, 2, 0), 1e-10);
+    assertEquals(12.3, AlpEncoderDecoder.decodeDouble(123, 2, 1), 1e-10);
+    assertEquals(0.0, AlpEncoderDecoder.decodeDouble(0, 5, 0), 0.0);
+  }
+
+  @Test
+  public void testDoubleEncodeRounding() {
+    // Verify rounding behavior (magic number trick rounds to nearest)
+    assertEquals(5L, AlpEncoderDecoder.encodeDouble(5.4, 0, 0));
+    assertEquals(6L, AlpEncoderDecoder.encodeDouble(5.6, 0, 0));
+    assertEquals(-5L, AlpEncoderDecoder.encodeDouble(-5.4, 0, 0));
+    assertEquals(-6L, AlpEncoderDecoder.encodeDouble(-5.6, 0, 0));
+    assertEquals(0L, AlpEncoderDecoder.encodeDouble(0.0, 0, 0));
+  }
+
+  @Test
+  public void testDoubleEncodeDecodeWithFactor() {
+    // Verify that encode/decode with non-zero factor works correctly.
+    // The key correctness property: encode uses value * POW10[e] * 
POW10_NEGATIVE[f],
+    // and decode uses encoded * POW10[f] * POW10_NEGATIVE[e].
+    double value = 12.3;
+    long encoded = AlpEncoderDecoder.encodeDouble(value, 2, 1);
+    assertEquals(123L, encoded); // 12.3 * 100 * 0.1 = 123
+    double decoded = AlpEncoderDecoder.decodeDouble(encoded, 2, 1);
+    assertEquals(Double.doubleToRawLongBits(value), 
Double.doubleToRawLongBits(decoded));
+  }
+
+  @Test
+  public void testDoubleEncodeDecodeArithmeticOrder() {
+    // This test verifies that the exact order of operations in encode/decode
+    // is critical for IEEE 754 correctness. The encode uses
+    // fastRound(value * POW10[e] * POW10_NEGATIVE[f]) and decode uses
+    // (encoded * POW10[f] * POW10_NEGATIVE[e]), both as single expressions.
+    // Splitting the multiplies or reordering changes rounding.
+    double[] testValues = {0.123456789, 1.23456789, 12.3456789, 123.456789, 
1234.56789};
+    for (double value : testValues) {
+      for (int e = 0; e <= 10; e++) {
+        for (int f = 0; f <= e; f++) {
+          if (!AlpEncoderDecoder.isDoubleException(value, e, f)) {
+            long encoded = AlpEncoderDecoder.encodeDouble(value, e, f);
+            double decoded = AlpEncoderDecoder.decodeDouble(encoded, e, f);
+            assertEquals(
+                "Roundtrip failed for " + value + " (e=" + e + ", f=" + f + 
")",
+                Double.doubleToRawLongBits(value),
+                Double.doubleToRawLongBits(decoded));
+          }
+        }
+      }
+    }
+  }
+
+  // ========== Bit Width Tests (renamed methods) ==========

Review Comment:
   bitWidthForLong isn't reimplementing an existing BytesUtils method, it's the 
long counterpart to getWidthFromMaxInt, which only handles ints. The javadoc on 
it says so now. This test just covers that helper.



##########
parquet-column/src/test/java/org/apache/parquet/column/values/alp/AlpBitPackingTest.java:
##########
@@ -0,0 +1,283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.parquet.column.values.alp;
+
+import static org.junit.Assert.*;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.parquet.bytes.ByteBufferInputStream;
+import org.apache.parquet.bytes.BytesInput;
+import org.apache.parquet.bytes.DirectByteBufferAllocator;
+import org.apache.parquet.column.values.bitpacking.BytePacker;
+import org.apache.parquet.column.values.bitpacking.BytePackerForLong;
+import org.apache.parquet.column.values.bitpacking.Packer;
+import org.junit.Test;
+
+/**
+ * Tests for bit-packing behavior in the ALP encoding pipeline.
+ */
+public class AlpBitPackingTest {
+
+  @Test
+  public void testBytePackerIntRoundTrip() {
+    // Verify BytePacker pack/unpack round-trip for various bit widths
+    for (int bitWidth = 1; bitWidth <= 31; bitWidth++) {
+      BytePacker packer = Packer.LITTLE_ENDIAN.newBytePacker(bitWidth);
+      int maxVal = (int) Math.min((1L << bitWidth) - 1, Integer.MAX_VALUE);
+
+      int[] input = new int[8];
+      for (int i = 0; i < 8; i++) {
+        input[i] = (maxVal / 8) * i;
+      }
+
+      byte[] packed = new byte[bitWidth];
+      packer.pack8Values(input, 0, packed, 0);
+
+      int[] unpacked = new int[8];
+      ByteBuffer buf = ByteBuffer.wrap(packed);
+      packer.unpack8Values(buf, 0, unpacked, 0);
+
+      for (int i = 0; i < 8; i++) {
+        assertEquals("BitWidth=" + bitWidth + " index=" + i, input[i], 
unpacked[i]);
+      }
+    }
+  }
+
+  @Test
+  public void testBytePackerForLongRoundTrip() {
+    // Verify BytePackerForLong pack/unpack round-trip for various bit widths
+    for (int bitWidth = 1; bitWidth <= 63; bitWidth++) {

Review Comment:
   Extended the loops to the full width: int through 32 and long through 64, 
with the top bit of each width exercised.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to