Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +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 | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 45c1a73 | 2017-12-21 11:53:31 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.internal |
| 6 | |
| 7 | import kotlin.test.Test |
| 8 | import kotlin.test.assertEquals |
| 9 | import kotlin.test.assertFalse |
| 10 | import kotlin.test.assertTrue |
| 11 | |
| 12 | class LinkedListTest { |
| 13 | data class IntNode(val i: Int) : LinkedListNode() |
| 14 | |
| 15 | @Test |
| 16 | fun testSimpleAddLastRemove() { |
| 17 | val list = LinkedListHead() |
| 18 | assertContents(list) |
| 19 | val n1 = IntNode(1).apply { list.addLast(this) } |
| 20 | assertContents(list, 1) |
| 21 | val n2 = IntNode(2).apply { list.addLast(this) } |
| 22 | assertContents(list, 1, 2) |
| 23 | val n3 = IntNode(3).apply { list.addLast(this) } |
| 24 | assertContents(list, 1, 2, 3) |
| 25 | val n4 = IntNode(4).apply { list.addLast(this) } |
| 26 | assertContents(list, 1, 2, 3, 4) |
| 27 | assertTrue(n1.remove()) |
| 28 | assertContents(list, 2, 3, 4) |
| 29 | assertTrue(n3.remove()) |
| 30 | assertContents(list, 2, 4) |
| 31 | assertTrue(n4.remove()) |
| 32 | assertContents(list, 2) |
| 33 | assertTrue(n2.remove()) |
| 34 | assertFalse(n2.remove()) |
| 35 | assertContents(list) |
| 36 | } |
| 37 | |
| 38 | private fun assertContents(list: LinkedListHead, vararg expected: Int) { |
| 39 | val n = expected.size |
| 40 | val actual = IntArray(n) |
| 41 | var index = 0 |
| 42 | list.forEach<IntNode> { actual[index++] = it.i } |
| 43 | assertEquals(n, index) |
| 44 | for (i in 0 until n) assertEquals(expected[i], actual[i], "item i") |
| 45 | assertEquals(expected.isEmpty(), list.isEmpty) |
| 46 | } |
| 47 | } |