Debian Codegraph @

src/test/java/com/zaxxer/hikari/util/TestFastList.java

raw
1 /*
2 * Copyright (C) 2013, 2014 Brett Wooldridge
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.zaxxer.hikari.util;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertSame;
23
24 import java.sql.Statement;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27
28 import org.junit.Test;
29
30 import com.zaxxer.hikari.mocks.StubStatement;
31
32 public class TestFastList
33 {
34 @Test
35 public void testAddRemove()
36 {
37 ArrayList<Statement> verifyList = new ArrayList<>();
38
39 FastList<Statement> list = new FastList<>(Statement.class);
40 for (int i = 0; i < 32; i++)
41 {
42 StubStatement statement = new StubStatement(null);
43 list.add(statement);
44 verifyList.add(statement);
45 }
46
47 for (int i = 0; i < 32; i++)
48 {
49 assertNotNull("Element " + i + " was null but should be " + verifyList.get(i), list.get(0));
50 int size = list.size();
51 list.remove(verifyList.get(i));
52 assertSame(size - 1, list.size());
53 }
54 }
55
56 @Test
57 public void testAddRemoveTail()
58 {
59 ArrayList<Statement> verifyList = new ArrayList<>();
60
61 FastList<Statement> list = new FastList<>(Statement.class);
62 for (int i = 0; i < 32; i++)
63 {
64 StubStatement statement = new StubStatement(null);
65 list.add(statement);
66 verifyList.add(statement);
67 }
68
69 for (int i = 31; i >= 0; i--)
70 {
71 assertNotNull("Element " + i, list.get(i));
72 int size = list.size();
73 list.remove(verifyList.get(i));
74 assertSame(size - 1, list.size());
75 }
76 }
77
78 @Test
79 public void testOverflow()
80 {
81 ArrayList<Statement> verifyList = new ArrayList<>();
82
83 FastList<Statement> list = new FastList<>(Statement.class);
84 for (int i = 0; i < 100; i++)
85 {
86 StubStatement statement = new StubStatement(null);
87 list.add(statement);
88 verifyList.add(statement);
89 }
90
91 for (int i = 0; i < 100; i++)
92 {
93 assertNotNull("Element " + i, list.get(i));
94 assertSame(verifyList.get(i), list.get(i));
95 }
96 }
97
98 @Test
99 public void testIterator()
100 {
101 FastList<Statement> list = new FastList<>(Statement.class);
102 for (int i = 0; i < 100; i++)
103 {
104 StubStatement statement = new StubStatement(null);
105 list.add(statement);
106 }
107
108 Iterator<Statement> iter = list.iterator();
109 for (int i = 0; i < list.size(); i++) {
110 assertSame(list.get(i), iter.next());
111 }
112 }
113
114 @Test
115 public void testClear()
116 {
117 FastList<Statement> list = new FastList<>(Statement.class);
118 for (int i = 0; i < 100; i++)
119 {
120 StubStatement statement = new StubStatement(null);
121 list.add(statement);
122 }
123
124 assertNotEquals(0, list.size());
125 list.clear();
126 assertEquals(0, list.size());
127 // also check that all elements are now null
128 for (int i = 0; i < 100; i++) {
129 assertEquals(null, list.get(i));
130 }
131 }
132
133 @Test
134 public void testRemoveLast()
135 {
136 FastList<Statement> list = new FastList<>(Statement.class);
137
138 Statement last = null;
139 for (int i = 0; i < 100; i++)
140 {
141 StubStatement statement = new StubStatement(null);
142 list.add(statement);
143 last = statement;
144 }
145
146 assertEquals(last, list.removeLast());
147 assertEquals(99, list.size());
148 }
149
150 @Test
151 public void testPolyMorphism1()
152 {
153 class Foo implements Base2 {
154
155 }
156
157 class Bar extends Foo {
158
159 }
160
161 FastList<Base> list = new FastList<>(Base.class, 2);
162 list.add(new Foo());
163 list.add(new Foo());
164 list.add(new Bar());
165 }
166
167 interface Base
168 {
169
170 }
171
172 interface Base2 extends Base
173 {
174
175 }
176 }