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 | |
---|
19 | package org.apache.hadoop.io; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | |
---|
23 | import org.apache.commons.logging.Log; |
---|
24 | import org.apache.commons.logging.LogFactory; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | public class TestWritableUtils extends TestCase { |
---|
29 | private static final Log LOG = LogFactory.getLog(TestWritableUtils.class); |
---|
30 | |
---|
31 | public static void testValue(int val, int vintlen) throws IOException { |
---|
32 | DataOutputBuffer buf = new DataOutputBuffer(); |
---|
33 | DataInputBuffer inbuf = new DataInputBuffer(); |
---|
34 | WritableUtils.writeVInt(buf, val); |
---|
35 | if (LOG.isDebugEnabled()) { |
---|
36 | LOG.debug("Value = " + val); |
---|
37 | BytesWritable printer = new BytesWritable(); |
---|
38 | printer.set(buf.getData(), 0, buf.getLength()); |
---|
39 | LOG.debug("Buffer = " + printer); |
---|
40 | } |
---|
41 | inbuf.reset(buf.getData(), 0, buf.getLength()); |
---|
42 | assertEquals(val, WritableUtils.readVInt(inbuf)); |
---|
43 | assertEquals(vintlen, buf.getLength()); |
---|
44 | assertEquals(vintlen, WritableUtils.getVIntSize(val)); |
---|
45 | assertEquals(vintlen, WritableUtils.decodeVIntSize(buf.getData()[0])); |
---|
46 | } |
---|
47 | |
---|
48 | public static void testVInt() throws Exception { |
---|
49 | testValue(12, 1); |
---|
50 | testValue(127, 1); |
---|
51 | testValue(-112, 1); |
---|
52 | testValue(-113, 2); |
---|
53 | testValue(-128, 2); |
---|
54 | testValue(128, 2); |
---|
55 | testValue(-129, 2); |
---|
56 | testValue(255, 2); |
---|
57 | testValue(-256, 2); |
---|
58 | testValue(256, 3); |
---|
59 | testValue(-257, 3); |
---|
60 | testValue(65535, 3); |
---|
61 | testValue(-65536, 3); |
---|
62 | testValue(65536, 4); |
---|
63 | testValue(-65537, 4); |
---|
64 | } |
---|
65 | } |
---|