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