# Test a function modifies argument

**URL:** https://discourse.julialang.org/t/test-a-function-modifies-argument/29688
**Category:** New to Julia
**Created:** [October 9, 2019, 5:54pm UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688 "2019-10-09T17:54:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)
#### Post date: [October 9, 2019, 5:54pm UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688/1 "2019-10-09T17:54:21Z")

</div>

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`:

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

```

Do I just write multiple lines? Like this:

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

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 9, 2019, 6:26pm UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688/2 "2019-10-09T18:26:15Z")

</div>

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

```julia
@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

```julia
w = copy(v)
do_something!(v)
@test w !== v

```

---

<div class="post-metadata">

### Author: ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)
#### Post date: [October 9, 2019, 8:24pm UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688/3 "2019-10-09T20:24:49Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [October 9, 2019, 11:08pm UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688/4 "2019-10-09T23:08:50Z")

</div>

> [@squirrel](#):
>
> Copying `v` would necessarily make it different than `w` and so `w!==v` would always be true.

I think Tamas meant `@test w != v`

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [October 10, 2019, 12:18am UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688/5 "2019-10-10T00:18:56Z")

</div>

> [@squirrel](#):
>
> I could do something like this:
> 
> ```julia
> v = [1//1, 2//1, 3//1, 4//1]
> w = do_something!(v)
> @test w === v
> 
> ```

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

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 10, 2019, 5:30am UTC](https://discourse.julialang.org/t/test-a-function-modifies-argument/29688/6 "2019-10-10T05:30:14Z")

</div>

Yes, thanks for the correction.
