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;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertThrows;
023
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 TestOrderedFloat32 {
035  private static final Float[] VALUES =
036    new Float[] { Float.NaN, 1f, 22f, 333f, 4444f, 55555f, 666666f, 7777777f, 8888888f, 9999999f };
037
038  @Test
039  public void testIsNullableIsFalse() {
040    final DataType<Float> type = new OrderedFloat32(Order.ASCENDING);
041
042    assertFalse(type.isNullable());
043  }
044
045  @Test
046  public void testEncodedClassIsFloat() {
047    final DataType<Float> type = new OrderedFloat32(Order.ASCENDING);
048
049    assertEquals(Float.class, type.encodedClass());
050  }
051
052  @Test
053  public void testEncodedLength() {
054    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
055    for (final DataType<Float> type : new OrderedFloat32[] { new OrderedFloat32(Order.ASCENDING),
056      new OrderedFloat32(Order.DESCENDING) }) {
057      for (final Float val : VALUES) {
058        buffer.setPosition(0);
059        type.encode(buffer, val);
060        assertEquals(buffer.getPosition(), type.encodedLength(val),
061          "encodedLength does not match actual, " + val);
062      }
063    }
064  }
065
066  @Test
067  public void testEncodeNoSupportForNull() {
068    final DataType<Float> type = new OrderedFloat32(Order.ASCENDING);
069
070    assertThrows(IllegalArgumentException.class,
071      () -> type.encode(new SimplePositionedMutableByteRange(20), null));
072  }
073
074  @Test
075  public void testEncodedFloatLength() {
076    final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
077    for (final OrderedFloat32 type : new OrderedFloat32[] { new OrderedFloat32(Order.ASCENDING),
078      new OrderedFloat32(Order.DESCENDING) }) {
079      for (final Float val : VALUES) {
080        buffer.setPosition(0);
081        type.encodeFloat(buffer, val);
082        assertEquals(buffer.getPosition(), type.encodedLength(val),
083          "encodedLength does not match actual, " + val);
084      }
085    }
086  }
087}