Skip to content

Commit af29eef

Browse files
authored
Increase test coverage for Enum (#14473)
* Add test for Enum.slice with start > 0 and step > 0 * Cover more branches of sort internals
1 parent 3c3637c commit af29eef

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/elixir/test/elixir/enum_test.exs

+23
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ defmodule EnumTest do
11661166
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..4) == [1, 2, 3, 1, 2]
11671167
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..4//2) == [1, 3, 2]
11681168
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..5//2) == [1, 3, 2]
1169+
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(1..6//2) == [2, 1, 3]
11691170
end
11701171

11711172
test "slice on pruned infinite streams" do
@@ -1206,6 +1207,13 @@ defmodule EnumTest do
12061207
assert Enum.sort([5, 3, 2, 4, 1], &(&1 >= &2)) == [5, 4, 3, 2, 1]
12071208
assert Enum.sort([5, 3, 2, 4, 1], :asc) == [1, 2, 3, 4, 5]
12081209
assert Enum.sort([5, 3, 2, 4, 1], :desc) == [5, 4, 3, 2, 1]
1210+
1211+
assert Enum.sort([3, 2, 1, 3, 2, 3], :asc) == [1, 2, 2, 3, 3, 3]
1212+
assert Enum.sort([3, 2, 1, 3, 2, 3], :desc) == [3, 3, 3, 2, 2, 1]
1213+
1214+
shuffled = Enum.shuffle(1..100)
1215+
assert Enum.sort(shuffled, :asc) == Enum.to_list(1..100)
1216+
assert Enum.sort(shuffled, :desc) == Enum.reverse(1..100)
12091217
end
12101218

12111219
test "sort/2 with module" do
@@ -1219,6 +1227,21 @@ defmodule EnumTest do
12191227
[~D[2020-01-01], ~D[2019-01-01], ~D[2018-01-01]]
12201228
end
12211229

1230+
test "sort/2 with streams" do
1231+
sort_stream = fn list, sorter -> list |> Stream.map(& &1) |> Enum.sort(sorter) end
1232+
1233+
assert sort_stream.([5, 3, 2, 4, 1], &(&1 >= &2)) == [5, 4, 3, 2, 1]
1234+
assert sort_stream.([5, 3, 2, 4, 1], :asc) == [1, 2, 3, 4, 5]
1235+
assert sort_stream.([5, 3, 2, 4, 1], :desc) == [5, 4, 3, 2, 1]
1236+
1237+
assert sort_stream.([3, 2, 1, 3, 2, 3], :asc) == [1, 2, 2, 3, 3, 3]
1238+
assert sort_stream.([3, 2, 1, 3, 2, 3], :desc) == [3, 3, 3, 2, 2, 1]
1239+
1240+
shuffled = Enum.shuffle(1..100)
1241+
assert sort_stream.(shuffled, :asc) == Enum.to_list(1..100)
1242+
assert sort_stream.(shuffled, :desc) == Enum.reverse(1..100)
1243+
end
1244+
12221245
test "sort_by/3" do
12231246
collection = [
12241247
[sorted_data: 4],

0 commit comments

Comments
 (0)