This project has retired. For details please refer to its Attic page.
TestVarint xref
View Javadoc

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.giraph.utils;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  import java.util.concurrent.ThreadLocalRandom;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  public class TestVarint {
29    private long[] genLongs(int n) {
30      long[] res = new long[n];
31      for (int i = 0; i < n; i++) {
32        res[i] = ThreadLocalRandom.current().nextLong();
33      }
34      return res;
35    }
36  
37    private int[] genInts(int n) {
38      int[] res = new int[n];
39      for (int i = 0; i < n; i++) {
40        res[i] = ThreadLocalRandom.current().nextInt();
41      }
42      return res;
43    }
44  
45    private void writeLongs(DataOutput out, long[] array) throws IOException {
46      for (int i = 0; i < array.length; i++) {
47        Varint.writeSignedVarLong(array[i], out);
48      }
49    }
50  
51    private void writeInts(DataOutput out, int[] array) throws IOException {
52      for (int i = 0; i < array.length; i++) {
53        Varint.writeSignedVarInt(array[i], out);
54      }
55    }
56  
57    private void readLongs(DataInput in, long[] array) throws IOException {
58      for (int i = 0; i < array.length; i++) {
59        array[i] = Varint.readSignedVarLong(in);
60      }
61    }
62  
63    private void readInts(DataInput in, int[] array) throws IOException {
64      for (int i = 0; i < array.length; i++) {
65        array[i] = Varint.readSignedVarInt(in);
66      }
67    }
68  
69    private void testVarLong(long value) throws IOException {
70      UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
71      Varint.writeSignedVarLong(value, os);
72  
73      UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
74      long newValue = Varint.readSignedVarLong(is);
75  
76      Assert.assertEquals(Varint.sizeOfSignedVarLong(value), os.getPos());
77      Assert.assertEquals(value, newValue);
78  
79      if (value >= 0) {
80        os = new UnsafeByteArrayOutputStream();
81        Varint.writeUnsignedVarLong(value, os);
82        is = new UnsafeByteArrayInputStream(os.getByteArray());
83        newValue = Varint.readUnsignedVarLong(is);
84        Assert.assertEquals(Varint.sizeOfUnsignedVarLong(value), os.getPos());
85        Assert.assertEquals(value, newValue);
86      }
87    }
88  
89    private void testVarInt(int value) throws IOException {
90      UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
91      Varint.writeSignedVarInt(value, os);
92  
93      UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
94      int newValue = Varint.readSignedVarInt(is);
95  
96      Assert.assertEquals(Varint.sizeOfSignedVarLong(value), os.getPos());
97      Assert.assertEquals(value, newValue);
98  
99      if (value >= 0) {
100       os = new UnsafeByteArrayOutputStream();
101       Varint.writeUnsignedVarInt(value, os);
102       is = new UnsafeByteArrayInputStream(os.getByteArray());
103       newValue = Varint.readUnsignedVarInt(is);
104       Assert.assertEquals(Varint.sizeOfUnsignedVarInt(value), os.getPos());
105       Assert.assertEquals(value, newValue);
106     }
107   }
108 
109   @Test
110   public void testVars() throws IOException {
111     testVarLong(0);
112     testVarLong(Long.MIN_VALUE);
113     testVarLong(Long.MAX_VALUE);
114     testVarLong(-123456789999l);
115     testVarLong(12342356789999l);
116     testVarInt(0);
117     testVarInt(4);
118     testVarInt(-1);
119     testVarInt(1);
120     testVarInt(Integer.MIN_VALUE);
121     testVarInt(Integer.MAX_VALUE);
122     testVarInt(Integer.MAX_VALUE - 1);
123   }
124 
125   @Test
126   public void testVarLongSmall() throws IOException {
127     long[] array = new long[] {1, 2, 3, -5, 0, 12345678987l, Long.MIN_VALUE};
128     UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
129     writeLongs(os, array);
130 
131     long[] resArray = new long[array.length];
132     UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
133     readLongs(is, resArray);
134 
135     Assert.assertArrayEquals(array, resArray);
136   }
137 
138   @Test
139   public void testVarIntSmall() throws IOException {
140     int[] array = new int[] {13, -2, 3, 0, 123456789, Integer.MIN_VALUE, Integer.MAX_VALUE};
141     UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
142     writeInts(os, array);
143 
144     int[] resArray = new int[array.length];
145     UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
146     readInts(is, resArray);
147 
148     Assert.assertArrayEquals(array, resArray);
149   }
150 
151   @Test
152   public void testVarLongLarge() throws IOException {
153     int n = 1000000;
154     long[] array = genLongs(n);
155     UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
156 
157     long startTime = System.currentTimeMillis();
158     writeLongs(os, array);
159     long endTime = System.currentTimeMillis();
160     System.out.println("Write time: " + (endTime - startTime) / 1000.0);
161 
162     long[] resArray = new long[array.length];
163     UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
164     startTime = System.currentTimeMillis();
165     readLongs(is, resArray);
166     endTime = System.currentTimeMillis();
167     System.out.println("Read time: " + (endTime - startTime) / 1000.0);
168 
169     Assert.assertArrayEquals(array, resArray);
170   }
171 
172   @Test
173   public void testVarIntLarge() throws IOException {
174     int n = 1000000;
175     int[] array = genInts(n);
176     UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream();
177 
178     long startTime = System.currentTimeMillis();
179     writeInts(os, array);
180     long endTime = System.currentTimeMillis();
181     System.out.println("Write time: " + (endTime - startTime) / 1000.0);
182 
183     int[] resArray = new int[array.length];
184     UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(os.getByteArray());
185     startTime = System.currentTimeMillis();
186     readInts(is, resArray);
187     endTime = System.currentTimeMillis();
188     System.out.println("Read time: " + (endTime - startTime) / 1000.0);
189 
190     Assert.assertArrayEquals(array, resArray);
191   }
192 
193   @Test
194   public void testSmall() throws IOException {
195     for (int i = -100000; i <= 100000; i++) {
196       testVarInt(i);
197       testVarLong(i);
198     }
199   }
200 
201 }