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 = new Long[] {
039    1L, 22L, 333L, 4444L, 55555L, 666666L, 7777777L, 88888888L, 999999999L
040  };
041
042  private static final Double[] DOUBLE_VALUES = new Double[] {
043    Double.NaN, 1.1, 22.2, 333.3, 4444.4, 55555.5, 666666.6, 7777777.7, 88888888.8, 999999999.9
044  };
045
046  private static final BigDecimal[] BIG_DECIMAL_VALUES = new BigDecimal[] {
047    new BigDecimal(1), new BigDecimal(22), new BigDecimal(333), new BigDecimal(4444),
048    new BigDecimal(55555), new BigDecimal(666666), new BigDecimal(7777777),
049    new BigDecimal(88888888), new BigDecimal(999999999)
050  };
051
052  private static final BigInteger[] BIG_INTEGER_VALUES = new BigInteger[] {
053    new BigInteger("1"), new BigInteger("22"), new BigInteger("333"), new BigInteger("4444"),
054    new BigInteger("55555"), new BigInteger("666666"), new BigInteger("7777777"),
055    new BigInteger("88888888"), new BigInteger("999999999")
056  };
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060      HBaseClassTestRule.forClass(TestOrderedNumeric.class);
061
062  @Rule
063  public ExpectedException exception = ExpectedException.none();
064
065  @Test
066  public void testEncodedClassIsNumber() {
067    final DataType<Number> type = new OrderedNumeric(Order.ASCENDING);
068
069    assertEquals(Number.class, type.encodedClass());
070  }
071
072  @Test
073  public void testEncodedLength() {
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 : DOUBLE_VALUES) {
078        buffer.setPosition(0);
079        type.encode(buffer, val);
080        assertEquals("encodedLength does not match actual, " + val,
081            buffer.getPosition(), type.encodedLength(val));
082      }
083    }
084  }
085
086  @Test
087  public void testEncodedBigDecimalLength() {
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_DECIMAL_VALUES) {
092        buffer.setPosition(0);
093        type.encode(buffer, val);
094        assertEquals("encodedLength does not match actual, " + val,
095            buffer.getPosition(), type.encodedLength(val));
096      }
097    }
098  }
099
100  @Test
101  public void testEncodedBigIntegerLength() {
102    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
103    for (final DataType<Number> type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING),
104      new OrderedNumeric(Order.DESCENDING) }) {
105      for (final Number val : BIG_INTEGER_VALUES) {
106        buffer.setPosition(0);
107        type.encode(buffer, val);
108        assertEquals("encodedLength does not match actual, " + val,
109            buffer.getPosition(), type.encodedLength(val));
110      }
111    }
112  }
113
114  @Test
115  public void testEncodedNullLength() {
116    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
117    final DataType<Number> type = new OrderedNumeric(Order.ASCENDING);
118
119    buffer.setPosition(0);
120    type.encode(buffer, null);
121    type.encode(new SimplePositionedMutableByteRange(20), null);
122
123    assertEquals("encodedLength does not match actual, " + null,
124        buffer.getPosition(), type.encodedLength(null));
125  }
126
127  @Test
128  public void testEncodedLongLength() {
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 Long val : LONG_VALUES) {
133        buffer.setPosition(0);
134        type.encodeLong(buffer, val);
135        assertEquals("encodedLength does not match actual, " + val,
136            buffer.getPosition(), type.encodedLength(val));
137      }
138    }
139  }
140
141  @Test
142  public void testEncodedDoubleLength() {
143    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
144    for (final OrderedNumeric type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING),
145      new OrderedNumeric(Order.DESCENDING) }) {
146      for (final Double val : DOUBLE_VALUES) {
147        buffer.setPosition(0);
148        type.encodeDouble(buffer, val);
149        assertEquals("encodedLength does not match actual, " + val,
150            buffer.getPosition(), type.encodedLength(val));
151      }
152    }
153  }
154}