001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.filter;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNotEquals;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.math.BigDecimal;
026import org.apache.hadoop.hbase.testclassification.FilterTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(FilterTests.TAG)
033@Tag(SmallTests.TAG)
034public class TestBigDecimalComparator {
035
036  @Test
037  public void testObjectEquals() {
038    BigDecimal bd = new BigDecimal(Double.MIN_VALUE);
039    // Check that equals returns true for identical objects
040    final BigDecimalComparator bdc = new BigDecimalComparator(bd);
041    assertTrue(bdc.equals(bdc));
042    assertEquals(bdc.hashCode(), bdc.hashCode());
043
044    // Check that equals returns true for the same object
045    final BigDecimalComparator bdc1 = new BigDecimalComparator(bd);
046    final BigDecimalComparator bdc2 = new BigDecimalComparator(bd);
047    assertTrue(bdc1.equals(bdc2));
048    assertEquals(bdc1.hashCode(), bdc2.hashCode());
049
050    // Check that equals returns false for different objects
051    final BigDecimalComparator bdc3 = new BigDecimalComparator(bd);
052    final BigDecimalComparator bdc4 = new BigDecimalComparator(new BigDecimal(Long.MIN_VALUE));
053    assertFalse(bdc3.equals(bdc4));
054    assertNotEquals(bdc3.hashCode(), bdc4.hashCode());
055
056    // Check that equals returns false for a different type
057    final BigDecimalComparator bdc5 = new BigDecimalComparator(bd);
058    assertFalse(bdc5.equals(0));
059  }
060
061  @Test
062  public void testEqualsValue() {
063    // given
064    BigDecimal bd1 = new BigDecimal(Double.MAX_VALUE);
065    BigDecimal bd2 = new BigDecimal(Double.MIN_VALUE);
066    byte[] value1 = Bytes.toBytes(bd1);
067    byte[] value2 = Bytes.toBytes(bd2);
068    BigDecimalComparator comparator1 = new BigDecimalComparator(bd1);
069    BigDecimalComparator comparator2 = new BigDecimalComparator(bd2);
070
071    // when
072    int comp1 = comparator1.compareTo(value1);
073    int comp2 = comparator2.compareTo(value2);
074
075    // then
076    assertEquals(0, comp1);
077    assertEquals(0, comp2);
078  }
079
080  @Test
081  public void testGreaterThanValue() {
082    // given
083    byte[] val1 = Bytes.toBytes(new BigDecimal("1000000000000000000000000000000.9999999999999999"));
084    byte[] val2 = Bytes.toBytes(new BigDecimal(0));
085    byte[] val3 = Bytes.toBytes(new BigDecimal(Double.MIN_VALUE));
086    BigDecimal bd = new BigDecimal(Double.MAX_VALUE);
087    BigDecimalComparator comparator = new BigDecimalComparator(bd);
088
089    // when
090    int comp1 = comparator.compareTo(val1);
091    int comp2 = comparator.compareTo(val2);
092    int comp3 = comparator.compareTo(val3);
093
094    // then
095    assertEquals(1, comp1);
096    assertEquals(1, comp2);
097    assertEquals(1, comp3);
098  }
099
100  @Test
101  public void testLessThanValue() {
102    // given
103    byte[] val1 = Bytes.toBytes(new BigDecimal("-1000000000000000000000000000000"));
104    byte[] val2 = Bytes.toBytes(new BigDecimal(0));
105    byte[] val3 = Bytes.toBytes(new BigDecimal(1));
106    BigDecimal bd = new BigDecimal("-1000000000000000000000000000000.0000000000000001");
107    BigDecimalComparator comparator = new BigDecimalComparator(bd);
108
109    // when
110    int comp1 = comparator.compareTo(val1);
111    int comp2 = comparator.compareTo(val2);
112    int comp3 = comparator.compareTo(val3);
113
114    // then
115    assertEquals(-1, comp1);
116    assertEquals(-1, comp2);
117    assertEquals(-1, comp3);
118  }
119
120}