I'm currently working on a project trying to determine how long different sorting algorithms take to sort different sized arrays. To measure the time, I've decided to use XCTest in Swift Playgrounds since it can automate the process of running the algorithm multiple times and averaging it out. But I have an issue with this method because I have to test a large variety array sizes from 15 elements up to 1500 or so, at 5 element intervals (ie. 15 elements, 20 elements, 25 elements...).
The only way I've been able to do this with one test is multiple functions with the different size and measuring the performance. Here is a sample of what that looks like:
class insertionSortPerformanceTest: XCTestCase {
func testMeasure10() {
measure {
_ = insertionSort(arrLen: 10)
}
}
func testMeasure15() {
measure {
_ = insertionSort(arrLen: 15)
}
}
func testMeasure20() {
measure {
_ = insertionSort(arrLen: 20)
}
}
}
insertionSort()
works by generating an array of length arrLen
and populating it with random numbers.
Is there a way to automate this process somehow?
Also, is there a way to take the output in the console and save it as a string so I can parse it for the relevant information later?
It's not exactly what you described but with the advent of Swift Macros, you can do very similar things in pure Swift. My colleague and I have developed a neat Swift Macro for parameterized XCTest. With this, you can simply use:
The macro will create a copy of your method for a given parameter set. So, of course, it's not exactly your scenario but, data-driven testing is closer than before.
Check it out here: https://github.com/PGSSoft/XCTestParametrizedMacro