Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package kotlinx.coroutines.experimental.channels |
| 18 | |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 19 | import kotlinx.coroutines.experimental.* |
| 20 | import kotlin.math.* |
| 21 | import kotlin.test.* |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 22 | |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 23 | class ChannelsTest: TestBase() { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 24 | private val testList = listOf(1, 2, 3) |
| 25 | |
| 26 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 27 | fun testIterableAsReceiveChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 28 | assertEquals(testList, testList.asReceiveChannel().toList()) |
| 29 | } |
| 30 | |
| 31 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 32 | fun testSequenceAsReceiveChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 33 | assertEquals(testList, testList.asSequence().asReceiveChannel().toList()) |
| 34 | } |
| 35 | |
| 36 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 37 | fun testAssociate() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 38 | assertEquals(testList.associate { it * 2 to it * 3 }, |
| 39 | testList.asReceiveChannel().associate { it * 2 to it * 3 }.toMap()) |
| 40 | } |
| 41 | |
| 42 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 43 | fun testAssociateBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 44 | assertEquals(testList.associateBy { it % 2 }, testList.asReceiveChannel().associateBy { it % 2 }) |
| 45 | } |
| 46 | |
| 47 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 48 | fun testAssociateBy2() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 49 | assertEquals(testList.associateBy({ it * 2}, { it * 3 }), |
| 50 | testList.asReceiveChannel().associateBy({ it * 2}, { it * 3 }).toMap()) |
| 51 | } |
| 52 | |
| 53 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 54 | fun testDistinct() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 55 | assertEquals(testList.map { it % 2 }.distinct(), testList.asReceiveChannel().map { it % 2 }.distinct().toList()) |
| 56 | } |
| 57 | |
| 58 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 59 | fun testDistinctBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 60 | assertEquals(testList.distinctBy { it % 2 }.toList(), testList.asReceiveChannel().distinctBy { it % 2 }.toList()) |
| 61 | } |
| 62 | |
| 63 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 64 | fun testToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 65 | val target = mutableListOf<Int>() |
| 66 | testList.asReceiveChannel().toCollection(target) |
| 67 | assertEquals(testList, target) |
| 68 | } |
| 69 | |
| 70 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 71 | fun testDrop() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 72 | for (i in 0..testList.size) { |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 73 | assertEquals(testList.drop(i), testList.asReceiveChannel().drop(i).toList(), "Drop $i") |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 78 | fun testElementAtOrElse() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 79 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 84 | fun testFirst() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 85 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 97 | fun testFirstOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 98 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 104 | fun testFlatMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 105 | assertEquals(testList.flatMap { (0..it).toList() }, testList.asReceiveChannel().flatMap { (0..it).asReceiveChannel() }.toList()) |
| 106 | |
| 107 | } |
| 108 | |
| 109 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 110 | fun testFold() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 111 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 116 | fun testFoldIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 117 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 122 | fun testGroupBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 123 | assertEquals(testList.groupBy { it % 2 }, testList.asReceiveChannel().groupBy { it % 2 }) |
| 124 | } |
| 125 | |
| 126 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 127 | fun testGroupBy2() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 128 | assertEquals(testList.groupBy({ -it }, { it + 100 }), testList.asReceiveChannel().groupBy({ -it }, { it + 100 }).toMap()) |
| 129 | |
| 130 | } |
| 131 | |
| 132 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 133 | fun testMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 134 | assertEquals(testList.map { it + 10 }, testList.asReceiveChannel().map { it + 10 }.toList()) |
| 135 | |
| 136 | } |
| 137 | |
| 138 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 139 | fun testMapToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 140 | val c = mutableListOf<Int>() |
| 141 | testList.asReceiveChannel().mapTo(c) { it + 10 } |
| 142 | assertEquals(testList.map { it + 10 }, c) |
| 143 | } |
| 144 | |
| 145 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 146 | fun testMapToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 147 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 154 | fun testEmptyList() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 155 | assertTrue(emptyList<Nothing>().asReceiveChannel().toList().isEmpty()) |
| 156 | } |
| 157 | |
| 158 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 159 | fun testToList() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 160 | assertEquals(testList, testList.asReceiveChannel().toList()) |
| 161 | |
| 162 | } |
| 163 | |
| 164 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 165 | fun testEmptySet() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 166 | assertTrue(emptyList<Nothing>().asReceiveChannel().toSet().isEmpty()) |
| 167 | |
| 168 | } |
| 169 | |
| 170 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 171 | fun testToSet() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 172 | assertEquals(testList.toSet(), testList.asReceiveChannel().toSet()) |
| 173 | } |
| 174 | |
| 175 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 176 | fun testToMutableSet() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 177 | assertEquals(testList.toMutableSet(), testList.asReceiveChannel().toMutableSet()) |
| 178 | } |
| 179 | |
| 180 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 181 | fun testEmptySequence() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 182 | val channel = Channel<Nothing>() |
| 183 | channel.close() |
| 184 | |
| 185 | assertTrue(emptyList<Nothing>().asReceiveChannel().count() == 0) |
| 186 | } |
| 187 | |
| 188 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 189 | fun testEmptyMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 190 | val channel = Channel<Pair<Nothing, Nothing>>() |
| 191 | channel.close() |
| 192 | |
| 193 | assertTrue(channel.toMap().isEmpty()) |
| 194 | } |
| 195 | |
| 196 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 197 | fun testToMap() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 198 | val values = testList.map { it to it.toString() } |
| 199 | assertEquals(values.toMap(), values.asReceiveChannel().toMap()) |
| 200 | } |
| 201 | |
| 202 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 203 | fun testReduce() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 204 | assertEquals(testList.reduce { acc, e -> acc * e }, |
| 205 | testList.asReceiveChannel().reduce { acc, e -> acc * e }) |
| 206 | } |
| 207 | |
| 208 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 209 | fun testReduceIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 210 | assertEquals(testList.reduceIndexed { index, acc, e -> index + acc * e }, |
| 211 | testList.asReceiveChannel().reduceIndexed { index, acc, e -> index + acc * e }) |
| 212 | } |
| 213 | |
| 214 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 215 | fun testTake() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 216 | for (i in 0..testList.size) { |
| 217 | assertEquals(testList.take(i), testList.asReceiveChannel().take(i).toList()) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 222 | fun testPartition() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 223 | assertEquals(testList.partition { it % 2 == 0 }, testList.asReceiveChannel().partition { it % 2 == 0 }) |
| 224 | } |
| 225 | |
| 226 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 227 | fun testZip() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 228 | val other = listOf("a", "b") |
| 229 | assertEquals(testList.zip(other), testList.asReceiveChannel().zip(other.asReceiveChannel()).toList()) |
| 230 | } |
| 231 | |
| 232 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 233 | fun testElementAt() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 234 | testList.indices.forEach { i -> |
| 235 | assertEquals(testList[i], testList.asReceiveChannel().elementAt(i)) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 240 | fun testElementAtOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 241 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 249 | fun testFind() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 250 | repeat(3) { mod -> |
| 251 | assertEquals(testList.find { it % 2 == mod }, |
| 252 | testList.asReceiveChannel().find { it % 2 == mod }) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 257 | fun testFindLast() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 258 | repeat(3) { mod -> |
| 259 | assertEquals(testList.findLast { it % 2 == mod }, testList.asReceiveChannel().findLast { it % 2 == mod }) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 264 | fun testIndexOf() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 265 | repeat(testList.size + 1) { i -> |
| 266 | assertEquals(testList.indexOf(i), testList.asReceiveChannel().indexOf(i)) |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 271 | fun testLastIndexOf() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 272 | repeat(testList.size + 1) { i -> |
| 273 | assertEquals(testList.lastIndexOf(i), testList.asReceiveChannel().lastIndexOf(i)) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 278 | fun testIndexOfFirst() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 279 | repeat(3) { mod -> |
| 280 | assertEquals(testList.indexOfFirst { it % 2 == mod }, |
| 281 | testList.asReceiveChannel().indexOfFirst { it % 2 == mod }) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 286 | fun testIndexOfLast() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 287 | repeat(3) { mod -> |
| 288 | assertEquals(testList.indexOfLast { it % 2 != mod }, |
| 289 | testList.asReceiveChannel().indexOfLast { it % 2 != mod }) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 294 | fun testLastOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 295 | assertEquals(testList.lastOrNull(), testList.asReceiveChannel().lastOrNull()) |
| 296 | assertEquals(null, emptyList<Int>().asReceiveChannel().lastOrNull()) |
| 297 | } |
| 298 | |
| 299 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 300 | fun testSingleOrNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 301 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 315 | fun testDropWhile() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 316 | repeat(3) { mod -> |
| 317 | assertEquals(testList.dropWhile { it % 2 == mod }, |
| 318 | testList.asReceiveChannel().dropWhile { it % 2 == mod }.toList()) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 323 | fun testFilter() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 324 | repeat(3) { mod -> |
| 325 | assertEquals(testList.filter { it % 2 == mod }, |
| 326 | testList.asReceiveChannel().filter { it % 2 == mod }.toList()) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 331 | fun testFilterToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 332 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 340 | fun testFilterToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 341 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 350 | fun testFilterNot() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 351 | repeat(3) { mod -> |
| 352 | assertEquals(testList.filterNot { it % 2 == mod }, |
| 353 | testList.asReceiveChannel().filterNot { it % 2 == mod }.toList()) |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 358 | fun testFilterNotToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 359 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 367 | fun testFilterNotToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 368 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 377 | fun testFilterNotNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 378 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 385 | fun testFilterNotNullToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 386 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 394 | fun testFilterNotNullToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 395 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 404 | fun testFilterIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 405 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 412 | fun testFilterIndexedToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 413 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 421 | fun testFilterIndexedToChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 422 | repeat(3) { mod -> |
| 423 | val c = produce<Int> { |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 424 | testList.asReceiveChannel().filterIndexedTo(channel) { index, _ -> index % 2 == mod } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 425 | } |
| 426 | assertEquals(testList.filterIndexed { index, _ -> index % 2 == mod }, c.toList()) |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 431 | fun testTakeWhile() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 432 | repeat(3) { mod -> |
| 433 | assertEquals(testList.takeWhile { it % 2 != mod }, |
| 434 | testList.asReceiveChannel().takeWhile { it % 2 != mod }.toList()) |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 439 | fun testToChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 440 | val c = produce<Int> { |
| 441 | testList.asReceiveChannel().toChannel(channel) |
| 442 | } |
| 443 | assertEquals(testList, c.toList()) |
| 444 | } |
| 445 | |
| 446 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 447 | fun testMapIndexed() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 448 | assertEquals(testList.mapIndexed { index, i -> index + i }, |
| 449 | testList.asReceiveChannel().mapIndexed { index, i -> index + i }.toList()) |
| 450 | } |
| 451 | |
| 452 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 453 | fun testMapIndexedToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 454 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 460 | fun testMapIndexedToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 461 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 468 | fun testMapNotNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 469 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 476 | fun testMapNotNullToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 477 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 485 | fun testMapNotNullToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 486 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 495 | fun testMapIndexedNotNull() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 496 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 503 | fun testMapIndexedNotNullToCollection() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 504 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 512 | fun testMapIndexedNotNullToSendChannel() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 513 | 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 Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 522 | fun testWithIndex() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 523 | assertEquals(testList.withIndex().toList(), testList.asReceiveChannel().withIndex().toList()) |
| 524 | } |
| 525 | |
| 526 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 527 | fun testMaxBy() = runTest { |
| 528 | assertEquals(testList.maxBy { 10 - abs(it - 2) }, |
| 529 | testList.asReceiveChannel().maxBy { 10 - abs(it - 2) }) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 533 | fun testMaxWith() = runTest { |
| 534 | val cmp = compareBy<Int> { 10 - abs(it - 2) } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 535 | assertEquals(testList.maxWith(cmp), |
| 536 | testList.asReceiveChannel().maxWith(cmp)) |
| 537 | } |
| 538 | |
| 539 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 540 | fun testMinBy() = runTest { |
| 541 | assertEquals(testList.minBy { abs(it - 2) }, |
| 542 | testList.asReceiveChannel().minBy { abs(it - 2) }) |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 546 | fun testMinWith() = runTest { |
| 547 | val cmp = compareBy<Int> { abs(it - 2) } |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 548 | assertEquals(testList.minWith(cmp), |
| 549 | testList.asReceiveChannel().minWith(cmp)) |
| 550 | } |
| 551 | |
| 552 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 553 | fun testSumBy() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 554 | assertEquals(testList.sumBy { it * 3 }, |
| 555 | testList.asReceiveChannel().sumBy { it * 3 }) |
| 556 | } |
| 557 | |
| 558 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 559 | 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 Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | @Test |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 566 | fun testRequireNoNulls() = runTest { |
Roman Elizarov | b555d91 | 2017-08-17 21:01:33 +0300 | [diff] [blame] | 567 | assertEquals(testList.requireNoNulls(), testList.asReceiveChannel().requireNoNulls().toList()) |
| 568 | } |
| 569 | } |