2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.util
;
20 import static org
.junit
.Assert
.assertTrue
;
22 import java
.nio
.ByteBuffer
;
23 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
24 import org
.apache
.hadoop
.hbase
.nio
.ByteBuff
;
25 import org
.apache
.hadoop
.hbase
.nio
.MultiByteBuff
;
26 import org
.apache
.hadoop
.hbase
.nio
.SingleByteBuff
;
27 import org
.apache
.hadoop
.hbase
.testclassification
.MiscTests
;
28 import org
.apache
.hadoop
.hbase
.testclassification
.SmallTests
;
29 import org
.junit
.ClassRule
;
30 import org
.junit
.Test
;
31 import org
.junit
.experimental
.categories
.Category
;
33 @Category({ MiscTests
.class, SmallTests
.class })
34 public class TestByteBuffUtils
{
37 public static final HBaseClassTestRule CLASS_RULE
=
38 HBaseClassTestRule
.forClass(TestByteBuffUtils
.class);
41 public void testCopyAndCompare() throws Exception
{
42 ByteBuffer bb1
= ByteBuffer
.allocate(50);
43 ByteBuffer bb2
= ByteBuffer
.allocate(50);
44 MultiByteBuff src
= new MultiByteBuff(bb1
, bb2
);
45 for (int i
= 0; i
< 7; i
++) {
50 ByteBuffer bb3
= ByteBuffer
.allocate(50);
51 ByteBuffer bb4
= ByteBuffer
.allocate(50);
52 MultiByteBuff mbbDst
= new MultiByteBuff(bb3
, bb4
);
53 // copy from MBB to MBB
54 mbbDst
.put(0, src
, 0, 100);
55 int compareTo
= ByteBuff
.compareTo(src
, 0, 100, mbbDst
, 0, 100);
56 assertTrue(compareTo
== 0);
57 // Copy from MBB to SBB
58 bb3
= ByteBuffer
.allocate(100);
59 SingleByteBuff sbbDst
= new SingleByteBuff(bb3
);
61 sbbDst
.put(0, src
, 0, 100);
62 compareTo
= ByteBuff
.compareTo(src
, 0, 100, sbbDst
, 0, 100);
63 assertTrue(compareTo
== 0);
64 // Copy from SBB to SBB
65 bb3
= ByteBuffer
.allocate(100);
66 SingleByteBuff sbb
= new SingleByteBuff(bb3
);
67 for (int i
= 0; i
< 7; i
++) {
72 bb4
= ByteBuffer
.allocate(100);
73 sbbDst
= new SingleByteBuff(bb4
);
74 sbbDst
.put(0, sbb
, 0, 100);
75 compareTo
= ByteBuff
.compareTo(sbb
, 0, 100, sbbDst
, 0, 100);
76 assertTrue(compareTo
== 0);
77 // copy from SBB to MBB
79 mbbDst
= new MultiByteBuff(bb3
, bb4
);
81 mbbDst
.put(0, sbb
, 0, 100);
82 compareTo
= ByteBuff
.compareTo(sbb
, 0, 100, mbbDst
, 0, 100);
83 assertTrue(compareTo
== 0);