blob: dca99d5ee9b5412708956dd1742213b4bc96b59d [file] [log] [blame]
Roman Elizarov475c0432017-07-11 22:12:42 +03001/*
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
17package kotlinx.coroutines.experimental.internal
18
Roman Elizarov5d94a262017-12-28 00:23:39 +030019import com.devexperts.dxlab.lincheck.*
20import com.devexperts.dxlab.lincheck.annotations.*
21import com.devexperts.dxlab.lincheck.paramgen.*
22import com.devexperts.dxlab.lincheck.stress.*
Roman Elizarov6129c942018-01-10 17:34:40 +030023import kotlinx.coroutines.experimental.*
Roman Elizarov5d94a262017-12-28 00:23:39 +030024import kotlin.test.*
Roman Elizarov475c0432017-07-11 22:12:42 +030025
Roman Elizarov475c0432017-07-11 22:12:42 +030026@Param(name = "value", gen = IntGen::class, conf = "1:3")
Roman Elizarov6129c942018-01-10 17:34:40 +030027class LockFreeListLinearizabilityTest : TestBase() {
Roman Elizarov475c0432017-07-11 22:12:42 +030028 class Node(val value: Int): LockFreeLinkedListNode()
29
30 lateinit var q: LockFreeLinkedListHead
31
32 @Reset
33 fun reset() {
34 q = LockFreeLinkedListHead()
35 }
36
37 @Operation
38 fun addLast(@Param(name = "value") value: Int) {
39 q.addLast(Node(value))
40 }
41
42 @Operation
43 fun addLastIfNotSame(@Param(name = "value") value: Int) {
44 q.addLastIfPrev(Node(value)) { !it.isSame(value) }
45 }
46
47 @Operation
48 fun removeFirst(): Int? {
49 val node = q.removeFirstOrNull() ?: return null
50 return (node as Node).value
51 }
52
53 @Operation
54 fun removeFirstOrPeekIfNotSame(@Param(name = "value") value: Int): Int? {
55 val node = q.removeFirstIfIsInstanceOfOrPeekIf<Node> { !it.isSame(value) } ?: return null
Vsevolod Tolstopyatov87f2faa2018-04-30 22:53:02 +030056 return node.value
Roman Elizarov475c0432017-07-11 22:12:42 +030057 }
58
59 fun Any.isSame(value: Int) = this is Node && this.value == value
60
61 @Test
62 fun testAddRemoveLinearizability() {
Roman Elizarov6129c942018-01-10 17:34:40 +030063 val options = StressOptions()
64 .iterations(100)
65 .invocationsPerIteration(1000 * stressTestMultiplier)
66 .addThread(1, 2)
67 .addThread(1, 2)
68 .addThread(1, 2)
69 .addThread(1, 2)
70 LinChecker.check(LockFreeListLinearizabilityTest::class.java, options)
Roman Elizarov475c0432017-07-11 22:12:42 +030071 }
72}