Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +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 | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.internal |
| 6 | |
Roman Elizarov | cafccfa | 2018-01-10 19:06:56 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 8 | import kotlin.test.* |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 9 | import java.util.* |
| 10 | |
Roman Elizarov | cafccfa | 2018-01-10 19:06:56 +0300 | [diff] [blame] | 11 | class ThreadSafeHeapTest : TestBase() { |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 12 | class Node(val value: Int) : ThreadSafeHeapNode, Comparable<Node> { |
| 13 | override var index = -1 |
Roman Elizarov | a888975 | 2017-07-11 12:51:45 +0300 | [diff] [blame] | 14 | override fun compareTo(other: Node): Int = value.compareTo(other.value) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 15 | override fun equals(other: Any?): Boolean = other is Node && other.value == value |
| 16 | override fun hashCode(): Int = value |
| 17 | override fun toString(): String = "$value" |
| 18 | } |
| 19 | |
| 20 | @Test |
| 21 | fun testBasic() { |
| 22 | val h = ThreadSafeHeap<Node>() |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 23 | assertEquals(null, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 24 | val n1 = Node(1) |
| 25 | h.addLast(n1) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 26 | assertEquals(n1, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 27 | val n2 = Node(2) |
| 28 | h.addLast(n2) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 29 | assertEquals(n1, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 30 | val n3 = Node(3) |
| 31 | h.addLast(n3) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 32 | assertEquals(n1, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 33 | val n4 = Node(4) |
| 34 | h.addLast(n4) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 35 | assertEquals(n1, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 36 | val n5 = Node(5) |
| 37 | h.addLast(n5) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 38 | assertEquals(n1, h.peek()) |
| 39 | assertEquals(n1, h.removeFirstOrNull()) |
| 40 | assertEquals(-1, n1.index) |
| 41 | assertEquals(n2, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 42 | h.remove(n2) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 43 | assertEquals(n3, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 44 | h.remove(n4) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 45 | assertEquals(n3, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 46 | h.remove(n3) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 47 | assertEquals(n5, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 48 | h.remove(n5) |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 49 | assertEquals(null, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
| 53 | fun testRandomSort() { |
Roman Elizarov | cafccfa | 2018-01-10 19:06:56 +0300 | [diff] [blame] | 54 | val n = 1000 * stressTestMultiplier |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 55 | val r = Random(1) |
| 56 | val h = ThreadSafeHeap<Node>() |
| 57 | val a = IntArray(n) { r.nextInt() } |
| 58 | repeat(n) { h.addLast(Node(a[it])) } |
| 59 | a.sort() |
Roman Elizarov | 9d47b52 | 2017-12-21 22:40:34 +0300 | [diff] [blame] | 60 | repeat(n) { assertEquals(Node(a[it]), h.removeFirstOrNull()) } |
| 61 | assertEquals(null, h.peek()) |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 62 | } |
Roman Elizarov | cafccfa | 2018-01-10 19:06:56 +0300 | [diff] [blame] | 63 | |
| 64 | @Test |
| 65 | fun testRandomRemove() { |
| 66 | val n = 1000 * stressTestMultiplier |
| 67 | check(n % 2 == 0) { "Must be even" } |
| 68 | val r = Random(1) |
| 69 | val h = ThreadSafeHeap<Node>() |
| 70 | val set = TreeSet<Node>() |
| 71 | repeat(n) { |
| 72 | val node = Node(r.nextInt()) |
| 73 | h.addLast(node) |
| 74 | assertTrue(set.add(node)) |
| 75 | } |
| 76 | while (!h.isEmpty) { |
| 77 | // pick random node to remove |
| 78 | val rndNode: Node |
| 79 | while (true) { |
| 80 | val tail = set.tailSet(Node(r.nextInt())) |
| 81 | if (!tail.isEmpty()) { |
| 82 | rndNode = tail.first() |
| 83 | break |
| 84 | } |
| 85 | } |
| 86 | assertTrue(set.remove(rndNode)) |
| 87 | assertTrue(h.remove(rndNode)) |
| 88 | // remove head and validate |
| 89 | val headNode = h.removeFirstOrNull()!! // must not be null!!! |
| 90 | assertTrue(headNode === set.first(), "Expected ${set.first()}, but found $headNode, remaining size ${h.size}") |
| 91 | assertTrue(set.remove(headNode)) |
| 92 | assertEquals(set.size, h.size) |
| 93 | } |
| 94 | } |
Roman Elizarov | a047a11 | 2017-07-10 18:50:07 +0300 | [diff] [blame] | 95 | } |