Debian Codegraph @

src/test/java/io/reactivex/rxjava3/tck/BaseTck.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.tck;
15
16 import java.util.*;
17
18 import org.reactivestreams.Publisher;
19 import org.reactivestreams.tck.*;
20 import org.testng.annotations.Test;
21
22 import io.reactivex.rxjava3.core.Flowable;
23 import io.reactivex.rxjava3.exceptions.TestException;
24
25 /**
26 * Base abstract class for Flowable verifications, contains support for creating
27 * Iterable range of values.
28 *
29 * @param <T> the element type
30 */
31 @Test
32 public abstract class BaseTck<T> extends PublisherVerification<T> {
33
34 public BaseTck() {
35 this(25L);
36 }
37
38 public BaseTck(long timeout) {
39 super(new TestEnvironment(timeout));
40 }
41
42 @Override
43 public Publisher<T> createFailedPublisher() {
44 return Flowable.error(new TestException());
45 }
46
47 @Override
48 public long maxElementsFromPublisher() {
49 return 1024;
50 }
51
52 /**
53 * Creates an Iterable with the specified number of elements or an infinite one if
54 * {@code elements >} {@link Integer#MAX_VALUE}.
55 * @param elements the number of elements to return, {@link Integer#MAX_VALUE} means an infinite sequence
56 * @return the Iterable
57 */
58 protected Iterable<Long> iterate(long elements) {
59 return iterate(elements > Integer.MAX_VALUE, elements);
60 }
61
62 protected Iterable<Long> iterate(boolean useInfinite, long elements) {
63 return useInfinite ? new InfiniteRange() : new FiniteRange(elements);
64 }
65
66 /**
67 * Create an array of Long values, ranging from 0L to elements - 1L.
68 * @param elements the number of elements to return
69 * @return the array
70 */
71 protected Long[] array(long elements) {
72 Long[] a = new Long[(int)elements];
73 for (int i = 0; i < elements; i++) {
74 a[i] = (long)i;
75 }
76 return a;
77 }
78
79 static final class FiniteRange implements Iterable<Long> {
80 final long end;
81 FiniteRange(long end) {
82 this.end = end;
83 }
84
85 @Override
86 public Iterator<Long> iterator() {
87 return new FiniteRangeIterator(end);
88 }
89
90 static final class FiniteRangeIterator implements Iterator<Long> {
91 final long end;
92 long count;
93
94 FiniteRangeIterator(long end) {
95 this.end = end;
96 }
97
98 @Override
99 public boolean hasNext() {
100 return count != end;
101 }
102
103 @Override
104 public Long next() {
105 long c = count;
106 if (c != end) {
107 count = c + 1;
108 return c;
109 }
110 throw new NoSuchElementException();
111 }
112
113 @Override
114 public void remove() {
115 throw new UnsupportedOperationException();
116 }
117 }
118 }
119
120 static final class InfiniteRange implements Iterable<Long> {
121 @Override
122 public Iterator<Long> iterator() {
123 return new InfiniteRangeIterator();
124 }
125
126 static final class InfiniteRangeIterator implements Iterator<Long> {
127 long count;
128
129 @Override
130 public boolean hasNext() {
131 return true;
132 }
133
134 @Override
135 public Long next() {
136 return count++;
137 }
138
139 @Override
140 public void remove() {
141 throw new UnsupportedOperationException();
142 }
143 }
144 }
145 }