blob: 93f04272b8d20d0912d6c8d2ecdcfc843d515743 [file] [log] [blame]
Roman Elizarov475c0432017-07-11 22:12:42 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov475c0432017-07-11 22:12:42 +03003 */
4
5package kotlinx.coroutines.experimental.internal
6
Roman Elizarov5d94a262017-12-28 00:23:39 +03007import com.devexperts.dxlab.lincheck.*
8import com.devexperts.dxlab.lincheck.annotations.*
9import com.devexperts.dxlab.lincheck.paramgen.*
10import com.devexperts.dxlab.lincheck.stress.*
Roman Elizarov6129c942018-01-10 17:34:40 +030011import kotlinx.coroutines.experimental.*
Roman Elizarov5d94a262017-12-28 00:23:39 +030012import kotlin.test.*
Roman Elizarov475c0432017-07-11 22:12:42 +030013
Roman Elizarov475c0432017-07-11 22:12:42 +030014@Param(name = "value", gen = IntGen::class, conf = "1:3")
Roman Elizarov6129c942018-01-10 17:34:40 +030015class LockFreeListLinearizabilityTest : TestBase() {
Roman Elizarov475c0432017-07-11 22:12:42 +030016 class Node(val value: Int): LockFreeLinkedListNode()
17
18 lateinit var q: LockFreeLinkedListHead
19
20 @Reset
21 fun reset() {
22 q = LockFreeLinkedListHead()
23 }
24
25 @Operation
26 fun addLast(@Param(name = "value") value: Int) {
27 q.addLast(Node(value))
28 }
29
30 @Operation
31 fun addLastIfNotSame(@Param(name = "value") value: Int) {
32 q.addLastIfPrev(Node(value)) { !it.isSame(value) }
33 }
34
35 @Operation
36 fun removeFirst(): Int? {
37 val node = q.removeFirstOrNull() ?: return null
38 return (node as Node).value
39 }
40
41 @Operation
42 fun removeFirstOrPeekIfNotSame(@Param(name = "value") value: Int): Int? {
43 val node = q.removeFirstIfIsInstanceOfOrPeekIf<Node> { !it.isSame(value) } ?: return null
Vsevolod Tolstopyatov87f2faa2018-04-30 22:53:02 +030044 return node.value
Roman Elizarov475c0432017-07-11 22:12:42 +030045 }
46
47 fun Any.isSame(value: Int) = this is Node && this.value == value
48
49 @Test
50 fun testAddRemoveLinearizability() {
Roman Elizarov6129c942018-01-10 17:34:40 +030051 val options = StressOptions()
52 .iterations(100)
53 .invocationsPerIteration(1000 * stressTestMultiplier)
54 .addThread(1, 2)
55 .addThread(1, 2)
56 .addThread(1, 2)
57 .addThread(1, 2)
58 LinChecker.check(LockFreeListLinearizabilityTest::class.java, options)
Roman Elizarov475c0432017-07-11 22:12:42 +030059 }
60}