FIR: Adjust testData for spec tests: contracts

This commit is contained in:
Denis Zharkov
2020-04-15 13:05:05 +03:00
parent 243f9bb758
commit 86e1aadd31
78 changed files with 7320 additions and 0 deletions
@@ -0,0 +1,84 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 1
* DESCRIPTION: val/var reassignment and/or uninitialized variable usages based on CallsInPlace effect with wrong invocation kind
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
value_1.inc()
}
// TESTCASE NUMBER: 2
fun case_2() {
val value_1: Int
funWithAtMostOnceCallsInPlace { value_1 = 10 }
value_1.inc()
}
// TESTCASE NUMBER: 3
fun case_3() {
val value_1: Int
funWithUnknownCallsInPlace { value_1 = 10 }
value_1.inc()
}
// TESTCASE NUMBER: 4
fun case_4() {
var value_1: Int
var value_2: Int
funWithAtMostOnceCallsInPlace { value_1 = 10 }
funWithUnknownCallsInPlace { value_2 = 10 }
value_1.dec()
value_2.div(10)
}
// TESTCASE NUMBER: 5
class case_5 {
val value_1: Int
val value_2: Int
val value_3: Int
var value_4: Int
var value_5: Int
init {
funWithAtMostOnceCallsInPlace { value_1 = 1 }
funWithUnknownCallsInPlace { value_2 = 1 }
funWithAtLeastOnceCallsInPlace { value_3 = 1 }
funWithAtMostOnceCallsInPlace { value_4 = 2 }
funWithUnknownCallsInPlace { value_5 = 3 }
}
}
// TESTCASE NUMBER: 6
fun case_6() {
val value_1: Int
for (i in 0..1)
funWithExactlyOnceCallsInPlace { value_1 = 10 }
value_1.dec()
}
// TESTCASE NUMBER: 7
fun case_7() {
var value_1: Int
var i = 0
while (i < 10) {
funWithExactlyOnceCallsInPlace { value_1 = 10 }
i++
}
value_1.dec()
}
// TESTCASE NUMBER: 8
fun case_8() {
var value_1: Int
if (true) funWithAtLeastOnceCallsInPlace { value_1 = 10 }
value_1.dec()
}
@@ -0,0 +1,86 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 2
* DESCRIPTION: val/var reassignment and/or uninitialized variable usages based on nested CallsInPlace effects with wrong invocation kind
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithAtMostOnceCallsInPlace {
value_1 = 1
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
// TESTCASE NUMBER: 2
fun case_2() {
val value_1: Int
funWithAtMostOnceCallsInPlace {
funWithAtMostOnceCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
// TESTCASE NUMBER: 3
fun case_3() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithAtMostOnceCallsInPlace {
value_1 = 1
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
// TESTCASE NUMBER: 4
fun case_4() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
funWithUnknownCallsInPlace {
value_1 = 1
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
@@ -0,0 +1,169 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 3
* DESCRIPTION: val/var reassignment and/or uninitialized variable usages with compelx control flow inside/outside lambda of contract function with CallsInPlace effect
* HELPERS: enumClasses, contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1(value_1: EnumClass?) {
val value_2: Int
when (value_1) {
EnumClass.NORTH -> funWithExactlyOnceCallsInPlace { value_2 = 1 }
EnumClass.SOUTH -> funWithExactlyOnceCallsInPlace { value_2 = 2 }
EnumClass.EAST -> funWithExactlyOnceCallsInPlace { value_2 = 4 }
null -> funWithExactlyOnceCallsInPlace { value_2 = 5 }
}
value_2.inc()
}
// TESTCASE NUMBER: 2
fun case_2(value_1: Any?) {
val value_2: Int
funWithAtMostOnceCallsInPlace {
if (value_1 is String) {
value_2 = 0
} else if (value_1 == null) {
value_2 = 1
} else {
funWithAtMostOnceCallsInPlace { value_2 = 2 }
}
value_2.dec()
}
value_2.dec()
}
// TESTCASE NUMBER: 3
class case_3(value_1: Any?) {
var value_2: Int
init {
if (value_1 is String) {
funWithUnknownCallsInPlace { value_2 = 0 }
value_2.div(10)
} else if (value_1 == null) {
funWithAtLeastOnceCallsInPlace { value_2 = 1 }
value_2.div(10)
} else {
value_2 = 2
}
value_2.div(10)
}
}
// TESTCASE NUMBER: 4
fun case_4(value_1: EnumClassSingle?) {
var value_2: Int
funWithAtMostOnceCallsInPlace {
when (value_1) {
EnumClassSingle.EVERYTHING -> {
funWithExactlyOnceCallsInPlace { value_2 = 1 }
++value_2
}
null -> {
funWithUnknownCallsInPlace { value_2 = 2 }
}
}
value_2.minus(5)
}
value_2.minus(5)
}
// TESTCASE NUMBER: 5
fun case_5() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
funWithAtMostOnceCallsInPlace { value_2 = 1 }
}
value_2++
}
// TESTCASE NUMBER: 6
fun case_6() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
throw Exception()
} finally {
println(value_2.inc())
}
value_2++
}
// TESTCASE NUMBER: 7
fun case_7() {
var value_1: Int
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
funWithAtMostOnceCallsInPlace { value_1 = 10 }
}
}
println(value_1.inc())
}
// TESTCASE NUMBER: 8
fun case_8() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
funWithUnknownCallsInPlace {
x = 42
}
return@outer
}
throw Exception()
}
println(x.inc())
}
// TESTCASE NUMBER: 9
fun case_9() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
x = 42
return@outer
}
}
println(x.inc())
}
// TESTCASE NUMBER: 10
fun case_10() {
var x: Int
funWithAtLeastOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
x = 41
return@outer
}
funWithUnknownCallsInPlace {
x = 42
return@outer
}
return@outer
}
println(x.inc())
}
@@ -0,0 +1,94 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 4
* DESCRIPTION: CallsInPlace contract functions with name shadowing and wrong invocation kind
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
val value_1 = 10
value_1.inc()
}
value_1.inc()
}
// TESTCASE NUMBER: 2
fun case_2() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
value_1.inc()
}
// TESTCASE NUMBER: 3
fun case_3() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
val value_1: Int
funWithAtMostOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithAtMostOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
// TESTCASE NUMBER: 4
fun case_4() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithAtMostOnceCallsInPlace {
value_1 = 1
}
value_1.dec()
}
// TESTCASE NUMBER: 5
fun case_5() {
val value_1: Int
funWithUnknownCallsInPlace {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
value_1 = 10
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithUnknownCallsInPlace {
value_1 = 1
}
value_1.dec()
}
@@ -0,0 +1,51 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 5
* DESCRIPTION: CallsInPlace contract functions with invalid lambda passing to function parameter.
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace({ value_1 = 10 })
value_1.inc()
}
// TESTCASE NUMBER: 2
fun case_2() {
var value_1: Int
val l = { value_1 = 10 }
funWithAtLeastOnceCallsInPlace(l)
value_1.inc()
}
// TESTCASE NUMBER: 3
fun case_3() {
var value_1: Int
val l = fun () { value_1 = 10 }
funWithAtLeastOnceCallsInPlace(l)
value_1.inc()
}
// TESTCASE NUMBER: 4
fun case_4() {
var value_1: Int
funWithAtLeastOnceCallsInPlace(fun () { value_1 = 10 })
value_1.inc()
}
// TESTCASE NUMBER: 5
fun case_5() {
val value_1: Int
val o = object {
fun l() { value_1 = 10 }
}
funWithExactlyOnceCallsInPlace(o::l)
value_1.inc()
}
@@ -0,0 +1,27 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 6
* DESCRIPTION: Check the lack of CallsInPlace effect on the lambda in the parentheses.
* UNEXPECTED BEHAVIOUR
* ISSUES: KT-26229
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace({ value_1 = 11 })
value_1.inc()
}
// TESTCASE NUMBER: 2
fun case_2() {
var value_1: Int
funWithAtLeastOnceCallsInPlace({ value_1 = 11 })
value_1.inc()
}
@@ -0,0 +1,189 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 3
* DESCRIPTION: val/var assignments or subsequent usages with compelx control flow inside/outside lambda of contract function with CallsInPlace effect
* HELPERS: enumClasses, contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1(value_1: EnumClass?) {
val value_2: Int
when (value_1) {
EnumClass.NORTH -> funWithExactlyOnceCallsInPlace { value_2 = 1 }
EnumClass.SOUTH -> funWithExactlyOnceCallsInPlace { value_2 = 2 }
EnumClass.WEST -> funWithExactlyOnceCallsInPlace { value_2 = 3 }
EnumClass.EAST -> funWithExactlyOnceCallsInPlace { value_2 = 4 }
null -> funWithExactlyOnceCallsInPlace { value_2 = 5 }
}
value_2.inc()
}
// TESTCASE NUMBER: 2
fun case_2(value_1: Any?) {
val value_2: Int
funWithAtMostOnceCallsInPlace {
if (value_1 is String) {
value_2 = 0
} else if (value_1 == null) {
value_2 = 1
} else {
funWithExactlyOnceCallsInPlace { value_2 = 2 }
value_2.dec()
}
value_2.dec()
}
}
// TESTCASE NUMBER: 3
class case_3(value_1: Any?) {
var value_2: Int
init {
if (value_1 is String) {
funWithExactlyOnceCallsInPlace { value_2 = 0 }
value_2.div(10)
} else if (value_1 == null) {
funWithAtLeastOnceCallsInPlace { value_2 = 1 }
value_2.div(10)
} else {
value_2 = 2
}
value_2.div(10)
}
}
// TESTCASE NUMBER: 4
fun case_4(value_1: EnumClassSingle?) {
var value_2: Int
funWithAtMostOnceCallsInPlace {
when (value_1) {
EnumClassSingle.EVERYTHING -> {
funWithExactlyOnceCallsInPlace { value_2 = 1 }
++value_2
}
null -> {
funWithAtLeastOnceCallsInPlace { value_2 = 2 }
--value_2
}
}
value_2.minus(5)
}
}
// TESTCASE NUMBER: 5
fun case_5() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
funWithExactlyOnceCallsInPlace { value_2 = 1 }
}
value_2++
}
// TESTCASE NUMBER: 6
fun case_6() {
var value_2: Int
try {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
} catch (e: Exception) {
throw Exception()
} finally {
funWithAtLeastOnceCallsInPlace { value_2 = 10 }
}
value_2++
}
// TESTCASE NUMBER: 7
fun case_7() {
var value_1: Int
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
throw Exception()
}
} finally {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
}
println(value_1.inc())
}
// TESTCASE NUMBER: 8
fun case_8() {
var value_1: Int
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
try {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
} catch (e: Exception) {
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
}
}
println(value_1.inc())
}
// TESTCASE NUMBER: 9
fun case_9() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
funWithUnknownCallsInPlace {
x = 42
return@outer
}
}
throw Exception()
}
println(x.inc())
}
// TESTCASE NUMBER: 10
fun case_10() {
val x: Int
funWithExactlyOnceCallsInPlace outer@ {
funWithAtLeastOnceCallsInPlace {
x = 42
return@outer
}
}
println(x.inc())
}
// TESTCASE NUMBER: 11
fun case_11() {
var x: Int
funWithAtLeastOnceCallsInPlace outer@ {
funWithAtMostOnceCallsInPlace {
x = 41
return@outer
}
funWithUnknownCallsInPlace {
x = 42
return@outer
}
return@case_11
}
println(x.inc())
}
@@ -0,0 +1,140 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 4
* DESCRIPTION: CallsInPlace contract functions with name shadowing
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
val value_1 = 10
value_1.inc()
}
}
// TESTCASE NUMBER: 2
fun case_2() {
val value_1: Int
funWithExactlyOnceCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
}
// TESTCASE NUMBER: 3
fun case_3() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
// TESTCASE NUMBER: 4
fun case_4() {
val value_1: Int
funWithAtMostOnceCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithUnknownCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
// TESTCASE NUMBER: 5
fun case_5() {
val value_1: Int
funWithUnknownCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
}
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
value_1.inc()
}
// TESTCASE NUMBER: 6
fun case_6() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
val value_1: Int
funWithExactlyOnceCallsInPlace {
value_1 = 10
}
funWithAtMostOnceCallsInPlace {
value_1.inc()
}
value_1.inc()
}
funWithAtLeastOnceCallsInPlace { value_1 = 1 }
value_1.dec()
}
// TESTCASE NUMBER: 7
fun case_7() {
val value_1: Int
funWithAtLeastOnceCallsInPlace {
var value_1: Int
funWithAtLeastOnceCallsInPlace { value_1 = 10 }
funWithUnknownCallsInPlace { value_1.inc() }
value_1.inc()
}
funWithExactlyOnceCallsInPlace { value_1 = 1 }
value_1.dec()
}
// TESTCASE NUMBER: 8
fun case_8() {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
var value_1: Int
funWithAtLeastOnceCallsInPlace {
value_1 = 10
}
funWithAtLeastOnceCallsInPlace {
value_1.inc()
}
value_1++
}
funWithAtLeastOnceCallsInPlace {
value_1 = 1
}
value_1--
}
@@ -0,0 +1,60 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, initialization
* NUMBER: 6
* DESCRIPTION: Check the lack of CallsInPlace effect on the lambda in the parentheses.
* UNEXPECTED BEHAVIOUR
* ISSUES: KT-26229
* HELPERS: contractFunctions
*/
// FILE: contracts.kt
package contracts
import kotlin.contracts.*
// TESTCASE NUMBER: 3
inline fun case_3(block1: () -> Unit, block2: () -> Unit, block3: () -> Unit) {
contract {
callsInPlace(block1, InvocationKind.EXACTLY_ONCE)
callsInPlace(block2, InvocationKind.AT_LEAST_ONCE)
callsInPlace(block3, InvocationKind.EXACTLY_ONCE)
}
block1()
block2()
block2()
block3()
}
// FILE: main.kt
import contracts.*
// TESTCASE NUMBER: 1
fun case_1() {
val value_1: Int
funWithExactlyOnceCallsInPlace({ value_1 = 11 })
value_1.inc()
}
// TESTCASE NUMBER: 2
fun case_2() {
var value_1: Int
funWithAtLeastOnceCallsInPlace({ value_1 = 11 })
value_1.inc()
}
// TESTCASE NUMBER: 3
fun case_3() {
val value_1: Int
var value_2: Int
val value_3: Int
contracts.case_3({ value_1 = 1 }, { value_2 = 2 }, { value_3 = 3 })
value_1.inc()
value_2.inc()
value_3.inc()
}
@@ -0,0 +1,45 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (NEGATIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 1
* DESCRIPTION: Using not allowed break and continue inside lambda of contract function
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1(value_1: Boolean) {
while (value_1) {
funWithExactlyOnceCallsInPlace {
break
}
println("1")
}
loop@ for (i in 0..10) {
funWithExactlyOnceCallsInPlace {
break@loop
}
println("1")
}
}
// TESTCASE NUMBER: 2
fun case_2(value_1: Boolean) {
for (i in 0..10) {
funWithExactlyOnceCallsInPlace {
continue
}
println("1")
}
loop@ while (value_1) {
funWithExactlyOnceCallsInPlace {
continue@loop
}
println("1")
}
}
@@ -0,0 +1,113 @@
// !DIAGNOSTICS: -UNUSED_VARIABLE
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 1
* DESCRIPTION: Unreachable code detection using contract function with CallsInPlace effect
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
funWithExactlyOnceCallsInPlace {
throw Exception()
}
println("1")
}
// TESTCASE NUMBER: 2
fun case_2() {
funWithAtLeastOnceCallsInPlace {
throw Exception()
}
println("1")
}
// TESTCASE NUMBER: 3
fun case_3() {
funWithExactlyOnceCallsInPlace {
return
}
println("1")
}
// TESTCASE NUMBER: 4
fun case_4() {
funWithAtLeastOnceCallsInPlace {
return
}
println("1")
}
// TESTCASE NUMBER: 5
fun case_5(args: Array<String>) {
fun nestedFun_1() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_1
}
println("1")
}
fun nestedFun_2() {
args.forEach {
funWithAtLeastOnceCallsInPlace {
return@forEach
}
println("1")
}
}
fun nestedFun_3() {
fun nestedFun_4() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_4
}
println("1")
}
println("1")
}
}
// TESTCASE NUMBER: 6
fun case_6(args: Array<String>) {
args.forEach {
funWithExactlyOnceCallsInPlace {
return@forEach
}
println("1")
}
args.forEach {
fun nestedFun_1() {
funWithExactlyOnceCallsInPlace {
return@nestedFun_1
}
println("1")
}
}
args.forEach {
fun nestedFun_2() {
funWithExactlyOnceCallsInPlace {
return
}
println("1")
}
}
}
// TESTCASE NUMBER: 7
fun case_7() {
val value_1 = funWithExactlyOnceCallsInPlace {
throw Exception()
println(1)
}
println(value_1)
}
// TESTCASE NUMBER: 8
fun case_8() {
println(funWithExactlyOnceCallsInPlace { return; println(1) })
return
}
@@ -0,0 +1,96 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 3
* DESCRIPTION: Unreachable code detection using local functions or labdas combined with contract functions with CallsInPlace effect
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
funWithExactlyOnceCallsInPlace {
throw Exception()
}
println("1")
}
// TESTCASE NUMBER: 2
fun case_2() {
funWithAtLeastOnceCallsInPlace {
throw Exception()
}
println("1")
}
// TESTCASE NUMBER: 3
fun case_3() {
funWithExactlyOnceCallsInPlace {
return
}
println("1")
}
// TESTCASE NUMBER: 4
fun case_4() {
funWithAtLeastOnceCallsInPlace {
return
}
println("1")
}
// TESTCASE NUMBER: 5
fun case_5(args: Array<String>) {
fun nestedFun_1() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_1
}
println("1")
}
fun nestedFun_2() {
args.forEach {
funWithAtLeastOnceCallsInPlace {
return@forEach
}
println("1")
}
}
fun nestedFun_3() {
fun nestedFun_4() {
funWithAtLeastOnceCallsInPlace {
return@nestedFun_4
}
println("1")
}
println("1")
}
}
// TESTCASE NUMBER: 6
fun case_6(args: Array<String>) {
args.forEach {
funWithExactlyOnceCallsInPlace {
return@forEach
}
println("1")
}
args.forEach {
fun nestedFun_1() {
funWithExactlyOnceCallsInPlace {
return@nestedFun_1
}
println("1")
}
}
args.forEach {
fun nestedFun_2() {
funWithExactlyOnceCallsInPlace {
return
}
println("1")
}
}
}
@@ -0,0 +1,56 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 4
* DESCRIPTION: Unreachable code detection using nested contract functions with CallsInPlace effect
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1() {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
throw Exception()
}
println("1")
}
println("2")
}
println("3")
}
// TESTCASE NUMBER: 2
fun case_2() {
funWithAtLeastOnceCallsInPlace {
funWithAtLeastOnceCallsInPlace label_1@ {
funWithAtLeastOnceCallsInPlace {
return@label_1
}
println("1")
}
println("2")
}
funWithAtLeastOnceCallsInPlace {
return
}
println("3")
}
// TESTCASE NUMBER: 3
fun case_3() {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
funWithExactlyOnceCallsInPlace {
return@funWithExactlyOnceCallsInPlace
}
println("1")
}
println("2")
}
println("3")
}
@@ -0,0 +1,60 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 5
* DESCRIPTION: Unreachable code detection using contract functions with complex control flow inside
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1(b: Boolean?) {
funWithExactlyOnceCallsInPlace {
if (b == null) return
when (b) {
true -> {
println(1)
return
}
false -> {
println(2)
throw Exception()
}
}
println(3)
}
println(3)
}
// TESTCASE NUMBER: 2
fun case_2(b: Boolean?, c: Boolean) {
funWithAtLeastOnceCallsInPlace {
when (b) {
true -> {
println(1)
return
}
else -> {
if (b == null) {
funWithExactlyOnceCallsInPlace {
when {
c == true -> throw Exception()
else -> funWithAtLeastOnceCallsInPlace { return }
}
println(3)
}
println(3)
} else {
throw Exception()
}
println(3)
}
}
println(3)
}
println(3)
}
@@ -0,0 +1,68 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
// SKIP_TXT
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 6
* DESCRIPTION: Check for lack of unreachable code report when in complex control flow of contract function lambda not all branches are doing non-local return
* HELPERS: contractFunctions
*/
// TESTCASE NUMBER: 1
fun case_1(b: Boolean?, c: Boolean) {
funWithExactlyOnceCallsInPlace {
if (b == null) return
try {
when (b) {
true -> {
println(1)
return
}
false -> {
println(2)
throw Exception()
}
}
} catch (e: Exception) {
if (c) {
return@funWithExactlyOnceCallsInPlace
} else {
return
}
}
println(3)
}
println(3)
}
// TESTCASE NUMBER: 2
fun case_2(b: Boolean?, c: Boolean) {
funWithAtLeastOnceCallsInPlace {
when (b) {
true -> {
println(1)
return
}
else -> {
if (b == null) {
funWithExactlyOnceCallsInPlace {
when {
c == true -> throw Exception()
else -> funWithAtMostOnceCallsInPlace { return }
}
println(3)
}
println(3)
} else {
throw Exception()
}
println(3)
}
}
println(3)
}
println(3)
}
@@ -0,0 +1,32 @@
// !USE_EXPERIMENTAL: kotlin.contracts.ExperimentalContracts
/*
* KOTLIN DIAGNOSTICS NOT LINKED SPEC TEST (POSITIVE)
*
* SECTIONS: contracts, analysis, controlFlow, unreachableCode
* NUMBER: 7
* DESCRIPTION: Smart initialization with correspond contract function with default value before lambda.
* ISSUES: KT-26444
*/
// FILE: contracts.kt
package contracts
import kotlin.contracts.*
// TESTCASE NUMBER: 1
fun case_1(x: Double = 1.0, block: () -> Unit): Double {
contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) }
return x
}
// FILE: main.kt
import contracts.*
// TESTCASE NUMBER: 1
fun case_1() {
contracts.case_1 { throw Exception() }
println(1)
}