blob: 20c3d500ef6ad43747ce12d3bd2b4444d13cbe8e [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Generated by javadoc (17) -->
<title>Source code</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="source: package: org.apache.hadoop.hbase.util, class: TestBytes">
<meta name="generator" content="javadoc/SourceToHTMLConverter">
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body class="source-page">
<main role="main">
<div class="source-container">
<pre><span class="source-line-no">001</span><span id="line-1">/*</span>
<span class="source-line-no">002</span><span id="line-2"> * Licensed to the Apache Software Foundation (ASF) under one</span>
<span class="source-line-no">003</span><span id="line-3"> * or more contributor license agreements. See the NOTICE file</span>
<span class="source-line-no">004</span><span id="line-4"> * distributed with this work for additional information</span>
<span class="source-line-no">005</span><span id="line-5"> * regarding copyright ownership. The ASF licenses this file</span>
<span class="source-line-no">006</span><span id="line-6"> * to you under the Apache License, Version 2.0 (the</span>
<span class="source-line-no">007</span><span id="line-7"> * "License"); you may not use this file except in compliance</span>
<span class="source-line-no">008</span><span id="line-8"> * with the License. You may obtain a copy of the License at</span>
<span class="source-line-no">009</span><span id="line-9"> *</span>
<span class="source-line-no">010</span><span id="line-10"> * http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="source-line-no">011</span><span id="line-11"> *</span>
<span class="source-line-no">012</span><span id="line-12"> * Unless required by applicable law or agreed to in writing, software</span>
<span class="source-line-no">013</span><span id="line-13"> * distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="source-line-no">014</span><span id="line-14"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="source-line-no">015</span><span id="line-15"> * See the License for the specific language governing permissions and</span>
<span class="source-line-no">016</span><span id="line-16"> * limitations under the License.</span>
<span class="source-line-no">017</span><span id="line-17"> */</span>
<span class="source-line-no">018</span><span id="line-18">package org.apache.hadoop.hbase.util;</span>
<span class="source-line-no">019</span><span id="line-19"></span>
<span class="source-line-no">020</span><span id="line-20">import static org.junit.Assert.assertArrayEquals;</span>
<span class="source-line-no">021</span><span id="line-21">import static org.junit.Assert.assertEquals;</span>
<span class="source-line-no">022</span><span id="line-22">import static org.junit.Assert.assertFalse;</span>
<span class="source-line-no">023</span><span id="line-23">import static org.junit.Assert.assertNotNull;</span>
<span class="source-line-no">024</span><span id="line-24">import static org.junit.Assert.assertNotSame;</span>
<span class="source-line-no">025</span><span id="line-25">import static org.junit.Assert.assertTrue;</span>
<span class="source-line-no">026</span><span id="line-26">import static org.junit.Assert.fail;</span>
<span class="source-line-no">027</span><span id="line-27"></span>
<span class="source-line-no">028</span><span id="line-28">import java.io.ByteArrayInputStream;</span>
<span class="source-line-no">029</span><span id="line-29">import java.io.ByteArrayOutputStream;</span>
<span class="source-line-no">030</span><span id="line-30">import java.io.DataInputStream;</span>
<span class="source-line-no">031</span><span id="line-31">import java.io.DataOutputStream;</span>
<span class="source-line-no">032</span><span id="line-32">import java.io.IOException;</span>
<span class="source-line-no">033</span><span id="line-33">import java.lang.reflect.Field;</span>
<span class="source-line-no">034</span><span id="line-34">import java.lang.reflect.Modifier;</span>
<span class="source-line-no">035</span><span id="line-35">import java.math.BigDecimal;</span>
<span class="source-line-no">036</span><span id="line-36">import java.nio.ByteBuffer;</span>
<span class="source-line-no">037</span><span id="line-37">import java.util.ArrayList;</span>
<span class="source-line-no">038</span><span id="line-38">import java.util.Arrays;</span>
<span class="source-line-no">039</span><span id="line-39">import java.util.List;</span>
<span class="source-line-no">040</span><span id="line-40">import java.util.Random;</span>
<span class="source-line-no">041</span><span id="line-41">import java.util.concurrent.ThreadLocalRandom;</span>
<span class="source-line-no">042</span><span id="line-42">import org.apache.hadoop.hbase.HBaseClassTestRule;</span>
<span class="source-line-no">043</span><span id="line-43">import org.apache.hadoop.hbase.testclassification.MediumTests;</span>
<span class="source-line-no">044</span><span id="line-44">import org.apache.hadoop.hbase.testclassification.MiscTests;</span>
<span class="source-line-no">045</span><span id="line-45">import org.apache.hadoop.hbase.unsafe.HBasePlatformDependent;</span>
<span class="source-line-no">046</span><span id="line-46">import org.apache.hadoop.io.WritableUtils;</span>
<span class="source-line-no">047</span><span id="line-47">import org.junit.Assert;</span>
<span class="source-line-no">048</span><span id="line-48">import org.junit.ClassRule;</span>
<span class="source-line-no">049</span><span id="line-49">import org.junit.Test;</span>
<span class="source-line-no">050</span><span id="line-50">import org.junit.experimental.categories.Category;</span>
<span class="source-line-no">051</span><span id="line-51"></span>
<span class="source-line-no">052</span><span id="line-52">@Category({ MiscTests.class, MediumTests.class })</span>
<span class="source-line-no">053</span><span id="line-53">public class TestBytes {</span>
<span class="source-line-no">054</span><span id="line-54"> @ClassRule</span>
<span class="source-line-no">055</span><span id="line-55"> public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestBytes.class);</span>
<span class="source-line-no">056</span><span id="line-56"></span>
<span class="source-line-no">057</span><span id="line-57"> private static void setUnsafe(boolean value) throws Exception {</span>
<span class="source-line-no">058</span><span id="line-58"> Field field = Bytes.class.getDeclaredField("UNSAFE_UNALIGNED");</span>
<span class="source-line-no">059</span><span id="line-59"> field.setAccessible(true);</span>
<span class="source-line-no">060</span><span id="line-60"></span>
<span class="source-line-no">061</span><span id="line-61"> Field modifiersField = ReflectionUtils.getModifiersField();</span>
<span class="source-line-no">062</span><span id="line-62"> modifiersField.setAccessible(true);</span>
<span class="source-line-no">063</span><span id="line-63"> int oldModifiers = field.getModifiers();</span>
<span class="source-line-no">064</span><span id="line-64"> modifiersField.setInt(field, oldModifiers &amp; ~Modifier.FINAL);</span>
<span class="source-line-no">065</span><span id="line-65"> try {</span>
<span class="source-line-no">066</span><span id="line-66"> field.set(null, value);</span>
<span class="source-line-no">067</span><span id="line-67"> } finally {</span>
<span class="source-line-no">068</span><span id="line-68"> modifiersField.setInt(field, oldModifiers);</span>
<span class="source-line-no">069</span><span id="line-69"> }</span>
<span class="source-line-no">070</span><span id="line-70"> assertEquals(Bytes.UNSAFE_UNALIGNED, value);</span>
<span class="source-line-no">071</span><span id="line-71"> }</span>
<span class="source-line-no">072</span><span id="line-72"></span>
<span class="source-line-no">073</span><span id="line-73"> @Test</span>
<span class="source-line-no">074</span><span id="line-74"> public void testShort() throws Exception {</span>
<span class="source-line-no">075</span><span id="line-75"> testShort(false);</span>
<span class="source-line-no">076</span><span id="line-76"> }</span>
<span class="source-line-no">077</span><span id="line-77"></span>
<span class="source-line-no">078</span><span id="line-78"> @Test</span>
<span class="source-line-no">079</span><span id="line-79"> public void testShortUnsafe() throws Exception {</span>
<span class="source-line-no">080</span><span id="line-80"> testShort(true);</span>
<span class="source-line-no">081</span><span id="line-81"> }</span>
<span class="source-line-no">082</span><span id="line-82"></span>
<span class="source-line-no">083</span><span id="line-83"> private static void testShort(boolean unsafe) throws Exception {</span>
<span class="source-line-no">084</span><span id="line-84"> setUnsafe(unsafe);</span>
<span class="source-line-no">085</span><span id="line-85"> try {</span>
<span class="source-line-no">086</span><span id="line-86"> for (short n : Arrays.asList(Short.MIN_VALUE, (short) -100, (short) -1, (short) 0, (short) 1,</span>
<span class="source-line-no">087</span><span id="line-87"> (short) 300, Short.MAX_VALUE)) {</span>
<span class="source-line-no">088</span><span id="line-88"> byte[] bytes = Bytes.toBytes(n);</span>
<span class="source-line-no">089</span><span id="line-89"> assertEquals(Bytes.toShort(bytes, 0, bytes.length), n);</span>
<span class="source-line-no">090</span><span id="line-90"> }</span>
<span class="source-line-no">091</span><span id="line-91"> } finally {</span>
<span class="source-line-no">092</span><span id="line-92"> setUnsafe(HBasePlatformDependent.unaligned());</span>
<span class="source-line-no">093</span><span id="line-93"> }</span>
<span class="source-line-no">094</span><span id="line-94"> }</span>
<span class="source-line-no">095</span><span id="line-95"></span>
<span class="source-line-no">096</span><span id="line-96"> @Test</span>
<span class="source-line-no">097</span><span id="line-97"> public void testNullHashCode() {</span>
<span class="source-line-no">098</span><span id="line-98"> byte[] b = null;</span>
<span class="source-line-no">099</span><span id="line-99"> Exception ee = null;</span>
<span class="source-line-no">100</span><span id="line-100"> try {</span>
<span class="source-line-no">101</span><span id="line-101"> Bytes.hashCode(b);</span>
<span class="source-line-no">102</span><span id="line-102"> } catch (Exception e) {</span>
<span class="source-line-no">103</span><span id="line-103"> ee = e;</span>
<span class="source-line-no">104</span><span id="line-104"> }</span>
<span class="source-line-no">105</span><span id="line-105"> assertNotNull(ee);</span>
<span class="source-line-no">106</span><span id="line-106"> }</span>
<span class="source-line-no">107</span><span id="line-107"></span>
<span class="source-line-no">108</span><span id="line-108"> @Test</span>
<span class="source-line-no">109</span><span id="line-109"> public void testAdd() {</span>
<span class="source-line-no">110</span><span id="line-110"> byte[] a = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };</span>
<span class="source-line-no">111</span><span id="line-111"> byte[] b = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };</span>
<span class="source-line-no">112</span><span id="line-112"> byte[] c = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };</span>
<span class="source-line-no">113</span><span id="line-113"> byte[] result1 = Bytes.add(a, b, c);</span>
<span class="source-line-no">114</span><span id="line-114"> byte[] result2 = Bytes.add(new byte[][] { a, b, c });</span>
<span class="source-line-no">115</span><span id="line-115"> assertEquals(0, Bytes.compareTo(result1, result2));</span>
<span class="source-line-no">116</span><span id="line-116"> }</span>
<span class="source-line-no">117</span><span id="line-117"></span>
<span class="source-line-no">118</span><span id="line-118"> @Test</span>
<span class="source-line-no">119</span><span id="line-119"> public void testSplit() {</span>
<span class="source-line-no">120</span><span id="line-120"> byte[] lowest = Bytes.toBytes("AAA");</span>
<span class="source-line-no">121</span><span id="line-121"> byte[] middle = Bytes.toBytes("CCC");</span>
<span class="source-line-no">122</span><span id="line-122"> byte[] highest = Bytes.toBytes("EEE");</span>
<span class="source-line-no">123</span><span id="line-123"> byte[][] parts = Bytes.split(lowest, highest, 1);</span>
<span class="source-line-no">124</span><span id="line-124"> for (byte[] bytes : parts) {</span>
<span class="source-line-no">125</span><span id="line-125"> System.out.println(Bytes.toString(bytes));</span>
<span class="source-line-no">126</span><span id="line-126"> }</span>
<span class="source-line-no">127</span><span id="line-127"> assertEquals(3, parts.length);</span>
<span class="source-line-no">128</span><span id="line-128"> assertTrue(Bytes.equals(parts[1], middle));</span>
<span class="source-line-no">129</span><span id="line-129"> // Now divide into three parts. Change highest so split is even.</span>
<span class="source-line-no">130</span><span id="line-130"> highest = Bytes.toBytes("DDD");</span>
<span class="source-line-no">131</span><span id="line-131"> parts = Bytes.split(lowest, highest, 2);</span>
<span class="source-line-no">132</span><span id="line-132"> for (byte[] part : parts) {</span>
<span class="source-line-no">133</span><span id="line-133"> System.out.println(Bytes.toString(part));</span>
<span class="source-line-no">134</span><span id="line-134"> }</span>
<span class="source-line-no">135</span><span id="line-135"> assertEquals(4, parts.length);</span>
<span class="source-line-no">136</span><span id="line-136"> // Assert that 3rd part is 'CCC'.</span>
<span class="source-line-no">137</span><span id="line-137"> assertTrue(Bytes.equals(parts[2], middle));</span>
<span class="source-line-no">138</span><span id="line-138"> }</span>
<span class="source-line-no">139</span><span id="line-139"></span>
<span class="source-line-no">140</span><span id="line-140"> @Test</span>
<span class="source-line-no">141</span><span id="line-141"> public void testSplit2() {</span>
<span class="source-line-no">142</span><span id="line-142"> // More split tests.</span>
<span class="source-line-no">143</span><span id="line-143"> byte[] lowest = Bytes.toBytes("http://A");</span>
<span class="source-line-no">144</span><span id="line-144"> byte[] highest = Bytes.toBytes("http://z");</span>
<span class="source-line-no">145</span><span id="line-145"> byte[] middle = Bytes.toBytes("http://]");</span>
<span class="source-line-no">146</span><span id="line-146"> byte[][] parts = Bytes.split(lowest, highest, 1);</span>
<span class="source-line-no">147</span><span id="line-147"> for (byte[] part : parts) {</span>
<span class="source-line-no">148</span><span id="line-148"> System.out.println(Bytes.toString(part));</span>
<span class="source-line-no">149</span><span id="line-149"> }</span>
<span class="source-line-no">150</span><span id="line-150"> assertEquals(3, parts.length);</span>
<span class="source-line-no">151</span><span id="line-151"> assertTrue(Bytes.equals(parts[1], middle));</span>
<span class="source-line-no">152</span><span id="line-152"> }</span>
<span class="source-line-no">153</span><span id="line-153"></span>
<span class="source-line-no">154</span><span id="line-154"> @Test</span>
<span class="source-line-no">155</span><span id="line-155"> public void testSplit3() {</span>
<span class="source-line-no">156</span><span id="line-156"> // Test invalid split cases</span>
<span class="source-line-no">157</span><span id="line-157"> byte[] low = { 1, 1, 1 };</span>
<span class="source-line-no">158</span><span id="line-158"> byte[] high = { 1, 1, 3 };</span>
<span class="source-line-no">159</span><span id="line-159"></span>
<span class="source-line-no">160</span><span id="line-160"> // If swapped, should throw IAE</span>
<span class="source-line-no">161</span><span id="line-161"> try {</span>
<span class="source-line-no">162</span><span id="line-162"> Bytes.split(high, low, 1);</span>
<span class="source-line-no">163</span><span id="line-163"> fail("Should not be able to split if low &gt; high");</span>
<span class="source-line-no">164</span><span id="line-164"> } catch (IllegalArgumentException iae) {</span>
<span class="source-line-no">165</span><span id="line-165"> // Correct</span>
<span class="source-line-no">166</span><span id="line-166"> }</span>
<span class="source-line-no">167</span><span id="line-167"></span>
<span class="source-line-no">168</span><span id="line-168"> // Single split should work</span>
<span class="source-line-no">169</span><span id="line-169"> byte[][] parts = Bytes.split(low, high, 1);</span>
<span class="source-line-no">170</span><span id="line-170"> for (int i = 0; i &lt; parts.length; i++) {</span>
<span class="source-line-no">171</span><span id="line-171"> System.out.println("" + i + " -&gt; " + Bytes.toStringBinary(parts[i]));</span>
<span class="source-line-no">172</span><span id="line-172"> }</span>
<span class="source-line-no">173</span><span id="line-173"> assertEquals("Returned split should have 3 parts but has " + parts.length, 3, parts.length);</span>
<span class="source-line-no">174</span><span id="line-174"></span>
<span class="source-line-no">175</span><span id="line-175"> // If split more than once, use additional byte to split</span>
<span class="source-line-no">176</span><span id="line-176"> parts = Bytes.split(low, high, 2);</span>
<span class="source-line-no">177</span><span id="line-177"> assertNotNull("Split with an additional byte", parts);</span>
<span class="source-line-no">178</span><span id="line-178"> assertEquals(parts.length, low.length + 1);</span>
<span class="source-line-no">179</span><span id="line-179"></span>
<span class="source-line-no">180</span><span id="line-180"> // Split 0 times should throw IAE</span>
<span class="source-line-no">181</span><span id="line-181"> try {</span>
<span class="source-line-no">182</span><span id="line-182"> Bytes.split(low, high, 0);</span>
<span class="source-line-no">183</span><span id="line-183"> fail("Should not be able to split 0 times");</span>
<span class="source-line-no">184</span><span id="line-184"> } catch (IllegalArgumentException iae) {</span>
<span class="source-line-no">185</span><span id="line-185"> // Correct</span>
<span class="source-line-no">186</span><span id="line-186"> }</span>
<span class="source-line-no">187</span><span id="line-187"> }</span>
<span class="source-line-no">188</span><span id="line-188"></span>
<span class="source-line-no">189</span><span id="line-189"> @Test</span>
<span class="source-line-no">190</span><span id="line-190"> public void testToInt() {</span>
<span class="source-line-no">191</span><span id="line-191"> int[] ints = { -1, 123, Integer.MIN_VALUE, Integer.MAX_VALUE };</span>
<span class="source-line-no">192</span><span id="line-192"> for (int anInt : ints) {</span>
<span class="source-line-no">193</span><span id="line-193"> byte[] b = Bytes.toBytes(anInt);</span>
<span class="source-line-no">194</span><span id="line-194"> assertEquals(anInt, Bytes.toInt(b));</span>
<span class="source-line-no">195</span><span id="line-195"> byte[] b2 = bytesWithOffset(b);</span>
<span class="source-line-no">196</span><span id="line-196"> assertEquals(anInt, Bytes.toInt(b2, 1));</span>
<span class="source-line-no">197</span><span id="line-197"> assertEquals(anInt, Bytes.toInt(b2, 1, Bytes.SIZEOF_INT));</span>
<span class="source-line-no">198</span><span id="line-198"> }</span>
<span class="source-line-no">199</span><span id="line-199"> }</span>
<span class="source-line-no">200</span><span id="line-200"></span>
<span class="source-line-no">201</span><span id="line-201"> @Test</span>
<span class="source-line-no">202</span><span id="line-202"> public void testToLong() {</span>
<span class="source-line-no">203</span><span id="line-203"> long[] longs = { -1L, 123L, Long.MIN_VALUE, Long.MAX_VALUE };</span>
<span class="source-line-no">204</span><span id="line-204"> for (long aLong : longs) {</span>
<span class="source-line-no">205</span><span id="line-205"> byte[] b = Bytes.toBytes(aLong);</span>
<span class="source-line-no">206</span><span id="line-206"> assertEquals(aLong, Bytes.toLong(b));</span>
<span class="source-line-no">207</span><span id="line-207"> byte[] b2 = bytesWithOffset(b);</span>
<span class="source-line-no">208</span><span id="line-208"> assertEquals(aLong, Bytes.toLong(b2, 1));</span>
<span class="source-line-no">209</span><span id="line-209"> assertEquals(aLong, Bytes.toLong(b2, 1, Bytes.SIZEOF_LONG));</span>
<span class="source-line-no">210</span><span id="line-210"> }</span>
<span class="source-line-no">211</span><span id="line-211"> }</span>
<span class="source-line-no">212</span><span id="line-212"></span>
<span class="source-line-no">213</span><span id="line-213"> @Test</span>
<span class="source-line-no">214</span><span id="line-214"> public void testToFloat() {</span>
<span class="source-line-no">215</span><span id="line-215"> float[] floats = { -1f, 123.123f, Float.MAX_VALUE };</span>
<span class="source-line-no">216</span><span id="line-216"> for (float aFloat : floats) {</span>
<span class="source-line-no">217</span><span id="line-217"> byte[] b = Bytes.toBytes(aFloat);</span>
<span class="source-line-no">218</span><span id="line-218"> assertEquals(aFloat, Bytes.toFloat(b), 0.0f);</span>
<span class="source-line-no">219</span><span id="line-219"> byte[] b2 = bytesWithOffset(b);</span>
<span class="source-line-no">220</span><span id="line-220"> assertEquals(aFloat, Bytes.toFloat(b2, 1), 0.0f);</span>
<span class="source-line-no">221</span><span id="line-221"> }</span>
<span class="source-line-no">222</span><span id="line-222"> }</span>
<span class="source-line-no">223</span><span id="line-223"></span>
<span class="source-line-no">224</span><span id="line-224"> @Test</span>
<span class="source-line-no">225</span><span id="line-225"> public void testToDouble() {</span>
<span class="source-line-no">226</span><span id="line-226"> double[] doubles = { Double.MIN_VALUE, Double.MAX_VALUE };</span>
<span class="source-line-no">227</span><span id="line-227"> for (double aDouble : doubles) {</span>
<span class="source-line-no">228</span><span id="line-228"> byte[] b = Bytes.toBytes(aDouble);</span>
<span class="source-line-no">229</span><span id="line-229"> assertEquals(aDouble, Bytes.toDouble(b), 0.0);</span>
<span class="source-line-no">230</span><span id="line-230"> byte[] b2 = bytesWithOffset(b);</span>
<span class="source-line-no">231</span><span id="line-231"> assertEquals(aDouble, Bytes.toDouble(b2, 1), 0.0);</span>
<span class="source-line-no">232</span><span id="line-232"> }</span>
<span class="source-line-no">233</span><span id="line-233"> }</span>
<span class="source-line-no">234</span><span id="line-234"></span>
<span class="source-line-no">235</span><span id="line-235"> @Test</span>
<span class="source-line-no">236</span><span id="line-236"> public void testToBigDecimal() {</span>
<span class="source-line-no">237</span><span id="line-237"> BigDecimal[] decimals =</span>
<span class="source-line-no">238</span><span id="line-238"> { new BigDecimal("-1"), new BigDecimal("123.123"), new BigDecimal("123123123123") };</span>
<span class="source-line-no">239</span><span id="line-239"> for (BigDecimal decimal : decimals) {</span>
<span class="source-line-no">240</span><span id="line-240"> byte[] b = Bytes.toBytes(decimal);</span>
<span class="source-line-no">241</span><span id="line-241"> assertEquals(decimal, Bytes.toBigDecimal(b));</span>
<span class="source-line-no">242</span><span id="line-242"> byte[] b2 = bytesWithOffset(b);</span>
<span class="source-line-no">243</span><span id="line-243"> assertEquals(decimal, Bytes.toBigDecimal(b2, 1, b.length));</span>
<span class="source-line-no">244</span><span id="line-244"> }</span>
<span class="source-line-no">245</span><span id="line-245"> }</span>
<span class="source-line-no">246</span><span id="line-246"></span>
<span class="source-line-no">247</span><span id="line-247"> private byte[] bytesWithOffset(byte[] src) {</span>
<span class="source-line-no">248</span><span id="line-248"> // add one byte in front to test offset</span>
<span class="source-line-no">249</span><span id="line-249"> byte[] result = new byte[src.length + 1];</span>
<span class="source-line-no">250</span><span id="line-250"> result[0] = (byte) 0xAA;</span>
<span class="source-line-no">251</span><span id="line-251"> System.arraycopy(src, 0, result, 1, src.length);</span>
<span class="source-line-no">252</span><span id="line-252"> return result;</span>
<span class="source-line-no">253</span><span id="line-253"> }</span>
<span class="source-line-no">254</span><span id="line-254"></span>
<span class="source-line-no">255</span><span id="line-255"> @Test</span>
<span class="source-line-no">256</span><span id="line-256"> public void testToBytesForByteBuffer() {</span>
<span class="source-line-no">257</span><span id="line-257"> byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };</span>
<span class="source-line-no">258</span><span id="line-258"> ByteBuffer target = ByteBuffer.wrap(array);</span>
<span class="source-line-no">259</span><span id="line-259"> target.position(2);</span>
<span class="source-line-no">260</span><span id="line-260"> target.limit(7);</span>
<span class="source-line-no">261</span><span id="line-261"></span>
<span class="source-line-no">262</span><span id="line-262"> byte[] actual = Bytes.toBytes(target);</span>
<span class="source-line-no">263</span><span id="line-263"> byte[] expected = { 0, 1, 2, 3, 4, 5, 6 };</span>
<span class="source-line-no">264</span><span id="line-264"> assertArrayEquals(expected, actual);</span>
<span class="source-line-no">265</span><span id="line-265"> assertEquals(2, target.position());</span>
<span class="source-line-no">266</span><span id="line-266"> assertEquals(7, target.limit());</span>
<span class="source-line-no">267</span><span id="line-267"></span>
<span class="source-line-no">268</span><span id="line-268"> ByteBuffer target2 = target.slice();</span>
<span class="source-line-no">269</span><span id="line-269"> assertEquals(0, target2.position());</span>
<span class="source-line-no">270</span><span id="line-270"> assertEquals(5, target2.limit());</span>
<span class="source-line-no">271</span><span id="line-271"></span>
<span class="source-line-no">272</span><span id="line-272"> byte[] actual2 = Bytes.toBytes(target2);</span>
<span class="source-line-no">273</span><span id="line-273"> byte[] expected2 = { 2, 3, 4, 5, 6 };</span>
<span class="source-line-no">274</span><span id="line-274"> assertArrayEquals(expected2, actual2);</span>
<span class="source-line-no">275</span><span id="line-275"> assertEquals(0, target2.position());</span>
<span class="source-line-no">276</span><span id="line-276"> assertEquals(5, target2.limit());</span>
<span class="source-line-no">277</span><span id="line-277"> }</span>
<span class="source-line-no">278</span><span id="line-278"></span>
<span class="source-line-no">279</span><span id="line-279"> @Test</span>
<span class="source-line-no">280</span><span id="line-280"> public void testGetBytesForByteBuffer() {</span>
<span class="source-line-no">281</span><span id="line-281"> byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };</span>
<span class="source-line-no">282</span><span id="line-282"> ByteBuffer target = ByteBuffer.wrap(array);</span>
<span class="source-line-no">283</span><span id="line-283"> target.position(2);</span>
<span class="source-line-no">284</span><span id="line-284"> target.limit(7);</span>
<span class="source-line-no">285</span><span id="line-285"></span>
<span class="source-line-no">286</span><span id="line-286"> byte[] actual = Bytes.getBytes(target);</span>
<span class="source-line-no">287</span><span id="line-287"> byte[] expected = { 2, 3, 4, 5, 6 };</span>
<span class="source-line-no">288</span><span id="line-288"> assertArrayEquals(expected, actual);</span>
<span class="source-line-no">289</span><span id="line-289"> assertEquals(2, target.position());</span>
<span class="source-line-no">290</span><span id="line-290"> assertEquals(7, target.limit());</span>
<span class="source-line-no">291</span><span id="line-291"> }</span>
<span class="source-line-no">292</span><span id="line-292"></span>
<span class="source-line-no">293</span><span id="line-293"> @Test</span>
<span class="source-line-no">294</span><span id="line-294"> public void testReadAsVLong() throws Exception {</span>
<span class="source-line-no">295</span><span id="line-295"> long[] longs = { -1L, 123L, Long.MIN_VALUE, Long.MAX_VALUE };</span>
<span class="source-line-no">296</span><span id="line-296"> for (long aLong : longs) {</span>
<span class="source-line-no">297</span><span id="line-297"> ByteArrayOutputStream baos = new ByteArrayOutputStream();</span>
<span class="source-line-no">298</span><span id="line-298"> DataOutputStream output = new DataOutputStream(baos);</span>
<span class="source-line-no">299</span><span id="line-299"> WritableUtils.writeVLong(output, aLong);</span>
<span class="source-line-no">300</span><span id="line-300"> byte[] long_bytes_no_offset = baos.toByteArray();</span>
<span class="source-line-no">301</span><span id="line-301"> assertEquals(aLong, Bytes.readAsVLong(long_bytes_no_offset, 0));</span>
<span class="source-line-no">302</span><span id="line-302"> byte[] long_bytes_with_offset = bytesWithOffset(long_bytes_no_offset);</span>
<span class="source-line-no">303</span><span id="line-303"> assertEquals(aLong, Bytes.readAsVLong(long_bytes_with_offset, 1));</span>
<span class="source-line-no">304</span><span id="line-304"> }</span>
<span class="source-line-no">305</span><span id="line-305"> }</span>
<span class="source-line-no">306</span><span id="line-306"></span>
<span class="source-line-no">307</span><span id="line-307"> @Test</span>
<span class="source-line-no">308</span><span id="line-308"> public void testToStringBinaryForBytes() {</span>
<span class="source-line-no">309</span><span id="line-309"> byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };</span>
<span class="source-line-no">310</span><span id="line-310"> String actual = Bytes.toStringBinary(array);</span>
<span class="source-line-no">311</span><span id="line-311"> String expected = "09azAZ@\\x01";</span>
<span class="source-line-no">312</span><span id="line-312"> assertEquals(expected, actual);</span>
<span class="source-line-no">313</span><span id="line-313"></span>
<span class="source-line-no">314</span><span id="line-314"> String actual2 = Bytes.toStringBinary(array, 2, 3);</span>
<span class="source-line-no">315</span><span id="line-315"> String expected2 = "azA";</span>
<span class="source-line-no">316</span><span id="line-316"> assertEquals(expected2, actual2);</span>
<span class="source-line-no">317</span><span id="line-317"> }</span>
<span class="source-line-no">318</span><span id="line-318"></span>
<span class="source-line-no">319</span><span id="line-319"> @Test</span>
<span class="source-line-no">320</span><span id="line-320"> public void testToStringBinaryForArrayBasedByteBuffer() {</span>
<span class="source-line-no">321</span><span id="line-321"> byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };</span>
<span class="source-line-no">322</span><span id="line-322"> ByteBuffer target = ByteBuffer.wrap(array);</span>
<span class="source-line-no">323</span><span id="line-323"> String actual = Bytes.toStringBinary(target);</span>
<span class="source-line-no">324</span><span id="line-324"> String expected = "09azAZ@\\x01";</span>
<span class="source-line-no">325</span><span id="line-325"> assertEquals(expected, actual);</span>
<span class="source-line-no">326</span><span id="line-326"> }</span>
<span class="source-line-no">327</span><span id="line-327"></span>
<span class="source-line-no">328</span><span id="line-328"> @Test</span>
<span class="source-line-no">329</span><span id="line-329"> public void testToStringBinaryForReadOnlyByteBuffer() {</span>
<span class="source-line-no">330</span><span id="line-330"> byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };</span>
<span class="source-line-no">331</span><span id="line-331"> ByteBuffer target = ByteBuffer.wrap(array).asReadOnlyBuffer();</span>
<span class="source-line-no">332</span><span id="line-332"> String actual = Bytes.toStringBinary(target);</span>
<span class="source-line-no">333</span><span id="line-333"> String expected = "09azAZ@\\x01";</span>
<span class="source-line-no">334</span><span id="line-334"> assertEquals(expected, actual);</span>
<span class="source-line-no">335</span><span id="line-335"> }</span>
<span class="source-line-no">336</span><span id="line-336"></span>
<span class="source-line-no">337</span><span id="line-337"> @Test</span>
<span class="source-line-no">338</span><span id="line-338"> public void testBinarySearch() {</span>
<span class="source-line-no">339</span><span id="line-339"> byte[][] arr = { { 1 }, { 3 }, { 5 }, { 7 }, { 9 }, { 11 }, { 13 }, { 15 }, };</span>
<span class="source-line-no">340</span><span id="line-340"> byte[] key1 = { 3, 1 };</span>
<span class="source-line-no">341</span><span id="line-341"> byte[] key2 = { 4, 9 };</span>
<span class="source-line-no">342</span><span id="line-342"> byte[] key2_2 = { 4 };</span>
<span class="source-line-no">343</span><span id="line-343"> byte[] key3 = { 5, 11 };</span>
<span class="source-line-no">344</span><span id="line-344"> byte[] key4 = { 0 };</span>
<span class="source-line-no">345</span><span id="line-345"> byte[] key5 = { 2 };</span>
<span class="source-line-no">346</span><span id="line-346"></span>
<span class="source-line-no">347</span><span id="line-347"> assertEquals(1, Bytes.binarySearch(arr, key1, 0, 1));</span>
<span class="source-line-no">348</span><span id="line-348"> assertEquals(0, Bytes.binarySearch(arr, key1, 1, 1));</span>
<span class="source-line-no">349</span><span id="line-349"> assertEquals(-(2 + 1), Arrays.binarySearch(arr, key2_2, Bytes.BYTES_COMPARATOR));</span>
<span class="source-line-no">350</span><span id="line-350"> assertEquals(-(2 + 1), Bytes.binarySearch(arr, key2, 0, 1));</span>
<span class="source-line-no">351</span><span id="line-351"> assertEquals(4, Bytes.binarySearch(arr, key2, 1, 1));</span>
<span class="source-line-no">352</span><span id="line-352"> assertEquals(2, Bytes.binarySearch(arr, key3, 0, 1));</span>
<span class="source-line-no">353</span><span id="line-353"> assertEquals(5, Bytes.binarySearch(arr, key3, 1, 1));</span>
<span class="source-line-no">354</span><span id="line-354"> assertEquals(-1, Bytes.binarySearch(arr, key4, 0, 1));</span>
<span class="source-line-no">355</span><span id="line-355"> assertEquals(-2, Bytes.binarySearch(arr, key5, 0, 1));</span>
<span class="source-line-no">356</span><span id="line-356"></span>
<span class="source-line-no">357</span><span id="line-357"> // Search for values to the left and to the right of each item in the array.</span>
<span class="source-line-no">358</span><span id="line-358"> for (int i = 0; i &lt; arr.length; ++i) {</span>
<span class="source-line-no">359</span><span id="line-359"> assertEquals(-(i + 1), Bytes.binarySearch(arr, new byte[] { (byte) (arr[i][0] - 1) }, 0, 1));</span>
<span class="source-line-no">360</span><span id="line-360"> assertEquals(-(i + 2), Bytes.binarySearch(arr, new byte[] { (byte) (arr[i][0] + 1) }, 0, 1));</span>
<span class="source-line-no">361</span><span id="line-361"> }</span>
<span class="source-line-no">362</span><span id="line-362"> }</span>
<span class="source-line-no">363</span><span id="line-363"></span>
<span class="source-line-no">364</span><span id="line-364"> @Test</span>
<span class="source-line-no">365</span><span id="line-365"> public void testToStringBytesBinaryReversible() {</span>
<span class="source-line-no">366</span><span id="line-366"> byte[] randomBytes = new byte[1000];</span>
<span class="source-line-no">367</span><span id="line-367"> for (int i = 0; i &lt; 1000; i++) {</span>
<span class="source-line-no">368</span><span id="line-368"> Bytes.random(randomBytes);</span>
<span class="source-line-no">369</span><span id="line-369"> verifyReversibleForBytes(randomBytes);</span>
<span class="source-line-no">370</span><span id="line-370"> }</span>
<span class="source-line-no">371</span><span id="line-371"> // some specific cases</span>
<span class="source-line-no">372</span><span id="line-372"> verifyReversibleForBytes(new byte[] {});</span>
<span class="source-line-no">373</span><span id="line-373"> verifyReversibleForBytes(new byte[] { '\\', 'x', 'A', 'D' });</span>
<span class="source-line-no">374</span><span id="line-374"> verifyReversibleForBytes(new byte[] { '\\', 'x', 'A', 'D', '\\' });</span>
<span class="source-line-no">375</span><span id="line-375"> }</span>
<span class="source-line-no">376</span><span id="line-376"></span>
<span class="source-line-no">377</span><span id="line-377"> private void verifyReversibleForBytes(byte[] originalBytes) {</span>
<span class="source-line-no">378</span><span id="line-378"> String convertedString = Bytes.toStringBinary(originalBytes);</span>
<span class="source-line-no">379</span><span id="line-379"> byte[] convertedBytes = Bytes.toBytesBinary(convertedString);</span>
<span class="source-line-no">380</span><span id="line-380"> if (Bytes.compareTo(originalBytes, convertedBytes) != 0) {</span>
<span class="source-line-no">381</span><span id="line-381"> fail("Not reversible for\nbyte[]: " + Arrays.toString(originalBytes) + ",\nStringBinary: "</span>
<span class="source-line-no">382</span><span id="line-382"> + convertedString);</span>
<span class="source-line-no">383</span><span id="line-383"> }</span>
<span class="source-line-no">384</span><span id="line-384"> }</span>
<span class="source-line-no">385</span><span id="line-385"></span>
<span class="source-line-no">386</span><span id="line-386"> @Test</span>
<span class="source-line-no">387</span><span id="line-387"> public void testStartsWith() {</span>
<span class="source-line-no">388</span><span id="line-388"> assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("h")));</span>
<span class="source-line-no">389</span><span id="line-389"> assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("")));</span>
<span class="source-line-no">390</span><span id="line-390"> assertTrue(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("hello")));</span>
<span class="source-line-no">391</span><span id="line-391"> assertFalse(Bytes.startsWith(Bytes.toBytes("hello"), Bytes.toBytes("helloworld")));</span>
<span class="source-line-no">392</span><span id="line-392"> assertFalse(Bytes.startsWith(Bytes.toBytes(""), Bytes.toBytes("hello")));</span>
<span class="source-line-no">393</span><span id="line-393"> }</span>
<span class="source-line-no">394</span><span id="line-394"></span>
<span class="source-line-no">395</span><span id="line-395"> @Test</span>
<span class="source-line-no">396</span><span id="line-396"> public void testIncrementBytes() {</span>
<span class="source-line-no">397</span><span id="line-397"> assertTrue(checkTestIncrementBytes(10, 1));</span>
<span class="source-line-no">398</span><span id="line-398"> assertTrue(checkTestIncrementBytes(12, 123435445));</span>
<span class="source-line-no">399</span><span id="line-399"> assertTrue(checkTestIncrementBytes(124634654, 1));</span>
<span class="source-line-no">400</span><span id="line-400"> assertTrue(checkTestIncrementBytes(10005460, 5005645));</span>
<span class="source-line-no">401</span><span id="line-401"> assertTrue(checkTestIncrementBytes(1, -1));</span>
<span class="source-line-no">402</span><span id="line-402"> assertTrue(checkTestIncrementBytes(10, -1));</span>
<span class="source-line-no">403</span><span id="line-403"> assertTrue(checkTestIncrementBytes(10, -5));</span>
<span class="source-line-no">404</span><span id="line-404"> assertTrue(checkTestIncrementBytes(1005435000, -5));</span>
<span class="source-line-no">405</span><span id="line-405"> assertTrue(checkTestIncrementBytes(10, -43657655));</span>
<span class="source-line-no">406</span><span id="line-406"> assertTrue(checkTestIncrementBytes(-1, 1));</span>
<span class="source-line-no">407</span><span id="line-407"> assertTrue(checkTestIncrementBytes(-26, 5034520));</span>
<span class="source-line-no">408</span><span id="line-408"> assertTrue(checkTestIncrementBytes(-10657200, 5));</span>
<span class="source-line-no">409</span><span id="line-409"> assertTrue(checkTestIncrementBytes(-12343250, 45376475));</span>
<span class="source-line-no">410</span><span id="line-410"> assertTrue(checkTestIncrementBytes(-10, -5));</span>
<span class="source-line-no">411</span><span id="line-411"> assertTrue(checkTestIncrementBytes(-12343250, -5));</span>
<span class="source-line-no">412</span><span id="line-412"> assertTrue(checkTestIncrementBytes(-12, -34565445));</span>
<span class="source-line-no">413</span><span id="line-413"> assertTrue(checkTestIncrementBytes(-1546543452, -34565445));</span>
<span class="source-line-no">414</span><span id="line-414"> }</span>
<span class="source-line-no">415</span><span id="line-415"></span>
<span class="source-line-no">416</span><span id="line-416"> private static boolean checkTestIncrementBytes(long val, long amount) {</span>
<span class="source-line-no">417</span><span id="line-417"> byte[] value = Bytes.toBytes(val);</span>
<span class="source-line-no">418</span><span id="line-418"> byte[] testValue = { -1, -1, -1, -1, -1, -1, -1, -1 };</span>
<span class="source-line-no">419</span><span id="line-419"> if (value[0] &gt; 0) {</span>
<span class="source-line-no">420</span><span id="line-420"> testValue = new byte[Bytes.SIZEOF_LONG];</span>
<span class="source-line-no">421</span><span id="line-421"> }</span>
<span class="source-line-no">422</span><span id="line-422"> System.arraycopy(value, 0, testValue, testValue.length - value.length, value.length);</span>
<span class="source-line-no">423</span><span id="line-423"></span>
<span class="source-line-no">424</span><span id="line-424"> long incrementResult = Bytes.toLong(Bytes.incrementBytes(value, amount));</span>
<span class="source-line-no">425</span><span id="line-425"></span>
<span class="source-line-no">426</span><span id="line-426"> return (Bytes.toLong(testValue) + amount) == incrementResult;</span>
<span class="source-line-no">427</span><span id="line-427"> }</span>
<span class="source-line-no">428</span><span id="line-428"></span>
<span class="source-line-no">429</span><span id="line-429"> @Test</span>
<span class="source-line-no">430</span><span id="line-430"> public void testFixedSizeString() throws IOException {</span>
<span class="source-line-no">431</span><span id="line-431"> ByteArrayOutputStream baos = new ByteArrayOutputStream();</span>
<span class="source-line-no">432</span><span id="line-432"> DataOutputStream dos = new DataOutputStream(baos);</span>
<span class="source-line-no">433</span><span id="line-433"> Bytes.writeStringFixedSize(dos, "Hello", 5);</span>
<span class="source-line-no">434</span><span id="line-434"> Bytes.writeStringFixedSize(dos, "World", 18);</span>
<span class="source-line-no">435</span><span id="line-435"> Bytes.writeStringFixedSize(dos, "", 9);</span>
<span class="source-line-no">436</span><span id="line-436"></span>
<span class="source-line-no">437</span><span id="line-437"> try {</span>
<span class="source-line-no">438</span><span id="line-438"> // Use a long dash which is three bytes in UTF-8. If encoding happens</span>
<span class="source-line-no">439</span><span id="line-439"> // using ISO-8859-1, this will fail.</span>
<span class="source-line-no">440</span><span id="line-440"> Bytes.writeStringFixedSize(dos, "Too\u2013Long", 9);</span>
<span class="source-line-no">441</span><span id="line-441"> fail("Exception expected");</span>
<span class="source-line-no">442</span><span id="line-442"> } catch (IOException ex) {</span>
<span class="source-line-no">443</span><span id="line-443"> assertEquals(</span>
<span class="source-line-no">444</span><span id="line-444"> "Trying to write 10 bytes (Too\\xE2\\x80\\x93Long) into a field of " + "length 9",</span>
<span class="source-line-no">445</span><span id="line-445"> ex.getMessage());</span>
<span class="source-line-no">446</span><span id="line-446"> }</span>
<span class="source-line-no">447</span><span id="line-447"></span>
<span class="source-line-no">448</span><span id="line-448"> ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());</span>
<span class="source-line-no">449</span><span id="line-449"> DataInputStream dis = new DataInputStream(bais);</span>
<span class="source-line-no">450</span><span id="line-450"> assertEquals("Hello", Bytes.readStringFixedSize(dis, 5));</span>
<span class="source-line-no">451</span><span id="line-451"> assertEquals("World", Bytes.readStringFixedSize(dis, 18));</span>
<span class="source-line-no">452</span><span id="line-452"> assertEquals("", Bytes.readStringFixedSize(dis, 9));</span>
<span class="source-line-no">453</span><span id="line-453"> }</span>
<span class="source-line-no">454</span><span id="line-454"></span>
<span class="source-line-no">455</span><span id="line-455"> @Test</span>
<span class="source-line-no">456</span><span id="line-456"> public void testCopy() {</span>
<span class="source-line-no">457</span><span id="line-457"> byte[] bytes = Bytes.toBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ");</span>
<span class="source-line-no">458</span><span id="line-458"> byte[] copy = Bytes.copy(bytes);</span>
<span class="source-line-no">459</span><span id="line-459"> assertNotSame(bytes, copy);</span>
<span class="source-line-no">460</span><span id="line-460"> assertTrue(Bytes.equals(bytes, copy));</span>
<span class="source-line-no">461</span><span id="line-461"> }</span>
<span class="source-line-no">462</span><span id="line-462"></span>
<span class="source-line-no">463</span><span id="line-463"> @Test</span>
<span class="source-line-no">464</span><span id="line-464"> public void testToBytesBinaryTrailingBackslashes() {</span>
<span class="source-line-no">465</span><span id="line-465"> try {</span>
<span class="source-line-no">466</span><span id="line-466"> Bytes.toBytesBinary("abc\\x00\\x01\\");</span>
<span class="source-line-no">467</span><span id="line-467"> } catch (StringIndexOutOfBoundsException ex) {</span>
<span class="source-line-no">468</span><span id="line-468"> fail("Illegal string access: " + ex.getMessage());</span>
<span class="source-line-no">469</span><span id="line-469"> }</span>
<span class="source-line-no">470</span><span id="line-470"> }</span>
<span class="source-line-no">471</span><span id="line-471"></span>
<span class="source-line-no">472</span><span id="line-472"> @Test</span>
<span class="source-line-no">473</span><span id="line-473"> public void testToStringBinary_toBytesBinary_Reversable() {</span>
<span class="source-line-no">474</span><span id="line-474"> String bytes = Bytes.toStringBinary(Bytes.toBytes(2.17));</span>
<span class="source-line-no">475</span><span id="line-475"> assertEquals(2.17, Bytes.toDouble(Bytes.toBytesBinary(bytes)), 0);</span>
<span class="source-line-no">476</span><span id="line-476"> }</span>
<span class="source-line-no">477</span><span id="line-477"></span>
<span class="source-line-no">478</span><span id="line-478"> @Test</span>
<span class="source-line-no">479</span><span id="line-479"> public void testUnsignedBinarySearch() {</span>
<span class="source-line-no">480</span><span id="line-480"> byte[] bytes = new byte[] { 0, 5, 123, 127, -128, -100, -1 };</span>
<span class="source-line-no">481</span><span id="line-481"> Assert.assertEquals(1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) 5));</span>
<span class="source-line-no">482</span><span id="line-482"> Assert.assertEquals(3, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) 127));</span>
<span class="source-line-no">483</span><span id="line-483"> Assert.assertEquals(4, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) -128));</span>
<span class="source-line-no">484</span><span id="line-484"> Assert.assertEquals(5, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) -100));</span>
<span class="source-line-no">485</span><span id="line-485"> Assert.assertEquals(6, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) -1));</span>
<span class="source-line-no">486</span><span id="line-486"> Assert.assertEquals(-1 - 1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) 2));</span>
<span class="source-line-no">487</span><span id="line-487"> Assert.assertEquals(-6 - 1, Bytes.unsignedBinarySearch(bytes, 0, bytes.length, (byte) -5));</span>
<span class="source-line-no">488</span><span id="line-488"> }</span>
<span class="source-line-no">489</span><span id="line-489"></span>
<span class="source-line-no">490</span><span id="line-490"> @Test</span>
<span class="source-line-no">491</span><span id="line-491"> public void testUnsignedIncrement() {</span>
<span class="source-line-no">492</span><span id="line-492"> byte[] a = Bytes.toBytes(0);</span>
<span class="source-line-no">493</span><span id="line-493"> int a2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(a), 0);</span>
<span class="source-line-no">494</span><span id="line-494"> Assert.assertEquals(1, a2);</span>
<span class="source-line-no">495</span><span id="line-495"></span>
<span class="source-line-no">496</span><span id="line-496"> byte[] b = Bytes.toBytes(-1);</span>
<span class="source-line-no">497</span><span id="line-497"> byte[] actual = Bytes.unsignedCopyAndIncrement(b);</span>
<span class="source-line-no">498</span><span id="line-498"> Assert.assertNotSame(b, actual);</span>
<span class="source-line-no">499</span><span id="line-499"> byte[] expected = new byte[] { 1, 0, 0, 0, 0 };</span>
<span class="source-line-no">500</span><span id="line-500"> assertArrayEquals(expected, actual);</span>
<span class="source-line-no">501</span><span id="line-501"></span>
<span class="source-line-no">502</span><span id="line-502"> byte[] c = Bytes.toBytes(255);// should wrap to the next significant byte</span>
<span class="source-line-no">503</span><span id="line-503"> int c2 = Bytes.toInt(Bytes.unsignedCopyAndIncrement(c), 0);</span>
<span class="source-line-no">504</span><span id="line-504"> Assert.assertEquals(256, c2);</span>
<span class="source-line-no">505</span><span id="line-505"> }</span>
<span class="source-line-no">506</span><span id="line-506"></span>
<span class="source-line-no">507</span><span id="line-507"> @Test</span>
<span class="source-line-no">508</span><span id="line-508"> public void testIndexOf() {</span>
<span class="source-line-no">509</span><span id="line-509"> byte[] array = Bytes.toBytes("hello");</span>
<span class="source-line-no">510</span><span id="line-510"> assertEquals(1, Bytes.indexOf(array, (byte) 'e'));</span>
<span class="source-line-no">511</span><span id="line-511"> assertEquals(4, Bytes.indexOf(array, (byte) 'o'));</span>
<span class="source-line-no">512</span><span id="line-512"> assertEquals(-1, Bytes.indexOf(array, (byte) 'a'));</span>
<span class="source-line-no">513</span><span id="line-513"> assertEquals(0, Bytes.indexOf(array, Bytes.toBytes("hel")));</span>
<span class="source-line-no">514</span><span id="line-514"> assertEquals(2, Bytes.indexOf(array, Bytes.toBytes("ll")));</span>
<span class="source-line-no">515</span><span id="line-515"> assertEquals(-1, Bytes.indexOf(array, Bytes.toBytes("hll")));</span>
<span class="source-line-no">516</span><span id="line-516"> }</span>
<span class="source-line-no">517</span><span id="line-517"></span>
<span class="source-line-no">518</span><span id="line-518"> @Test</span>
<span class="source-line-no">519</span><span id="line-519"> public void testContains() {</span>
<span class="source-line-no">520</span><span id="line-520"> byte[] array = Bytes.toBytes("hello world");</span>
<span class="source-line-no">521</span><span id="line-521"> assertTrue(Bytes.contains(array, (byte) 'e'));</span>
<span class="source-line-no">522</span><span id="line-522"> assertTrue(Bytes.contains(array, (byte) 'd'));</span>
<span class="source-line-no">523</span><span id="line-523"> assertFalse(Bytes.contains(array, (byte) 'a'));</span>
<span class="source-line-no">524</span><span id="line-524"> assertTrue(Bytes.contains(array, Bytes.toBytes("world")));</span>
<span class="source-line-no">525</span><span id="line-525"> assertTrue(Bytes.contains(array, Bytes.toBytes("ello")));</span>
<span class="source-line-no">526</span><span id="line-526"> assertFalse(Bytes.contains(array, Bytes.toBytes("owo")));</span>
<span class="source-line-no">527</span><span id="line-527"> }</span>
<span class="source-line-no">528</span><span id="line-528"></span>
<span class="source-line-no">529</span><span id="line-529"> @Test</span>
<span class="source-line-no">530</span><span id="line-530"> public void testZero() {</span>
<span class="source-line-no">531</span><span id="line-531"> byte[] array = Bytes.toBytes("hello");</span>
<span class="source-line-no">532</span><span id="line-532"> Bytes.zero(array);</span>
<span class="source-line-no">533</span><span id="line-533"> for (byte b : array) {</span>
<span class="source-line-no">534</span><span id="line-534"> assertEquals(0, b);</span>
<span class="source-line-no">535</span><span id="line-535"> }</span>
<span class="source-line-no">536</span><span id="line-536"> array = Bytes.toBytes("hello world");</span>
<span class="source-line-no">537</span><span id="line-537"> Bytes.zero(array, 2, 7);</span>
<span class="source-line-no">538</span><span id="line-538"> assertFalse(array[0] == 0);</span>
<span class="source-line-no">539</span><span id="line-539"> assertFalse(array[1] == 0);</span>
<span class="source-line-no">540</span><span id="line-540"> for (int i = 2; i &lt; 9; i++) {</span>
<span class="source-line-no">541</span><span id="line-541"> assertEquals(0, array[i]);</span>
<span class="source-line-no">542</span><span id="line-542"> }</span>
<span class="source-line-no">543</span><span id="line-543"> for (int i = 9; i &lt; array.length; i++) {</span>
<span class="source-line-no">544</span><span id="line-544"> assertFalse(array[i] == 0);</span>
<span class="source-line-no">545</span><span id="line-545"> }</span>
<span class="source-line-no">546</span><span id="line-546"> }</span>
<span class="source-line-no">547</span><span id="line-547"></span>
<span class="source-line-no">548</span><span id="line-548"> @Test</span>
<span class="source-line-no">549</span><span id="line-549"> public void testPutBuffer() {</span>
<span class="source-line-no">550</span><span id="line-550"> byte[] b = new byte[100];</span>
<span class="source-line-no">551</span><span id="line-551"> for (byte i = 0; i &lt; 100; i++) {</span>
<span class="source-line-no">552</span><span id="line-552"> Bytes.putByteBuffer(b, i, ByteBuffer.wrap(new byte[] { i }));</span>
<span class="source-line-no">553</span><span id="line-553"> }</span>
<span class="source-line-no">554</span><span id="line-554"> for (byte i = 0; i &lt; 100; i++) {</span>
<span class="source-line-no">555</span><span id="line-555"> Assert.assertEquals(i, b[i]);</span>
<span class="source-line-no">556</span><span id="line-556"> }</span>
<span class="source-line-no">557</span><span id="line-557"> }</span>
<span class="source-line-no">558</span><span id="line-558"></span>
<span class="source-line-no">559</span><span id="line-559"> @Test</span>
<span class="source-line-no">560</span><span id="line-560"> public void testToFromHex() {</span>
<span class="source-line-no">561</span><span id="line-561"> List&lt;String&gt; testStrings = new ArrayList&lt;&gt;(8);</span>
<span class="source-line-no">562</span><span id="line-562"> testStrings.addAll(Arrays.asList("", "00", "A0", "ff", "FFffFFFFFFFFFF", "12",</span>
<span class="source-line-no">563</span><span id="line-563"> "0123456789abcdef", "283462839463924623984692834692346ABCDFEDDCA0"));</span>
<span class="source-line-no">564</span><span id="line-564"> for (String testString : testStrings) {</span>
<span class="source-line-no">565</span><span id="line-565"> byte[] byteData = Bytes.fromHex(testString);</span>
<span class="source-line-no">566</span><span id="line-566"> Assert.assertEquals(testString.length() / 2, byteData.length);</span>
<span class="source-line-no">567</span><span id="line-567"> String result = Bytes.toHex(byteData);</span>
<span class="source-line-no">568</span><span id="line-568"> Assert.assertTrue(testString.equalsIgnoreCase(result));</span>
<span class="source-line-no">569</span><span id="line-569"> }</span>
<span class="source-line-no">570</span><span id="line-570"></span>
<span class="source-line-no">571</span><span id="line-571"> List&lt;byte[]&gt; testByteData = new ArrayList&lt;&gt;(5);</span>
<span class="source-line-no">572</span><span id="line-572"> testByteData.addAll(Arrays.asList(new byte[0], new byte[1], new byte[10],</span>
<span class="source-line-no">573</span><span id="line-573"> new byte[] { 1, 2, 3, 4, 5 }, new byte[] { (byte) 0xFF }));</span>
<span class="source-line-no">574</span><span id="line-574"> Random rand = ThreadLocalRandom.current();</span>
<span class="source-line-no">575</span><span id="line-575"> for (int i = 0; i &lt; 20; i++) {</span>
<span class="source-line-no">576</span><span id="line-576"> byte[] bytes = new byte[rand.nextInt(100)];</span>
<span class="source-line-no">577</span><span id="line-577"> Bytes.random(bytes);</span>
<span class="source-line-no">578</span><span id="line-578"> testByteData.add(bytes);</span>
<span class="source-line-no">579</span><span id="line-579"> }</span>
<span class="source-line-no">580</span><span id="line-580"></span>
<span class="source-line-no">581</span><span id="line-581"> for (byte[] testData : testByteData) {</span>
<span class="source-line-no">582</span><span id="line-582"> String hexString = Bytes.toHex(testData);</span>
<span class="source-line-no">583</span><span id="line-583"> Assert.assertEquals(testData.length * 2, hexString.length());</span>
<span class="source-line-no">584</span><span id="line-584"> byte[] result = Bytes.fromHex(hexString);</span>
<span class="source-line-no">585</span><span id="line-585"> assertArrayEquals(testData, result);</span>
<span class="source-line-no">586</span><span id="line-586"> }</span>
<span class="source-line-no">587</span><span id="line-587"> }</span>
<span class="source-line-no">588</span><span id="line-588"></span>
<span class="source-line-no">589</span><span id="line-589"> @Test</span>
<span class="source-line-no">590</span><span id="line-590"> public void testFindCommonPrefix() throws Exception {</span>
<span class="source-line-no">591</span><span id="line-591"> testFindCommonPrefix(false);</span>
<span class="source-line-no">592</span><span id="line-592"> }</span>
<span class="source-line-no">593</span><span id="line-593"></span>
<span class="source-line-no">594</span><span id="line-594"> @Test</span>
<span class="source-line-no">595</span><span id="line-595"> public void testFindCommonPrefixUnsafe() throws Exception {</span>
<span class="source-line-no">596</span><span id="line-596"> testFindCommonPrefix(true);</span>
<span class="source-line-no">597</span><span id="line-597"> }</span>
<span class="source-line-no">598</span><span id="line-598"></span>
<span class="source-line-no">599</span><span id="line-599"> private static void testFindCommonPrefix(boolean unsafe) throws Exception {</span>
<span class="source-line-no">600</span><span id="line-600"> setUnsafe(unsafe);</span>
<span class="source-line-no">601</span><span id="line-601"> try {</span>
<span class="source-line-no">602</span><span id="line-602"> // tests for common prefixes less than 8 bytes in length (i.e. using non-vectorized path)</span>
<span class="source-line-no">603</span><span id="line-603"> byte[] hello = Bytes.toBytes("hello");</span>
<span class="source-line-no">604</span><span id="line-604"> byte[] helloWorld = Bytes.toBytes("helloworld");</span>
<span class="source-line-no">605</span><span id="line-605"></span>
<span class="source-line-no">606</span><span id="line-606"> assertEquals(5,</span>
<span class="source-line-no">607</span><span id="line-607"> Bytes.findCommonPrefix(hello, helloWorld, hello.length, helloWorld.length, 0, 0));</span>
<span class="source-line-no">608</span><span id="line-608"> assertEquals(5, Bytes.findCommonPrefix(hello, hello, hello.length, hello.length, 0, 0));</span>
<span class="source-line-no">609</span><span id="line-609"> assertEquals(3,</span>
<span class="source-line-no">610</span><span id="line-610"> Bytes.findCommonPrefix(hello, hello, hello.length - 2, hello.length - 2, 2, 2));</span>
<span class="source-line-no">611</span><span id="line-611"> assertEquals(0, Bytes.findCommonPrefix(hello, hello, 0, 0, 0, 0));</span>
<span class="source-line-no">612</span><span id="line-612"></span>
<span class="source-line-no">613</span><span id="line-613"> // tests for common prefixes greater than 8 bytes in length which may use the vectorized path</span>
<span class="source-line-no">614</span><span id="line-614"> byte[] hellohello = Bytes.toBytes("hellohello");</span>
<span class="source-line-no">615</span><span id="line-615"> byte[] hellohellohi = Bytes.toBytes("hellohellohi");</span>
<span class="source-line-no">616</span><span id="line-616"></span>
<span class="source-line-no">617</span><span id="line-617"> assertEquals(10, Bytes.findCommonPrefix(hellohello, hellohellohi, hellohello.length,</span>
<span class="source-line-no">618</span><span id="line-618"> hellohellohi.length, 0, 0));</span>
<span class="source-line-no">619</span><span id="line-619"> assertEquals(10, Bytes.findCommonPrefix(hellohellohi, hellohello, hellohellohi.length,</span>
<span class="source-line-no">620</span><span id="line-620"> hellohello.length, 0, 0));</span>
<span class="source-line-no">621</span><span id="line-621"> assertEquals(10,</span>
<span class="source-line-no">622</span><span id="line-622"> Bytes.findCommonPrefix(hellohello, hellohello, hellohello.length, hellohello.length, 0, 0));</span>
<span class="source-line-no">623</span><span id="line-623"></span>
<span class="source-line-no">624</span><span id="line-624"> hellohello[2] = 0;</span>
<span class="source-line-no">625</span><span id="line-625"> assertEquals(2, Bytes.findCommonPrefix(hellohello, hellohellohi, hellohello.length,</span>
<span class="source-line-no">626</span><span id="line-626"> hellohellohi.length, 0, 0));</span>
<span class="source-line-no">627</span><span id="line-627"> } finally {</span>
<span class="source-line-no">628</span><span id="line-628"> setUnsafe(HBasePlatformDependent.unaligned());</span>
<span class="source-line-no">629</span><span id="line-629"> }</span>
<span class="source-line-no">630</span><span id="line-630"> }</span>
<span class="source-line-no">631</span><span id="line-631">}</span>
</pre>
</div>
</main>
</body>
</html>