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.types; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.math.BigDecimal; 023import java.math.BigInteger; 024import org.apache.hadoop.hbase.testclassification.MiscTests; 025import org.apache.hadoop.hbase.testclassification.SmallTests; 026import org.apache.hadoop.hbase.util.Order; 027import org.apache.hadoop.hbase.util.PositionedByteRange; 028import org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange; 029import org.junit.jupiter.api.Tag; 030import org.junit.jupiter.api.Test; 031 032@Tag(MiscTests.TAG) 033@Tag(SmallTests.TAG) 034public class TestOrderedNumeric { 035 private static final Long[] LONG_VALUES = 036 new Long[] { 1L, 22L, 333L, 4444L, 55555L, 666666L, 7777777L, 88888888L, 999999999L }; 037 038 private static final Double[] DOUBLE_VALUES = new Double[] { Double.NaN, 1.1, 22.2, 333.3, 4444.4, 039 55555.5, 666666.6, 7777777.7, 88888888.8, 999999999.9 }; 040 041 private static final BigDecimal[] BIG_DECIMAL_VALUES = 042 new BigDecimal[] { new BigDecimal(1), new BigDecimal(22), new BigDecimal(333), 043 new BigDecimal(4444), new BigDecimal(55555), new BigDecimal(666666), new BigDecimal(7777777), 044 new BigDecimal(88888888), new BigDecimal(999999999) }; 045 046 private static final BigInteger[] BIG_INTEGER_VALUES = 047 new BigInteger[] { new BigInteger("1"), new BigInteger("22"), new BigInteger("333"), 048 new BigInteger("4444"), new BigInteger("55555"), new BigInteger("666666"), 049 new BigInteger("7777777"), new BigInteger("88888888"), new BigInteger("999999999") }; 050 051 @Test 052 public void testEncodedClassIsNumber() { 053 final DataType<Number> type = new OrderedNumeric(Order.ASCENDING); 054 055 assertEquals(Number.class, type.encodedClass()); 056 } 057 058 @Test 059 public void testEncodedLength() { 060 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 061 for (final DataType<Number> type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING), 062 new OrderedNumeric(Order.DESCENDING) }) { 063 for (final Number val : DOUBLE_VALUES) { 064 buffer.setPosition(0); 065 type.encode(buffer, val); 066 assertEquals(buffer.getPosition(), type.encodedLength(val), 067 "encodedLength does not match actual, " + val); 068 } 069 } 070 } 071 072 @Test 073 public void testEncodedBigDecimalLength() { 074 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 075 for (final DataType<Number> type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING), 076 new OrderedNumeric(Order.DESCENDING) }) { 077 for (final Number val : BIG_DECIMAL_VALUES) { 078 buffer.setPosition(0); 079 type.encode(buffer, val); 080 assertEquals(buffer.getPosition(), type.encodedLength(val), 081 "encodedLength does not match actual, " + val); 082 } 083 } 084 } 085 086 @Test 087 public void testEncodedBigIntegerLength() { 088 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 089 for (final DataType<Number> type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING), 090 new OrderedNumeric(Order.DESCENDING) }) { 091 for (final Number val : BIG_INTEGER_VALUES) { 092 buffer.setPosition(0); 093 type.encode(buffer, val); 094 assertEquals(buffer.getPosition(), type.encodedLength(val), 095 "encodedLength does not match actual, " + val); 096 } 097 } 098 } 099 100 @Test 101 public void testEncodedNullLength() { 102 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 103 final DataType<Number> type = new OrderedNumeric(Order.ASCENDING); 104 105 buffer.setPosition(0); 106 type.encode(buffer, null); 107 type.encode(new SimplePositionedMutableByteRange(20), null); 108 109 assertEquals(buffer.getPosition(), type.encodedLength(null), 110 "encodedLength does not match actual, " + null); 111 } 112 113 @Test 114 public void testEncodedLongLength() { 115 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 116 for (final OrderedNumeric type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING), 117 new OrderedNumeric(Order.DESCENDING) }) { 118 for (final Long val : LONG_VALUES) { 119 buffer.setPosition(0); 120 type.encodeLong(buffer, val); 121 assertEquals(buffer.getPosition(), type.encodedLength(val), 122 "encodedLength does not match actual, " + val); 123 } 124 } 125 } 126 127 @Test 128 public void testEncodedDoubleLength() { 129 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 130 for (final OrderedNumeric type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING), 131 new OrderedNumeric(Order.DESCENDING) }) { 132 for (final Double val : DOUBLE_VALUES) { 133 buffer.setPosition(0); 134 type.encodeDouble(buffer, val); 135 assertEquals(buffer.getPosition(), type.encodedLength(val), 136 "encodedLength does not match actual, " + val); 137 } 138 } 139 } 140}