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 TestOrderedFloat64 { 035 private static final Double[] VALUES = new Double[] { Double.NaN, 1.1, 22.2, 333.3, 4444.4, 036 55555.5, 666666.6, 7777777.7, 88888888.8, 999999999.9 }; 037 038 @Test 039 public void testIsNullableIsFalse() { 040 final DataType<Double> type = new OrderedFloat64(Order.ASCENDING); 041 042 assertFalse(type.isNullable()); 043 } 044 045 @Test 046 public void testEncodedClassIsDouble() { 047 final DataType<Double> type = new OrderedFloat64(Order.ASCENDING); 048 049 assertEquals(Double.class, type.encodedClass()); 050 } 051 052 @Test 053 public void testEncodedLength() { 054 final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20); 055 for (final DataType<Double> type : new OrderedFloat64[] { new OrderedFloat64(Order.ASCENDING), 056 new OrderedFloat64(Order.DESCENDING) }) { 057 for (final Double 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<Double> type = new OrderedFloat64(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 OrderedFloat64 type : new OrderedFloat64[] { new OrderedFloat64(Order.ASCENDING), 078 new OrderedFloat64(Order.DESCENDING) }) { 079 for (final Double val : VALUES) { 080 buffer.setPosition(0); 081 type.encodeDouble(buffer, val); 082 assertEquals(buffer.getPosition(), type.encodedLength(val), 083 "encodedLength does not match actual, " + val); 084 } 085 } 086 } 087}