1 /* <lambda>null2 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.coroutines.flow 6 7 import kotlinx.coroutines.* 8 import kotlin.test.* 9 10 class IndexedTest : TestBase() { 11 12 @Test 13 fun testWithIndex() = runTest { 14 val flow = flowOf(3, 2, 1).withIndex() 15 assertEquals(listOf(IndexedValue(0, 3), IndexedValue(1, 2), IndexedValue(2, 1)), flow.toList()) 16 } 17 18 @Test 19 fun testWithIndexEmpty() = runTest { 20 val flow = emptyFlow<Int>().withIndex() 21 assertEquals(emptyList(), flow.toList()) 22 } 23 24 @Test 25 fun testCollectIndexed() = runTest { 26 val result = ArrayList<IndexedValue<Long>>() 27 flowOf(3L, 2L, 1L).collectIndexed { index, value -> 28 result.add(IndexedValue(index, value)) 29 } 30 assertEquals(listOf(IndexedValue(0, 3L), IndexedValue(1, 2L), IndexedValue(2, 1L)), result) 31 } 32 33 @Test 34 fun testCollectIndexedEmptyFlow() = runTest { 35 val flow = flow<Int> { 36 expect(1) 37 } 38 39 flow.collectIndexed { _, _ -> 40 expectUnreached() 41 } 42 43 finish(2) 44 } 45 } 46