View Javadoc

1   package org.apache.directmemory.memory.buffer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_FULL;
23  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_MAX_DOUBLE;
24  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_MAX_SINGLE;
25  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_MAX_TRIPPLE;
26  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_MIN_DOUBLE;
27  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_MIN_SINGLE;
28  import static org.apache.directmemory.memory.buffer.Int32Compressor.INT32_MIN_TRIPPLE;
29  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_FULL;
30  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_DOUBLE;
31  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_FIFTH;
32  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_QUAD;
33  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_SEVENTH;
34  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_SINGLE;
35  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_SIXTH;
36  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MAX_TRIPPLE;
37  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_DOUBLE;
38  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_FIFTH;
39  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_QUAD;
40  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_SEVENTH;
41  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_SINGLE;
42  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_SIXTH;
43  import static org.apache.directmemory.memory.buffer.Int64Compressor.INT64_MIN_TRIPPLE;
44  import static org.junit.Assert.assertEquals;
45  
46  import java.util.Random;
47  
48  import org.apache.directmemory.memory.allocator.FixedSizeUnsafeAllocator;
49  import org.junit.Test;
50  
51  public class IntLongCompressionTestCase
52  {
53  
54      private static final int TEST_VALUES_COUNT = 1000000;
55  
56      @Test
57      public void testInt32Compression()
58          throws Exception
59      {
60          FixedSizeUnsafeAllocator allocator = new FixedSizeUnsafeAllocator( 1, 5 );
61          try
62          {
63              for ( int i = 0; i < 4; i++ )
64              {
65                  int[] values = new int[TEST_VALUES_COUNT];
66  
67                  Random random = new Random( -System.currentTimeMillis() );
68                  int min = ( 0x1 << ( i * 8 ) );
69                  int max = ( 0xFF << ( i * 8 ) );
70                  for ( int o = 0; o < TEST_VALUES_COUNT; o++ )
71                  {
72                      values[o] = (int) ( random.nextDouble() * ( max - min + 1 ) ) + min;
73                  }
74  
75                  MemoryBuffer buffer = allocator.allocate( 5 );
76  
77                  for ( int v = 0; v < TEST_VALUES_COUNT; v++ )
78                  {
79                      int value = values[v];
80                      buffer.clear();
81                      buffer.writeCompressedInt( value );
82                      checkIntLength( value, buffer.writerIndex() );
83  
84                      int result = buffer.readCompressedInt();
85                      assertEquals( value, result );
86                  }
87                  allocator.free( buffer );
88              }
89          }
90          finally
91          {
92              allocator.close();
93          }
94      }
95  
96      @Test
97      public void testInt64Compression()
98          throws Exception
99      {
100 
101         FixedSizeUnsafeAllocator allocator = new FixedSizeUnsafeAllocator( 1, 9 );
102         try
103         {
104             for ( int i = 0; i < 8; i++ )
105             {
106                 long[] values = new long[TEST_VALUES_COUNT];
107 
108                 Random random = new Random( -System.currentTimeMillis() );
109                 int min = ( 0x1 << ( i * 8 ) );
110                 int max = ( 0xFF << ( i * 8 ) );
111                 for ( int o = 0; o < TEST_VALUES_COUNT; o++ )
112                 {
113                     values[o] = (long) ( random.nextDouble() * ( max - min + 1 ) ) + min;
114                 }
115 
116                 MemoryBuffer buffer = allocator.allocate( 5 );
117 
118                 for ( int v = 0; v < TEST_VALUES_COUNT; v++ )
119                 {
120                     long value = values[v];
121                     buffer.clear();
122                     buffer.writeCompressedLong( value );
123                     checkLongLength( value, buffer.writerIndex() );
124 
125                     long result = buffer.readCompressedLong();
126                     assertEquals( value, result );
127                 }
128                 allocator.free( buffer );
129             }
130         }
131         finally
132         {
133             allocator.close();
134         }
135     }
136 
137     private void checkIntLength( int value, long delta )
138     {
139         if ( value >= INT32_MIN_SINGLE && value <= INT32_MAX_SINGLE )
140         {
141             assertEquals( "Compressed bytesize for value " + value + " is too large", 2, delta );
142         }
143         else if ( value >= INT32_MIN_DOUBLE && value <= INT32_MAX_DOUBLE )
144         {
145             assertEquals( "Compressed bytesize for value " + value + " is too large", 3, delta );
146         }
147         else if ( value >= INT32_MIN_TRIPPLE && value <= INT32_MAX_TRIPPLE )
148         {
149             assertEquals( "Compressed bytesize for value " + value + " is too large", 4, delta );
150         }
151         else if ( value >= INT32_FULL && value <= INT32_FULL )
152         {
153             assertEquals( "Compressed bytesize for value " + value + " is too large", 5, delta );
154         }
155     }
156 
157     private void checkLongLength( long value, long delta )
158     {
159         if ( value >= INT64_MIN_SINGLE && value <= INT64_MAX_SINGLE )
160         {
161             assertEquals( "Compressed bytesize for value " + value + " is too large", 2, delta );
162         }
163         else if ( value >= INT64_MIN_DOUBLE && value <= INT64_MAX_DOUBLE )
164         {
165             assertEquals( "Compressed bytesize for value " + value + " is too large", 3, delta );
166         }
167         else if ( value >= INT64_MIN_TRIPPLE && value <= INT64_MAX_TRIPPLE )
168         {
169             assertEquals( "Compressed bytesize for value " + value + " is too large", 4, delta );
170         }
171         else if ( value >= INT64_MIN_QUAD && value <= INT64_MAX_QUAD )
172         {
173             assertEquals( "Compressed bytesize for value " + value + " is too large", 5, delta );
174         }
175         else if ( value >= INT64_MIN_FIFTH && value <= INT64_MAX_FIFTH )
176         {
177             assertEquals( "Compressed bytesize for value " + value + " is too large", 6, delta );
178         }
179         else if ( value >= INT64_MIN_SIXTH && value <= INT64_MAX_SIXTH )
180         {
181             assertEquals( "Compressed bytesize for value " + value + " is too large", 7, delta );
182         }
183         else if ( value >= INT64_MIN_SEVENTH && value <= INT64_MAX_SEVENTH )
184         {
185             assertEquals( "Compressed bytesize for value " + value + " is too large", 8, delta );
186         }
187         else if ( value >= INT64_FULL && value <= INT64_FULL )
188         {
189             assertEquals( "Compressed bytesize for value " + value + " is too large", 9, delta );
190         }
191     }
192 
193 }