| 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.operators.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertEquals; |
| 17 |
|
| 18 |
import java.util.Arrays; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.functions.*; |
| 25 |
|
| 26 |
public class SingleZipTest extends RxJavaTest { |
| 27 |
|
| 28 |
@Test |
| 29 |
public void zip2() { |
| 30 |
Single.zip(Single.just(1), Single.just(2), new BiFunction<Integer, Integer, Object>() { |
| 31 |
@Override |
| 32 |
public Object apply(Integer a, Integer b) throws Exception { |
| 33 |
return a + "" + b; |
| 34 |
} |
| 35 |
}) |
| 36 |
.test() |
| 37 |
.assertResult("12"); |
| 38 |
} |
| 39 |
|
| 40 |
@Test |
| 41 |
public void zip3() { |
| 42 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), new Function3<Integer, Integer, Integer, Object>() { |
| 43 |
@Override |
| 44 |
public Object apply(Integer a, Integer b, Integer c) throws Exception { |
| 45 |
return a + "" + b + c; |
| 46 |
} |
| 47 |
}) |
| 48 |
.test() |
| 49 |
.assertResult("123"); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void zip4() { |
| 54 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), |
| 55 |
Single.just(4), |
| 56 |
new Function4<Integer, Integer, Integer, Integer, Object>() { |
| 57 |
@Override |
| 58 |
public Object apply(Integer a, Integer b, Integer c, Integer d) throws Exception { |
| 59 |
return a + "" + b + c + d; |
| 60 |
} |
| 61 |
}) |
| 62 |
.test() |
| 63 |
.assertResult("1234"); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void zip5() { |
| 68 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), |
| 69 |
Single.just(4), Single.just(5), |
| 70 |
new Function5<Integer, Integer, Integer, Integer, Integer, Object>() { |
| 71 |
@Override |
| 72 |
public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e) throws Exception { |
| 73 |
return a + "" + b + c + d + e; |
| 74 |
} |
| 75 |
}) |
| 76 |
.test() |
| 77 |
.assertResult("12345"); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void zip6() { |
| 82 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), |
| 83 |
Single.just(4), Single.just(5), Single.just(6), |
| 84 |
new Function6<Integer, Integer, Integer, Integer, Integer, Integer, Object>() { |
| 85 |
@Override |
| 86 |
public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f) |
| 87 |
throws Exception { |
| 88 |
return a + "" + b + c + d + e + f; |
| 89 |
} |
| 90 |
}) |
| 91 |
.test() |
| 92 |
.assertResult("123456"); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void zip7() { |
| 97 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), |
| 98 |
Single.just(4), Single.just(5), Single.just(6), |
| 99 |
Single.just(7), |
| 100 |
new Function7<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Object>() { |
| 101 |
@Override |
| 102 |
public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f, Integer g) |
| 103 |
throws Exception { |
| 104 |
return a + "" + b + c + d + e + f + g; |
| 105 |
} |
| 106 |
}) |
| 107 |
.test() |
| 108 |
.assertResult("1234567"); |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void zip8() { |
| 113 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), |
| 114 |
Single.just(4), Single.just(5), Single.just(6), |
| 115 |
Single.just(7), Single.just(8), |
| 116 |
new Function8<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Object>() { |
| 117 |
@Override |
| 118 |
public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f, Integer g, |
| 119 |
Integer h) throws Exception { |
| 120 |
return a + "" + b + c + d + e + f + g + h; |
| 121 |
} |
| 122 |
}) |
| 123 |
.test() |
| 124 |
.assertResult("12345678"); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void zip9() { |
| 129 |
Single.zip(Single.just(1), Single.just(2), Single.just(3), |
| 130 |
Single.just(4), Single.just(5), Single.just(6), |
| 131 |
Single.just(7), Single.just(8), Single.just(9), |
| 132 |
new Function9<Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Object>() { |
| 133 |
@Override |
| 134 |
public Object apply(Integer a, Integer b, Integer c, Integer d, Integer e, Integer f, Integer g, |
| 135 |
Integer h, Integer i) throws Exception { |
| 136 |
return a + "" + b + c + d + e + f + g + h + i; |
| 137 |
} |
| 138 |
}) |
| 139 |
.test() |
| 140 |
.assertResult("123456789"); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void noDisposeOnAllSuccess() { |
| 145 |
final AtomicInteger counter = new AtomicInteger(); |
| 146 |
|
| 147 |
Single<Integer> source = Single.just(1).doOnDispose(new Action() { |
| 148 |
@Override |
| 149 |
public void run() throws Exception { |
| 150 |
counter.getAndIncrement(); |
| 151 |
} |
| 152 |
}); |
| 153 |
|
| 154 |
Single.zip(source, source, new BiFunction<Integer, Integer, Object>() { |
| 155 |
@Override |
| 156 |
public Integer apply(Integer a, Integer b) throws Exception { |
| 157 |
return a + b; |
| 158 |
} |
| 159 |
}) |
| 160 |
.test() |
| 161 |
.assertResult(2); |
| 162 |
|
| 163 |
assertEquals(0, counter.get()); |
| 164 |
} |
| 165 |
|
| 166 |
@Test |
| 167 |
public void noDisposeOnAllSuccess2() { |
| 168 |
final AtomicInteger counter = new AtomicInteger(); |
| 169 |
|
| 170 |
Single<Integer> source = Single.just(1).doOnDispose(new Action() { |
| 171 |
@Override |
| 172 |
public void run() throws Exception { |
| 173 |
counter.getAndIncrement(); |
| 174 |
} |
| 175 |
}); |
| 176 |
|
| 177 |
Single.zip(Arrays.asList(source, source), new Function<Object[], Object>() { |
| 178 |
@Override |
| 179 |
public Integer apply(Object[] o) throws Exception { |
| 180 |
return (Integer)o[0] + (Integer)o[1]; |
| 181 |
} |
| 182 |
}) |
| 183 |
.test() |
| 184 |
.assertResult(2); |
| 185 |
|
| 186 |
assertEquals(0, counter.get()); |
| 187 |
} |
| 188 |
} |