[120] | 1 | /** |
---|
| 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 |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 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. |
---|
| 17 | */ |
---|
| 18 | package org.apache.hadoop.io; |
---|
| 19 | |
---|
| 20 | import junit.framework.TestCase; |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * This is the unit test for BytesWritable. |
---|
| 24 | */ |
---|
| 25 | public class TestBytesWritable extends TestCase { |
---|
| 26 | |
---|
| 27 | public void testSizeChange() throws Exception { |
---|
| 28 | byte[] hadoop = "hadoop".getBytes(); |
---|
| 29 | BytesWritable buf = new BytesWritable(hadoop); |
---|
| 30 | int size = buf.getLength(); |
---|
| 31 | int orig_capacity = buf.getCapacity(); |
---|
| 32 | buf.setSize(size*2); |
---|
| 33 | int new_capacity = buf.getCapacity(); |
---|
| 34 | System.arraycopy(buf.getBytes(), 0, buf.getBytes(), size, size); |
---|
| 35 | assertTrue(new_capacity >= size * 2); |
---|
| 36 | assertEquals(size * 2, buf.getLength()); |
---|
| 37 | assertTrue(new_capacity != orig_capacity); |
---|
| 38 | buf.setSize(size*4); |
---|
| 39 | assertTrue(new_capacity != buf.getCapacity()); |
---|
| 40 | for(int i=0; i < size*2; ++i) { |
---|
| 41 | assertEquals(hadoop[i%size], buf.getBytes()[i]); |
---|
| 42 | } |
---|
| 43 | // shrink the buffer |
---|
| 44 | buf.setCapacity(1); |
---|
| 45 | // make sure the size has been cut down too |
---|
| 46 | assertEquals(1, buf.getLength()); |
---|
| 47 | // but that the data is still there |
---|
| 48 | assertEquals(hadoop[0], buf.getBytes()[0]); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | public void testHash() throws Exception { |
---|
| 52 | byte[] owen = "owen".getBytes(); |
---|
| 53 | BytesWritable buf = new BytesWritable(owen); |
---|
| 54 | assertEquals(4347922, buf.hashCode()); |
---|
| 55 | buf.setCapacity(10000); |
---|
| 56 | assertEquals(4347922, buf.hashCode()); |
---|
| 57 | buf.setSize(0); |
---|
| 58 | assertEquals(1, buf.hashCode()); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | public void testCompare() throws Exception { |
---|
| 62 | byte[][] values = new byte[][]{"abc".getBytes(), |
---|
| 63 | "ad".getBytes(), |
---|
| 64 | "abcd".getBytes(), |
---|
| 65 | "".getBytes(), |
---|
| 66 | "b".getBytes()}; |
---|
| 67 | BytesWritable[] buf = new BytesWritable[values.length]; |
---|
| 68 | for(int i=0; i < values.length; ++i) { |
---|
| 69 | buf[i] = new BytesWritable(values[i]); |
---|
| 70 | } |
---|
| 71 | // check to make sure the compare function is symetric and reflexive |
---|
| 72 | for(int i=0; i < values.length; ++i) { |
---|
| 73 | for(int j=0; j < values.length; ++j) { |
---|
| 74 | assertTrue(buf[i].compareTo(buf[j]) == -buf[j].compareTo(buf[i])); |
---|
| 75 | assertTrue((i == j) == (buf[i].compareTo(buf[j]) == 0)); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | assertTrue(buf[0].compareTo(buf[1]) < 0); |
---|
| 79 | assertTrue(buf[1].compareTo(buf[2]) > 0); |
---|
| 80 | assertTrue(buf[2].compareTo(buf[3]) > 0); |
---|
| 81 | assertTrue(buf[3].compareTo(buf[4]) < 0); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | private void checkToString(byte[] input, String expected) { |
---|
| 85 | String actual = new BytesWritable(input).toString(); |
---|
| 86 | assertEquals(expected, actual); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | public void testToString() { |
---|
| 90 | checkToString(new byte[]{0,1,2,0x10}, "00 01 02 10"); |
---|
| 91 | checkToString(new byte[]{-0x80, -0x7f, -0x1, -0x2, 1, 0}, |
---|
| 92 | "80 81 ff fe 01 00"); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|