# How to test whether a function is dispatched correctly?

**URL:** <https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 5, 2019, 1:01pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959 "2019-06-05T13:01:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [June 5, 2019, 1:01pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/1 "2019-06-05T13:01:44Z")

</div>

e.g.

```julia
foo(x::Int) = fpp(x)
foo(x::Float64) = fqq(x)

@test iscalled(foo, 1.0, fqq)

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 5, 2019, 1:08pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/2 "2019-06-05T13:08:02Z")

</div>

have you considered code\_lowered?

```julia
fpp(x)=x
fqq(x)=2*x
foo(x::Int) = fpp(x)
foo(x::Float64) = fqq(x)

foo(1)
foo(1.0)

@code_lowered foo(1)
@code_lowered foo(1.0)

```

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [June 5, 2019, 1:08pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/3 "2019-06-05T13:08:47Z")

</div>

Thanks, I need to write it into a test file. Is it possible?

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 5, 2019, 1:17pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/4 "2019-06-05T13:17:14Z")

</div>

I guess so.  
But there are probably more elegant answers to your question. Let us hope someone else chimes in.

Then again, I am not sure if I understand your problem; i.e. why you want to test this.

I guess you could search the result of code\_lowered for the string/symbol that you are looking for.

```julia
res=@code_lowered foo(1.0)
dump(res)

```

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [June 5, 2019, 1:22pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/5 "2019-06-05T13:22:13Z")

</div>

How about `functionloc(foo, (Float64,)) == @functionloc foo(1.0)`? Is it necessary to make `fqq` get involved?

---

<div class="post-metadata">

**Author:** ![cserteGT3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/csertegt3/32/8283_2.png) [@cserteGT3](https://discourse.julialang.org/u/cserteGT3)\
**Post date:** [June 5, 2019, 1:23pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/6 "2019-06-05T13:23:15Z")

</div>

Isn’t `@which` is what you’re looking for?

```julia
julia> fpp(x)=x
fpp (generic function with 1 method)

julia> fqq(x)=2*x
fqq (generic function with 1 method)

julia> foo(x::Int) = fpp(x)
foo (generic function with 1 method)

julia> foo(x::Float64) = fqq(x)
foo (generic function with 2 methods)

julia> @which foo(1)
foo(x::Int64) in Main at REPL[3]:1

julia> @which foo(1.0)
foo(x::Float64) in Main at REPL[4]:1

```

But I don’t know how to use it in tests.

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [June 5, 2019, 1:34pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/7 "2019-06-05T13:34:37Z")

</div>

using case:

I have a fallback of slow implementation, I want to make sure it is dispatched to the faster one in test.

`@code_lowered`, `@which` and `functionloc` only dive into the first level. Is it possible to show the list of function stack?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 5, 2019, 1:36pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/8 "2019-06-05T13:36:27Z")

</div>

Well, to test you need to figure out how to identify the result once you figured out which one to test. In this case, I believe what you should test is the signature of the method, if you have something more specific you want to test, you’ll also likely need to calculate that based on the signature.

After you decided on that, `which(...).sig` gives you the signature and you can do whatever test you want on that type.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [June 5, 2019, 1:42pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/9 "2019-06-05T13:42:35Z")

</div>

I guess this can also be done using explicitly trait-based dispatch like [Array-traits](https://docs.julialang.org/en/latest/manual/arrays/#Array-traits-1) unless you want to keep the signature of `foo` concise.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 5, 2019, 1:45pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/10 "2019-06-05T13:45:35Z")

</div>

Well, this is a requirement you’ll have to define precisely before it could be tested. You see, in general, there can be arbitrary code inside a function and they can all dispatch to different methods. Also, in general, the dispatch is not fixed until runtime so it’s impossible to determine that without running the function. It’s also important to define what do you mean “dispatch to the faster one”. Does it have to call that method? Or does it have to **NOT** call anything else? Or both?

You can avoid the problem of having to run the function by requiring static dispatch. Or you can use the debugger (interpreter) and trace every single function call and make your decision based on that. (I don’t know how it’s done but it should be possible.)

And if you want to rely on static dispatch information, you’ll need to use the `code_typed` family. You can walk the code recursively from there though you’ll also need to make the decision if you want to rely on any optimizations that requires inlining. If yes, then you’ll have to look at the line info if your function is inlined, which might still not be super reliable for small functions, or you can disable inlining but that can cause dynamic dispatch to show up that wouldn’t otherwise in the real code.

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [June 5, 2019, 1:55pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/11 "2019-06-05T13:55:22Z")

</div>

Thanks,  
The correct statement is  
**it have to call that method**  
And the function will be runned in the test.

yes, something like `@enter` in `Debugger` which searches AST upto a certain depth, to see whether a `function` is called. But should be able to be included in a test (useful in test driven development).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 5, 2019, 1:57pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/12 "2019-06-05T13:57:46Z")

</div>

If a runtime call of that method is good enough for you then interpreter should work. The code in the interpreter should have all what you need to do that in a test. I’ve never used it so I don’t know if it’s set up to do it but I’ll be surprised if it isn’t… Someone that have used the debugger should help you on that instead…

---

<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:** [June 5, 2019, 1:59pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/13 "2019-06-05T13:59:39Z")

</div>

Sorry if I am missing some detail, but I am wondering if this this something that is reasonable to test for.

In Julia, one usually tests for

1. getting the correct results (like all other languages),

2. the code being compiler-friendly (eg `Test.@inferred`, this is specific to Julia).

The chain of calls that achieves this is an implementation detail and may not survive a refactoring, so hardcoding this in unit tests is a cost one undertakes for little benefit.

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [June 5, 2019, 2:03pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/14 "2019-06-05T14:03:41Z")

</div>

Maybe I should use `btime` in the test to make sure it is faster. This is more fundamental. Thanks

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 5, 2019, 2:04pm UTC](https://discourse.julialang.org/t/how-to-test-whether-a-function-is-dispatched-correctly/24959/15 "2019-06-05T14:04:20Z")

</div>

That’s exactly what I’m talking about defining the test subject. It’s on the OP to come up with a stable enough target to test for. This is related to the complexity of the project and involves figuring out what changes are likely.

The threshold for such a test is certainly higher, so it should be done only for certain important calls (testing a single dispatch with `which` is easy and doesn’t have to be too limited). This is a performance test so not surviving refactoring isn’t necessarily an issue (it doesn’t signal breaking) as long as the test can be fixed accordingly (and ideally the test is unlikely turning into no-op after refactoring)
