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.util; 019 020import java.nio.ByteBuffer; 021import org.apache.hadoop.hbase.HBaseClassTestRule; 022import org.apache.hadoop.hbase.testclassification.MiscTests; 023import org.apache.hadoop.hbase.testclassification.SmallTests; 024import org.junit.Assert; 025import org.junit.ClassRule; 026import org.junit.Test; 027import org.junit.experimental.categories.Category; 028 029@Category({ MiscTests.class, SmallTests.class }) 030public class TestSimplePositionedMutableByteRange { 031 032 @ClassRule 033 public static final HBaseClassTestRule CLASS_RULE = 034 HBaseClassTestRule.forClass(TestSimplePositionedMutableByteRange.class); 035 036 @Test 037 public void testPosition() { 038 PositionedByteRange r = new SimplePositionedMutableByteRange(new byte[5], 1, 3); 039 040 // exercise single-byte put 041 r.put(Bytes.toBytes("f")[0]).put(Bytes.toBytes("o")[0]).put(Bytes.toBytes("o")[0]); 042 Assert.assertEquals(3, r.getPosition()); 043 Assert.assertArrayEquals( 044 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, 045 r.getBytes()); 046 047 // exercise multi-byte put 048 r.setPosition(0); 049 r.put(Bytes.toBytes("f")).put(Bytes.toBytes("o")).put(Bytes.toBytes("o")); 050 Assert.assertEquals(3, r.getPosition()); 051 Assert.assertArrayEquals( 052 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, 053 r.getBytes()); 054 055 // exercise single-byte get 056 r.setPosition(0); 057 Assert.assertEquals(Bytes.toBytes("f")[0], r.get()); 058 Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); 059 Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); 060 061 r.setPosition(1); 062 Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); 063 064 // exercise multi-byte get 065 r.setPosition(0); 066 byte[] dst = new byte[3]; 067 r.get(dst); 068 Assert.assertArrayEquals(Bytes.toBytes("foo"), dst); 069 070 // set position to the end of the range; this should not throw. 071 r.setPosition(3); 072 } 073 074 @Test 075 public void testPutAndGetPrimitiveTypes() { 076 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); 077 int i1 = 18, i2 = 2; 078 short s1 = 0; 079 long l1 = 1234L; 080 pbr.putInt(i1); 081 pbr.putInt(i2); 082 pbr.putShort(s1); 083 pbr.putLong(l1); 084 pbr.putVLong(0); 085 pbr.putVLong(l1); 086 pbr.putVLong(Long.MAX_VALUE); 087 pbr.putVLong(Long.MIN_VALUE); 088 // rewind 089 pbr.setPosition(0); 090 Assert.assertEquals(i1, pbr.getInt()); 091 Assert.assertEquals(i2, pbr.getInt()); 092 Assert.assertEquals(s1, pbr.getShort()); 093 Assert.assertEquals(l1, pbr.getLong()); 094 Assert.assertEquals(0, pbr.getVLong()); 095 Assert.assertEquals(l1, pbr.getVLong()); 096 Assert.assertEquals(Long.MAX_VALUE, pbr.getVLong()); 097 Assert.assertEquals(Long.MIN_VALUE, pbr.getVLong()); 098 } 099 100 @Test 101 public void testPutGetAPIsCompareWithBBAPIs() { 102 // confirm that the long/int/short writing is same as BBs 103 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); 104 int i1 = -234, i2 = 2; 105 short s1 = 0; 106 long l1 = 1234L; 107 pbr.putInt(i1); 108 pbr.putShort(s1); 109 pbr.putInt(i2); 110 pbr.putLong(l1); 111 // rewind 112 pbr.setPosition(0); 113 Assert.assertEquals(i1, pbr.getInt()); 114 Assert.assertEquals(s1, pbr.getShort()); 115 Assert.assertEquals(i2, pbr.getInt()); 116 Assert.assertEquals(l1, pbr.getLong()); 117 // Read back using BB APIs 118 ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes()); 119 Assert.assertEquals(i1, bb.getInt()); 120 Assert.assertEquals(s1, bb.getShort()); 121 Assert.assertEquals(i2, bb.getInt()); 122 Assert.assertEquals(l1, bb.getLong()); 123 } 124}