[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 | |
---|
| 19 | package org.apache.hadoop.io.file.tfile; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.util.Random; |
---|
| 23 | |
---|
| 24 | import junit.framework.Assert; |
---|
| 25 | import junit.framework.TestCase; |
---|
| 26 | |
---|
| 27 | import org.apache.hadoop.conf.Configuration; |
---|
| 28 | import org.apache.hadoop.fs.FSDataInputStream; |
---|
| 29 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
| 30 | import org.apache.hadoop.fs.FileSystem; |
---|
| 31 | import org.apache.hadoop.fs.Path; |
---|
| 32 | |
---|
| 33 | public class TestVLong extends TestCase { |
---|
| 34 | private static String ROOT = |
---|
| 35 | System.getProperty("test.build.data", "/tmp/tfile-test"); |
---|
| 36 | private Configuration conf; |
---|
| 37 | private FileSystem fs; |
---|
| 38 | private Path path; |
---|
| 39 | private String outputFile = "TestVLong"; |
---|
| 40 | |
---|
| 41 | @Override |
---|
| 42 | public void setUp() throws IOException { |
---|
| 43 | conf = new Configuration(); |
---|
| 44 | path = new Path(ROOT, outputFile); |
---|
| 45 | fs = path.getFileSystem(conf); |
---|
| 46 | if (fs.exists(path)) { |
---|
| 47 | fs.delete(path, false); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | @Override |
---|
| 52 | public void tearDown() throws IOException { |
---|
| 53 | if (fs.exists(path)) { |
---|
| 54 | fs.delete(path, false); |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public void testVLongByte() throws IOException { |
---|
| 59 | FSDataOutputStream out = fs.create(path); |
---|
| 60 | for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; ++i) { |
---|
| 61 | Utils.writeVLong(out, i); |
---|
| 62 | } |
---|
| 63 | out.close(); |
---|
| 64 | Assert.assertEquals("Incorrect encoded size", (1 << Byte.SIZE) + 96, fs |
---|
| 65 | .getFileStatus( |
---|
| 66 | path).getLen()); |
---|
| 67 | |
---|
| 68 | FSDataInputStream in = fs.open(path); |
---|
| 69 | for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; ++i) { |
---|
| 70 | long n = Utils.readVLong(in); |
---|
| 71 | Assert.assertEquals(n, i); |
---|
| 72 | } |
---|
| 73 | in.close(); |
---|
| 74 | fs.delete(path, false); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | private long writeAndVerify(int shift) throws IOException { |
---|
| 78 | FSDataOutputStream out = fs.create(path); |
---|
| 79 | for (int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; ++i) { |
---|
| 80 | Utils.writeVLong(out, ((long) i) << shift); |
---|
| 81 | } |
---|
| 82 | out.close(); |
---|
| 83 | FSDataInputStream in = fs.open(path); |
---|
| 84 | for (int i = Short.MIN_VALUE; i <= Short.MAX_VALUE; ++i) { |
---|
| 85 | long n = Utils.readVLong(in); |
---|
| 86 | Assert.assertEquals(n, ((long) i) << shift); |
---|
| 87 | } |
---|
| 88 | in.close(); |
---|
| 89 | long ret = fs.getFileStatus(path).getLen(); |
---|
| 90 | fs.delete(path, false); |
---|
| 91 | return ret; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public void testVLongShort() throws IOException { |
---|
| 95 | long size = writeAndVerify(0); |
---|
| 96 | Assert.assertEquals("Incorrect encoded size", (1 << Short.SIZE) * 2 |
---|
| 97 | + ((1 << Byte.SIZE) - 40) |
---|
| 98 | * (1 << Byte.SIZE) - 128 - 32, size); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | public void testVLong3Bytes() throws IOException { |
---|
| 102 | long size = writeAndVerify(Byte.SIZE); |
---|
| 103 | Assert.assertEquals("Incorrect encoded size", (1 << Short.SIZE) * 3 |
---|
| 104 | + ((1 << Byte.SIZE) - 32) * (1 << Byte.SIZE) - 40 - 1, size); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | public void testVLong4Bytes() throws IOException { |
---|
| 108 | long size = writeAndVerify(Byte.SIZE * 2); |
---|
| 109 | Assert.assertEquals("Incorrect encoded size", (1 << Short.SIZE) * 4 |
---|
| 110 | + ((1 << Byte.SIZE) - 16) * (1 << Byte.SIZE) - 32 - 2, size); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public void testVLong5Bytes() throws IOException { |
---|
| 114 | long size = writeAndVerify(Byte.SIZE * 3); |
---|
| 115 | Assert.assertEquals("Incorrect encoded size", (1 << Short.SIZE) * 6 - 256 |
---|
| 116 | - 16 - 3, size); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | private void verifySixOrMoreBytes(int bytes) throws IOException { |
---|
| 120 | long size = writeAndVerify(Byte.SIZE * (bytes - 2)); |
---|
| 121 | Assert.assertEquals("Incorrect encoded size", (1 << Short.SIZE) |
---|
| 122 | * (bytes + 1) - 256 - bytes + 1, size); |
---|
| 123 | } |
---|
| 124 | public void testVLong6Bytes() throws IOException { |
---|
| 125 | verifySixOrMoreBytes(6); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public void testVLong7Bytes() throws IOException { |
---|
| 129 | verifySixOrMoreBytes(7); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | public void testVLong8Bytes() throws IOException { |
---|
| 133 | verifySixOrMoreBytes(8); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | public void testVLongRandom() throws IOException { |
---|
| 137 | int count = 1024 * 1024; |
---|
| 138 | long data[] = new long[count]; |
---|
| 139 | Random rng = new Random(); |
---|
| 140 | for (int i = 0; i < data.length; ++i) { |
---|
| 141 | int shift = rng.nextInt(Long.SIZE) + 1; |
---|
| 142 | long mask = (1L << shift) - 1; |
---|
| 143 | long a = rng.nextInt() << 32; |
---|
| 144 | long b = ((long) rng.nextInt()) & 0xffffffff; |
---|
| 145 | data[i] = (a + b) & mask; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | FSDataOutputStream out = fs.create(path); |
---|
| 149 | for (int i = 0; i < data.length; ++i) { |
---|
| 150 | Utils.writeVLong(out, data[i]); |
---|
| 151 | } |
---|
| 152 | out.close(); |
---|
| 153 | |
---|
| 154 | FSDataInputStream in = fs.open(path); |
---|
| 155 | for (int i = 0; i < data.length; ++i) { |
---|
| 156 | Assert.assertEquals(Utils.readVLong(in), data[i]); |
---|
| 157 | } |
---|
| 158 | in.close(); |
---|
| 159 | fs.delete(path, false); |
---|
| 160 | } |
---|
| 161 | } |
---|