summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjohnwahba <john.a.wahba@gmail.com>2017-06-25 02:23:22 -0700
committerJosé Valim <jose.valim@gmail.com>2017-06-25 11:23:22 +0200
commit2af8540578338713cbce9c966db0553eeccbc3d6 (patch)
treeaaad37049293d22c6d79fd5bb9681a0dafbcd193
parentde866552b107b17da56b89c172093d02bc145234 (diff)
downloadelixir-2af8540578338713cbce9c966db0553eeccbc3d6.tar.gz
Fix stream cycle over empty enumerable (#6253)
-rw-r--r--lib/elixir/lib/stream.ex6
-rw-r--r--lib/elixir/test/elixir/stream_test.exs8
2 files changed, 14 insertions, 0 deletions
diff --git a/lib/elixir/lib/stream.ex b/lib/elixir/lib/stream.ex
index ae7f22cc2..e797c0239 100644
--- a/lib/elixir/lib/stream.ex
+++ b/lib/elixir/lib/stream.ex
@@ -1155,6 +1155,10 @@ defmodule Stream do
@spec cycle(Enumerable.t) :: Enumerable.t
def cycle(enumerable)
+ def cycle([]) do
+ raise ArgumentError, "cannot cycle over empty enumerable"
+ end
+
def cycle(enumerable) when is_list(enumerable) do
unfold {enumerable, enumerable}, fn
{source, [h | t]} -> {h, {source, t}}
@@ -1185,6 +1189,8 @@ defmodule Stream do
{:stream_cycle, acc} ->
{:halted, acc}
else
+ {state, []} when state in [:done, :halted] ->
+ raise ArgumentError, "cannot cycle over empty enumerable"
{state, acc} when state in [:done, :halted] ->
do_cycle(cycle, cycle, {:cont, acc})
{:suspended, acc, continuation} ->
diff --git a/lib/elixir/test/elixir/stream_test.exs b/lib/elixir/test/elixir/stream_test.exs
index 69d7f9efc..4dd905e30 100644
--- a/lib/elixir/test/elixir/stream_test.exs
+++ b/lib/elixir/test/elixir/stream_test.exs
@@ -168,6 +168,14 @@ defmodule StreamTest do
stream = Stream.cycle([1, 2, 3])
assert is_function(stream)
+ assert_raise ArgumentError, "cannot cycle over empty enumerable", fn ->
+ Stream.cycle([])
+ end
+
+ assert_raise ArgumentError, "cannot cycle over empty enumerable", fn ->
+ Stream.cycle(%{}) |> Enum.to_list()
+ end
+
assert Stream.cycle([1, 2, 3]) |> Stream.take(5) |> Enum.to_list == [1, 2, 3, 1, 2]
assert Enum.take(stream, 5) == [1, 2, 3, 1, 2]
end