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