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 java.math.BigDecimal; 021import org.apache.hadoop.hbase.HBaseClassTestRule; 022import org.apache.hadoop.hbase.testclassification.FilterTests; 023import org.apache.hadoop.hbase.testclassification.SmallTests; 024import org.apache.hadoop.hbase.util.Bytes; 025import org.junit.Assert; 026import org.junit.ClassRule; 027import org.junit.Test; 028import org.junit.experimental.categories.Category; 029 030@Category({ FilterTests.class, SmallTests.class }) 031public class TestBigDecimalComparator { 032 033 @ClassRule 034 public static final HBaseClassTestRule CLASS_RULE = 035 HBaseClassTestRule.forClass(TestBigDecimalComparator.class); 036 037 @Test 038 public void testObjectEquals() { 039 BigDecimal bd = new BigDecimal(Double.MIN_VALUE); 040 // Check that equals returns true for identical objects 041 final BigDecimalComparator bdc = new BigDecimalComparator(bd); 042 Assert.assertTrue(bdc.equals(bdc)); 043 Assert.assertEquals(bdc.hashCode(), bdc.hashCode()); 044 045 // Check that equals returns true for the same object 046 final BigDecimalComparator bdc1 = new BigDecimalComparator(bd); 047 final BigDecimalComparator bdc2 = new BigDecimalComparator(bd); 048 Assert.assertTrue(bdc1.equals(bdc2)); 049 Assert.assertEquals(bdc1.hashCode(), bdc2.hashCode()); 050 051 // Check that equals returns false for different objects 052 final BigDecimalComparator bdc3 = new BigDecimalComparator(bd); 053 final BigDecimalComparator bdc4 = new BigDecimalComparator(new BigDecimal(Long.MIN_VALUE)); 054 Assert.assertFalse(bdc3.equals(bdc4)); 055 Assert.assertNotEquals(bdc3.hashCode(), bdc4.hashCode()); 056 057 // Check that equals returns false for a different type 058 final BigDecimalComparator bdc5 = new BigDecimalComparator(bd); 059 Assert.assertFalse(bdc5.equals(0)); 060 } 061 062 @Test 063 public void testEqualsValue() { 064 // given 065 BigDecimal bd1 = new BigDecimal(Double.MAX_VALUE); 066 BigDecimal bd2 = new BigDecimal(Double.MIN_VALUE); 067 byte[] value1 = Bytes.toBytes(bd1); 068 byte[] value2 = Bytes.toBytes(bd2); 069 BigDecimalComparator comparator1 = new BigDecimalComparator(bd1); 070 BigDecimalComparator comparator2 = new BigDecimalComparator(bd2); 071 072 // when 073 int comp1 = comparator1.compareTo(value1); 074 int comp2 = comparator2.compareTo(value2); 075 076 // then 077 Assert.assertEquals(0, comp1); 078 Assert.assertEquals(0, comp2); 079 } 080 081 @Test 082 public void testGreaterThanValue() { 083 // given 084 byte[] val1 = Bytes.toBytes(new BigDecimal("1000000000000000000000000000000.9999999999999999")); 085 byte[] val2 = Bytes.toBytes(new BigDecimal(0)); 086 byte[] val3 = Bytes.toBytes(new BigDecimal(Double.MIN_VALUE)); 087 BigDecimal bd = new BigDecimal(Double.MAX_VALUE); 088 BigDecimalComparator comparator = new BigDecimalComparator(bd); 089 090 // when 091 int comp1 = comparator.compareTo(val1); 092 int comp2 = comparator.compareTo(val2); 093 int comp3 = comparator.compareTo(val3); 094 095 // then 096 Assert.assertEquals(1, comp1); 097 Assert.assertEquals(1, comp2); 098 Assert.assertEquals(1, comp3); 099 } 100 101 @Test 102 public void testLessThanValue() { 103 // given 104 byte[] val1 = Bytes.toBytes(new BigDecimal("-1000000000000000000000000000000")); 105 byte[] val2 = Bytes.toBytes(new BigDecimal(0)); 106 byte[] val3 = Bytes.toBytes(new BigDecimal(1)); 107 BigDecimal bd = new BigDecimal("-1000000000000000000000000000000.0000000000000001"); 108 BigDecimalComparator comparator = new BigDecimalComparator(bd); 109 110 // when 111 int comp1 = comparator.compareTo(val1); 112 int comp2 = comparator.compareTo(val2); 113 int comp3 = comparator.compareTo(val3); 114 115 // then 116 Assert.assertEquals(-1, comp1); 117 Assert.assertEquals(-1, comp2); 118 Assert.assertEquals(-1, comp3); 119 } 120 121}