Property-based testing concept in Kotlin - Time & Space Complexity
When using property-based testing, we check many inputs automatically to find problems.
We want to know how the time needed grows as we test more cases.
Analyze the time complexity of the following code snippet.
import io.kotest.property.checkAll
fun testSumProperty() {
checkAll { a, b ->
val result = a + b
assert(result >= a && result >= b)
}
}
This code tests the sum of two integers for many random pairs automatically.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Running the test block for each pair of inputs.
- How many times: The number of generated input pairs, usually fixed by the testing framework.
Each new input pair adds one more test run, so the total work grows directly with the number of tests.
| Input Size (number of tests) | Approx. Operations |
|---|---|
| 10 | 10 test runs |
| 100 | 100 test runs |
| 1000 | 1000 test runs |
Pattern observation: Doubling the number of tests doubles the total work.
Time Complexity: O(n)
This means the time needed grows in a straight line as we add more test cases.
[X] Wrong: "Property-based testing runs instantly no matter how many tests."
[OK] Correct: Each test case takes time, so more tests mean more total time.
Understanding how testing time grows helps you write better tests and explain your choices clearly.
"What if we increased the number of input parameters from two to three? How would the time complexity change?"