1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
35 public class IoBufferTest {
36 @Test
37 public void testNormalizeCapacity() {
38
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
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
53 assertEquals(n, IoBufferImpl.normalizeCapacity(n - 1));
54 }
55
56
57 assertEquals(n << 1, IoBufferImpl.normalizeCapacity(n + 1));
58 }
59
60
61
62 long time = System.currentTimeMillis();
63
64 for (int i = 0; i < 1 << 27; i++) {
65 int n = IoBufferImpl.normalizeCapacity(i);
66
67
68
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
78
79
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
85
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
109
110
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 }