Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.channels |
| 6 | |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
| 8 | import kotlin.math.* |
| 9 | import kotlin.test.* |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 10 | |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 11 | class ChannelsTest: TestBase() { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 12 | private val testList = listOf(1, 2, 3) |
| 13 | |
| 14 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 15 | fun testIterableAsReceiveChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 16 | assertEquals(testList, testList.asReceiveChannel().toList()) |
| 17 | } |
| 18 | |
| 19 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 20 | fun testSequenceAsReceiveChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 21 | assertEquals(testList, testList.asSequence().asReceiveChannel().toList()) |
| 22 | } |
| 23 | |
| 24 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 25 | fun testAssociate() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 26 | assertEquals(testList.associate { it * 2 to it * 3 }, |
| 27 | testList.asReceiveChannel().associate { it * 2 to it * 3 }.toMap()) |
| 28 | } |
| 29 | |
| 30 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 31 | fun testAssociateBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 32 | assertEquals(testList.associateBy { it % 2 }, testList.asReceiveChannel().associateBy { it % 2 }) |
| 33 | } |
| 34 | |
| 35 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 36 | fun testAssociateBy2() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 37 | assertEquals(testList.associateBy({ it * 2}, { it * 3 }), |
| 38 | testList.asReceiveChannel().associateBy({ it * 2}, { it * 3 }).toMap()) |
| 39 | } |
| 40 | |
| 41 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 42 | fun testDistinct() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 43 | assertEquals(testList.map { it % 2 }.distinct(), testList.asReceiveChannel().map { it % 2 }.distinct().toList()) |
| 44 | } |
| 45 | |
| 46 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 47 | fun testDistinctBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 48 | assertEquals(testList.distinctBy { it % 2 }.toList(), testList.asReceiveChannel().distinctBy { it % 2 }.toList()) |
| 49 | } |
| 50 | |
| 51 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 52 | fun testToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 53 | val target = mutableListOf<Int>() |
| 54 | testList.asReceiveChannel().toCollection(target) |
| 55 | assertEquals(testList, target) |
| 56 | } |
| 57 | |
| 58 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 59 | fun testDrop() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 60 | for (i in 0..testList.size) { |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 61 | assertEquals(testList.drop(i), testList.asReceiveChannel().drop(i).toList(), "Drop $i") |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 66 | fun testElementAtOrElse() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 67 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 72 | fun testFirst() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 73 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 85 | fun testFirstOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 86 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 92 | fun testFlatMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 93 | assertEquals(testList.flatMap { (0..it).toList() }, testList.asReceiveChannel().flatMap { (0..it).asReceiveChannel() }.toList()) |
| 94 | |
| 95 | } |
| 96 | |
| 97 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 98 | fun testFold() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 99 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 104 | fun testFoldIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 105 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 110 | fun testGroupBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 111 | assertEquals(testList.groupBy { it % 2 }, testList.asReceiveChannel().groupBy { it % 2 }) |
| 112 | } |
| 113 | |
| 114 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 115 | fun testGroupBy2() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 116 | assertEquals(testList.groupBy({ -it }, { it + 100 }), testList.asReceiveChannel().groupBy({ -it }, { it + 100 }).toMap()) |
| 117 | |
| 118 | } |
| 119 | |
| 120 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 121 | fun testMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 122 | assertEquals(testList.map { it + 10 }, testList.asReceiveChannel().map { it + 10 }.toList()) |
| 123 | |
| 124 | } |
| 125 | |
| 126 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 127 | fun testMapToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 128 | val c = mutableListOf<Int>() |
| 129 | testList.asReceiveChannel().mapTo(c) { it + 10 } |
| 130 | assertEquals(testList.map { it + 10 }, c) |
| 131 | } |
| 132 | |
| 133 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 134 | fun testMapToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 135 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 142 | fun testEmptyList() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 143 | assertTrue(emptyList<Nothing>().asReceiveChannel().toList().isEmpty()) |
| 144 | } |
| 145 | |
| 146 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 147 | fun testToList() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 148 | assertEquals(testList, testList.asReceiveChannel().toList()) |
| 149 | |
| 150 | } |
| 151 | |
| 152 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 153 | fun testEmptySet() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 154 | assertTrue(emptyList<Nothing>().asReceiveChannel().toSet().isEmpty()) |
| 155 | |
| 156 | } |
| 157 | |
| 158 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 159 | fun testToSet() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 160 | assertEquals(testList.toSet(), testList.asReceiveChannel().toSet()) |
| 161 | } |
| 162 | |
| 163 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 164 | fun testToMutableSet() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 165 | assertEquals(testList.toMutableSet(), testList.asReceiveChannel().toMutableSet()) |
| 166 | } |
| 167 | |
| 168 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 169 | fun testEmptySequence() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 170 | val channel = Channel<Nothing>() |
| 171 | channel.close() |
| 172 | |
| 173 | assertTrue(emptyList<Nothing>().asReceiveChannel().count() == 0) |
| 174 | } |
| 175 | |
| 176 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 177 | fun testEmptyMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 178 | val channel = Channel<Pair<Nothing, Nothing>>() |
| 179 | channel.close() |
| 180 | |
| 181 | assertTrue(channel.toMap().isEmpty()) |
| 182 | } |
| 183 | |
| 184 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 185 | fun testToMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 186 | val values = testList.map { it to it.toString() } |
| 187 | assertEquals(values.toMap(), values.asReceiveChannel().toMap()) |
| 188 | } |
| 189 | |
| 190 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 191 | fun testReduce() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 192 | assertEquals(testList.reduce { acc, e -> acc * e }, |
| 193 | testList.asReceiveChannel().reduce { acc, e -> acc * e }) |
| 194 | } |
| 195 | |
| 196 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 197 | fun testReduceIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 198 | assertEquals(testList.reduceIndexed { index, acc, e -> index + acc * e }, |
| 199 | testList.asReceiveChannel().reduceIndexed { index, acc, e -> index + acc * e }) |
| 200 | } |
| 201 | |
| 202 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 203 | fun testTake() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 204 | for (i in 0..testList.size) { |
| 205 | assertEquals(testList.take(i), testList.asReceiveChannel().take(i).toList()) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 210 | fun testPartition() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 211 | assertEquals(testList.partition { it % 2 == 0 }, testList.asReceiveChannel().partition { it % 2 == 0 }) |
| 212 | } |
| 213 | |
| 214 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 215 | fun testZip() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 216 | val other = listOf("a", "b") |
| 217 | assertEquals(testList.zip(other), testList.asReceiveChannel().zip(other.asReceiveChannel()).toList()) |
| 218 | } |
| 219 | |
| 220 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 221 | fun testElementAt() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 222 | testList.indices.forEach { i -> |
| 223 | assertEquals(testList[i], testList.asReceiveChannel().elementAt(i)) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 228 | fun testElementAtOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 229 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 237 | fun testFind() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 238 | repeat(3) { mod -> |
| 239 | assertEquals(testList.find { it % 2 == mod }, |
| 240 | testList.asReceiveChannel().find { it % 2 == mod }) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 245 | fun testFindLast() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 246 | repeat(3) { mod -> |
| 247 | assertEquals(testList.findLast { it % 2 == mod }, testList.asReceiveChannel().findLast { it % 2 == mod }) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 252 | fun testIndexOf() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 253 | repeat(testList.size + 1) { i -> |
| 254 | assertEquals(testList.indexOf(i), testList.asReceiveChannel().indexOf(i)) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 259 | fun testLastIndexOf() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 260 | repeat(testList.size + 1) { i -> |
| 261 | assertEquals(testList.lastIndexOf(i), testList.asReceiveChannel().lastIndexOf(i)) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 266 | fun testIndexOfFirst() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 267 | repeat(3) { mod -> |
| 268 | assertEquals(testList.indexOfFirst { it % 2 == mod }, |
| 269 | testList.asReceiveChannel().indexOfFirst { it % 2 == mod }) |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 274 | fun testIndexOfLast() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 275 | repeat(3) { mod -> |
| 276 | assertEquals(testList.indexOfLast { it % 2 != mod }, |
| 277 | testList.asReceiveChannel().indexOfLast { it % 2 != mod }) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 282 | fun testLastOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 283 | assertEquals(testList.lastOrNull(), testList.asReceiveChannel().lastOrNull()) |
| 284 | assertEquals(null, emptyList<Int>().asReceiveChannel().lastOrNull()) |
| 285 | } |
| 286 | |
| 287 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 288 | fun testSingleOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 289 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 303 | fun testDropWhile() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 304 | repeat(3) { mod -> |
| 305 | assertEquals(testList.dropWhile { it % 2 == mod }, |
| 306 | testList.asReceiveChannel().dropWhile { it % 2 == mod }.toList()) |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 311 | fun testFilter() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 312 | repeat(3) { mod -> |
| 313 | assertEquals(testList.filter { it % 2 == mod }, |
| 314 | testList.asReceiveChannel().filter { it % 2 == mod }.toList()) |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 319 | fun testFilterToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 320 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 328 | fun testFilterToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 329 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 338 | fun testFilterNot() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 339 | repeat(3) { mod -> |
| 340 | assertEquals(testList.filterNot { it % 2 == mod }, |
| 341 | testList.asReceiveChannel().filterNot { it % 2 == mod }.toList()) |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 346 | fun testFilterNotToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 347 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 355 | fun testFilterNotToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 356 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 365 | fun testFilterNotNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 366 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 373 | fun testFilterNotNullToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 374 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 382 | fun testFilterNotNullToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 383 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 392 | fun testFilterIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 393 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 400 | fun testFilterIndexedToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 401 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 409 | fun testFilterIndexedToChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 410 | repeat(3) { mod -> |
| 411 | val c = produce<Int> { |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 412 | testList.asReceiveChannel().filterIndexedTo(channel) { index, _ -> index % 2 == mod } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 413 | } |
| 414 | assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c.toList()) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 419 | fun testTakeWhile() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 420 | repeat(3) { mod -> |
| 421 | assertEquals(testList.takeWhile { it % 2 != mod }, |
| 422 | testList.asReceiveChannel().takeWhile { it % 2 != mod }.toList()) |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 427 | fun testToChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 428 | val c = produce<Int> { |
| 429 | testList.asReceiveChannel().toChannel(channel) |
| 430 | } |
| 431 | assertEquals(testList, c.toList()) |
| 432 | } |
| 433 | |
| 434 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 435 | fun testMapIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 436 | assertEquals(testList.mapIndexed { index, i -> index + i }, |
| 437 | testList.asReceiveChannel().mapIndexed { index, i -> index + i }.toList()) |
| 438 | } |
| 439 | |
| 440 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 441 | fun testMapIndexedToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 442 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 448 | fun testMapIndexedToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 449 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 456 | fun testMapNotNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 457 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 464 | fun testMapNotNullToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 465 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 473 | fun testMapNotNullToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 474 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 483 | fun testMapIndexedNotNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 484 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 491 | fun testMapIndexedNotNullToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 492 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 500 | fun testMapIndexedNotNullToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 501 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 510 | fun testWithIndex() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 511 | assertEquals(testList.withIndex().toList(), testList.asReceiveChannel().withIndex().toList()) |
| 512 | } |
| 513 | |
| 514 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 515 | fun testMaxBy() = runTest { |
| 516 | assertEquals(testList.maxBy { 10 - abs(it - 2) }, |
| 517 | testList.asReceiveChannel().maxBy { 10 - abs(it - 2) }) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 521 | fun testMaxWith() = runTest { |
| 522 | val cmp = compareBy<Int> { 10 - abs(it - 2) } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 523 | assertEquals(testList.maxWith(cmp), |
| 524 | testList.asReceiveChannel().maxWith(cmp)) |
| 525 | } |
| 526 | |
| 527 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 528 | fun testMinBy() = runTest { |
| 529 | assertEquals(testList.minBy { abs(it - 2) }, |
| 530 | testList.asReceiveChannel().minBy { abs(it - 2) }) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 534 | fun testMinWith() = runTest { |
| 535 | val cmp = compareBy<Int> { abs(it - 2) } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 536 | assertEquals(testList.minWith(cmp), |
| 537 | testList.asReceiveChannel().minWith(cmp)) |
| 538 | } |
| 539 | |
| 540 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 541 | fun testSumBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 542 | assertEquals(testList.sumBy { it * 3 }, |
| 543 | testList.asReceiveChannel().sumBy { it * 3 }) |
| 544 | } |
| 545 | |
| 546 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 547 | 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 Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 554 | fun testRequireNoNulls() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 555 | assertEquals(testList.requireNoNulls(), testList.asReceiveChannel().requireNoNulls().toList()) |
| 556 | } |
| 557 | } |