Skip to content

Commit

Permalink
Display enum values as enum names instead of <className>@<hashCode> (
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkoval authored Apr 11, 2024
1 parent c2617aa commit 7114b41
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ private fun adornedStringRepresentation(any: Any?): String {
// have trivial `toString` implementation, which is used here.
if (any == null || any.javaClass.isImmutableWithNiceToString)
return any.toString()
// For enum types, we can always display their name.
if (any.javaClass.isEnum) {
return (any as Enum<*>).name
}
// simplified representation for Continuations
// (we usually do not really care about details).
if (any is Continuation<*>)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Lincheck
*
* Copyright (C) 2019 - 2024 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

import org.jetbrains.kotlinx.lincheck.*
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.managed.modelchecking.*
import org.jetbrains.kotlinx.lincheck_test.util.*
import org.junit.*

class EnumRepresentationTest {
private var x: MyEnum = MyEnum.VALUE_1
private var counter = 0

@Operation
fun operation(): Int {
x = MyEnum.VALUE_2
return counter++
}

@Test
fun test() = ModelCheckingOptions()
.checkImpl(this::class.java)
.checkLincheckOutput("enum_representation.txt")
}

private enum class MyEnum {
VALUE_1, VALUE_2
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The following interleaving leads to the error:
| | switch |
| cancelledOp() + cancel: SUSPENDED + CANCELLED | |
| invokeOnCancellation(cancelledOp$2$1) at CoroutineCancellationTraceReportingTest.cancelledOp(CoroutineCancellationTraceReportingTest.kt:29) | |
| getResult(): CoroutineSingletons@1 at CoroutineCancellationTraceReportingTest.cancelledOp(CoroutineCancellationTraceReportingTest.kt:59) | |
| getResult(): COROUTINE_SUSPENDED at CoroutineCancellationTraceReportingTest.cancelledOp(CoroutineCancellationTraceReportingTest.kt:59) | |
| trySuspend(): true at CancellableContinuationImpl.getResult(CancellableContinuationImpl.kt:300) | |
| getParentHandle(): null at CancellableContinuationImpl.getResult(CancellableContinuationImpl.kt:310) | |
| CANCELLED BEFORE RESUMPTION | |
Expand All @@ -40,7 +40,7 @@ Detailed trace:
| invokeOnCancellationImpl(InvokeOnCancel@1) at CancellableContinuationImpl.invokeOnCancellation(CancellableContinuationImpl.kt:399) | |
| _state.get(): Active@1 at CancellableContinuationImpl.invokeOnCancellationImpl(CancellableContinuationImpl.kt:403) | |
| _state.compareAndSet(Active@1,InvokeOnCancel@1): true at CancellableContinuationImpl.invokeOnCancellationImpl(CancellableContinuationImpl.kt:407) | |
| getResult(): CoroutineSingletons@1 at CoroutineCancellationTraceReportingTest.cancelledOp(CoroutineCancellationTraceReportingTest.kt:59) | |
| getResult(): COROUTINE_SUSPENDED at CoroutineCancellationTraceReportingTest.cancelledOp(CoroutineCancellationTraceReportingTest.kt:59) | |
| trySuspend(): true at CancellableContinuationImpl.getResult(CancellableContinuationImpl.kt:300) | |
| _decisionAndIndex.get(): 536870911 at CancellableContinuationImpl.trySuspend(CancellableContinuationImpl.kt:0) | |
| _decisionAndIndex.compareAndSet(536870911,1073741823): true at CancellableContinuationImpl.trySuspend(CancellableContinuationImpl.kt:278) | |
Expand Down
36 changes: 36 additions & 0 deletions src/jvm/test/resources/expected_logs/enum_representation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
= Invalid execution results =
| ------------------------------- |
| Thread 1 | Thread 2 |
| ------------------------------- |
| operation(): 0 | operation(): 0 |
| ------------------------------- |

The following interleaving leads to the error:
| ----------------------------------------------------------------------------------------------------- |
| Thread 1 | Thread 2 |
| ----------------------------------------------------------------------------------------------------- |
| | operation(): 0 |
| | x.WRITE(VALUE_2) at EnumRepresentationTest.operation(EnumRepresentationTest.kt:25) |
| | counter.READ: 0 at EnumRepresentationTest.operation(EnumRepresentationTest.kt:26) |
| | switch |
| operation(): 0 | |
| | counter.WRITE(1) at EnumRepresentationTest.operation(EnumRepresentationTest.kt:26) |
| | result: 0 |
| ----------------------------------------------------------------------------------------------------- |

Detailed trace:
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Thread 1 | Thread 2 |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| | operation(): 0 |
| | x.WRITE(VALUE_2) at EnumRepresentationTest.operation(EnumRepresentationTest.kt:25) |
| | counter.READ: 0 at EnumRepresentationTest.operation(EnumRepresentationTest.kt:26) |
| | switch |
| operation(): 0 | |
| x.WRITE(VALUE_2) at EnumRepresentationTest.operation(EnumRepresentationTest.kt:25) | |
| counter.READ: 0 at EnumRepresentationTest.operation(EnumRepresentationTest.kt:26) | |
| counter.WRITE(1) at EnumRepresentationTest.operation(EnumRepresentationTest.kt:26) | |
| result: 0 | |
| | counter.WRITE(1) at EnumRepresentationTest.operation(EnumRepresentationTest.kt:26) |
| | result: 0 |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Loading

0 comments on commit 7114b41

Please sign in to comment.