Roman Elizarov | 475c043 | 2017-07-11 22:12:42 +0300 | [diff] [blame] | 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 | |
Roman Elizarov | 5d94a26 | 2017-12-28 00:23:39 +0300 | [diff] [blame] | 19 | import com.devexperts.dxlab.lincheck.* |
| 20 | import com.devexperts.dxlab.lincheck.annotations.* |
| 21 | import com.devexperts.dxlab.lincheck.paramgen.* |
| 22 | import com.devexperts.dxlab.lincheck.stress.* |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 23 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 5d94a26 | 2017-12-28 00:23:39 +0300 | [diff] [blame] | 24 | import kotlin.test.* |
Roman Elizarov | 475c043 | 2017-07-11 22:12:42 +0300 | [diff] [blame] | 25 | |
Roman Elizarov | 475c043 | 2017-07-11 22:12:42 +0300 | [diff] [blame] | 26 | @Param(name = "value", gen = IntGen::class, conf = "1:3") |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 27 | class LockFreeListLinearizabilityTest : TestBase() { |
Roman Elizarov | 475c043 | 2017-07-11 22:12:42 +0300 | [diff] [blame] | 28 | 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 Tolstopyatov | 87f2faa | 2018-04-30 22:53:02 +0300 | [diff] [blame] | 56 | return node.value |
Roman Elizarov | 475c043 | 2017-07-11 22:12:42 +0300 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | fun Any.isSame(value: Int) = this is Node && this.value == value |
| 60 | |
| 61 | @Test |
| 62 | fun testAddRemoveLinearizability() { |
Roman Elizarov | 6129c94 | 2018-01-10 17:34:40 +0300 | [diff] [blame] | 63 | 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 Elizarov | 475c043 | 2017-07-11 22:12:42 +0300 | [diff] [blame] | 71 | } |
| 72 | } |