View Javadoc

1   package org.apache.directmemory.memory.allocator;
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 java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import junit.framework.Assert;
27  
28  import org.apache.directmemory.memory.buffer.MemoryBuffer;
29  import org.junit.Test;
30  
31  public class SlabByteBufferAllocatorTest
32  {
33      @Test
34      public void allocationTest()
35          throws IOException
36      {
37          
38          List<FixedSizeByteBufferAllocatorImpl> slabs = new ArrayList<FixedSizeByteBufferAllocatorImpl>();
39          slabs.add( new FixedSizeByteBufferAllocatorImpl( 0, 1024, 128, 1 ) );
40          slabs.add( new FixedSizeByteBufferAllocatorImpl( 1, 1024, 256, 1 ) );
41          slabs.add( new FixedSizeByteBufferAllocatorImpl( 2, 1024, 512, 1 ) );
42          slabs.add( new FixedSizeByteBufferAllocatorImpl( 3, 1024, 1024, 1 ) );
43          
44          
45          Allocator allocator = new SlabByteBufferAllocator( 0, slabs, false );
46          
47          MemoryBuffer bf1 = allocator.allocate( 250 );
48          Assert.assertEquals( 256, bf1.maxCapacity() );
49          Assert.assertEquals( 250, bf1.capacity() );
50          
51          MemoryBuffer bf2 = allocator.allocate( 251 );
52          Assert.assertEquals( 256, bf2.maxCapacity() );
53          Assert.assertEquals( 251, bf2.capacity() );
54          
55          MemoryBuffer bf3 = allocator.allocate( 200 );
56          Assert.assertEquals( 256, bf3.maxCapacity() );
57          Assert.assertEquals( 200, bf3.capacity() );
58          
59          MemoryBuffer bf4 = allocator.allocate( 100 );
60          Assert.assertEquals( 128, bf4.maxCapacity() );
61          Assert.assertEquals( 100, bf4.capacity() );
62          
63          MemoryBuffer bf5 = allocator.allocate( 550 );
64          Assert.assertEquals( 1024, bf5.maxCapacity() );
65          Assert.assertEquals( 550, bf5.capacity() );
66          
67          MemoryBuffer bf6 = allocator.allocate( 800 );
68          Assert.assertNull( bf6 );
69  
70          allocator.free( bf5 );
71          
72          MemoryBuffer bf7 = allocator.allocate( 800 );
73          Assert.assertEquals( 1024, bf7.maxCapacity() );
74          Assert.assertEquals( 800, bf7.capacity() );
75       
76          allocator.close();
77      }
78      
79  }