Skip to content

Commit 170b797

Browse files
authored
More test coverage for Enum for non-list inputs (#14471)
* Add tests for Enum.max/2 with empty fallback * Add tests for Enum.min/2 with empty fallback * Add tests for Enum.random/1 with streams or empty range * Add tests for Enum.reduce/3 for streams & maps
1 parent 36ac4fc commit 170b797

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

lib/elixir/test/elixir/enum_test.exs

+49
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,11 @@ defmodule EnumTest do
562562
end
563563
end
564564

565+
test "max/2 with empty fallback" do
566+
assert Enum.max([], fn -> 0 end) === 0
567+
assert Enum.max([1, 2], fn -> 0 end) === 2
568+
end
569+
565570
test "max/2 with stable sorting" do
566571
assert Enum.max([1, 1.0], &>=/2) === 1
567572
assert Enum.max([1.0, 1], &>=/2) === 1.0
@@ -646,6 +651,11 @@ defmodule EnumTest do
646651
end
647652
end
648653

654+
test "min/2 with empty fallback" do
655+
assert Enum.min([], fn -> 0 end) === 0
656+
assert Enum.min([1, 2], fn -> 0 end) === 1
657+
end
658+
649659
test "min/2 with stable sorting" do
650660
assert Enum.min([1, 1.0], &<=/2) === 1
651661
assert Enum.min([1.0, 1], &<=/2) === 1.0
@@ -820,6 +830,21 @@ defmodule EnumTest do
820830
assert Enum.random([1, 2, 3, 4, 5]) == 3
821831
end
822832

833+
test "random/1 with streams" do
834+
random_stream = fn list -> list |> Stream.map(& &1) |> Enum.random() end
835+
836+
assert_raise Enum.EmptyError, fn -> random_stream.([]) end
837+
assert random_stream.([1]) == 1
838+
839+
seed = {1406, 407_414, 139_258}
840+
:rand.seed(:exsss, seed)
841+
842+
assert random_stream.([1, 2]) == 2
843+
assert random_stream.([1, 2, 3]) == 3
844+
assert random_stream.([1, 2, 3, 4]) == 1
845+
assert random_stream.([1, 2, 3, 4, 5]) == 3
846+
end
847+
823848
test "reduce/2" do
824849
assert Enum.reduce([1, 2, 3], fn x, acc -> x + acc end) == 6
825850

@@ -837,6 +862,13 @@ defmodule EnumTest do
837862
assert Enum.reduce([1, 2, 3], 1, fn x, acc -> x + acc end) == 7
838863
end
839864

865+
test "reduce/3 with streams" do
866+
reduce_stream = fn list, acc, fun -> list |> Stream.map(& &1) |> Enum.reduce(acc, fun) end
867+
868+
assert reduce_stream.([], 1, fn x, acc -> x + acc end) == 1
869+
assert reduce_stream.([1, 2, 3], 1, fn x, acc -> x + acc end) == 7
870+
end
871+
840872
test "reduce_while/3" do
841873
assert Enum.reduce_while([1, 2, 3], 1, fn i, acc -> {:cont, acc + i} end) == 7
842874
assert Enum.reduce_while([1, 2, 3], 1, fn _i, acc -> {:halt, acc} end) == 1
@@ -2023,6 +2055,11 @@ defmodule EnumTest.Range do
20232055
assert_raise Enum.EmptyError, fn -> Enum.max(1..0//1) end
20242056
end
20252057

2058+
test "max/2 with empty fallback" do
2059+
assert Enum.max(.., fn -> 0 end) === 0
2060+
assert Enum.max(1..2, fn -> 0 end) === 2
2061+
end
2062+
20262063
test "max_by/2" do
20272064
assert Enum.max_by(1..1, fn x -> :math.pow(-2, x) end) == 1
20282065
assert Enum.max_by(1..3, fn x -> :math.pow(-2, x) end) == 2
@@ -2061,6 +2098,11 @@ defmodule EnumTest.Range do
20612098
assert_raise Enum.EmptyError, fn -> Enum.min(1..0//1) end
20622099
end
20632100

2101+
test "min/2 with empty fallback" do
2102+
assert Enum.min(.., fn -> 0 end) === 0
2103+
assert Enum.min(1..2, fn -> 0 end) === 1
2104+
end
2105+
20642106
test "min_by/2" do
20652107
assert Enum.min_by(1..1, fn x -> :math.pow(-2, x) end) == 1
20662108
assert Enum.min_by(1..3, fn x -> :math.pow(-2, x) end) == 3
@@ -2112,6 +2154,8 @@ defmodule EnumTest.Range do
21122154

21132155
assert Enum.random(1..10//2) == 7
21142156
assert Enum.random(1..10//2) == 5
2157+
2158+
assert_raise Enum.EmptyError, fn -> Enum.random(..) end
21152159
end
21162160

21172161
test "reduce/2" do
@@ -2646,6 +2690,11 @@ defmodule EnumTest.Map do
26462690
Enum.slice(map, 0, 0.99)
26472691
end
26482692
end
2693+
2694+
test "reduce/3" do
2695+
assert Enum.reduce(%{}, 1, fn x, acc -> x + acc end) == 1
2696+
assert Enum.reduce(%{a: 1, b: 2}, 1, fn {_, x}, acc -> x + acc end) == 4
2697+
end
26492698
end
26502699

26512700
defmodule EnumTest.SideEffects do

0 commit comments

Comments
 (0)