Skip to content

Commit

Permalink
Add representation tests for trace debugger native calls
Browse files Browse the repository at this point in the history
  • Loading branch information
zhelenskiy committed Feb 28, 2025
1 parent c1768d4 commit 98421d4
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Lincheck
*
* Copyright (C) 2019 - 2025 JetBrains s.r.o.
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.jetbrains.kotlinx.lincheck_test.representation.trace_debugger

import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.isInTraceDebuggerMode
import org.jetbrains.kotlinx.lincheck_test.representation.BaseTraceRepresentationTest
import org.jetbrains.kotlinx.lincheck_test.trace_debugger.NotRandom
import org.junit.Assume.assumeTrue
import org.junit.Before

class SuccessfulCustomRandomCallRepresentationTest : BaseTraceRepresentationTest("trace_debugger/successful_nextInt") {
@Before
fun setUp() {
assumeTrue(isInTraceDebuggerMode)
}

@Operation
override fun operation() {
NotRandom.nextInt(0, 1)
}
}

class FailingCustomRandomCallRepresentationTest : BaseTraceRepresentationTest("trace_debugger/failing_nextInt") {
@Before
fun setUp() {
assumeTrue(isInTraceDebuggerMode)
}

@Operation
override fun operation() {
NotRandom.nextInt(1, 1)
}
}

class CustomRandomBytesCallRepresentationTest : BaseTraceRepresentationTest("trace_debugger/random_bytes") {
@Before
fun setUp() {
assumeTrue(isInTraceDebuggerMode)
}

@Operation
override fun operation() {
NotRandom.nextBytes(ByteArray(10))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ class RandomInt5Test : RandomTests() {
}

class FakeRandomTest : RandomTests() {
class NotRandom : Random() {
private var x = 0
override fun nextBits(bitCount: Int): Int = x++
}
@Operation
fun operation() = NotRandom().nextBits(10)
fun operation() = NotRandom.nextBits(10)
}

class RandomLong1Test : RandomTests() {
Expand Down Expand Up @@ -325,16 +321,16 @@ class JRandomGaussianTest : RandomTests() {
fun operation() = JRandom().nextGaussian()
}

object NotRandom : Random() {
override fun nextBits(bitCount: Int): Int = 0
}

class StubRandomCheckTest : RandomTests() {
override val alsoRunInLincheckMode: Boolean get() = false

object MyRandom : Random() {
override fun nextBits(bitCount: Int): Int = 0
}

@Operation
fun operation() {
val randomList = List(10) { MyRandom.nextInt() }
val randomList = List(10) { NotRandom.nextInt() }
val expectedResult = List(10) { 0 }
require(randomList == expectedResult) { "Wrong randomizer: $randomList != $expectedResult" }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
= Invalid execution results =
| ---------------------------------------- |
| Thread 1 |
| ---------------------------------------- |
| operation(): IllegalArgumentException #1 |
| ---------------------------------------- |

---
The number next to an exception name helps you find its stack trace provided after the interleaving section
---

The following interleaving leads to the error:
| ---------------------------------------- |
| Thread 1 |
| ---------------------------------------- |
| operation(): IllegalArgumentException #1 |
| ---------------------------------------- |

Exception stack traces:
#1: java.lang.IllegalArgumentException: Random range is empty: [1, 1).
at kotlin.random.RandomKt.checkRangeBounds(Random.kt:378)
at kotlin.random.Random.nextInt(Random.kt:65)
at org.jetbrains.kotlinx.lincheck_test.representation.trace_debugger.FailingCustomRandomCallRepresentationTest.operation(RandomRepresentationTests.kt:40)
at java.base/java.lang.Thread.run(Thread.java:840)

Detailed trace:
| ---------------------------------------- |
| Thread 1 |
| ---------------------------------------- |
| operation(): IllegalArgumentException #1 |
| ---------------------------------------- |
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
= Invalid execution results =
| ----------------- |
| Thread 1 |
| ----------------- |
| operation(): void |
| ----------------- |

The following interleaving leads to the error:
| ----------- |
| Thread 1 |
| ----------- |
| operation() |
| ----------- |

Detailed trace:
| --------------------------------------------------------------------------------------------------------------------------------------- |
| Thread 1 |
| --------------------------------------------------------------------------------------------------------------------------------------- |
| operation() |
| NotRandom#1.nextBytes(ByteArray#1): ByteArray#1 at CustomRandomBytesCallRepresentationTest.operation(RandomRepresentationTests.kt:52) |
| nextBytes(ByteArray#1, 0, 10): ByteArray#1 at Random.nextBytes(Random.kt:253) |
| ByteArray#1[0] = 0 at Random.nextBytes(Random.kt:230) |
| ByteArray#1[1] = 0 at Random.nextBytes(Random.kt:231) |
| ByteArray#1[2] = 0 at Random.nextBytes(Random.kt:232) |
| ByteArray#1[3] = 0 at Random.nextBytes(Random.kt:233) |
| ByteArray#1[4] = 0 at Random.nextBytes(Random.kt:230) |
| ByteArray#1[5] = 0 at Random.nextBytes(Random.kt:231) |
| ByteArray#1[6] = 0 at Random.nextBytes(Random.kt:232) |
| ByteArray#1[7] = 0 at Random.nextBytes(Random.kt:233) |
| ByteArray#1[8] = 0 at Random.nextBytes(Random.kt:240) |
| ByteArray#1[9] = 0 at Random.nextBytes(Random.kt:240) |
| result: void |
| --------------------------------------------------------------------------------------------------------------------------------------- |
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
= Invalid execution results =
| ----------------- |
| Thread 1 |
| ----------------- |
| operation(): void |
| ----------------- |

The following interleaving leads to the error:
| ----------- |
| Thread 1 |
| ----------- |
| operation() |
| ----------- |

Detailed trace:
| ----------- |
| Thread 1 |
| ----------- |
| operation() |
| ----------- |

0 comments on commit 98421d4

Please sign in to comment.