swiftgenericsswift-testing

How to test a generic function using Swift Testing with parameters?


The setup:
My app has a generic function that point-mirrors a quadratic matrix of arbitrary elements at its center:

func pointMirroredMatrix<T>(_ matrix: inout [[T]]) {
    assert(matrix.count > 0 && matrix[0].count > 0) // Ensure matrix is not empty
    assert(matrix.count == matrix[0].count) // Ensure matrix is quadratic
    let n = matrix.count // The matrix is n x n
    for row in 0 ..< n/2 {
        for col in 0 ..< n {
            // Swapping elements
            (matrix[row][col], matrix[n - 1 - row][n - 1 - col]) = (matrix[n - 1 - row][n - 1 - col], matrix[row][col])
        }
    }
}

I want to write a Swift test that checks if func pointMirroredMatrix works correctly.
I thus defined first:

typealias PointMirroredMatrixParams = (matrix: [[Any]], expectedResult: [[Any]])
let pointMirroredMatrixArgs: [PointMirroredMatrixParams] = [(matrix:         [[0, 1], [0, 0]], 
                                                             expectedResult: [[0, 0], [1, 0]])]

struct VisionTests {
    
    @Test("pointMirroredMatrix", arguments: pointMirroredMatrixArgs)
    func pointMirroredMatrix(p: PointMirroredMatrixParams) throws {
        // see below
    }  

Please note that the matrix elements are of type Any, because @Test does not allow to use generics.
The test should do something as follows:

var mirroredMatrix = deepCopyMatrix(p.matrix) // Make deep copy
vision.pointMirroredMatrix(&mirroredMatrix) // Mirror the copy
let mirroredCorrectly = maticesAreEqual(p.expectedResult, mirroredMatrix)
#expect(mirroredCorrectly)  

The problem:
I am not able to write func maticesAreEqual, because the matrices passed by the test parameters are of type Any. I tried the following without success:

func maticesAreEqual(_ matrix1: [[Any]], _ matrix2: [[Any]]) -> Bool {
    guard matrix1.count == matrix2.count else { return false }
    guard matrix1[0].count == matrix2[0].count else { return false }
    guard !matrix1.isEmpty else { return true }
    guard type(of: matrix1[0][0]) == type(of: matrix2[0][0]) else { return false }
    guard type(of: matrix1[0][0]) == (any Equatable).self else { return false }
    // Here it is known that both matrices have the same dimension, their elements are of the same type and are equatable
    // But how to find out if they are equal?
    for i in 0 ..< matrix1.count {
        for j in 0 ..< matrix1[i].count {
            if matrix1[i][j] != matrix2[i][j] { return false } // Build error
        }
    }
    return true
}  

but the instruction in the inner loop does not compile due to the type Any. I get the error Type 'Any' cannot conform to 'Collection'.

My question:
How can I test func pointMirroredMatrix with matrices that have elements of different type, passed in by @Test?


Solution

  • You don't need to test pointMirroredMatrix<T> with more than one type T. That would be testing that Swift generic is generic, which is unnecessary. You just want to know that a particular input to your function results in the correct output.

    I think you'd be better off making your pointMirroredMatrix<T> a throws method instead of using assert. That way, you can test that, say, an empty matrix or a nonquadratic matrix throws. Remember too that assert is ignored in a release build, so you are not really catching bad input.