reverseString that takes a String and returns its reverse.testDoubleReverse that checks the property: reversing twice returns the original string."All tests passed!" if all tests succeed.reverseString that takes a String and returns its reverse.testDoubleReverse that checks the property: reversing twice returns the original string."All tests passed!" if all tests succeed.reverseString functionreverseString that takes a String parameter named input and returns the reversed string using reversed().Use the built-in reversed() function on the input string.
val named sampleStrings and assign it a list of strings: "hello", "world", "kotlin", "property", "test".Use listOf to create the list with the exact strings.
testDoubleReversetestDoubleReverse with no parameters. Inside, use a for loop with variable str to iterate over sampleStrings. For each str, check if reverseString(reverseString(str)) == str. If any check fails, print "Test failed for: " + str and return false. If all pass, return true.Use a for loop and if statement to check the property for each string.
testDoubleReverse() and if it returns true, print "All tests passed!".Use an if statement to check the return value and print the success message.
What is the main idea behind property-based testing in Kotlin?
Which Kotlin library is commonly used for property-based testing?
Consider this Kotlin property-based test snippet using Kotest:
import io.kotest.property.checkAll
fun isSorted(list: List<Int>): Boolean = list.zipWithNext().all { it.first <= it.second }
checkAll { list ->
assert(isSorted(list.sorted()))
}What will this test do?
isSorted.What is wrong with this Kotlin property-based test using Kotest?
checkAll<Int> { n ->
assert(n > 0)
}Choose the best explanation.
checkAll<Int> which generates all Int values, including negatives and zero.assert(n > 0) expects all numbers to be positive, which is false for many generated values.You want to test a function reverseTwice(list: List<Int>): List<Int> that reverses a list twice. Which property-based test best checks that reverseTwice returns the original list?
fun reverseTwice(list: List<Int>): List<Int> = list.reversed().reversed()
reverseTwice(list) equals the original list, which correctly tests the property.Pass the test and this pattern moves unknown