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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.buffer;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.nio.ByteBuffer;
27  
28  import org.junit.Test;
29  
30  /**
31   * Tests {@link IoBuffer}.
32   * 
33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34   */
35  public class IoBufferTest {
36      @Test
37      public void testNormalizeCapacity() {
38          // A few sanity checks
39          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(-10));
40          assertEquals(0, IoBufferImpl.normalizeCapacity(0));
41          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(Integer.MAX_VALUE));
42          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(Integer.MIN_VALUE));
43          assertEquals(Integer.MAX_VALUE, IoBufferImpl.normalizeCapacity(Integer.MAX_VALUE - 10));
44  
45          // A sanity check test for all the powers of 2
46          for (int i = 0; i < 30; i++) {
47              int n = 1 << i;
48  
49              assertEquals(n, IoBufferImpl.normalizeCapacity(n));
50  
51              if (i > 1) {
52                  // test that n - 1 will be normalized to n (notice that n = 2^i)
53                  assertEquals(n, IoBufferImpl.normalizeCapacity(n - 1));
54              }
55  
56              // test that n + 1 will be normalized to 2^(i + 1)
57              assertEquals(n << 1, IoBufferImpl.normalizeCapacity(n + 1));
58          }
59  
60          // The first performance test measures the time to normalize integers
61          // from 0 to 2^27 (it tests 2^27 integers)
62          long time = System.currentTimeMillis();
63  
64          for (int i = 0; i < 1 << 27; i++) {
65              int n = IoBufferImpl.normalizeCapacity(i);
66  
67              // do a simple superfluous test to prevent possible compiler or JVM
68              // optimizations of not executing non used code/variables
69              if (n == -1) {
70                  System.out.println("n should never be -1");
71              }
72          }
73  
74          long time2 = System.currentTimeMillis();
75          System.out.println("Time for performance test 1: " + (time2 - time) + "ms");
76  
77          // The second performance test measures the time to normalize integers
78          // from Integer.MAX_VALUE to Integer.MAX_VALUE - 2^27 (it tests 2^27
79          // integers)
80          time = System.currentTimeMillis();
81          for (int i = Integer.MAX_VALUE; i > Integer.MAX_VALUE - (1 << 27); i--) {
82              int n = IoBufferImpl.normalizeCapacity(i);
83  
84              // do a simple superfluous test to prevent possible compiler or JVM
85              // optimizations of not executing non used code/variables
86              if (n == -1) {
87                  System.out.println("n should never be -1");
88              }
89          }
90  
91          time2 = System.currentTimeMillis();
92          System.out.println("Time for performance test 2: " + (time2 - time) + "ms");
93      }
94      
95      @Test 
96      public void autoExpand() { 
97          IoBuffer buffer = IoBuffer.allocate(8, false); 
98          buffer.setAutoExpand(true); 
99           
100         assertTrue("Should AutoExpand", buffer.isAutoExpand()); 
101          
102         IoBuffer slice = buffer.slice(); 
103         assertFalse("Should *NOT* AutoExpand", buffer.isAutoExpand()); 
104         assertFalse("Should *NOT* AutoExpand", slice.isAutoExpand()); 
105     } 
106 
107     /**
108      * This class extends the AbstractIoBuffer class to have direct access to
109      * the protected IoBuffer.normalizeCapacity() method and to expose it for
110      * the tests.
111      */
112     private static class IoBufferImpl extends AbstractIoBuffer {
113 
114         public static int normalizeCapacity(int requestedCapacity) {
115             return IoBuffer.normalizeCapacity(requestedCapacity);
116         }
117 
118         protected IoBufferImpl(AbstractIoBuffer parent) {
119             super(parent);
120         }
121 
122         protected IoBuffer asReadOnlyBuffer0() {
123             return null;
124         }
125 
126         protected void buf(ByteBuffer newBuf) {
127         }
128 
129         protected IoBuffer duplicate0() {
130             return null;
131         }
132 
133         protected IoBuffer slice0() {
134             return null;
135         }
136 
137         public byte[] array() {
138             return null;
139         }
140 
141         public int arrayOffset() {
142             return 0;
143         }
144 
145         public ByteBuffer buf() {
146             return null;
147         }
148 
149         public void free() {
150         }
151 
152         public boolean hasArray() {
153             return false;
154         }
155 
156     }
157 }