blob: 0fe1fc974927acf86a2deaca12fcb25a03ca75e7 [file] [log] [blame]
Roman Elizarovb555d912017-08-17 21:01:33 +03001/*
2 * Copyright 2016-2017 JetBrains s.r.o.
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
17package kotlinx.coroutines.experimental.channels
18
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030019import kotlinx.coroutines.experimental.*
20import kotlin.math.*
21import kotlin.test.*
Roman Elizarovb555d912017-08-17 21:01:33 +030022
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030023class ChannelsTest: TestBase() {
Roman Elizarovb555d912017-08-17 21:01:33 +030024 private val testList = listOf(1, 2, 3)
25
26 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030027 fun testIterableAsReceiveChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030028 assertEquals(testList, testList.asReceiveChannel().toList())
29 }
30
31 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030032 fun testSequenceAsReceiveChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030033 assertEquals(testList, testList.asSequence().asReceiveChannel().toList())
34 }
35
36 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030037 fun testAssociate() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030038 assertEquals(testList.associate { it * 2 to it * 3 },
39 testList.asReceiveChannel().associate { it * 2 to it * 3 }.toMap())
40 }
41
42 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030043 fun testAssociateBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030044 assertEquals(testList.associateBy { it % 2 }, testList.asReceiveChannel().associateBy { it % 2 })
45 }
46
47 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030048 fun testAssociateBy2() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030049 assertEquals(testList.associateBy({ it * 2}, { it * 3 }),
50 testList.asReceiveChannel().associateBy({ it * 2}, { it * 3 }).toMap())
51 }
52
53 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030054 fun testDistinct() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030055 assertEquals(testList.map { it % 2 }.distinct(), testList.asReceiveChannel().map { it % 2 }.distinct().toList())
56 }
57
58 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030059 fun testDistinctBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030060 assertEquals(testList.distinctBy { it % 2 }.toList(), testList.asReceiveChannel().distinctBy { it % 2 }.toList())
61 }
62
63 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030064 fun testToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030065 val target = mutableListOf<Int>()
66 testList.asReceiveChannel().toCollection(target)
67 assertEquals(testList, target)
68 }
69
70 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030071 fun testDrop() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030072 for (i in 0..testList.size) {
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030073 assertEquals(testList.drop(i), testList.asReceiveChannel().drop(i).toList(), "Drop $i")
Roman Elizarovb555d912017-08-17 21:01:33 +030074 }
75 }
76
77 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030078 fun testElementAtOrElse() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030079 assertEquals(testList.elementAtOrElse(2) { 42 }, testList.asReceiveChannel().elementAtOrElse(2) { 42 })
80 assertEquals(testList.elementAtOrElse(9) { 42 }, testList.asReceiveChannel().elementAtOrElse(9) { 42 })
81 }
82
83 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030084 fun testFirst() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030085 assertEquals(testList.first(), testList.asReceiveChannel().first())
86 for (i in testList) {
87 assertEquals(testList.first { it == i }, testList.asReceiveChannel().first { it == i })
88 }
89 try {
90 testList.asReceiveChannel().first { it == 9 }
91 fail()
92 } catch (nse: NoSuchElementException) {
93 }
94 }
95
96 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030097 fun testFirstOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030098 assertEquals(testList.firstOrNull(), testList.asReceiveChannel().firstOrNull())
99 assertEquals(testList.firstOrNull { it == 2 }, testList.asReceiveChannel().firstOrNull { it == 2 })
100 assertEquals(testList.firstOrNull { it == 9 }, testList.asReceiveChannel().firstOrNull { it == 9 })
101 }
102
103 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300104 fun testFlatMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300105 assertEquals(testList.flatMap { (0..it).toList() }, testList.asReceiveChannel().flatMap { (0..it).asReceiveChannel() }.toList())
106
107 }
108
109 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300110 fun testFold() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300111 assertEquals(testList.fold(mutableListOf(42)) { acc, e -> acc.apply { add(e) } },
112 testList.asReceiveChannel().fold(mutableListOf(42)) { acc, e -> acc.apply { add(e) } }.toList())
113 }
114
115 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300116 fun testFoldIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300117 assertEquals(testList.foldIndexed(mutableListOf(42)) { index, acc, e -> acc.apply { add(index + e) } },
118 testList.asReceiveChannel().foldIndexed(mutableListOf(42)) { index, acc, e -> acc.apply { add(index + e) } }.toList())
119 }
120
121 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300122 fun testGroupBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300123 assertEquals(testList.groupBy { it % 2 }, testList.asReceiveChannel().groupBy { it % 2 })
124 }
125
126 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300127 fun testGroupBy2() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300128 assertEquals(testList.groupBy({ -it }, { it + 100 }), testList.asReceiveChannel().groupBy({ -it }, { it + 100 }).toMap())
129
130 }
131
132 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300133 fun testMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300134 assertEquals(testList.map { it + 10 }, testList.asReceiveChannel().map { it + 10 }.toList())
135
136 }
137
138 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300139 fun testMapToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300140 val c = mutableListOf<Int>()
141 testList.asReceiveChannel().mapTo(c) { it + 10 }
142 assertEquals(testList.map { it + 10 }, c)
143 }
144
145 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300146 fun testMapToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300147 val c = produce<Int> {
148 testList.asReceiveChannel().mapTo(channel) { it + 10 }
149 }
150 assertEquals(testList.map { it + 10 }, c.toList())
151 }
152
153 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300154 fun testEmptyList() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300155 assertTrue(emptyList<Nothing>().asReceiveChannel().toList().isEmpty())
156 }
157
158 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300159 fun testToList() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300160 assertEquals(testList, testList.asReceiveChannel().toList())
161
162 }
163
164 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300165 fun testEmptySet() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300166 assertTrue(emptyList<Nothing>().asReceiveChannel().toSet().isEmpty())
167
168 }
169
170 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300171 fun testToSet() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300172 assertEquals(testList.toSet(), testList.asReceiveChannel().toSet())
173 }
174
175 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300176 fun testToMutableSet() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300177 assertEquals(testList.toMutableSet(), testList.asReceiveChannel().toMutableSet())
178 }
179
180 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300181 fun testEmptySequence() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300182 val channel = Channel<Nothing>()
183 channel.close()
184
185 assertTrue(emptyList<Nothing>().asReceiveChannel().count() == 0)
186 }
187
188 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300189 fun testEmptyMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300190 val channel = Channel<Pair<Nothing, Nothing>>()
191 channel.close()
192
193 assertTrue(channel.toMap().isEmpty())
194 }
195
196 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300197 fun testToMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300198 val values = testList.map { it to it.toString() }
199 assertEquals(values.toMap(), values.asReceiveChannel().toMap())
200 }
201
202 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300203 fun testReduce() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300204 assertEquals(testList.reduce { acc, e -> acc * e },
205 testList.asReceiveChannel().reduce { acc, e -> acc * e })
206 }
207
208 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300209 fun testReduceIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300210 assertEquals(testList.reduceIndexed { index, acc, e -> index + acc * e },
211 testList.asReceiveChannel().reduceIndexed { index, acc, e -> index + acc * e })
212 }
213
214 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300215 fun testTake() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300216 for (i in 0..testList.size) {
217 assertEquals(testList.take(i), testList.asReceiveChannel().take(i).toList())
218 }
219 }
220
221 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300222 fun testPartition() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300223 assertEquals(testList.partition { it % 2 == 0 }, testList.asReceiveChannel().partition { it % 2 == 0 })
224 }
225
226 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300227 fun testZip() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300228 val other = listOf("a", "b")
229 assertEquals(testList.zip(other), testList.asReceiveChannel().zip(other.asReceiveChannel()).toList())
230 }
231
232 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300233 fun testElementAt() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300234 testList.indices.forEach { i ->
235 assertEquals(testList[i], testList.asReceiveChannel().elementAt(i))
236 }
237 }
238
239 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300240 fun testElementAtOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300241 testList.indices.forEach { i ->
242 assertEquals(testList[i], testList.asReceiveChannel().elementAtOrNull(i))
243 }
244 assertEquals(null, testList.asReceiveChannel().elementAtOrNull(-1))
245 assertEquals(null, testList.asReceiveChannel().elementAtOrNull(testList.size))
246 }
247
248 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300249 fun testFind() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300250 repeat(3) { mod ->
251 assertEquals(testList.find { it % 2 == mod },
252 testList.asReceiveChannel().find { it % 2 == mod })
253 }
254 }
255
256 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300257 fun testFindLast() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300258 repeat(3) { mod ->
259 assertEquals(testList.findLast { it % 2 == mod }, testList.asReceiveChannel().findLast { it % 2 == mod })
260 }
261 }
262
263 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300264 fun testIndexOf() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300265 repeat(testList.size + 1) { i ->
266 assertEquals(testList.indexOf(i), testList.asReceiveChannel().indexOf(i))
267 }
268 }
269
270 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300271 fun testLastIndexOf() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300272 repeat(testList.size + 1) { i ->
273 assertEquals(testList.lastIndexOf(i), testList.asReceiveChannel().lastIndexOf(i))
274 }
275 }
276
277 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300278 fun testIndexOfFirst() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300279 repeat(3) { mod ->
280 assertEquals(testList.indexOfFirst { it % 2 == mod },
281 testList.asReceiveChannel().indexOfFirst { it % 2 == mod })
282 }
283 }
284
285 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300286 fun testIndexOfLast() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300287 repeat(3) { mod ->
288 assertEquals(testList.indexOfLast { it % 2 != mod },
289 testList.asReceiveChannel().indexOfLast { it % 2 != mod })
290 }
291 }
292
293 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300294 fun testLastOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300295 assertEquals(testList.lastOrNull(), testList.asReceiveChannel().lastOrNull())
296 assertEquals(null, emptyList<Int>().asReceiveChannel().lastOrNull())
297 }
298
299 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300300 fun testSingleOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300301 assertEquals(1, listOf(1).asReceiveChannel().singleOrNull())
302 assertEquals(null, listOf(1, 2).asReceiveChannel().singleOrNull())
303 assertEquals(null, emptyList<Int>().asReceiveChannel().singleOrNull())
304 repeat(testList.size + 1) { i ->
305 assertEquals(testList.singleOrNull { it == i },
306 testList.asReceiveChannel().singleOrNull { it == i })
307 }
308 repeat(3) { mod ->
309 assertEquals(testList.singleOrNull { it % 2 == mod },
310 testList.asReceiveChannel().singleOrNull { it % 2 == mod })
311 }
312 }
313
314 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300315 fun testDropWhile() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300316 repeat(3) { mod ->
317 assertEquals(testList.dropWhile { it % 2 == mod },
318 testList.asReceiveChannel().dropWhile { it % 2 == mod }.toList())
319 }
320 }
321
322 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300323 fun testFilter() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300324 repeat(3) { mod ->
325 assertEquals(testList.filter { it % 2 == mod },
326 testList.asReceiveChannel().filter { it % 2 == mod }.toList())
327 }
328 }
329
330 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300331 fun testFilterToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300332 repeat(3) { mod ->
333 val c = mutableListOf<Int>()
334 testList.asReceiveChannel().filterTo(c) { it % 2 == mod }
335 assertEquals(testList.filter { it % 2 == mod }, c)
336 }
337 }
338
339 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300340 fun testFilterToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300341 repeat(3) { mod ->
342 val c = produce<Int> {
343 testList.asReceiveChannel().filterTo(channel) { it % 2 == mod }
344 }
345 assertEquals(testList.filter { it % 2 == mod }, c.toList())
346 }
347 }
348
349 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300350 fun testFilterNot() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300351 repeat(3) { mod ->
352 assertEquals(testList.filterNot { it % 2 == mod },
353 testList.asReceiveChannel().filterNot { it % 2 == mod }.toList())
354 }
355 }
356
357 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300358 fun testFilterNotToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300359 repeat(3) { mod ->
360 val c = mutableListOf<Int>()
361 testList.asReceiveChannel().filterNotTo(c) { it % 2 == mod }
362 assertEquals(testList.filterNot { it % 2 == mod }, c)
363 }
364 }
365
366 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300367 fun testFilterNotToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300368 repeat(3) { mod ->
369 val c = produce<Int> {
370 testList.asReceiveChannel().filterNotTo(channel) { it % 2 == mod }
371 }
372 assertEquals(testList.filterNot { it % 2 == mod }, c.toList())
373 }
374 }
375
376 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300377 fun testFilterNotNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300378 repeat(3) { mod ->
379 assertEquals(testList.map { it.takeIf { it % 2 == mod } }.filterNotNull(),
380 testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNull().toList())
381 }
382 }
383
384 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300385 fun testFilterNotNullToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300386 repeat(3) { mod ->
387 val c = mutableListOf<Int>()
388 testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNullTo(c)
389 assertEquals(testList.map { it.takeIf { it % 2 == mod } }.filterNotNull(), c)
390 }
391 }
392
393 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300394 fun testFilterNotNullToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300395 repeat(3) { mod ->
396 val c = produce<Int> {
397 testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNullTo(channel)
398 }
399 assertEquals(testList.map { it.takeIf { it % 2 == mod } }.filterNotNull(), c.toList())
400 }
401 }
402
403 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300404 fun testFilterIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300405 repeat(3) { mod ->
406 assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod },
407 testList.asReceiveChannel().filterIndexed { index, _ -> index % 2 == mod }.toList())
408 }
409 }
410
411 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300412 fun testFilterIndexedToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300413 repeat(3) { mod ->
414 val c = mutableListOf<Int>()
415 testList.asReceiveChannel().filterIndexedTo(c) { index, _ -> index % 2 == mod }
416 assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c)
417 }
418 }
419
420 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300421 fun testFilterIndexedToChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300422 repeat(3) { mod ->
423 val c = produce<Int> {
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300424 testList.asReceiveChannel().filterIndexedTo(channel) { index, _ -> index % 2 == mod }
Roman Elizarovb555d912017-08-17 21:01:33 +0300425 }
426 assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c.toList())
427 }
428 }
429
430 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300431 fun testTakeWhile() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300432 repeat(3) { mod ->
433 assertEquals(testList.takeWhile { it % 2 != mod },
434 testList.asReceiveChannel().takeWhile { it % 2 != mod }.toList())
435 }
436 }
437
438 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300439 fun testToChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300440 val c = produce<Int> {
441 testList.asReceiveChannel().toChannel(channel)
442 }
443 assertEquals(testList, c.toList())
444 }
445
446 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300447 fun testMapIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300448 assertEquals(testList.mapIndexed { index, i -> index + i },
449 testList.asReceiveChannel().mapIndexed { index, i -> index + i }.toList())
450 }
451
452 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300453 fun testMapIndexedToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300454 val c = mutableListOf<Int>()
455 testList.asReceiveChannel().mapIndexedTo(c) { index, i -> index + i }
456 assertEquals(testList.mapIndexed { index, i -> index + i }, c)
457 }
458
459 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300460 fun testMapIndexedToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300461 val c = produce<Int> {
462 testList.asReceiveChannel().mapIndexedTo(channel) { index, i -> index + i }
463 }
464 assertEquals(testList.mapIndexed { index, i -> index + i }, c.toList())
465 }
466
467 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300468 fun testMapNotNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300469 repeat(3) { mod ->
470 assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } },
471 testList.asReceiveChannel().mapNotNull { i -> i.takeIf { i % 2 == mod } }.toList())
472 }
473 }
474
475 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300476 fun testMapNotNullToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300477 repeat(3) { mod ->
478 val c = mutableListOf<Int>()
479 testList.asReceiveChannel().mapNotNullTo(c) { i -> i.takeIf { i % 2 == mod } }
480 assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, c)
481 }
482 }
483
484 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300485 fun testMapNotNullToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300486 repeat(3) { mod ->
487 val c = produce<Int> {
488 testList.asReceiveChannel().mapNotNullTo(channel) { i -> i.takeIf { i % 2 == mod } }
489 }
490 assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, c.toList())
491 }
492 }
493
494 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300495 fun testMapIndexedNotNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300496 repeat(3) { mod ->
497 assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } },
498 testList.asReceiveChannel().mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }.toList())
499 }
500 }
501
502 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300503 fun testMapIndexedNotNullToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300504 repeat(3) { mod ->
505 val c = mutableListOf<Int>()
506 testList.asReceiveChannel().mapIndexedNotNullTo(c) { index, i -> index.takeIf { i % 2 == mod } }
507 assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, c)
508 }
509 }
510
511 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300512 fun testMapIndexedNotNullToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300513 repeat(3) { mod ->
514 val c = produce<Int> {
515 testList.asReceiveChannel().mapIndexedNotNullTo(channel) { index, i -> index.takeIf { i % 2 == mod } }
516 }
517 assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, c.toList())
518 }
519 }
520
521 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300522 fun testWithIndex() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300523 assertEquals(testList.withIndex().toList(), testList.asReceiveChannel().withIndex().toList())
524 }
525
526 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300527 fun testMaxBy() = runTest {
528 assertEquals(testList.maxBy { 10 - abs(it - 2) },
529 testList.asReceiveChannel().maxBy { 10 - abs(it - 2) })
Roman Elizarovb555d912017-08-17 21:01:33 +0300530 }
531
532 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300533 fun testMaxWith() = runTest {
534 val cmp = compareBy<Int> { 10 - abs(it - 2) }
Roman Elizarovb555d912017-08-17 21:01:33 +0300535 assertEquals(testList.maxWith(cmp),
536 testList.asReceiveChannel().maxWith(cmp))
537 }
538
539 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300540 fun testMinBy() = runTest {
541 assertEquals(testList.minBy { abs(it - 2) },
542 testList.asReceiveChannel().minBy { abs(it - 2) })
Roman Elizarovb555d912017-08-17 21:01:33 +0300543 }
544
545 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300546 fun testMinWith() = runTest {
547 val cmp = compareBy<Int> { abs(it - 2) }
Roman Elizarovb555d912017-08-17 21:01:33 +0300548 assertEquals(testList.minWith(cmp),
549 testList.asReceiveChannel().minWith(cmp))
550 }
551
552 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300553 fun testSumBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300554 assertEquals(testList.sumBy { it * 3 },
555 testList.asReceiveChannel().sumBy { it * 3 })
556 }
557
558 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300559 fun testSumByDouble() = runTest {
560 val expected = testList.sumByDouble { it * 3.0 }
561 val actual = testList.asReceiveChannel().sumByDouble { it * 3.0 }
562 assertEquals(expected, actual)
Roman Elizarovb555d912017-08-17 21:01:33 +0300563 }
564
565 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300566 fun testRequireNoNulls() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300567 assertEquals(testList.requireNoNulls(), testList.asReceiveChannel().requireNoNulls().toList())
568 }
569}