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]) 042 .put(Bytes.toBytes("o")[0]) 043 .put(Bytes.toBytes("o")[0]); 044 Assert.assertEquals(3, r.getPosition()); 045 Assert.assertArrayEquals( 046 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, 047 r.getBytes()); 048 049 // exercise multi-byte put 050 r.setPosition(0); 051 r.put(Bytes.toBytes("f")) 052 .put(Bytes.toBytes("o")) 053 .put(Bytes.toBytes("o")); 054 Assert.assertEquals(3, r.getPosition()); 055 Assert.assertArrayEquals( 056 new byte[] { 0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0 }, 057 r.getBytes()); 058 059 // exercise single-byte get 060 r.setPosition(0); 061 Assert.assertEquals(Bytes.toBytes("f")[0], r.get()); 062 Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); 063 Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); 064 065 r.setPosition(1); 066 Assert.assertEquals(Bytes.toBytes("o")[0], r.get()); 067 068 // exercise multi-byte get 069 r.setPosition(0); 070 byte[] dst = new byte[3]; 071 r.get(dst); 072 Assert.assertArrayEquals(Bytes.toBytes("foo"), dst); 073 074 // set position to the end of the range; this should not throw. 075 r.setPosition(3); 076 } 077 078 @Test 079 public void testPutAndGetPrimitiveTypes() throws Exception { 080 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); 081 int i1 = 18, i2 = 2; 082 short s1 = 0; 083 long l1 = 1234L; 084 pbr.putInt(i1); 085 pbr.putInt(i2); 086 pbr.putShort(s1); 087 pbr.putLong(l1); 088 pbr.putVLong(0); 089 pbr.putVLong(l1); 090 pbr.putVLong(Long.MAX_VALUE); 091 pbr.putVLong(Long.MIN_VALUE); 092 // rewind 093 pbr.setPosition(0); 094 Assert.assertEquals(i1, pbr.getInt()); 095 Assert.assertEquals(i2, pbr.getInt()); 096 Assert.assertEquals(s1, pbr.getShort()); 097 Assert.assertEquals(l1, pbr.getLong()); 098 Assert.assertEquals(0, pbr.getVLong()); 099 Assert.assertEquals(l1, pbr.getVLong()); 100 Assert.assertEquals(Long.MAX_VALUE, pbr.getVLong()); 101 Assert.assertEquals(Long.MIN_VALUE, pbr.getVLong()); 102 } 103 104 @Test 105 public void testPutGetAPIsCompareWithBBAPIs() throws Exception { 106 // confirm that the long/int/short writing is same as BBs 107 PositionedByteRange pbr = new SimplePositionedMutableByteRange(100); 108 int i1 = -234, i2 = 2; 109 short s1 = 0; 110 long l1 = 1234L; 111 pbr.putInt(i1); 112 pbr.putShort(s1); 113 pbr.putInt(i2); 114 pbr.putLong(l1); 115 // rewind 116 pbr.setPosition(0); 117 Assert.assertEquals(i1, pbr.getInt()); 118 Assert.assertEquals(s1, pbr.getShort()); 119 Assert.assertEquals(i2, pbr.getInt()); 120 Assert.assertEquals(l1, pbr.getLong()); 121 // Read back using BB APIs 122 ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes()); 123 Assert.assertEquals(i1, bb.getInt()); 124 Assert.assertEquals(s1, bb.getShort()); 125 Assert.assertEquals(i2, bb.getInt()); 126 Assert.assertEquals(l1, bb.getLong()); 127 } 128}