Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/util/MiscUtilTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.util;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.RxJavaTest;
23 import io.reactivex.rxjava3.functions.BiPredicate;
24 import io.reactivex.rxjava3.internal.util.AppendOnlyLinkedArrayList.NonThrowingPredicate;
25 import io.reactivex.rxjava3.testsupport.TestHelper;
26
27 public class MiscUtilTest extends RxJavaTest {
28 @Test
29 public void pow2UtilityClass() {
30 TestHelper.checkUtilityClass(Pow2.class);
31 }
32
33 @Test
34 public void isPowerOf2() {
35 for (int i = 1; i > 0; i *= 2) {
36 assertTrue(Pow2.isPowerOfTwo(i));
37 }
38
39 assertFalse(Pow2.isPowerOfTwo(3));
40 assertFalse(Pow2.isPowerOfTwo(5));
41 assertFalse(Pow2.isPowerOfTwo(6));
42 assertFalse(Pow2.isPowerOfTwo(7));
43 }
44
45 @Test
46 public void hashMapSupplier() {
47 TestHelper.checkEnum(HashMapSupplier.class);
48 }
49
50 @Test
51 public void arrayListSupplier() {
52 TestHelper.checkEnum(ArrayListSupplier.class);
53 }
54
55 @Test
56 public void errorModeEnum() {
57 TestHelper.checkEnum(ErrorMode.class);
58 }
59
60 @Test
61 public void linkedArrayList() {
62 LinkedArrayList list = new LinkedArrayList(2);
63 assertEquals(0, list.size());
64 list.add(1);
65 assertEquals(1, list.size());
66 list.add(2);
67 assertEquals(2, list.size());
68 list.add(3);
69 assertEquals(3, list.size());
70 assertEquals("[1, 2, 3]", list.toString());
71 }
72
73 @Test
74 public void appendOnlyLinkedArrayListForEachWhile() throws Exception {
75 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(2);
76
77 list.add(1);
78 list.add(2);
79 list.add(3);
80
81 final List<Integer> out = new ArrayList<>();
82
83 list.forEachWhile(new NonThrowingPredicate<Integer>() {
84 @Override
85 public boolean test(Integer t2) {
86 out.add(t2);
87 return t2 == 2;
88 }
89 });
90
91 assertEquals(Arrays.asList(1, 2), out);
92 }
93
94 @Test
95 public void appendOnlyLinkedArrayListForEachWhileBi() throws Throwable {
96 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(2);
97
98 list.add(1);
99 list.add(2);
100 list.add(3);
101
102 final List<Integer> out = new ArrayList<>();
103
104 list.forEachWhile(2, new BiPredicate<Integer, Integer>() {
105 @Override
106 public boolean test(Integer t1, Integer t2) throws Throwable {
107 out.add(t2);
108 return t1.equals(t2);
109 }
110 });
111
112 assertEquals(Arrays.asList(1, 2), out);
113 }
114
115 @Test
116 public void appendOnlyLinkedArrayListForEachWhilePreGrow() throws Exception {
117 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(12);
118
119 list.add(1);
120 list.add(2);
121 list.add(3);
122
123 final List<Integer> out = new ArrayList<>();
124
125 list.forEachWhile(new NonThrowingPredicate<Integer>() {
126 @Override
127 public boolean test(Integer t2) {
128 out.add(t2);
129 return t2 == 2;
130 }
131 });
132
133 assertEquals(Arrays.asList(1, 2), out);
134 }
135
136 @Test
137 public void appendOnlyLinkedArrayListForEachWhileExact() throws Exception {
138 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(3);
139
140 list.add(1);
141 list.add(2);
142 list.add(3);
143
144 final List<Integer> out = new ArrayList<>();
145
146 list.forEachWhile(new NonThrowingPredicate<Integer>() {
147 @Override
148 public boolean test(Integer t2) {
149 out.add(t2);
150 return t2 == 2;
151 }
152 });
153
154 assertEquals(Arrays.asList(1, 2), out);
155 }
156
157 @Test
158 public void appendOnlyLinkedArrayListForEachWhileAll() throws Exception {
159 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(2);
160
161 list.add(1);
162 list.add(2);
163 list.add(3);
164
165 final List<Integer> out = new ArrayList<>();
166
167 list.forEachWhile(new NonThrowingPredicate<Integer>() {
168 @Override
169 public boolean test(Integer t2) {
170 out.add(t2);
171 return t2 == 3;
172 }
173 });
174
175 assertEquals(Arrays.asList(1, 2, 3), out);
176 }
177
178 @Test
179 public void appendOnlyLinkedArrayListForEachWhileBigger() throws Exception {
180 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(4);
181
182 list.add(1);
183 list.add(2);
184 list.add(3);
185
186 final List<Integer> out = new ArrayList<>();
187
188 list.forEachWhile(new NonThrowingPredicate<Integer>() {
189 @Override
190 public boolean test(Integer t2) {
191 out.add(t2);
192 return false;
193 }
194 });
195
196 assertEquals(Arrays.asList(1, 2, 3), out);
197 }
198
199 @Test
200 public void appendOnlyLinkedArrayListForEachWhileBiPreGrow() throws Throwable {
201 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(12);
202
203 list.add(1);
204 list.add(2);
205 list.add(3);
206
207 final List<Integer> out = new ArrayList<>();
208
209 list.forEachWhile(2, new BiPredicate<Integer, Integer>() {
210 @Override
211 public boolean test(Integer t1, Integer t2) throws Throwable {
212 out.add(t2);
213 return t1.equals(t2);
214 }
215 });
216
217 assertEquals(Arrays.asList(1, 2), out);
218 }
219
220 @Test
221 public void appendOnlyLinkedArrayListForEachWhileBiExact() throws Throwable {
222 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(3);
223
224 list.add(1);
225 list.add(2);
226 list.add(3);
227
228 final List<Integer> out = new ArrayList<>();
229
230 list.forEachWhile(2, new BiPredicate<Integer, Integer>() {
231 @Override
232 public boolean test(Integer t1, Integer t2) throws Exception {
233 out.add(t2);
234 return t1.equals(t2);
235 }
236 });
237
238 assertEquals(Arrays.asList(1, 2), out);
239 }
240
241 @Test
242 public void appendOnlyLinkedArrayListForEachWhileBiAll() throws Throwable {
243 AppendOnlyLinkedArrayList<Integer> list = new AppendOnlyLinkedArrayList<>(2);
244
245 list.add(1);
246 list.add(2);
247 list.add(3);
248
249 final List<Integer> out = new ArrayList<>();
250
251 list.forEachWhile(3, new BiPredicate<Integer, Integer>() {
252 @Override
253 public boolean test(Integer t1, Integer t2) throws Exception {
254 out.add(t2);
255 return false;
256 }
257 });
258
259 assertEquals(Arrays.asList(1, 2, 3), out);
260 }
261
262 @Test
263 public void queueDrainHelperUtility() {
264 TestHelper.checkUtilityClass(QueueDrainHelper.class);
265 }
266 }