Test a function modifies argument

How should I write a @testset to verify that a function modifies its argument?

For example, here is a function that is passed a vector (of Rationals) v and a row index row:

function divideBelow!(v, row)
    f = v[row]
    for i in row:length(v)
    return v

Do I just write multiple lines? Like this:

@testset "DivideBelow Tests" begin
       v = [3//1, 6//5]
       divideBelow(v, 2)
       @test v == [3//1, 1//1]

It is common to return the modified argument (like you do), so you could check that directly, eg

@test divideBelow!([3//1, 6//5]) == [3//1, 1//1]

However, if you explicitly want to check that v was modified (instead of copied), you could make a copy and then compare, eg

w = copy(v)
@test w !== v

Copying v would necessarily make it different than w and so w!==v would always be true. But I think I see what you mean. I could do something like this:

v = [1//1, 2//1, 3//1, 4//1]
w = do_something!(v)
@test w === v

I think Tamas meant @test w != v

1 Like

This test will pass for do_nothing!(v) too! (assuming the function returns its argument)

Yes, thanks for the correction.