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 static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.nio.ByteBuffer; 023import org.apache.hadoop.hbase.ByteBufferExtendedCell; 024import org.apache.hadoop.hbase.ByteBufferKeyValue; 025import org.apache.hadoop.hbase.Cell; 026import org.apache.hadoop.hbase.KeyValue; 027import org.junit.jupiter.api.Test; 028 029public abstract class TestLittleEndianBytesBase { 030 031 @Test 032 public void testToInt() { 033 byte[] b = generateByteArray(32); 034 035 for (int i = 0; i <= b.length - Integer.BYTES; i++) { 036 int expected = readIntLE(b, i); 037 assertEquals(expected, LittleEndianBytes.toInt(b, i)); 038 } 039 } 040 041 @Test 042 public void testByteBufferToInt() { 043 byte[] b = generateByteArray(32); 044 ByteBuffer buf = ByteBuffer.wrap(b); 045 046 for (int i = 0; i <= b.length - Integer.BYTES; i++) { 047 int expected = readIntLE(b, i); 048 assertEquals(expected, LittleEndianBytes.toInt(buf, i)); 049 } 050 } 051 052 @Test 053 public void testPutInt() { 054 byte[] b = new byte[16]; 055 056 int offset = 5; 057 int value = 0x12345678; 058 LittleEndianBytes.putInt(b, offset, value); 059 int expected = readIntLE(b, offset); 060 assertEquals(value, expected); 061 062 offset += Integer.BYTES; 063 value = 0x9ABCDEF0; 064 LittleEndianBytes.putInt(b, offset, value); 065 expected = readIntLE(b, offset); 066 assertEquals(value, expected); 067 } 068 069 @Test 070 public void testGetRowAsIntFromByteBufferExtendedCell() { 071 Cell bbCell = createByteBufferExtendedCell(); 072 byte[] row = bbCell.getRowArray(); 073 074 for (int i = bbCell.getRowOffset(); i <= bbCell.getRowLength() - Integer.BYTES; i++) { 075 int expected = readIntLE(row, i); 076 assertEquals(expected, LittleEndianBytes.getRowAsInt(bbCell, i)); 077 } 078 } 079 080 @Test 081 public void testGetRowAsIntFromCell() { 082 KeyValue cell = createCell(); 083 byte[] row = cell.getRowArray(); 084 085 for (int i = 0; i <= cell.getRowLength() - Integer.BYTES; i++) { 086 int expected = readIntLE(row, cell.getRowOffset() + i); 087 assertEquals(expected, LittleEndianBytes.getRowAsInt(cell, i)); 088 } 089 } 090 091 @Test 092 public void testGetQualifierAsIntFromByteBufferExtendedCell() { 093 Cell bbCell = createByteBufferExtendedCell(); 094 byte[] qual = bbCell.getQualifierArray(); 095 096 for (int i = 0; i <= bbCell.getQualifierLength() - Integer.BYTES; i++) { 097 int expected = readIntLE(qual, bbCell.getQualifierOffset() + i); 098 assertEquals(expected, LittleEndianBytes.getQualifierAsInt(bbCell, i)); 099 } 100 } 101 102 @Test 103 public void testGetQualifierAsIntFromCell() { 104 KeyValue cell = createCell(); 105 byte[] qual = cell.getQualifierArray(); 106 107 for (int i = 0; i <= cell.getQualifierLength() - Integer.BYTES; i++) { 108 int expected = readIntLE(qual, cell.getQualifierOffset() + i); 109 assertEquals(expected, LittleEndianBytes.getQualifierAsInt(cell, i)); 110 } 111 } 112 113 private static KeyValue createCell() { 114 byte[] row = Bytes.toBytes("row_key_for_test_12345"); 115 byte[] family = Bytes.toBytes("f"); 116 byte[] qualifier = Bytes.toBytes("qualifier_12345"); 117 byte[] value = Bytes.toBytes(123456789); 118 return new KeyValue(row, family, qualifier, value); 119 } 120 121 private static ByteBufferExtendedCell createByteBufferExtendedCell() { 122 KeyValue kv = createCell(); 123 ByteBuffer buffer = ByteBuffer.wrap(kv.getBuffer()); 124 return new ByteBufferKeyValue(buffer, 0, buffer.remaining()); 125 } 126 127 private static byte[] generateByteArray(int size) { 128 byte[] b = new byte[size]; 129 for (int i = 0; i < b.length; i++) { 130 b[i] = (byte) (i * 3 + 7); 131 } 132 return b; 133 } 134 135 private static int readIntLE(byte[] b, int off) { 136 return (b[off] & 0xFF) | ((b[off + 1] & 0xFF) << 8) | ((b[off + 2] & 0xFF) << 16) 137 | ((b[off + 3] & 0xFF) << 24); 138 } 139}