Skip to content

Commit

Permalink
Update changelog and add test for mixed float/int lists
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Jul 25, 2024
1 parent 49d4773 commit 5f34ad2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ versions.

- The resource type name can now be overridden with
`#[register_impl(name = "...")]` (#638)
- Floats can be decoded from integers (#641, fixes #603)

### Fixed

- The optional `register` attribute on `#[register_impl]` works as advertised
now (#638)
now (#638)

### Changed

Expand Down
1 change: 1 addition & 0 deletions rustler_tests/lib/rustler_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ defmodule RustlerTest do

def sum_list(_), do: err()
def make_list(), do: err()
def sum_list_as_floats(_), do: err()

def compare_local_pids(_, _), do: err()
def are_equal_local_pids(_, _), do: err()
Expand Down
5 changes: 5 additions & 0 deletions rustler_tests/native/rustler_test/src/test_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ pub fn sum_list(iter: ListIterator) -> NifResult<i64> {
pub fn make_list() -> Vec<usize> {
vec![1, 2, 3]
}

#[rustler::nif]
pub fn sum_list_as_floats(list: Vec<f64>) -> f64 {
list.into_iter().sum()
}
6 changes: 6 additions & 0 deletions rustler_tests/test/list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ defmodule RustlerTest.ListTest do
test "simple list construction with sum" do
assert RustlerTest.sum_list(RustlerTest.make_list()) == 6
end

test "sum float list" do
assert_in_delta RustlerTest.sum_list_as_floats([1, 2, 3, 4]), 10.0, 0.01
assert_in_delta RustlerTest.sum_list_as_floats([1.0, 2.0, 3.0, 4.0]), 10.0, 0.01
assert_in_delta RustlerTest.sum_list_as_floats([1, 2.0, 3, 4.0]), 10.0, 0.01
end
end

0 comments on commit 5f34ad2

Please sign in to comment.