blob: b956f6cd831a6aee5570a32a29dcfccf15410a7d [file] [log] [blame]
Roman Elizarovb555d912017-08-17 21:01:33 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovb555d912017-08-17 21:01:33 +03003 */
4
5package kotlinx.coroutines.experimental.channels
6
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +03007import kotlinx.coroutines.experimental.*
8import kotlin.math.*
9import kotlin.test.*
Roman Elizarovb555d912017-08-17 21:01:33 +030010
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030011class ChannelsTest: TestBase() {
Roman Elizarovb555d912017-08-17 21:01:33 +030012 private val testList = listOf(1, 2, 3)
13
14 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030015 fun testIterableAsReceiveChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030016 assertEquals(testList, testList.asReceiveChannel().toList())
17 }
18
19 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030020 fun testSequenceAsReceiveChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030021 assertEquals(testList, testList.asSequence().asReceiveChannel().toList())
22 }
23
24 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030025 fun testAssociate() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030026 assertEquals(testList.associate { it * 2 to it * 3 },
27 testList.asReceiveChannel().associate { it * 2 to it * 3 }.toMap())
28 }
29
30 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030031 fun testAssociateBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030032 assertEquals(testList.associateBy { it % 2 }, testList.asReceiveChannel().associateBy { it % 2 })
33 }
34
35 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030036 fun testAssociateBy2() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030037 assertEquals(testList.associateBy({ it * 2}, { it * 3 }),
38 testList.asReceiveChannel().associateBy({ it * 2}, { it * 3 }).toMap())
39 }
40
41 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030042 fun testDistinct() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030043 assertEquals(testList.map { it % 2 }.distinct(), testList.asReceiveChannel().map { it % 2 }.distinct().toList())
44 }
45
46 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030047 fun testDistinctBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030048 assertEquals(testList.distinctBy { it % 2 }.toList(), testList.asReceiveChannel().distinctBy { it % 2 }.toList())
49 }
50
51 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030052 fun testToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030053 val target = mutableListOf<Int>()
54 testList.asReceiveChannel().toCollection(target)
55 assertEquals(testList, target)
56 }
57
58 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030059 fun testDrop() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030060 for (i in 0..testList.size) {
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030061 assertEquals(testList.drop(i), testList.asReceiveChannel().drop(i).toList(), "Drop $i")
Roman Elizarovb555d912017-08-17 21:01:33 +030062 }
63 }
64
65 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030066 fun testElementAtOrElse() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030067 assertEquals(testList.elementAtOrElse(2) { 42 }, testList.asReceiveChannel().elementAtOrElse(2) { 42 })
68 assertEquals(testList.elementAtOrElse(9) { 42 }, testList.asReceiveChannel().elementAtOrElse(9) { 42 })
69 }
70
71 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030072 fun testFirst() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030073 assertEquals(testList.first(), testList.asReceiveChannel().first())
74 for (i in testList) {
75 assertEquals(testList.first { it == i }, testList.asReceiveChannel().first { it == i })
76 }
77 try {
78 testList.asReceiveChannel().first { it == 9 }
79 fail()
80 } catch (nse: NoSuchElementException) {
81 }
82 }
83
84 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030085 fun testFirstOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030086 assertEquals(testList.firstOrNull(), testList.asReceiveChannel().firstOrNull())
87 assertEquals(testList.firstOrNull { it == 2 }, testList.asReceiveChannel().firstOrNull { it == 2 })
88 assertEquals(testList.firstOrNull { it == 9 }, testList.asReceiveChannel().firstOrNull { it == 9 })
89 }
90
91 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030092 fun testFlatMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030093 assertEquals(testList.flatMap { (0..it).toList() }, testList.asReceiveChannel().flatMap { (0..it).asReceiveChannel() }.toList())
94
95 }
96
97 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030098 fun testFold() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +030099 assertEquals(testList.fold(mutableListOf(42)) { acc, e -> acc.apply { add(e) } },
100 testList.asReceiveChannel().fold(mutableListOf(42)) { acc, e -> acc.apply { add(e) } }.toList())
101 }
102
103 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300104 fun testFoldIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300105 assertEquals(testList.foldIndexed(mutableListOf(42)) { index, acc, e -> acc.apply { add(index + e) } },
106 testList.asReceiveChannel().foldIndexed(mutableListOf(42)) { index, acc, e -> acc.apply { add(index + e) } }.toList())
107 }
108
109 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300110 fun testGroupBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300111 assertEquals(testList.groupBy { it % 2 }, testList.asReceiveChannel().groupBy { it % 2 })
112 }
113
114 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300115 fun testGroupBy2() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300116 assertEquals(testList.groupBy({ -it }, { it + 100 }), testList.asReceiveChannel().groupBy({ -it }, { it + 100 }).toMap())
117
118 }
119
120 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300121 fun testMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300122 assertEquals(testList.map { it + 10 }, testList.asReceiveChannel().map { it + 10 }.toList())
123
124 }
125
126 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300127 fun testMapToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300128 val c = mutableListOf<Int>()
129 testList.asReceiveChannel().mapTo(c) { it + 10 }
130 assertEquals(testList.map { it + 10 }, c)
131 }
132
133 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300134 fun testMapToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300135 val c = produce<Int> {
136 testList.asReceiveChannel().mapTo(channel) { it + 10 }
137 }
138 assertEquals(testList.map { it + 10 }, c.toList())
139 }
140
141 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300142 fun testEmptyList() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300143 assertTrue(emptyList<Nothing>().asReceiveChannel().toList().isEmpty())
144 }
145
146 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300147 fun testToList() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300148 assertEquals(testList, testList.asReceiveChannel().toList())
149
150 }
151
152 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300153 fun testEmptySet() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300154 assertTrue(emptyList<Nothing>().asReceiveChannel().toSet().isEmpty())
155
156 }
157
158 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300159 fun testToSet() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300160 assertEquals(testList.toSet(), testList.asReceiveChannel().toSet())
161 }
162
163 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300164 fun testToMutableSet() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300165 assertEquals(testList.toMutableSet(), testList.asReceiveChannel().toMutableSet())
166 }
167
168 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300169 fun testEmptySequence() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300170 val channel = Channel<Nothing>()
171 channel.close()
172
173 assertTrue(emptyList<Nothing>().asReceiveChannel().count() == 0)
174 }
175
176 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300177 fun testEmptyMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300178 val channel = Channel<Pair<Nothing, Nothing>>()
179 channel.close()
180
181 assertTrue(channel.toMap().isEmpty())
182 }
183
184 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300185 fun testToMap() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300186 val values = testList.map { it to it.toString() }
187 assertEquals(values.toMap(), values.asReceiveChannel().toMap())
188 }
189
190 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300191 fun testReduce() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300192 assertEquals(testList.reduce { acc, e -> acc * e },
193 testList.asReceiveChannel().reduce { acc, e -> acc * e })
194 }
195
196 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300197 fun testReduceIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300198 assertEquals(testList.reduceIndexed { index, acc, e -> index + acc * e },
199 testList.asReceiveChannel().reduceIndexed { index, acc, e -> index + acc * e })
200 }
201
202 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300203 fun testTake() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300204 for (i in 0..testList.size) {
205 assertEquals(testList.take(i), testList.asReceiveChannel().take(i).toList())
206 }
207 }
208
209 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300210 fun testPartition() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300211 assertEquals(testList.partition { it % 2 == 0 }, testList.asReceiveChannel().partition { it % 2 == 0 })
212 }
213
214 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300215 fun testZip() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300216 val other = listOf("a", "b")
217 assertEquals(testList.zip(other), testList.asReceiveChannel().zip(other.asReceiveChannel()).toList())
218 }
219
220 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300221 fun testElementAt() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300222 testList.indices.forEach { i ->
223 assertEquals(testList[i], testList.asReceiveChannel().elementAt(i))
224 }
225 }
226
227 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300228 fun testElementAtOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300229 testList.indices.forEach { i ->
230 assertEquals(testList[i], testList.asReceiveChannel().elementAtOrNull(i))
231 }
232 assertEquals(null, testList.asReceiveChannel().elementAtOrNull(-1))
233 assertEquals(null, testList.asReceiveChannel().elementAtOrNull(testList.size))
234 }
235
236 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300237 fun testFind() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300238 repeat(3) { mod ->
239 assertEquals(testList.find { it % 2 == mod },
240 testList.asReceiveChannel().find { it % 2 == mod })
241 }
242 }
243
244 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300245 fun testFindLast() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300246 repeat(3) { mod ->
247 assertEquals(testList.findLast { it % 2 == mod }, testList.asReceiveChannel().findLast { it % 2 == mod })
248 }
249 }
250
251 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300252 fun testIndexOf() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300253 repeat(testList.size + 1) { i ->
254 assertEquals(testList.indexOf(i), testList.asReceiveChannel().indexOf(i))
255 }
256 }
257
258 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300259 fun testLastIndexOf() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300260 repeat(testList.size + 1) { i ->
261 assertEquals(testList.lastIndexOf(i), testList.asReceiveChannel().lastIndexOf(i))
262 }
263 }
264
265 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300266 fun testIndexOfFirst() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300267 repeat(3) { mod ->
268 assertEquals(testList.indexOfFirst { it % 2 == mod },
269 testList.asReceiveChannel().indexOfFirst { it % 2 == mod })
270 }
271 }
272
273 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300274 fun testIndexOfLast() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300275 repeat(3) { mod ->
276 assertEquals(testList.indexOfLast { it % 2 != mod },
277 testList.asReceiveChannel().indexOfLast { it % 2 != mod })
278 }
279 }
280
281 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300282 fun testLastOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300283 assertEquals(testList.lastOrNull(), testList.asReceiveChannel().lastOrNull())
284 assertEquals(null, emptyList<Int>().asReceiveChannel().lastOrNull())
285 }
286
287 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300288 fun testSingleOrNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300289 assertEquals(1, listOf(1).asReceiveChannel().singleOrNull())
290 assertEquals(null, listOf(1, 2).asReceiveChannel().singleOrNull())
291 assertEquals(null, emptyList<Int>().asReceiveChannel().singleOrNull())
292 repeat(testList.size + 1) { i ->
293 assertEquals(testList.singleOrNull { it == i },
294 testList.asReceiveChannel().singleOrNull { it == i })
295 }
296 repeat(3) { mod ->
297 assertEquals(testList.singleOrNull { it % 2 == mod },
298 testList.asReceiveChannel().singleOrNull { it % 2 == mod })
299 }
300 }
301
302 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300303 fun testDropWhile() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300304 repeat(3) { mod ->
305 assertEquals(testList.dropWhile { it % 2 == mod },
306 testList.asReceiveChannel().dropWhile { it % 2 == mod }.toList())
307 }
308 }
309
310 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300311 fun testFilter() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300312 repeat(3) { mod ->
313 assertEquals(testList.filter { it % 2 == mod },
314 testList.asReceiveChannel().filter { it % 2 == mod }.toList())
315 }
316 }
317
318 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300319 fun testFilterToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300320 repeat(3) { mod ->
321 val c = mutableListOf<Int>()
322 testList.asReceiveChannel().filterTo(c) { it % 2 == mod }
323 assertEquals(testList.filter { it % 2 == mod }, c)
324 }
325 }
326
327 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300328 fun testFilterToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300329 repeat(3) { mod ->
330 val c = produce<Int> {
331 testList.asReceiveChannel().filterTo(channel) { it % 2 == mod }
332 }
333 assertEquals(testList.filter { it % 2 == mod }, c.toList())
334 }
335 }
336
337 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300338 fun testFilterNot() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300339 repeat(3) { mod ->
340 assertEquals(testList.filterNot { it % 2 == mod },
341 testList.asReceiveChannel().filterNot { it % 2 == mod }.toList())
342 }
343 }
344
345 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300346 fun testFilterNotToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300347 repeat(3) { mod ->
348 val c = mutableListOf<Int>()
349 testList.asReceiveChannel().filterNotTo(c) { it % 2 == mod }
350 assertEquals(testList.filterNot { it % 2 == mod }, c)
351 }
352 }
353
354 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300355 fun testFilterNotToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300356 repeat(3) { mod ->
357 val c = produce<Int> {
358 testList.asReceiveChannel().filterNotTo(channel) { it % 2 == mod }
359 }
360 assertEquals(testList.filterNot { it % 2 == mod }, c.toList())
361 }
362 }
363
364 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300365 fun testFilterNotNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300366 repeat(3) { mod ->
367 assertEquals(testList.map { it.takeIf { it % 2 == mod } }.filterNotNull(),
368 testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNull().toList())
369 }
370 }
371
372 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300373 fun testFilterNotNullToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300374 repeat(3) { mod ->
375 val c = mutableListOf<Int>()
376 testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNullTo(c)
377 assertEquals(testList.map { it.takeIf { it % 2 == mod } }.filterNotNull(), c)
378 }
379 }
380
381 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300382 fun testFilterNotNullToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300383 repeat(3) { mod ->
384 val c = produce<Int> {
385 testList.asReceiveChannel().map { it.takeIf { it % 2 == mod } }.filterNotNullTo(channel)
386 }
387 assertEquals(testList.map { it.takeIf { it % 2 == mod } }.filterNotNull(), c.toList())
388 }
389 }
390
391 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300392 fun testFilterIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300393 repeat(3) { mod ->
394 assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod },
395 testList.asReceiveChannel().filterIndexed { index, _ -> index % 2 == mod }.toList())
396 }
397 }
398
399 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300400 fun testFilterIndexedToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300401 repeat(3) { mod ->
402 val c = mutableListOf<Int>()
403 testList.asReceiveChannel().filterIndexedTo(c) { index, _ -> index % 2 == mod }
404 assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c)
405 }
406 }
407
408 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300409 fun testFilterIndexedToChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300410 repeat(3) { mod ->
411 val c = produce<Int> {
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300412 testList.asReceiveChannel().filterIndexedTo(channel) { index, _ -> index % 2 == mod }
Roman Elizarovb555d912017-08-17 21:01:33 +0300413 }
414 assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c.toList())
415 }
416 }
417
418 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300419 fun testTakeWhile() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300420 repeat(3) { mod ->
421 assertEquals(testList.takeWhile { it % 2 != mod },
422 testList.asReceiveChannel().takeWhile { it % 2 != mod }.toList())
423 }
424 }
425
426 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300427 fun testToChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300428 val c = produce<Int> {
429 testList.asReceiveChannel().toChannel(channel)
430 }
431 assertEquals(testList, c.toList())
432 }
433
434 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300435 fun testMapIndexed() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300436 assertEquals(testList.mapIndexed { index, i -> index + i },
437 testList.asReceiveChannel().mapIndexed { index, i -> index + i }.toList())
438 }
439
440 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300441 fun testMapIndexedToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300442 val c = mutableListOf<Int>()
443 testList.asReceiveChannel().mapIndexedTo(c) { index, i -> index + i }
444 assertEquals(testList.mapIndexed { index, i -> index + i }, c)
445 }
446
447 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300448 fun testMapIndexedToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300449 val c = produce<Int> {
450 testList.asReceiveChannel().mapIndexedTo(channel) { index, i -> index + i }
451 }
452 assertEquals(testList.mapIndexed { index, i -> index + i }, c.toList())
453 }
454
455 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300456 fun testMapNotNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300457 repeat(3) { mod ->
458 assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } },
459 testList.asReceiveChannel().mapNotNull { i -> i.takeIf { i % 2 == mod } }.toList())
460 }
461 }
462
463 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300464 fun testMapNotNullToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300465 repeat(3) { mod ->
466 val c = mutableListOf<Int>()
467 testList.asReceiveChannel().mapNotNullTo(c) { i -> i.takeIf { i % 2 == mod } }
468 assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, c)
469 }
470 }
471
472 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300473 fun testMapNotNullToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300474 repeat(3) { mod ->
475 val c = produce<Int> {
476 testList.asReceiveChannel().mapNotNullTo(channel) { i -> i.takeIf { i % 2 == mod } }
477 }
478 assertEquals(testList.mapNotNull { i -> i.takeIf { i % 2 == mod } }, c.toList())
479 }
480 }
481
482 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300483 fun testMapIndexedNotNull() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300484 repeat(3) { mod ->
485 assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } },
486 testList.asReceiveChannel().mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }.toList())
487 }
488 }
489
490 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300491 fun testMapIndexedNotNullToCollection() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300492 repeat(3) { mod ->
493 val c = mutableListOf<Int>()
494 testList.asReceiveChannel().mapIndexedNotNullTo(c) { index, i -> index.takeIf { i % 2 == mod } }
495 assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, c)
496 }
497 }
498
499 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300500 fun testMapIndexedNotNullToSendChannel() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300501 repeat(3) { mod ->
502 val c = produce<Int> {
503 testList.asReceiveChannel().mapIndexedNotNullTo(channel) { index, i -> index.takeIf { i % 2 == mod } }
504 }
505 assertEquals(testList.mapIndexedNotNull { index, i -> index.takeIf { i % 2 == mod } }, c.toList())
506 }
507 }
508
509 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300510 fun testWithIndex() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300511 assertEquals(testList.withIndex().toList(), testList.asReceiveChannel().withIndex().toList())
512 }
513
514 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300515 fun testMaxBy() = runTest {
516 assertEquals(testList.maxBy { 10 - abs(it - 2) },
517 testList.asReceiveChannel().maxBy { 10 - abs(it - 2) })
Roman Elizarovb555d912017-08-17 21:01:33 +0300518 }
519
520 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300521 fun testMaxWith() = runTest {
522 val cmp = compareBy<Int> { 10 - abs(it - 2) }
Roman Elizarovb555d912017-08-17 21:01:33 +0300523 assertEquals(testList.maxWith(cmp),
524 testList.asReceiveChannel().maxWith(cmp))
525 }
526
527 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300528 fun testMinBy() = runTest {
529 assertEquals(testList.minBy { abs(it - 2) },
530 testList.asReceiveChannel().minBy { abs(it - 2) })
Roman Elizarovb555d912017-08-17 21:01:33 +0300531 }
532
533 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300534 fun testMinWith() = runTest {
535 val cmp = compareBy<Int> { abs(it - 2) }
Roman Elizarovb555d912017-08-17 21:01:33 +0300536 assertEquals(testList.minWith(cmp),
537 testList.asReceiveChannel().minWith(cmp))
538 }
539
540 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300541 fun testSumBy() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300542 assertEquals(testList.sumBy { it * 3 },
543 testList.asReceiveChannel().sumBy { it * 3 })
544 }
545
546 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300547 fun testSumByDouble() = runTest {
548 val expected = testList.sumByDouble { it * 3.0 }
549 val actual = testList.asReceiveChannel().sumByDouble { it * 3.0 }
550 assertEquals(expected, actual)
Roman Elizarovb555d912017-08-17 21:01:33 +0300551 }
552
553 @Test
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +0300554 fun testRequireNoNulls() = runTest {
Roman Elizarovb555d912017-08-17 21:01:33 +0300555 assertEquals(testList.requireNoNulls(), testList.asReceiveChannel().requireNoNulls().toList())
556 }
557}