1 package org.apache.directmemory.memory.allocator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }