|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.unsafe.array; |
| 19 | + |
| 20 | +import org.apache.spark.unsafe.Platform; |
| 21 | +import org.apache.spark.unsafe.types.ByteArray; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class ByteArraySuite { |
| 26 | + private long getPrefixByByte(byte[] bytes) { |
| 27 | + final int minLen = Math.min(bytes.length, 8); |
| 28 | + long p = 0; |
| 29 | + for (int i = 0; i < minLen; ++i) { |
| 30 | + p |= ((long) Platform.getByte(bytes, Platform.BYTE_ARRAY_OFFSET + i) & 0xff) |
| 31 | + << (56 - 8 * i); |
| 32 | + } |
| 33 | + return p; |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testGetPrefix() { |
| 38 | + for (int i = 0; i <= 9; i++) { |
| 39 | + byte[] bytes = new byte[i]; |
| 40 | + int prefix = i - 1; |
| 41 | + while (prefix >= 0) { |
| 42 | + bytes[prefix] = (byte) prefix; |
| 43 | + prefix -= 1; |
| 44 | + } |
| 45 | + |
| 46 | + long result = ByteArray.getPrefix(bytes); |
| 47 | + long expected = getPrefixByByte(bytes); |
| 48 | + Assert.assertEquals(result, expected); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments