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;
021import static org.junit.Assert.assertFalse;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
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.ClassRule;
030import org.junit.Rule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033import org.junit.rules.ExpectedException;
034
035@Category({ MiscTests.class, SmallTests.class })
036public class TestOrderedFloat64 {
037  private static final Double[] VALUES = new Double[] { Double.NaN, 1.1, 22.2, 333.3, 4444.4,
038    55555.5, 666666.6, 7777777.7, 88888888.8, 999999999.9 };
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestOrderedFloat64.class);
043
044  @Rule
045  public ExpectedException exception = ExpectedException.none();
046
047  @Test
048  public void testIsNullableIsFalse() {
049    final DataType<Double> type = new OrderedFloat64(Order.ASCENDING);
050
051    assertFalse(type.isNullable());
052  }
053
054  @Test
055  public void testEncodedClassIsDouble() {
056    final DataType<Double> type = new OrderedFloat64(Order.ASCENDING);
057
058    assertEquals(Double.class, type.encodedClass());
059  }
060
061  @Test
062  public void testEncodedLength() {
063    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
064    for (final DataType<Double> type : new OrderedFloat64[] { new OrderedFloat64(Order.ASCENDING),
065      new OrderedFloat64(Order.DESCENDING) }) {
066      for (final Double val : VALUES) {
067        buffer.setPosition(0);
068        type.encode(buffer, val);
069        assertEquals("encodedLength does not match actual, " + val, buffer.getPosition(),
070          type.encodedLength(val));
071      }
072    }
073  }
074
075  @Test
076  public void testEncodeNoSupportForNull() {
077    exception.expect(IllegalArgumentException.class);
078
079    final DataType<Double> type = new OrderedFloat64(Order.ASCENDING);
080
081    type.encode(new SimplePositionedMutableByteRange(20), null);
082  }
083
084  @Test
085  public void testEncodedFloatLength() {
086    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
087    for (final OrderedFloat64 type : new OrderedFloat64[] { new OrderedFloat64(Order.ASCENDING),
088      new OrderedFloat64(Order.DESCENDING) }) {
089      for (final Double val : VALUES) {
090        buffer.setPosition(0);
091        type.encodeDouble(buffer, val);
092        assertEquals("encodedLength does not match actual, " + val, buffer.getPosition(),
093          type.encodedLength(val));
094      }
095    }
096  }
097}