ExUnit assert_in_delta with list?

I am trying to write a test that is comparing to list with specific precision.
For example

ExUnit.start()
defmodule MyTest do
  use ExUnit.Case

  test "list1 is equal to list2" do
    list1 = [0.0, -0.997, 2.993]
    list2 = [0.0, -1.0, 3.0]

    assert_in_delta list1, list2, 0.01
  end
end

I couldn’t find function doing that and I ended up with the following code.

test "list1 is equal to list2" do
    list1 = [0.0, -0.997, 2.993]
    list2 = [0.0, -1.0, 3.0]

    Enum.zip(list1, list2)
    |>  Enum.each(fn {a, b} ->
          assert_in_delta a, b, 0.01
        end)
  end

I wonder if it’s a better way of doing above.
Thanks!

That’s exactly the way to go. But of course you can wrap it in your own assert macro/function.

1 Like