1
+ /*
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
+
17
+ package kotlinx.coroutines.experimental.internal
18
+
19
+ import com.devexperts.dxlab.lincheck.LinChecker
20
+ import com.devexperts.dxlab.lincheck.annotations.CTest
21
+ import com.devexperts.dxlab.lincheck.annotations.Operation
22
+ import com.devexperts.dxlab.lincheck.annotations.Param
23
+ import com.devexperts.dxlab.lincheck.annotations.Reset
24
+ import com.devexperts.dxlab.lincheck.generators.IntGen
25
+ import org.junit.Test
26
+
27
+
28
+ @CTest(iterations = 100 , actorsPerThread = arrayOf(" 1:2" , " 1:2" , " 1:2" , " 1:2" ))
29
+ @Param(name = " value" , gen = IntGen ::class , conf = " 1:3" )
30
+ class LockFreeListLinearizabilityTest {
31
+ class Node (val value : Int ): LockFreeLinkedListNode()
32
+
33
+ lateinit var q: LockFreeLinkedListHead
34
+
35
+ @Reset
36
+ fun reset () {
37
+ q = LockFreeLinkedListHead ()
38
+ }
39
+
40
+ @Operation
41
+ fun addLast (@Param(name = " value" ) value : Int ) {
42
+ q.addLast(Node (value))
43
+ }
44
+
45
+ @Operation
46
+ fun addLastIfNotSame (@Param(name = " value" ) value : Int ) {
47
+ q.addLastIfPrev(Node (value)) { ! it.isSame(value) }
48
+ }
49
+
50
+ @Operation
51
+ fun removeFirst (): Int? {
52
+ val node = q.removeFirstOrNull() ? : return null
53
+ return (node as Node ).value
54
+ }
55
+
56
+ @Operation
57
+ fun removeFirstOrPeekIfNotSame (@Param(name = " value" ) value : Int ): Int? {
58
+ val node = q.removeFirstIfIsInstanceOfOrPeekIf<Node > { ! it.isSame(value) } ? : return null
59
+ return (node as Node ).value
60
+ }
61
+
62
+ fun Any.isSame (value : Int ) = this is Node && this .value == value
63
+
64
+ @Test
65
+ fun testAddRemoveLinearizability () {
66
+ LinChecker .check(this )
67
+ }
68
+ }
0 commit comments