From 06994efc23e9be6d4ec29ad79970dc6da4cfb1ae Mon Sep 17 00:00:00 2001 From: sabiwara Date: Sat, 25 May 2024 14:19:55 +0900 Subject: [PATCH] Remove hacks to support old versions --- lib/aja.ex | 70 ++-- lib/vector.ex | 27 +- test/enum_prop_test.exs | 828 +++++++++++++++++++------------------- test/vector_prop_test.exs | 321 ++++++++------- test/vector_test.exs | 7 +- 5 files changed, 611 insertions(+), 642 deletions(-) diff --git a/lib/aja.ex b/lib/aja.ex index ca2ef04..551c482 100644 --- a/lib/aja.ex +++ b/lib/aja.ex @@ -209,26 +209,21 @@ defmodule Aja do end end - # TODO remove when dropping support for Elixir < 1.12 - stepped_range_available? = Version.compare(System.version(), "1.12.0-rc.0") != :lt - - if stepped_range_available? do - defmacro vec({:"..//", _, [first, last, step]} = call) do - case Enum.map([first, last, step], &Macro.expand(&1, __CALLER__)) do - [first, last, step] when is_integer(first) and is_integer(last) and is_integer(step) -> - Range.new(first, last, step) - |> Enum.to_list() - |> ast_from_list(__CALLER__) - - _ -> - raise ArgumentError, ~s""" - Incorrect use of `Aja.vec/1`: - vec(#{Macro.to_string(call)}). - - The `vec(a..b//c)` syntax can only be used with constants: - vec(1..100//5) - """ - end + defmacro vec({:"..//", _, [first, last, step]} = call) do + case Enum.map([first, last, step], &Macro.expand(&1, __CALLER__)) do + [first, last, step] when is_integer(first) and is_integer(last) and is_integer(step) -> + Range.new(first, last, step) + |> Enum.to_list() + |> ast_from_list(__CALLER__) + + _ -> + raise ArgumentError, ~s""" + Incorrect use of `Aja.vec/1`: + vec(#{Macro.to_string(call)}). + + The `vec(a..b//c)` syntax can only be used with constants: + vec(1..100//5) + """ end end @@ -348,30 +343,25 @@ defmodule Aja do end end - plus_enabled? = Version.compare(System.version(), "1.11.0") != :lt - - if plus_enabled? do - @doc """ - Convenience operator to concatenate an enumerable `right` to a vector `left`. + @doc """ + Convenience operator to concatenate an enumerable `right` to a vector `left`. - `left` has to be an `Aja.Vector`, `right` can be any `Enumerable`. + `left` has to be an `Aja.Vector`, `right` can be any `Enumerable`. - It is just an alias for `Aja.Vector.concat/2`. + It is just an alias for `Aja.Vector.concat/2`. - Only available on Elixir versions >= 1.11. + Only available on Elixir versions >= 1.11. - ## Examples + ## Examples - iex> import Aja - iex> vec(5..1//-1) +++ vec([:boom, nil]) - vec([5, 4, 3, 2, 1, :boom, nil]) - iex> vec(5..1//-1) +++ 0..3 - vec([5, 4, 3, 2, 1, 0, 1, 2, 3]) + iex> import Aja + iex> vec(5..1//-1) +++ vec([:boom, nil]) + vec([5, 4, 3, 2, 1, :boom, nil]) + iex> vec(5..1//-1) +++ 0..3 + vec([5, 4, 3, 2, 1, 0, 1, 2, 3]) - """ - # TODO remove hack to support 1.10 - defdelegate unquote(if(plus_enabled?, do: String.to_atom("+++"), else: :++))(left, right), - to: Aja.Vector, - as: :concat - end + """ + defdelegate left +++ right, + to: Aja.Vector, + as: :concat end diff --git a/lib/vector.ex b/lib/vector.ex index b0d508b..3472b04 100644 --- a/lib/vector.ex +++ b/lib/vector.ex @@ -1,15 +1,4 @@ defmodule Aja.Vector do - # TODO remove doc hack when stop supporting 1.10 - plusplusplus_doc = ~S""" - ## Convenience [`+++/2`](`Aja.+++/2`) operator - - The `Aja.+++/2` operator can make appending to a vector more compact by aliasing `Aja.Vector.concat/2`: - - iex> import Aja - iex> vec([1, 2, 3]) +++ vec([4, 5]) - vec([1, 2, 3, 4, 5]) - """ - @moduledoc ~s""" Fast persistent vector with efficient appends and random access. @@ -88,10 +77,13 @@ defmodule Aja.Vector do iex> match?(v when vec_size(v) > 99, Aja.Vector.new(1..100)) true - #{if Version.compare(System.version(), "1.11.0") != :lt do - plusplusplus_doc - end} + ## Convenience [`+++/2`](`Aja.+++/2`) operator + + The `Aja.+++/2` operator can make appending to a vector more compact by aliasing `Aja.Vector.concat/2`: + iex> import Aja + iex> vec([1, 2, 3]) +++ vec([4, 5]) + vec([1, 2, 3, 4, 5]) ## Pattern-matching and opaque type @@ -200,9 +192,10 @@ defmodule Aja.Vector do **DO** Aja.Vector.concat(vector, enumerable) - #{if Version.compare(System.version(), "1.11.0") != :lt do - "vector +++ enumerable" - end} + + or + + vector +++ enumerable ### Prefer `Aja.Enum` and `Aja.Vector` to `Enum` for vectors diff --git a/test/enum_prop_test.exs b/test/enum_prop_test.exs index 29e180a..246aab8 100644 --- a/test/enum_prop_test.exs +++ b/test/enum_prop_test.exs @@ -16,423 +16,419 @@ defmodule Aja.Enum.PropTest do ]) end - # TODO remove when dropping support for Elixir < 1.12 - stepped_range_available? = Version.compare(System.version(), "1.12.0-rc.0") != :lt - - if stepped_range_available? do - property "Aja.Enum functions should return the same as mirrored Enum functions" do - check all(list <- list_of(value()), i1 <- integer(), i2 <- integer()) do - vector = Aja.Vector.new(list) - stream = Stream.map(list, & &1) - ord_map = Aja.OrdMap.new(list, &{&1, &1}) - map_set = MapSet.new(list) - - assert(^list = Aja.Enum.to_list(list)) - assert ^list = Aja.Enum.to_list(vector) - assert ^list = Aja.Enum.to_list(stream) - assert Enum.to_list(ord_map) === Aja.Enum.to_list(ord_map) - assert Enum.to_list(map_set) === Aja.Enum.to_list(map_set) - - list_length = length(list) - unique_length = MapSet.size(map_set) - - assert list_length === Aja.Enum.count(list) - assert list_length === Aja.Enum.count(vector) - assert list_length === Aja.Enum.count(stream) - assert unique_length === Aja.Enum.count(ord_map) - assert unique_length === Aja.Enum.count(map_set) - - assert Enum.count(i1..i2) == Aja.Enum.count(i1..i2) - - fun = fn x -> :erlang.phash2(x, 7) == 0 end - count_result = Enum.count(list, fun) - assert ^count_result = Aja.Enum.count(list, fun) - assert ^count_result = Aja.Enum.count(vector, fun) - assert ^count_result = Aja.Enum.count(stream, fun) - assert Enum.count(ord_map, fun) === Aja.Enum.count(ord_map, fun) - assert Enum.count(map_set, fun) === Aja.Enum.count(map_set, fun) - - expected_empty = Enum.empty?(list) - assert expected_empty == Aja.Enum.empty?(list) - assert expected_empty == Aja.Enum.empty?(vector) - assert expected_empty == Aja.Enum.empty?(stream) - assert expected_empty == Aja.Enum.empty?(ord_map) - assert expected_empty == Aja.Enum.empty?(map_set) - - assert Enum.empty?(i1..i2) == Aja.Enum.empty?(i1..i2) - - min_result = Enum.min(list) |> capture_error() - assert min_result === Aja.Enum.min(list) |> capture_error() - assert min_result === Aja.Enum.min(vector) |> capture_error() - assert min_result === Aja.Enum.min(stream) |> capture_error() - assert Enum.min(map_set) |> capture_error() === Aja.Enum.min(map_set) |> capture_error() - - max_result = Enum.max(list) |> capture_error() - assert max_result === Aja.Enum.max(vector) |> capture_error() - assert max_result === Aja.Enum.max(list) |> capture_error() - assert max_result === Aja.Enum.max(stream) |> capture_error() - assert Enum.max(map_set) |> capture_error() === Aja.Enum.max(map_set) |> capture_error() - - assert max_result === Aja.Enum.min(list, &>=/2) |> capture_error() - assert max_result === Aja.Enum.min(vector, &>=/2) |> capture_error() - assert max_result === Aja.Enum.min(stream, &>=/2) |> capture_error() - - assert Enum.min(map_set, &>=/2) |> capture_error() === - Aja.Enum.min(map_set, &>=/2) |> capture_error() - - assert min_result === Aja.Enum.max(list, &<=/2) |> capture_error() - assert min_result === Aja.Enum.max(vector, &<=/2) |> capture_error() - assert min_result === Aja.Enum.max(stream, &<=/2) |> capture_error() - - assert Enum.max(map_set, &>=/2) |> capture_error() === - Aja.Enum.max(map_set, &>=/2) |> capture_error() - - fun = &:erlang.phash2/1 - - min_by_result = capture_error(Aja.Enum.min_by(list, fun)) - - assert min_by_result === capture_error(Aja.Enum.min_by(vector, fun)) - assert min_by_result === capture_error(Aja.Enum.min_by(stream, fun)) - - max_by_result = capture_error(Aja.Enum.max_by(list, fun)) - - assert max_by_result === capture_error(Aja.Enum.max_by(vector, fun)) - assert max_by_result === capture_error(Aja.Enum.max_by(stream, fun)) - - assert max_by_result === capture_error(Aja.Enum.min_by(list, fun, &>=/2)) - assert max_by_result === capture_error(Aja.Enum.min_by(vector, fun, &>=/2)) - assert max_by_result === capture_error(Aja.Enum.min_by(stream, fun, &>=/2)) - - assert min_by_result === capture_error(Aja.Enum.max_by(list, fun, &<=/2)) - assert min_by_result === capture_error(Aja.Enum.max_by(vector, fun, &<=/2)) - assert min_by_result === capture_error(Aja.Enum.max_by(stream, fun, &<=/2)) - - assert Enum.at(list, i1) === Aja.Enum.at(list, i1) - assert Enum.at(list, i1) === Aja.Enum.at(vector, i1) - assert Enum.at(list, i1) === Aja.Enum.at(stream, i1) - assert Enum.at(map_set, i1) === Aja.Enum.at(map_set, i1) - - assert Enum.fetch(list, i1) === Aja.Enum.fetch(list, i1) - assert Enum.fetch(list, i1) === Aja.Enum.fetch(vector, i1) - assert Enum.fetch(list, i1) === Aja.Enum.fetch(stream, i1) - assert Enum.fetch(map_set, i1) === Aja.Enum.fetch(map_set, i1) - - fetch_result = Enum.fetch!(list, i1) |> capture_error() - assert fetch_result === Aja.Enum.fetch!(list, i1) |> capture_error() - assert fetch_result === Aja.Enum.fetch!(vector, i1) |> capture_error() - assert fetch_result === Aja.Enum.fetch!(stream, i1) |> capture_error() - - assert Enum.fetch!(map_set, i1) |> capture_error() === - Aja.Enum.fetch!(map_set, i1) |> capture_error() - - # amount must be >=0 - amount = abs(i2) - slice_1 = Enum.slice(list, i1, amount) - assert slice_1 === Aja.Enum.slice(list, i1, amount) - assert slice_1 === Aja.Enum.slice(vector, i1, amount) - assert slice_1 === Aja.Enum.slice(stream, i1, amount) - assert Enum.slice(map_set, i1, amount) === Aja.Enum.slice(map_set, i1, amount) - - slice_2 = Enum.slice(list, i1..i2//1) - assert slice_2 === Aja.Enum.slice(list, i1..i2//1) - assert slice_2 === Aja.Enum.slice(vector, i1..i2//1) - assert slice_2 === Aja.Enum.slice(stream, i1..i2//1) - assert Enum.slice(map_set, i1..i2//1) === Aja.Enum.slice(map_set, i1..i2//1) - - taken = Enum.take(list, i1) - assert taken === Aja.Enum.take(list, i1) - assert taken === Aja.Enum.take(vector, i1) - assert taken === Aja.Enum.take(stream, i1) - assert Enum.take(map_set, i1) === Aja.Enum.take(map_set, i1) - - dropped = Enum.drop(list, i1) - assert dropped === Aja.Enum.drop(list, i1) - assert dropped === Aja.Enum.drop(vector, i1) - assert dropped === Aja.Enum.drop(stream, i1) - assert Enum.drop(map_set, i1) === Aja.Enum.drop(map_set, i1) - - split_result = Enum.split(list, i1) - assert split_result === Aja.Enum.split(list, i1) - assert split_result === Aja.Enum.split(vector, i1) - assert split_result === Aja.Enum.split(stream, i1) - assert Enum.split(map_set, i1) === Aja.Enum.split(map_set, i1) - - fun = fn x -> :erlang.phash2(x, 7) != 0 end - - take_while_result = Enum.take_while(list, fun) - assert take_while_result === Aja.Enum.take_while(list, fun) - assert take_while_result === Aja.Enum.take_while(vector, fun) - assert take_while_result === Aja.Enum.take_while(stream, fun) - assert Enum.take_while(map_set, fun) === Aja.Enum.take_while(map_set, fun) - - drop_while_result = Enum.drop_while(list, fun) - assert drop_while_result === Aja.Enum.drop_while(list, fun) - assert drop_while_result === Aja.Enum.drop_while(vector, fun) - assert drop_while_result === Aja.Enum.drop_while(stream, fun) - assert Enum.drop_while(map_set, fun) === Aja.Enum.drop_while(map_set, fun) - - split_while_result = Enum.split_while(list, fun) - assert split_while_result === Aja.Enum.split_while(list, fun) - assert split_while_result === Aja.Enum.split_while(vector, fun) - assert split_while_result === Aja.Enum.split_while(stream, fun) - assert Enum.split_while(map_set, fun) === Aja.Enum.split_while(map_set, fun) - - reversed_list = Enum.reverse(list) - assert ^reversed_list = Aja.Enum.reverse(list) - assert ^reversed_list = Aja.Enum.reverse(vector) - assert ^reversed_list = Aja.Enum.reverse(stream) - assert Enum.reverse(map_set) === Aja.Enum.reverse(map_set) - - reduced_result = Enum.reduce(list, &[&1 | &2]) |> capture_error - assert ^reduced_result = Aja.Enum.reduce(list, &[&1 | &2]) |> capture_error() - assert ^reduced_result = Aja.Enum.reduce(vector, &[&1 | &2]) |> capture_error() - assert ^reduced_result = Aja.Enum.reduce(stream, &[&1 | &2]) |> capture_error() - - reversed_list = Enum.reduce(list, [], &[&1 | &2]) - assert ^reversed_list = Aja.Enum.reduce(list, [], &[&1 | &2]) - assert ^reversed_list = Aja.Enum.reduce(vector, [], &[&1 | &2]) - assert ^reversed_list = Aja.Enum.reduce(stream, [], &[&1 | &2]) - assert Enum.reverse(map_set) === Aja.Enum.reduce(map_set, [], &[&1 | &2]) - - inspected_list = Enum.map(list, &inspect/1) - assert ^inspected_list = Aja.Enum.map(list, &inspect/1) - assert ^inspected_list = Aja.Enum.map(vector, &inspect/1) - assert ^inspected_list = Aja.Enum.map(stream, &inspect/1) - assert Enum.map(map_set, &inspect/1) === Aja.Enum.map(map_set, &inspect/1) - - fun = fn x -> :erlang.phash2(x, 3) == 0 end - filtered_list = Enum.filter(list, fun) - assert ^filtered_list = Aja.Enum.filter(list, fun) - assert ^filtered_list = Aja.Enum.filter(vector, fun) - assert ^filtered_list = Aja.Enum.filter(stream, fun) - assert Enum.filter(map_set, fun) === Aja.Enum.filter(map_set, fun) - - rejected_list = Enum.reject(list, fun) - assert ^rejected_list = Aja.Enum.reject(list, fun) - assert ^rejected_list = Aja.Enum.reject(vector, fun) - assert ^rejected_list = Aja.Enum.reject(stream, fun) - assert Enum.reject(map_set, fun) === Aja.Enum.reject(map_set, fun) - - assert {^filtered_list, ^rejected_list} = Aja.Enum.split_with(list, fun) - assert {^filtered_list, ^rejected_list} = Aja.Enum.split_with(vector, fun) - assert {^filtered_list, ^rejected_list} = Aja.Enum.split_with(stream, fun) - assert Enum.split_with(map_set, fun) === Aja.Enum.split_with(map_set, fun) - - any_result = Enum.any?(list) - assert any_result === Aja.Enum.any?(list) - assert any_result === Aja.Enum.any?(vector) - assert any_result === Aja.Enum.any?(stream) - assert any_result === Aja.Enum.any?(map_set) - - all_result = Enum.all?(list) - assert all_result === Aja.Enum.all?(list) - assert all_result === Aja.Enum.all?(vector) - assert all_result === Aja.Enum.all?(stream) - assert all_result === Aja.Enum.all?(map_set) - - fun = fn x -> :erlang.phash2(x, 2) == 0 end - - any_fun_result = Enum.any?(list, fun) - assert any_fun_result === Aja.Enum.any?(list, fun) - assert any_fun_result === Aja.Enum.any?(vector, fun) - assert any_fun_result === Aja.Enum.any?(stream, fun) - assert any_fun_result === Aja.Enum.any?(map_set, fun) - - all_fun_result = Enum.all?(list, fun) - assert all_fun_result === Aja.Enum.all?(list, fun) - assert all_fun_result === Aja.Enum.all?(vector, fun) - assert all_fun_result === Aja.Enum.all?(stream, fun) - assert all_fun_result === Aja.Enum.all?(map_set, fun) - - fun = fn x -> :erlang.phash2(x, 10) == 0 end - - found = Enum.find(list, fun) - assert found === Aja.Enum.find(list, fun) - assert found === Aja.Enum.find(vector, fun) - assert found === Aja.Enum.find(stream, fun) - - found_index = Enum.find_index(list, fun) - assert found_index === Aja.Enum.find_index(list, fun) - assert found_index === Aja.Enum.find_index(vector, fun) - assert found_index === Aja.Enum.find_index(stream, fun) - - fun = fn x -> if(:erlang.phash2(x, 10) == 0, do: inspect(x)) end - - found_value = Enum.find_value(list, fun) - assert found_value === Aja.Enum.find_value(list, fun) - assert found_value === Aja.Enum.find_value(vector, fun) - assert found_value === Aja.Enum.find_value(stream, fun) - - sum_result = Enum.sum(list) |> capture_error() - assert ^sum_result = Aja.Enum.sum(list) |> capture_error() - assert ^sum_result = Aja.Enum.sum(vector) |> capture_error() - assert ^sum_result = Aja.Enum.sum(stream) |> capture_error() - assert capture_error(Enum.sum(map_set)) === capture_error(Aja.Enum.sum(map_set)) - - product_result = Enum.reduce(list, 1, &(&2 * &1)) |> capture_error() - assert ^product_result = Aja.Enum.product(list) |> capture_error() - assert ^product_result = Aja.Enum.product(vector) |> capture_error() - assert ^product_result = Aja.Enum.product(stream) |> capture_error() - - join_result = Enum.join(list, ",") |> capture_error() - assert ^join_result = Aja.Enum.join(list, ",") |> capture_error() - assert ^join_result = Aja.Enum.join(stream, ",") |> capture_error() - - assert capture_error(Enum.join(map_set, ",")) === - capture_error(Aja.Enum.join(map_set, ",")) - - # foldr VS foldl, the first argument to fail won't be the same - case join_result do - {:ok, ok_result} -> - assert ^ok_result = Aja.Enum.join(vector, ",") - - _error -> - reverse_error = list |> Enum.reverse() |> Enum.join(", ") |> capture_error() - assert ^reverse_error = Aja.Enum.join(vector, ",") |> capture_error() - end - - map_join_result = Enum.map_join(list, ",", &inspect/1) - assert ^map_join_result = Aja.Enum.map_join(list, ",", &inspect/1) - assert ^map_join_result = Aja.Enum.map_join(vector, ",", &inspect/1) - assert ^map_join_result = Aja.Enum.map_join(stream, ",", &inspect/1) - - assert Enum.map_join(map_set, ",", &inspect/1) === - Aja.Enum.map_join(map_set, ",", &inspect/1) - - intersperse_result = Enum.intersperse(list, :foo) - assert ^intersperse_result = Aja.Enum.intersperse(list, :foo) - assert ^intersperse_result = Aja.Enum.intersperse(vector, :foo) - assert ^intersperse_result = Aja.Enum.intersperse(stream, :foo) - assert Enum.intersperse(map_set, :foo) === Aja.Enum.intersperse(map_set, :foo) - - map_intersperse_result = Enum.map_intersperse(list, :bar, &inspect/1) - assert ^map_intersperse_result = Aja.Enum.map_intersperse(list, :bar, &inspect/1) - assert ^map_intersperse_result = Aja.Enum.map_intersperse(vector, :bar, &inspect/1) - assert ^map_intersperse_result = Aja.Enum.map_intersperse(stream, :bar, &inspect/1) - - assert Enum.map_intersperse(map_set, :bar, &inspect/1) === - Aja.Enum.map_intersperse(map_set, :bar, &inspect/1) - - fun = fn x -> [x, inspect(x)] end - flat_map_result = Enum.flat_map(list, fun) - assert ^flat_map_result = Aja.Enum.flat_map(list, fun) - assert ^flat_map_result = Aja.Enum.flat_map(vector, fun) - assert ^flat_map_result = Aja.Enum.flat_map(stream, fun) - - assert Enum.flat_map(map_set, fun) === Aja.Enum.flat_map(map_set, fun) - - freqs = Enum.frequencies(list) - assert ^freqs = Aja.Enum.frequencies(list) - assert ^freqs = Aja.Enum.frequencies(vector) - assert ^freqs = Aja.Enum.frequencies(stream) - assert Map.new(freqs, fn {k, _v} -> {k, 1} end) === Aja.Enum.frequencies(map_set) - - fun = fn x -> :erlang.phash2(x, 10) end - freqs_by_hash = Enum.frequencies_by(list, fun) - assert ^freqs_by_hash = Aja.Enum.frequencies_by(list, fun) - assert ^freqs_by_hash = Aja.Enum.frequencies_by(vector, fun) - assert ^freqs_by_hash = Aja.Enum.frequencies_by(stream, fun) - assert Enum.frequencies_by(map_set, fun) === Aja.Enum.frequencies_by(map_set, fun) - - groups_by_hash = Enum.group_by(list, fun) - assert ^groups_by_hash = Aja.Enum.group_by(list, fun) - assert ^groups_by_hash = Aja.Enum.group_by(vector, fun) - assert ^groups_by_hash = Aja.Enum.group_by(stream, fun) - assert Enum.group_by(map_set, fun) === Aja.Enum.group_by(map_set, fun) - - groups_by_hash2 = Enum.group_by(list, fun, &inspect/1) - assert ^groups_by_hash2 = Aja.Enum.group_by(list, fun, &inspect/1) - assert ^groups_by_hash2 = Aja.Enum.group_by(vector, fun, &inspect/1) - assert ^groups_by_hash2 = Aja.Enum.group_by(stream, fun, &inspect/1) - - assert Enum.group_by(map_set, fun, &inspect/1) === - Aja.Enum.group_by(map_set, fun, &inspect/1) - - assert Enum.uniq(list) === Aja.Enum.uniq(list) - assert Enum.uniq(list) === Aja.Enum.uniq(vector) - assert Enum.uniq(list) === Aja.Enum.uniq(stream) - assert Enum.uniq(map_set) === Aja.Enum.uniq(map_set) - - assert Enum.dedup(list) === Aja.Enum.dedup(list) - assert Enum.dedup(list) === Aja.Enum.dedup(vector) - assert Enum.dedup(list) === Aja.Enum.dedup(stream) - assert Enum.dedup(map_set) === Aja.Enum.dedup(map_set) - - fun = fn x -> :erlang.phash2(x, 10) end - assert Enum.uniq_by(list, fun) === Aja.Enum.uniq_by(list, fun) - assert Enum.uniq_by(list, fun) === Aja.Enum.uniq_by(vector, fun) - assert Enum.uniq_by(list, fun) === Aja.Enum.uniq_by(stream, fun) - assert Enum.uniq_by(map_set, fun) === Aja.Enum.uniq_by(map_set, fun) - - assert Enum.dedup_by(list, fun) === Aja.Enum.dedup_by(list, fun) - assert Enum.dedup_by(list, fun) === Aja.Enum.dedup_by(vector, fun) - assert Enum.dedup_by(list, fun) === Aja.Enum.dedup_by(stream, fun) - assert Enum.dedup_by(map_set, fun) === Aja.Enum.dedup_by(map_set, fun) - - index_list = Enum.with_index(list, i1) - assert ^index_list = Aja.Enum.with_index(list, i1) - assert ^index_list = Aja.Enum.with_index(vector, i1) - assert ^index_list = Aja.Enum.with_index(stream, i1) - assert Enum.with_index(map_set, i1) === Aja.Enum.with_index(map_set, i1) - - fun = fn x, i -> {x, i + i1} end - assert ^index_list = Aja.Enum.with_index(list, fun) - assert ^index_list = Aja.Enum.with_index(vector, fun) - assert ^index_list = Aja.Enum.with_index(stream, fun) - assert Enum.with_index(map_set, i1) === Aja.Enum.with_index(map_set, fun) - - fun = fn x, i -> {{x, i}, i + 1} end - assert {index_list, list_length + i1} === Aja.Enum.map_reduce(list, i1, fun) - assert {index_list, list_length + i1} === Aja.Enum.map_reduce(vector, i1, fun) - assert {index_list, list_length + i1} === Aja.Enum.map_reduce(stream, i1, fun) - assert Enum.map_reduce(map_set, i1, fun) === Aja.Enum.map_reduce(map_set, i1, fun) - - scanned_result = Enum.scan(list, &max/2) - assert ^scanned_result = Aja.Enum.scan(list, &max/2) - assert ^scanned_result = Aja.Enum.scan(vector, &max/2) - assert ^scanned_result = Aja.Enum.scan(stream, &max/2) - assert Enum.scan(map_set, &max/2) === Aja.Enum.scan(map_set, &max/2) - - scanned_result = Enum.scan(list, 42, &max/2) - assert ^scanned_result = Aja.Enum.scan(list, 42, &max/2) - assert ^scanned_result = Aja.Enum.scan(vector, 42, &max/2) - assert ^scanned_result = Aja.Enum.scan(stream, 42, &max/2) - assert Enum.scan(map_set, 42, &max/2) === Aja.Enum.scan(map_set, 42, &max/2) - - sorted = Enum.sort(list) - assert ^sorted = Aja.Enum.sort(list) - assert ^sorted = Aja.Enum.sort(vector) - assert ^sorted = Aja.Enum.sort(stream) - # floats / ints might not come in the same order - assert Enum.sort(map_set) == Aja.Enum.sort(map_set) - - fun = fn x -> :erlang.phash2(x) end - sorted_by = Enum.sort_by(list, fun) - assert ^sorted_by = Aja.Enum.sort_by(list, fun) - assert ^sorted_by = Aja.Enum.sort_by(vector, fun) - assert ^sorted_by = Aja.Enum.sort_by(stream, fun) - assert Enum.sort_by(map_set, fun) === Aja.Enum.sort_by(map_set, fun) - - shuffled = with_seed(fn -> Enum.shuffle(list) end) - assert ^shuffled = with_seed(fn -> Aja.Enum.shuffle(list) end) - assert ^shuffled = with_seed(fn -> Aja.Enum.shuffle(vector) end) - assert ^shuffled = with_seed(fn -> Aja.Enum.shuffle(stream) end) - - rand_result = with_seed(fn -> Enum.random(list) |> capture_error() end) - assert ^rand_result = with_seed(fn -> Aja.Enum.random(list) |> capture_error() end) - assert ^rand_result = with_seed(fn -> Aja.Enum.random(vector) |> capture_error() end) - - assert with_seed(fn -> Enum.random(stream) |> capture_error() end) === - with_seed(fn -> Aja.Enum.random(stream) |> capture_error() end) - - assert with_seed(fn -> Enum.random(map_set) |> capture_error() end) === - with_seed(fn -> Aja.Enum.random(map_set) |> capture_error() end) - - rand_taken = with_seed(fn -> Enum.take_random(list, amount) end) - assert ^rand_taken = with_seed(fn -> Aja.Enum.take_random(list, amount) end) - assert ^rand_taken = with_seed(fn -> Aja.Enum.take_random(vector, amount) end) - assert ^rand_taken = with_seed(fn -> Aja.Enum.take_random(stream, amount) end) + property "Aja.Enum functions should return the same as mirrored Enum functions" do + check all(list <- list_of(value()), i1 <- integer(), i2 <- integer()) do + vector = Aja.Vector.new(list) + stream = Stream.map(list, & &1) + ord_map = Aja.OrdMap.new(list, &{&1, &1}) + map_set = MapSet.new(list) + + assert(^list = Aja.Enum.to_list(list)) + assert ^list = Aja.Enum.to_list(vector) + assert ^list = Aja.Enum.to_list(stream) + assert Enum.to_list(ord_map) === Aja.Enum.to_list(ord_map) + assert Enum.to_list(map_set) === Aja.Enum.to_list(map_set) + + list_length = length(list) + unique_length = MapSet.size(map_set) + + assert list_length === Aja.Enum.count(list) + assert list_length === Aja.Enum.count(vector) + assert list_length === Aja.Enum.count(stream) + assert unique_length === Aja.Enum.count(ord_map) + assert unique_length === Aja.Enum.count(map_set) + + assert Enum.count(i1..i2) == Aja.Enum.count(i1..i2) + assert Enum.count(i1..i2//2) == Aja.Enum.count(i1..i2//2) + + fun = fn x -> :erlang.phash2(x, 7) == 0 end + count_result = Enum.count(list, fun) + assert ^count_result = Aja.Enum.count(list, fun) + assert ^count_result = Aja.Enum.count(vector, fun) + assert ^count_result = Aja.Enum.count(stream, fun) + assert Enum.count(ord_map, fun) === Aja.Enum.count(ord_map, fun) + assert Enum.count(map_set, fun) === Aja.Enum.count(map_set, fun) + + expected_empty = Enum.empty?(list) + assert expected_empty == Aja.Enum.empty?(list) + assert expected_empty == Aja.Enum.empty?(vector) + assert expected_empty == Aja.Enum.empty?(stream) + assert expected_empty == Aja.Enum.empty?(ord_map) + assert expected_empty == Aja.Enum.empty?(map_set) + + assert Enum.empty?(i1..i2) == Aja.Enum.empty?(i1..i2) + + min_result = Enum.min(list) |> capture_error() + assert min_result === Aja.Enum.min(list) |> capture_error() + assert min_result === Aja.Enum.min(vector) |> capture_error() + assert min_result === Aja.Enum.min(stream) |> capture_error() + assert Enum.min(map_set) |> capture_error() === Aja.Enum.min(map_set) |> capture_error() + + max_result = Enum.max(list) |> capture_error() + assert max_result === Aja.Enum.max(vector) |> capture_error() + assert max_result === Aja.Enum.max(list) |> capture_error() + assert max_result === Aja.Enum.max(stream) |> capture_error() + assert Enum.max(map_set) |> capture_error() === Aja.Enum.max(map_set) |> capture_error() + + assert max_result === Aja.Enum.min(list, &>=/2) |> capture_error() + assert max_result === Aja.Enum.min(vector, &>=/2) |> capture_error() + assert max_result === Aja.Enum.min(stream, &>=/2) |> capture_error() + + assert Enum.min(map_set, &>=/2) |> capture_error() === + Aja.Enum.min(map_set, &>=/2) |> capture_error() + + assert min_result === Aja.Enum.max(list, &<=/2) |> capture_error() + assert min_result === Aja.Enum.max(vector, &<=/2) |> capture_error() + assert min_result === Aja.Enum.max(stream, &<=/2) |> capture_error() + + assert Enum.max(map_set, &>=/2) |> capture_error() === + Aja.Enum.max(map_set, &>=/2) |> capture_error() + + fun = &:erlang.phash2/1 + + min_by_result = capture_error(Aja.Enum.min_by(list, fun)) + + assert min_by_result === capture_error(Aja.Enum.min_by(vector, fun)) + assert min_by_result === capture_error(Aja.Enum.min_by(stream, fun)) + + max_by_result = capture_error(Aja.Enum.max_by(list, fun)) + + assert max_by_result === capture_error(Aja.Enum.max_by(vector, fun)) + assert max_by_result === capture_error(Aja.Enum.max_by(stream, fun)) + + assert max_by_result === capture_error(Aja.Enum.min_by(list, fun, &>=/2)) + assert max_by_result === capture_error(Aja.Enum.min_by(vector, fun, &>=/2)) + assert max_by_result === capture_error(Aja.Enum.min_by(stream, fun, &>=/2)) + + assert min_by_result === capture_error(Aja.Enum.max_by(list, fun, &<=/2)) + assert min_by_result === capture_error(Aja.Enum.max_by(vector, fun, &<=/2)) + assert min_by_result === capture_error(Aja.Enum.max_by(stream, fun, &<=/2)) + + assert Enum.at(list, i1) === Aja.Enum.at(list, i1) + assert Enum.at(list, i1) === Aja.Enum.at(vector, i1) + assert Enum.at(list, i1) === Aja.Enum.at(stream, i1) + assert Enum.at(map_set, i1) === Aja.Enum.at(map_set, i1) + + assert Enum.fetch(list, i1) === Aja.Enum.fetch(list, i1) + assert Enum.fetch(list, i1) === Aja.Enum.fetch(vector, i1) + assert Enum.fetch(list, i1) === Aja.Enum.fetch(stream, i1) + assert Enum.fetch(map_set, i1) === Aja.Enum.fetch(map_set, i1) + + fetch_result = Enum.fetch!(list, i1) |> capture_error() + assert fetch_result === Aja.Enum.fetch!(list, i1) |> capture_error() + assert fetch_result === Aja.Enum.fetch!(vector, i1) |> capture_error() + assert fetch_result === Aja.Enum.fetch!(stream, i1) |> capture_error() + + assert Enum.fetch!(map_set, i1) |> capture_error() === + Aja.Enum.fetch!(map_set, i1) |> capture_error() + + # amount must be >=0 + amount = abs(i2) + slice_1 = Enum.slice(list, i1, amount) + assert slice_1 === Aja.Enum.slice(list, i1, amount) + assert slice_1 === Aja.Enum.slice(vector, i1, amount) + assert slice_1 === Aja.Enum.slice(stream, i1, amount) + assert Enum.slice(map_set, i1, amount) === Aja.Enum.slice(map_set, i1, amount) + + slice_2 = Enum.slice(list, i1..i2//1) + assert slice_2 === Aja.Enum.slice(list, i1..i2//1) + assert slice_2 === Aja.Enum.slice(vector, i1..i2//1) + assert slice_2 === Aja.Enum.slice(stream, i1..i2//1) + assert Enum.slice(map_set, i1..i2//1) === Aja.Enum.slice(map_set, i1..i2//1) + + taken = Enum.take(list, i1) + assert taken === Aja.Enum.take(list, i1) + assert taken === Aja.Enum.take(vector, i1) + assert taken === Aja.Enum.take(stream, i1) + assert Enum.take(map_set, i1) === Aja.Enum.take(map_set, i1) + + dropped = Enum.drop(list, i1) + assert dropped === Aja.Enum.drop(list, i1) + assert dropped === Aja.Enum.drop(vector, i1) + assert dropped === Aja.Enum.drop(stream, i1) + assert Enum.drop(map_set, i1) === Aja.Enum.drop(map_set, i1) + + split_result = Enum.split(list, i1) + assert split_result === Aja.Enum.split(list, i1) + assert split_result === Aja.Enum.split(vector, i1) + assert split_result === Aja.Enum.split(stream, i1) + assert Enum.split(map_set, i1) === Aja.Enum.split(map_set, i1) + + fun = fn x -> :erlang.phash2(x, 7) != 0 end + + take_while_result = Enum.take_while(list, fun) + assert take_while_result === Aja.Enum.take_while(list, fun) + assert take_while_result === Aja.Enum.take_while(vector, fun) + assert take_while_result === Aja.Enum.take_while(stream, fun) + assert Enum.take_while(map_set, fun) === Aja.Enum.take_while(map_set, fun) + + drop_while_result = Enum.drop_while(list, fun) + assert drop_while_result === Aja.Enum.drop_while(list, fun) + assert drop_while_result === Aja.Enum.drop_while(vector, fun) + assert drop_while_result === Aja.Enum.drop_while(stream, fun) + assert Enum.drop_while(map_set, fun) === Aja.Enum.drop_while(map_set, fun) + + split_while_result = Enum.split_while(list, fun) + assert split_while_result === Aja.Enum.split_while(list, fun) + assert split_while_result === Aja.Enum.split_while(vector, fun) + assert split_while_result === Aja.Enum.split_while(stream, fun) + assert Enum.split_while(map_set, fun) === Aja.Enum.split_while(map_set, fun) + + reversed_list = Enum.reverse(list) + assert ^reversed_list = Aja.Enum.reverse(list) + assert ^reversed_list = Aja.Enum.reverse(vector) + assert ^reversed_list = Aja.Enum.reverse(stream) + assert Enum.reverse(map_set) === Aja.Enum.reverse(map_set) + + reduced_result = Enum.reduce(list, &[&1 | &2]) |> capture_error + assert ^reduced_result = Aja.Enum.reduce(list, &[&1 | &2]) |> capture_error() + assert ^reduced_result = Aja.Enum.reduce(vector, &[&1 | &2]) |> capture_error() + assert ^reduced_result = Aja.Enum.reduce(stream, &[&1 | &2]) |> capture_error() + + reversed_list = Enum.reduce(list, [], &[&1 | &2]) + assert ^reversed_list = Aja.Enum.reduce(list, [], &[&1 | &2]) + assert ^reversed_list = Aja.Enum.reduce(vector, [], &[&1 | &2]) + assert ^reversed_list = Aja.Enum.reduce(stream, [], &[&1 | &2]) + assert Enum.reverse(map_set) === Aja.Enum.reduce(map_set, [], &[&1 | &2]) + + inspected_list = Enum.map(list, &inspect/1) + assert ^inspected_list = Aja.Enum.map(list, &inspect/1) + assert ^inspected_list = Aja.Enum.map(vector, &inspect/1) + assert ^inspected_list = Aja.Enum.map(stream, &inspect/1) + assert Enum.map(map_set, &inspect/1) === Aja.Enum.map(map_set, &inspect/1) + + fun = fn x -> :erlang.phash2(x, 3) == 0 end + filtered_list = Enum.filter(list, fun) + assert ^filtered_list = Aja.Enum.filter(list, fun) + assert ^filtered_list = Aja.Enum.filter(vector, fun) + assert ^filtered_list = Aja.Enum.filter(stream, fun) + assert Enum.filter(map_set, fun) === Aja.Enum.filter(map_set, fun) + + rejected_list = Enum.reject(list, fun) + assert ^rejected_list = Aja.Enum.reject(list, fun) + assert ^rejected_list = Aja.Enum.reject(vector, fun) + assert ^rejected_list = Aja.Enum.reject(stream, fun) + assert Enum.reject(map_set, fun) === Aja.Enum.reject(map_set, fun) + + assert {^filtered_list, ^rejected_list} = Aja.Enum.split_with(list, fun) + assert {^filtered_list, ^rejected_list} = Aja.Enum.split_with(vector, fun) + assert {^filtered_list, ^rejected_list} = Aja.Enum.split_with(stream, fun) + assert Enum.split_with(map_set, fun) === Aja.Enum.split_with(map_set, fun) + + any_result = Enum.any?(list) + assert any_result === Aja.Enum.any?(list) + assert any_result === Aja.Enum.any?(vector) + assert any_result === Aja.Enum.any?(stream) + assert any_result === Aja.Enum.any?(map_set) + + all_result = Enum.all?(list) + assert all_result === Aja.Enum.all?(list) + assert all_result === Aja.Enum.all?(vector) + assert all_result === Aja.Enum.all?(stream) + assert all_result === Aja.Enum.all?(map_set) + + fun = fn x -> :erlang.phash2(x, 2) == 0 end + + any_fun_result = Enum.any?(list, fun) + assert any_fun_result === Aja.Enum.any?(list, fun) + assert any_fun_result === Aja.Enum.any?(vector, fun) + assert any_fun_result === Aja.Enum.any?(stream, fun) + assert any_fun_result === Aja.Enum.any?(map_set, fun) + + all_fun_result = Enum.all?(list, fun) + assert all_fun_result === Aja.Enum.all?(list, fun) + assert all_fun_result === Aja.Enum.all?(vector, fun) + assert all_fun_result === Aja.Enum.all?(stream, fun) + assert all_fun_result === Aja.Enum.all?(map_set, fun) + + fun = fn x -> :erlang.phash2(x, 10) == 0 end + + found = Enum.find(list, fun) + assert found === Aja.Enum.find(list, fun) + assert found === Aja.Enum.find(vector, fun) + assert found === Aja.Enum.find(stream, fun) + + found_index = Enum.find_index(list, fun) + assert found_index === Aja.Enum.find_index(list, fun) + assert found_index === Aja.Enum.find_index(vector, fun) + assert found_index === Aja.Enum.find_index(stream, fun) + + fun = fn x -> if(:erlang.phash2(x, 10) == 0, do: inspect(x)) end + + found_value = Enum.find_value(list, fun) + assert found_value === Aja.Enum.find_value(list, fun) + assert found_value === Aja.Enum.find_value(vector, fun) + assert found_value === Aja.Enum.find_value(stream, fun) + + sum_result = Enum.sum(list) |> capture_error() + assert ^sum_result = Aja.Enum.sum(list) |> capture_error() + assert ^sum_result = Aja.Enum.sum(vector) |> capture_error() + assert ^sum_result = Aja.Enum.sum(stream) |> capture_error() + assert capture_error(Enum.sum(map_set)) === capture_error(Aja.Enum.sum(map_set)) + + product_result = Enum.reduce(list, 1, &(&2 * &1)) |> capture_error() + assert ^product_result = Aja.Enum.product(list) |> capture_error() + assert ^product_result = Aja.Enum.product(vector) |> capture_error() + assert ^product_result = Aja.Enum.product(stream) |> capture_error() + + join_result = Enum.join(list, ",") |> capture_error() + assert ^join_result = Aja.Enum.join(list, ",") |> capture_error() + assert ^join_result = Aja.Enum.join(stream, ",") |> capture_error() + + assert capture_error(Enum.join(map_set, ",")) === + capture_error(Aja.Enum.join(map_set, ",")) + + # foldr VS foldl, the first argument to fail won't be the same + case join_result do + {:ok, ok_result} -> + assert ^ok_result = Aja.Enum.join(vector, ",") + + _error -> + reverse_error = list |> Enum.reverse() |> Enum.join(", ") |> capture_error() + assert ^reverse_error = Aja.Enum.join(vector, ",") |> capture_error() end + + map_join_result = Enum.map_join(list, ",", &inspect/1) + assert ^map_join_result = Aja.Enum.map_join(list, ",", &inspect/1) + assert ^map_join_result = Aja.Enum.map_join(vector, ",", &inspect/1) + assert ^map_join_result = Aja.Enum.map_join(stream, ",", &inspect/1) + + assert Enum.map_join(map_set, ",", &inspect/1) === + Aja.Enum.map_join(map_set, ",", &inspect/1) + + intersperse_result = Enum.intersperse(list, :foo) + assert ^intersperse_result = Aja.Enum.intersperse(list, :foo) + assert ^intersperse_result = Aja.Enum.intersperse(vector, :foo) + assert ^intersperse_result = Aja.Enum.intersperse(stream, :foo) + assert Enum.intersperse(map_set, :foo) === Aja.Enum.intersperse(map_set, :foo) + + map_intersperse_result = Enum.map_intersperse(list, :bar, &inspect/1) + assert ^map_intersperse_result = Aja.Enum.map_intersperse(list, :bar, &inspect/1) + assert ^map_intersperse_result = Aja.Enum.map_intersperse(vector, :bar, &inspect/1) + assert ^map_intersperse_result = Aja.Enum.map_intersperse(stream, :bar, &inspect/1) + + assert Enum.map_intersperse(map_set, :bar, &inspect/1) === + Aja.Enum.map_intersperse(map_set, :bar, &inspect/1) + + fun = fn x -> [x, inspect(x)] end + flat_map_result = Enum.flat_map(list, fun) + assert ^flat_map_result = Aja.Enum.flat_map(list, fun) + assert ^flat_map_result = Aja.Enum.flat_map(vector, fun) + assert ^flat_map_result = Aja.Enum.flat_map(stream, fun) + + assert Enum.flat_map(map_set, fun) === Aja.Enum.flat_map(map_set, fun) + + freqs = Enum.frequencies(list) + assert ^freqs = Aja.Enum.frequencies(list) + assert ^freqs = Aja.Enum.frequencies(vector) + assert ^freqs = Aja.Enum.frequencies(stream) + assert Map.new(freqs, fn {k, _v} -> {k, 1} end) === Aja.Enum.frequencies(map_set) + + fun = fn x -> :erlang.phash2(x, 10) end + freqs_by_hash = Enum.frequencies_by(list, fun) + assert ^freqs_by_hash = Aja.Enum.frequencies_by(list, fun) + assert ^freqs_by_hash = Aja.Enum.frequencies_by(vector, fun) + assert ^freqs_by_hash = Aja.Enum.frequencies_by(stream, fun) + assert Enum.frequencies_by(map_set, fun) === Aja.Enum.frequencies_by(map_set, fun) + + groups_by_hash = Enum.group_by(list, fun) + assert ^groups_by_hash = Aja.Enum.group_by(list, fun) + assert ^groups_by_hash = Aja.Enum.group_by(vector, fun) + assert ^groups_by_hash = Aja.Enum.group_by(stream, fun) + assert Enum.group_by(map_set, fun) === Aja.Enum.group_by(map_set, fun) + + groups_by_hash2 = Enum.group_by(list, fun, &inspect/1) + assert ^groups_by_hash2 = Aja.Enum.group_by(list, fun, &inspect/1) + assert ^groups_by_hash2 = Aja.Enum.group_by(vector, fun, &inspect/1) + assert ^groups_by_hash2 = Aja.Enum.group_by(stream, fun, &inspect/1) + + assert Enum.group_by(map_set, fun, &inspect/1) === + Aja.Enum.group_by(map_set, fun, &inspect/1) + + assert Enum.uniq(list) === Aja.Enum.uniq(list) + assert Enum.uniq(list) === Aja.Enum.uniq(vector) + assert Enum.uniq(list) === Aja.Enum.uniq(stream) + assert Enum.uniq(map_set) === Aja.Enum.uniq(map_set) + + assert Enum.dedup(list) === Aja.Enum.dedup(list) + assert Enum.dedup(list) === Aja.Enum.dedup(vector) + assert Enum.dedup(list) === Aja.Enum.dedup(stream) + assert Enum.dedup(map_set) === Aja.Enum.dedup(map_set) + + fun = fn x -> :erlang.phash2(x, 10) end + assert Enum.uniq_by(list, fun) === Aja.Enum.uniq_by(list, fun) + assert Enum.uniq_by(list, fun) === Aja.Enum.uniq_by(vector, fun) + assert Enum.uniq_by(list, fun) === Aja.Enum.uniq_by(stream, fun) + assert Enum.uniq_by(map_set, fun) === Aja.Enum.uniq_by(map_set, fun) + + assert Enum.dedup_by(list, fun) === Aja.Enum.dedup_by(list, fun) + assert Enum.dedup_by(list, fun) === Aja.Enum.dedup_by(vector, fun) + assert Enum.dedup_by(list, fun) === Aja.Enum.dedup_by(stream, fun) + assert Enum.dedup_by(map_set, fun) === Aja.Enum.dedup_by(map_set, fun) + + index_list = Enum.with_index(list, i1) + assert ^index_list = Aja.Enum.with_index(list, i1) + assert ^index_list = Aja.Enum.with_index(vector, i1) + assert ^index_list = Aja.Enum.with_index(stream, i1) + assert Enum.with_index(map_set, i1) === Aja.Enum.with_index(map_set, i1) + + fun = fn x, i -> {x, i + i1} end + assert ^index_list = Aja.Enum.with_index(list, fun) + assert ^index_list = Aja.Enum.with_index(vector, fun) + assert ^index_list = Aja.Enum.with_index(stream, fun) + assert Enum.with_index(map_set, i1) === Aja.Enum.with_index(map_set, fun) + + fun = fn x, i -> {{x, i}, i + 1} end + assert {index_list, list_length + i1} === Aja.Enum.map_reduce(list, i1, fun) + assert {index_list, list_length + i1} === Aja.Enum.map_reduce(vector, i1, fun) + assert {index_list, list_length + i1} === Aja.Enum.map_reduce(stream, i1, fun) + assert Enum.map_reduce(map_set, i1, fun) === Aja.Enum.map_reduce(map_set, i1, fun) + + scanned_result = Enum.scan(list, &max/2) + assert ^scanned_result = Aja.Enum.scan(list, &max/2) + assert ^scanned_result = Aja.Enum.scan(vector, &max/2) + assert ^scanned_result = Aja.Enum.scan(stream, &max/2) + assert Enum.scan(map_set, &max/2) === Aja.Enum.scan(map_set, &max/2) + + scanned_result = Enum.scan(list, 42, &max/2) + assert ^scanned_result = Aja.Enum.scan(list, 42, &max/2) + assert ^scanned_result = Aja.Enum.scan(vector, 42, &max/2) + assert ^scanned_result = Aja.Enum.scan(stream, 42, &max/2) + assert Enum.scan(map_set, 42, &max/2) === Aja.Enum.scan(map_set, 42, &max/2) + + sorted = Enum.sort(list) + assert ^sorted = Aja.Enum.sort(list) + assert ^sorted = Aja.Enum.sort(vector) + assert ^sorted = Aja.Enum.sort(stream) + # floats / ints might not come in the same order + assert Enum.sort(map_set) == Aja.Enum.sort(map_set) + + fun = fn x -> :erlang.phash2(x) end + sorted_by = Enum.sort_by(list, fun) + assert ^sorted_by = Aja.Enum.sort_by(list, fun) + assert ^sorted_by = Aja.Enum.sort_by(vector, fun) + assert ^sorted_by = Aja.Enum.sort_by(stream, fun) + assert Enum.sort_by(map_set, fun) === Aja.Enum.sort_by(map_set, fun) + + shuffled = with_seed(fn -> Enum.shuffle(list) end) + assert ^shuffled = with_seed(fn -> Aja.Enum.shuffle(list) end) + assert ^shuffled = with_seed(fn -> Aja.Enum.shuffle(vector) end) + assert ^shuffled = with_seed(fn -> Aja.Enum.shuffle(stream) end) + + rand_result = with_seed(fn -> Enum.random(list) |> capture_error() end) + assert ^rand_result = with_seed(fn -> Aja.Enum.random(list) |> capture_error() end) + assert ^rand_result = with_seed(fn -> Aja.Enum.random(vector) |> capture_error() end) + + assert with_seed(fn -> Enum.random(stream) |> capture_error() end) === + with_seed(fn -> Aja.Enum.random(stream) |> capture_error() end) + + assert with_seed(fn -> Enum.random(map_set) |> capture_error() end) === + with_seed(fn -> Aja.Enum.random(map_set) |> capture_error() end) + + rand_taken = with_seed(fn -> Enum.take_random(list, amount) end) + assert ^rand_taken = with_seed(fn -> Aja.Enum.take_random(list, amount) end) + assert ^rand_taken = with_seed(fn -> Aja.Enum.take_random(vector, amount) end) + assert ^rand_taken = with_seed(fn -> Aja.Enum.take_random(stream, amount) end) end end diff --git a/test/vector_prop_test.exs b/test/vector_prop_test.exs index 488ac08..d08d3d7 100644 --- a/test/vector_prop_test.exs +++ b/test/vector_prop_test.exs @@ -164,239 +164,234 @@ defmodule Aja.Vector.PropTest do end end - # TODO remove when dropping support for Elixir < 1.12 - stepped_range_available? = Version.compare(System.version(), "1.12.0-rc.0") != :lt - - if stepped_range_available? do - property "Aja.Vector functions should return the same as mirrored Enum functions" do - check all(list <- list_of(value()), i1 <- integer(), i2 <- integer()) do - vector = Aja.Vector.new(list) - - assert_properties(vector) + property "Aja.Vector functions should return the same as mirrored Enum functions" do + check all(list <- list_of(value()), i1 <- integer(), i2 <- integer()) do + vector = Aja.Vector.new(list) - list_length = length(list) - assert list_length === Aja.Vector.size(vector) - assert list_length === Enum.count(vector) - assert match?(v when vec_size(v) == list_length, vector) - assert match?(v when vec_size(v) >= list_length, vector) - refute match?(v when vec_size(v) < list_length, vector) + assert_properties(vector) - assert capture_error_without_type(Enum.min(list)) === - capture_error_without_type(Aja.Enum.min(vector)) + list_length = length(list) + assert list_length === Aja.Vector.size(vector) + assert list_length === Enum.count(vector) + assert match?(v when vec_size(v) == list_length, vector) + assert match?(v when vec_size(v) >= list_length, vector) + refute match?(v when vec_size(v) < list_length, vector) - assert capture_error_without_type(Enum.max(list)) === - capture_error_without_type(Aja.Enum.max(vector)) + assert capture_error_without_type(Enum.min(list)) === + capture_error_without_type(Aja.Enum.min(vector)) - assert capture_error_without_type(Enum.min(list)) === - capture_error_without_type(Aja.Enum.max(vector, &<=/2)) + assert capture_error_without_type(Enum.max(list)) === + capture_error_without_type(Aja.Enum.max(vector)) - assert capture_error_without_type(Enum.max(list)) === - capture_error_without_type(Aja.Enum.min(vector, &>=/2)) + assert capture_error_without_type(Enum.min(list)) === + capture_error_without_type(Aja.Enum.max(vector, &<=/2)) - assert capture_error_without_type(Enum.min_by(list, &:erlang.phash2/1)) === - capture_error_without_type(Aja.Enum.min_by(vector, &:erlang.phash2/1)) + assert capture_error_without_type(Enum.max(list)) === + capture_error_without_type(Aja.Enum.min(vector, &>=/2)) - assert capture_error_without_type(Enum.max_by(list, &:erlang.phash2/1)) === - capture_error_without_type(Aja.Enum.max_by(vector, &:erlang.phash2/1)) + assert capture_error_without_type(Enum.min_by(list, &:erlang.phash2/1)) === + capture_error_without_type(Aja.Enum.min_by(vector, &:erlang.phash2/1)) - assert capture_error_without_type(Enum.min_by(list, &:erlang.phash2/1)) === - capture_error_without_type(Aja.Enum.max_by(vector, &:erlang.phash2/1, &<=/2)) + assert capture_error_without_type(Enum.max_by(list, &:erlang.phash2/1)) === + capture_error_without_type(Aja.Enum.max_by(vector, &:erlang.phash2/1)) - assert capture_error_without_type(Enum.max_by(list, &:erlang.phash2/1)) === - capture_error_without_type(Aja.Enum.min_by(vector, &:erlang.phash2/1, &>=/2)) + assert capture_error_without_type(Enum.min_by(list, &:erlang.phash2/1)) === + capture_error_without_type(Aja.Enum.max_by(vector, &:erlang.phash2/1, &<=/2)) - assert Enum.at(list, i1) === Aja.Vector.at(vector, i1) - assert Enum.at(list, i1, :default) === Aja.Vector.at(vector, i1, :default) - assert Enum.at(list, i1) === vector[i1] - assert Enum.fetch(list, i1) === Aja.Vector.fetch(vector, i1) - assert Enum.fetch(list, i1) === Aja.Enum.fetch(vector, i1) + assert capture_error_without_type(Enum.max_by(list, &:erlang.phash2/1)) === + capture_error_without_type(Aja.Enum.min_by(vector, &:erlang.phash2/1, &>=/2)) - assert capture_error_without_type(Enum.fetch!(list, i1)) === - capture_error_without_type(Aja.Enum.fetch!(vector, i1)) + assert Enum.at(list, i1) === Aja.Vector.at(vector, i1) + assert Enum.at(list, i1, :default) === Aja.Vector.at(vector, i1, :default) + assert Enum.at(list, i1) === vector[i1] + assert Enum.fetch(list, i1) === Aja.Vector.fetch(vector, i1) + assert Enum.fetch(list, i1) === Aja.Enum.fetch(vector, i1) - assert capture_error_without_type(Enum.fetch!(list, i1)) === - capture_error_without_type(Aja.Vector.fetch!(vector, i1)) + assert capture_error_without_type(Enum.fetch!(list, i1)) === + capture_error_without_type(Aja.Enum.fetch!(vector, i1)) - # amount must be >=0 - amount = abs(i2) - slice_1 = Enum.slice(list, i1, amount) - assert slice_1 === Enum.slice(vector, i1, amount) - assert Aja.Vector.new(slice_1) === Aja.Vector.slice(vector, i1, amount) + assert capture_error_without_type(Enum.fetch!(list, i1)) === + capture_error_without_type(Aja.Vector.fetch!(vector, i1)) - slice_2 = Enum.slice(list, i1..i2//1) - assert slice_2 === Enum.slice(vector, i1..i2//1) - assert Aja.Vector.new(slice_2) === Aja.Vector.slice(vector, i1..i2//1) + # amount must be >=0 + amount = abs(i2) + slice_1 = Enum.slice(list, i1, amount) + assert slice_1 === Enum.slice(vector, i1, amount) + assert Aja.Vector.new(slice_1) === Aja.Vector.slice(vector, i1, amount) - assert Enum.take(list, i1) |> Aja.Vector.new() === Aja.Vector.take(vector, i1) - assert Enum.drop(list, i1) |> Aja.Vector.new() === Aja.Vector.drop(vector, i1) + slice_2 = Enum.slice(list, i1..i2//1) + assert slice_2 === Enum.slice(vector, i1..i2//1) + assert Aja.Vector.new(slice_2) === Aja.Vector.slice(vector, i1..i2//1) - {l1, l2} = Enum.split(list, i1) - assert {Aja.Vector.new(l1), Aja.Vector.new(l2)} === Aja.Vector.split(vector, i1) + assert Enum.take(list, i1) |> Aja.Vector.new() === Aja.Vector.take(vector, i1) + assert Enum.drop(list, i1) |> Aja.Vector.new() === Aja.Vector.drop(vector, i1) - replaced_list = List.replace_at(list, i1, :replaced) - assert Aja.Vector.new(replaced_list) == Aja.Vector.replace_at(vector, i1, :replaced) + {l1, l2} = Enum.split(list, i1) + assert {Aja.Vector.new(l1), Aja.Vector.new(l2)} === Aja.Vector.split(vector, i1) - assert Aja.Vector.new(replaced_list) == - Aja.Vector.update_at(vector, i1, fn _ -> :replaced end) + replaced_list = List.replace_at(list, i1, :replaced) + assert Aja.Vector.new(replaced_list) == Aja.Vector.replace_at(vector, i1, :replaced) - assert Aja.Vector.new(replaced_list) == put_in(vector[i1], :replaced) - assert Aja.Vector.new(replaced_list) == update_in(vector[i1], fn _ -> :replaced end) + assert Aja.Vector.new(replaced_list) == + Aja.Vector.update_at(vector, i1, fn _ -> :replaced end) - deleted_list = List.delete_at(list, i1) - assert Aja.Vector.new(deleted_list) == Aja.Vector.delete_at(vector, i1) - assert {vector[i1], Aja.Vector.new(deleted_list)} == Aja.Vector.pop_at(vector, i1) - assert {vector[i1], Aja.Vector.new(deleted_list)} == pop_in(vector[i1]) + assert Aja.Vector.new(replaced_list) == put_in(vector[i1], :replaced) + assert Aja.Vector.new(replaced_list) == update_in(vector[i1], fn _ -> :replaced end) - assert list === Aja.Vector.to_list(vector) - assert Enum.reverse(list) |> Aja.Vector.new() === Aja.Vector.reverse(vector) + deleted_list = List.delete_at(list, i1) + assert Aja.Vector.new(deleted_list) == Aja.Vector.delete_at(vector, i1) + assert {vector[i1], Aja.Vector.new(deleted_list)} == Aja.Vector.pop_at(vector, i1) + assert {vector[i1], Aja.Vector.new(deleted_list)} == pop_in(vector[i1]) - assert Enum.reverse(list, ~c"abc") |> Aja.Vector.new() === - Aja.Vector.reverse(vector, ~c"abc") + assert list === Aja.Vector.to_list(vector) + assert Enum.reverse(list) |> Aja.Vector.new() === Aja.Vector.reverse(vector) - assert list === Aja.Vector.foldr(vector, [], &[&1 | &2]) - assert Enum.reverse(list) === Aja.Vector.foldl(vector, [], &[&1 | &2]) + assert Enum.reverse(list, ~c"abc") |> Aja.Vector.new() === + Aja.Vector.reverse(vector, ~c"abc") - assert capture_error_without_type(Enum.reduce(list, &[&1 | &2])) === - capture_error_without_type(Aja.Enum.reduce(vector, &[&1 | &2])) + assert list === Aja.Vector.foldr(vector, [], &[&1 | &2]) + assert Enum.reverse(list) === Aja.Vector.foldl(vector, [], &[&1 | &2]) - assert Enum.scan(list, &[&1 | &2]) |> Aja.Vector.new() === - Aja.Vector.scan(vector, &[&1 | &2]) + assert capture_error_without_type(Enum.reduce(list, &[&1 | &2])) === + capture_error_without_type(Aja.Enum.reduce(vector, &[&1 | &2])) - assert Enum.scan(list, [], &[&1 | &2]) |> Aja.Vector.new() === - Aja.Vector.scan(vector, [], &[&1 | &2]) + assert Enum.scan(list, &[&1 | &2]) |> Aja.Vector.new() === + Aja.Vector.scan(vector, &[&1 | &2]) - inspected_list = Enum.map(list, &inspect/1) - assert Aja.Vector.new(inspected_list) === Aja.Vector.map(vector, &inspect/1) - assert Aja.Vector.new(inspected_list) === Aja.Vector.new(list, &inspect/1) - assert Aja.Vector.new(inspected_list) === Aja.Vector.new(vector, &inspect/1) + assert Enum.scan(list, [], &[&1 | &2]) |> Aja.Vector.new() === + Aja.Vector.scan(vector, [], &[&1 | &2]) - filtered_list = Enum.filter(list, &hash_multiple_of_2/1) - filtered_vector = Aja.Vector.filter(vector, &hash_multiple_of_2/1) - assert Aja.Vector.new(filtered_list) === filtered_vector + inspected_list = Enum.map(list, &inspect/1) + assert Aja.Vector.new(inspected_list) === Aja.Vector.map(vector, &inspect/1) + assert Aja.Vector.new(inspected_list) === Aja.Vector.new(list, &inspect/1) + assert Aja.Vector.new(inspected_list) === Aja.Vector.new(vector, &inspect/1) - index_list = Enum.with_index(list, i1) - index_vector = Aja.Vector.new(index_list) - assert index_vector === Aja.Vector.with_index(vector, i1) - assert index_vector === Aja.Vector.with_index(vector, fn x, i -> {x, i + i1} end) + filtered_list = Enum.filter(list, &hash_multiple_of_2/1) + filtered_vector = Aja.Vector.filter(vector, &hash_multiple_of_2/1) + assert Aja.Vector.new(filtered_list) === filtered_vector - assert {index_vector, list_length + i1} === - Aja.Vector.map_reduce(vector, i1, fn x, i -> {{x, i}, i + 1} end) + index_list = Enum.with_index(list, i1) + index_vector = Aja.Vector.new(index_list) + assert index_vector === Aja.Vector.with_index(vector, i1) + assert index_vector === Aja.Vector.with_index(vector, fn x, i -> {x, i + i1} end) - assert index_vector === Aja.Vector.zip(vector, Aja.Vector.new(i1..(list_length + i1))) + assert {index_vector, list_length + i1} === + Aja.Vector.map_reduce(vector, i1, fn x, i -> {{x, i}, i + 1} end) - assert index_vector === - Aja.Vector.zip_with(vector, Aja.Vector.new(0..list_length), &{&1, &2 + i1}) + assert index_vector === Aja.Vector.zip(vector, Aja.Vector.new(i1..(list_length + i1))) - assert {vector, i1..(list_length + i1) |> Enum.drop(-1) |> Aja.Vector.new()} == - Aja.Vector.unzip(index_vector) + assert index_vector === + Aja.Vector.zip_with(vector, Aja.Vector.new(0..list_length), &{&1, &2 + i1}) - assert Enum.any?(list) === Aja.Enum.any?(vector) - assert Enum.all?(list) === Aja.Enum.all?(vector) + assert {vector, i1..(list_length + i1) |> Enum.drop(-1) |> Aja.Vector.new()} == + Aja.Vector.unzip(index_vector) - assert Enum.any?(list, &hash_multiple_of_2/1) === - Aja.Enum.any?(vector, &hash_multiple_of_2/1) + assert Enum.any?(list) === Aja.Enum.any?(vector) + assert Enum.all?(list) === Aja.Enum.all?(vector) - assert Enum.all?(list, &hash_multiple_of_2/1) === - Aja.Enum.all?(vector, &hash_multiple_of_2/1) + assert Enum.any?(list, &hash_multiple_of_2/1) === + Aja.Enum.any?(vector, &hash_multiple_of_2/1) - assert true === Aja.Enum.all?(Aja.Vector.new(filtered_list), &hash_multiple_of_2/1) + assert Enum.all?(list, &hash_multiple_of_2/1) === + Aja.Enum.all?(vector, &hash_multiple_of_2/1) - assert false === - Aja.Enum.any?(Aja.Vector.new(filtered_list), fn x -> !hash_multiple_of_2(x) end) + assert true === Aja.Enum.all?(Aja.Vector.new(filtered_list), &hash_multiple_of_2/1) - assert Enum.find(list, &hash_multiple_of_2/1) === - Aja.Enum.find(vector, &hash_multiple_of_2/1) + assert false === + Aja.Enum.any?(Aja.Vector.new(filtered_list), fn x -> !hash_multiple_of_2(x) end) - assert Enum.find_value(list, &hash_multiple_of_2/1) === - Aja.Enum.find_value(vector, &hash_multiple_of_2/1) + assert Enum.find(list, &hash_multiple_of_2/1) === + Aja.Enum.find(vector, &hash_multiple_of_2/1) - assert Enum.find_index(list, &hash_multiple_of_2/1) === - Aja.Enum.find_index(vector, &hash_multiple_of_2/1) + assert Enum.find_value(list, &hash_multiple_of_2/1) === + Aja.Enum.find_value(vector, &hash_multiple_of_2/1) - assert Enum.take_while(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === - Aja.Vector.take_while(vector, &hash_multiple_of_2/1) + assert Enum.find_index(list, &hash_multiple_of_2/1) === + Aja.Enum.find_index(vector, &hash_multiple_of_2/1) - assert Enum.drop_while(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === - Aja.Vector.drop_while(vector, &hash_multiple_of_2/1) + assert Enum.take_while(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === + Aja.Vector.take_while(vector, &hash_multiple_of_2/1) - {taken, dropped} = Enum.split_while(list, &hash_multiple_of_2/1) + assert Enum.drop_while(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === + Aja.Vector.drop_while(vector, &hash_multiple_of_2/1) - assert {Aja.Vector.new(taken), Aja.Vector.new(dropped)} === - Aja.Vector.split_while(vector, &hash_multiple_of_2/1) + {taken, dropped} = Enum.split_while(list, &hash_multiple_of_2/1) - assert capture_error_without_type(Enum.sum(list)) === - capture_error_without_type(Aja.Enum.sum(vector)) + assert {Aja.Vector.new(taken), Aja.Vector.new(dropped)} === + Aja.Vector.split_while(vector, &hash_multiple_of_2/1) - assert capture_error_without_type(Enum.reduce(list, 1, &(&2 * &1))) === - capture_error_without_type(Aja.Enum.product(vector)) + assert capture_error_without_type(Enum.sum(list)) === + capture_error_without_type(Aja.Enum.sum(vector)) - assert capture_error_without_type(Enum.join(list, ",")) === - capture_error_without_type(Aja.Enum.join(vector, ",")) + assert capture_error_without_type(Enum.reduce(list, 1, &(&2 * &1))) === + capture_error_without_type(Aja.Enum.product(vector)) - assert Enum.map_join(list, ",", &inspect/1) === Aja.Enum.map_join(vector, ",", &inspect/1) + assert capture_error_without_type(Enum.join(list, ",")) === + capture_error_without_type(Aja.Enum.join(vector, ",")) - assert Enum.intersperse(list, nil) === Aja.Enum.intersperse(vector, nil) + assert Enum.map_join(list, ",", &inspect/1) === Aja.Enum.map_join(vector, ",", &inspect/1) - assert Enum.intersperse(list, nil) |> Aja.Vector.new() === - Aja.Vector.intersperse(vector, nil) + assert Enum.intersperse(list, nil) === Aja.Enum.intersperse(vector, nil) - assert Enum.map_intersperse(list, nil, &inspect/1) |> Aja.Vector.new() === - Aja.Vector.map_intersperse(vector, nil, &inspect/1) + assert Enum.intersperse(list, nil) |> Aja.Vector.new() === + Aja.Vector.intersperse(vector, nil) - assert Enum.map_intersperse(list, nil, &inspect/1) === - Aja.Enum.map_intersperse(vector, nil, &inspect/1) + assert Enum.map_intersperse(list, nil, &inspect/1) |> Aja.Vector.new() === + Aja.Vector.map_intersperse(vector, nil, &inspect/1) - assert Enum.flat_map(list, &[&1, &1]) |> Aja.Vector.new() === - Aja.Vector.flat_map(vector, &[&1, &1]) + assert Enum.map_intersperse(list, nil, &inspect/1) === + Aja.Enum.map_intersperse(vector, nil, &inspect/1) - assert Enum.frequencies(list) === Aja.Enum.frequencies(vector) + assert Enum.flat_map(list, &[&1, &1]) |> Aja.Vector.new() === + Aja.Vector.flat_map(vector, &[&1, &1]) - assert Enum.frequencies_by(list, &hash_multiple_of_2/1) === - Aja.Enum.frequencies_by(vector, &hash_multiple_of_2/1) + assert Enum.frequencies(list) === Aja.Enum.frequencies(vector) - assert Enum.group_by(list, &hash_multiple_of_2/1) === - Aja.Enum.group_by(vector, &hash_multiple_of_2/1) + assert Enum.frequencies_by(list, &hash_multiple_of_2/1) === + Aja.Enum.frequencies_by(vector, &hash_multiple_of_2/1) - assert Enum.group_by(list, &hash_multiple_of_2/1, &inspect/1) === - Aja.Enum.group_by(vector, &hash_multiple_of_2/1, &inspect/1) + assert Enum.group_by(list, &hash_multiple_of_2/1) === + Aja.Enum.group_by(vector, &hash_multiple_of_2/1) - assert Enum.uniq(list) === Aja.Enum.uniq(vector) - assert Enum.dedup(list) === Aja.Enum.dedup(vector) - assert Enum.uniq(list) |> Aja.Vector.new() === Aja.Vector.uniq(vector) - assert Enum.dedup(list) |> Aja.Vector.new() === Aja.Vector.dedup(vector) + assert Enum.group_by(list, &hash_multiple_of_2/1, &inspect/1) === + Aja.Enum.group_by(vector, &hash_multiple_of_2/1, &inspect/1) - assert Enum.uniq_by(list, &hash_multiple_of_2/1) === - Aja.Enum.uniq_by(vector, &hash_multiple_of_2/1) + assert Enum.uniq(list) === Aja.Enum.uniq(vector) + assert Enum.dedup(list) === Aja.Enum.dedup(vector) + assert Enum.uniq(list) |> Aja.Vector.new() === Aja.Vector.uniq(vector) + assert Enum.dedup(list) |> Aja.Vector.new() === Aja.Vector.dedup(vector) - assert Enum.dedup_by(list, &hash_multiple_of_2/1) === - Aja.Enum.dedup_by(vector, &hash_multiple_of_2/1) + assert Enum.uniq_by(list, &hash_multiple_of_2/1) === + Aja.Enum.uniq_by(vector, &hash_multiple_of_2/1) - assert Enum.uniq_by(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === - Aja.Vector.uniq_by(vector, &hash_multiple_of_2/1) + assert Enum.dedup_by(list, &hash_multiple_of_2/1) === + Aja.Enum.dedup_by(vector, &hash_multiple_of_2/1) - assert Enum.dedup_by(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === - Aja.Vector.dedup_by(vector, &hash_multiple_of_2/1) + assert Enum.uniq_by(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === + Aja.Vector.uniq_by(vector, &hash_multiple_of_2/1) - shuffled = Aja.Vector.shuffle(vector) - assert ^list_length = Aja.Vector.size(shuffled) + assert Enum.dedup_by(list, &hash_multiple_of_2/1) |> Aja.Vector.new() === + Aja.Vector.dedup_by(vector, &hash_multiple_of_2/1) - assert min(list_length, amount) == - Aja.Vector.take_random(vector, amount) |> Aja.Vector.size() + shuffled = Aja.Vector.shuffle(vector) + assert ^list_length = Aja.Vector.size(shuffled) - assert Aja.Vector.new() == Aja.Vector.take_random(vector, 0) + assert min(list_length, amount) == + Aja.Vector.take_random(vector, amount) |> Aja.Vector.size() - if list_length != 0 do - rand = Aja.Enum.random(vector) - assert rand in vector - assert rand in shuffled + assert Aja.Vector.new() == Aja.Vector.take_random(vector, 0) - assert vec([rand]) = Aja.Vector.take_random(vector, 1) - assert rand in vector - assert rand in shuffled - end + if list_length != 0 do + rand = Aja.Enum.random(vector) + assert rand in vector + assert rand in shuffled - assert inspect(vector) =~ "vec([" + assert vec([rand]) = Aja.Vector.take_random(vector, 1) + assert rand in vector + assert rand in shuffled end + + assert inspect(vector) =~ "vec([" end end diff --git a/test/vector_test.exs b/test/vector_test.exs index 94ad77d..ac93b4c 100644 --- a/test/vector_test.exs +++ b/test/vector_test.exs @@ -3,12 +3,7 @@ defmodule Aja.VectorTest do import Aja, only: [vec: 1] - # TODO remove when dropping support for Elixir < 1.12 - stepped_range_available? = Version.compare(System.version(), "1.12.0-rc.0") != :lt - - if stepped_range_available? do - doctest Aja.Vector - end + doctest Aja.Vector import Aja.TestHelpers