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  
24  import junit.framework.Assert;
25  
26  import org.apache.directmemory.memory.buffer.MemoryBuffer;
27  import org.junit.Test;
28  
29  public class FixedSizeByteBufferAllocatorImplTest
30  {
31      @Test
32      public void allocationTest()
33          throws IOException
34      {
35  
36          Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 5000, 256, 1 );
37  
38          MemoryBuffer bf1 = allocator.allocate( 250 );
39          Assert.assertEquals( 256, bf1.maxCapacity() );
40          Assert.assertEquals( 250, bf1.capacity() );
41  
42          MemoryBuffer bf2 = allocator.allocate( 251 );
43          Assert.assertEquals( 256, bf2.maxCapacity() );
44          Assert.assertEquals( 251, bf2.capacity() );
45  
46          MemoryBuffer bf3 = allocator.allocate( 200 );
47          Assert.assertEquals( 256, bf3.maxCapacity() );
48          Assert.assertEquals( 200, bf3.capacity() );
49  
50          MemoryBuffer bf4 = allocator.allocate( 2000 );
51          Assert.assertNull( bf4 );
52  
53          MemoryBuffer bf5 = allocator.allocate( 298 );
54          Assert.assertNull( bf5 );
55  
56          MemoryBuffer bf6 = allocator.allocate( 128 );
57          Assert.assertEquals( 256, bf6.maxCapacity() );
58          Assert.assertEquals( 128, bf6.capacity() );
59  
60          allocator.close();
61      }
62  
63      @Test
64      public void releaseTest()
65          throws IOException
66      {
67  
68          Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );
69  
70          MemoryBuffer bf1 = allocator.allocate( 250 );
71          Assert.assertEquals( 256, bf1.maxCapacity() );
72          Assert.assertEquals( 250, bf1.capacity() );
73  
74          MemoryBuffer bf2 = allocator.allocate( 251 );
75          Assert.assertEquals( 256, bf2.maxCapacity() );
76          Assert.assertEquals( 251, bf2.capacity() );
77  
78          MemoryBuffer bf3 = allocator.allocate( 252 );
79          Assert.assertEquals( 256, bf3.maxCapacity() );
80          Assert.assertEquals( 252, bf3.capacity() );
81  
82          MemoryBuffer bf4 = allocator.allocate( 500 );
83          Assert.assertNull( bf4 );
84  
85          allocator.free( bf1 );
86          allocator.free( bf2 );
87  
88          MemoryBuffer bf5 = allocator.allocate( 500 );
89          Assert.assertNull( bf5 );
90  
91          MemoryBuffer bf6 = allocator.allocate( 249 );
92          Assert.assertEquals( 256, bf6.maxCapacity() );
93          Assert.assertEquals( 249, bf6.capacity() );
94  
95          MemoryBuffer bf7 = allocator.allocate( 248 );
96          Assert.assertEquals( 256, bf7.maxCapacity() );
97          Assert.assertEquals( 248, bf7.capacity() );
98  
99          allocator.close();
100     }
101 
102     @Test
103     public void allocateAndFreeTest()
104         throws IOException
105     {
106 
107         Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );
108 
109         for ( int i = 0; i < 1000; i++ )
110         {
111             MemoryBuffer bf1 = allocator.allocate( 250 );
112             Assert.assertEquals( 256, bf1.maxCapacity() );
113             Assert.assertEquals( 250, bf1.capacity() );
114 
115             allocator.free( bf1 );
116         }
117 
118         MemoryBuffer bf2 = allocator.allocate( 1000 );
119         Assert.assertNull( bf2 );
120 
121         for ( int i = 0; i < 3; i++ )
122         {
123             MemoryBuffer bf3 = allocator.allocate( 250 );
124             Assert.assertEquals( 256, bf3.maxCapacity() );
125             Assert.assertEquals( 250, bf3.capacity() );
126 
127         }
128 
129         MemoryBuffer bf4 = allocator.allocate( 238 );
130         Assert.assertNull( bf4 );
131 
132         allocator.close();
133     }
134 
135 }