-module(gleam@iterator_test). -compile(no_auto_import). -export([to_from_list_test/0, step_test/0, take_test/0, fold_test/0, map_test/0, flat_map_test/0, append_test/0, flatten_test/0, filter_test/0, repeat_test/0, cycle_test/0, unfold_test/0, range_test/0, drop_test/0, find_test/0, index_test/0, iterate_test/0, take_while_test/0, drop_while_test/0, scan_test/0, zip_test/0, chunk_test/0, sized_chunk_test/0, intersperse_test/0, any_test/0, all_test/0, group_test/0, reduce_test/0, last_test/0, empty_test/0, once_test/0, single_test/0, interleave_test/0, fold_until_test/0, try_fold_test/0]). -export_type([cat/0]). -type cat() :: {cat, integer()}. -spec to_from_list_test() -> nil. to_from_list_test() -> Test = fun(Subject) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, Subject) end, Test([]), Test([1]), Test([1, 2]), Test([1, 2, 4, 8]). -spec step_test() -> nil. step_test() -> Test = fun(Subject) -> Step = begin _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), gleam@iterator:step(_pipe@1) end, case Subject of [] -> _pipe@2 = Step, gleam@should:equal(_pipe@2, done); [H | T] -> {next, H2@1, T2@1} = case Step of {next, H2, T2} -> {next, H2, T2}; _try -> erlang:error(#{gleam_error => assert, message => <<"Assertion pattern match failed"/utf8>>, value => _try, module => <<"gleam/iterator_test"/utf8>>, function => <<"step_test"/utf8>>, line => 34}) end, _pipe@3 = H, gleam@should:equal(_pipe@3, H2@1), _pipe@4 = T2@1, _pipe@5 = gleam@iterator:to_list(_pipe@4), gleam@should:equal(_pipe@5, T) end end, Test([]), Test([1]), Test([1, 2]), Test([1, 2, 3]). -spec take_test() -> nil. take_test() -> Test = fun(N, Subject) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:take(_pipe@1, N), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal(_pipe@3, gleam@list:take(Subject, N)) end, Test(0, []), Test(1, []), Test(-1, []), Test(0, [0]), Test(1, [0]), Test(-1, [0]), Test(0, [0, 1, 2, 3, 4]), Test(1, [0, 1, 2, 3, 4]), Test(2, [0, 1, 2, 3, 4]), Test(22, [0, 1, 2, 3, 4]). -spec fold_test() -> nil. fold_test() -> Test = fun(Subject, Acc, F) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:fold(_pipe@1, Acc, F), gleam@should:equal(_pipe@2, gleam@list:fold(Subject, Acc, F)) end, F@1 = fun(Acc@1, E) -> [E | Acc@1] end, Test([], [], F@1), Test([1], [], F@1), Test([1, 2, 3], [], F@1), Test([1, 2, 3, 4, 5, 6, 7, 8], [], F@1). -spec map_test() -> nil. map_test() -> Test = fun(Subject, F) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:map(_pipe@1, F), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal(_pipe@3, gleam@list:map(Subject, F)) end, F@1 = fun(E) -> E * 2 end, Test([], F@1), Test([1], F@1), Test([1, 2, 3], F@1), Test([1, 2, 3, 4, 5, 6, 7, 8], F@1). -spec flat_map_test() -> nil. flat_map_test() -> Test = fun(Subject, F) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:flat_map(_pipe@1, F), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal( _pipe@3, begin _pipe@4 = Subject, _pipe@5 = gleam@list:map(_pipe@4, F), _pipe@6 = gleam@list:map(_pipe@5, fun gleam@iterator:to_list/1), gleam@list:flatten(_pipe@6) end ) end, F@1 = fun(I) -> gleam@iterator:range(I, I + 2) end, Test([], F@1), Test([1], F@1), Test([1, 2], F@1). -spec append_test() -> nil. append_test() -> Test = fun(Left, Right) -> _pipe = Left, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:append( _pipe@1, gleam@iterator:from_list(Right) ), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal(_pipe@3, gleam@list:flatten([Left, Right])) end, Test([], []), Test([1], [2]), Test([1, 2], [3, 4]). -spec flatten_test() -> nil. flatten_test() -> Test = fun(Lists) -> _pipe = Lists, _pipe@1 = gleam@list:map(_pipe, fun gleam@iterator:from_list/1), _pipe@2 = gleam@iterator:from_list(_pipe@1), _pipe@3 = gleam@iterator:flatten(_pipe@2), _pipe@4 = gleam@iterator:to_list(_pipe@3), gleam@should:equal(_pipe@4, gleam@list:flatten(Lists)) end, Test([[], []]), Test([[1], [2]]), Test([[1, 2], [3, 4]]). -spec filter_test() -> nil. filter_test() -> Test = fun(Subject, F) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:filter(_pipe@1, F), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal(_pipe@3, gleam@list:filter(Subject, F)) end, Even = fun(X) -> (X rem 2) =:= 0 end, Test([], Even), Test([1], Even), Test([1, 2], Even), Test([1, 2, 3], Even), Test([1, 2, 3, 4], Even), Test([1, 2, 3, 4, 5], Even), Test([1, 2, 3, 4, 5, 6], Even). -spec repeat_test() -> nil. repeat_test() -> _pipe = 1, _pipe@1 = gleam@iterator:repeat(_pipe), _pipe@2 = gleam@iterator:take(_pipe@1, 5), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal(_pipe@3, [1, 1, 1, 1, 1]). -spec cycle_test() -> nil. cycle_test() -> _pipe = [1, 2, 3], _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:cycle(_pipe@1), _pipe@3 = gleam@iterator:take(_pipe@2, 9), _pipe@4 = gleam@iterator:to_list(_pipe@3), gleam@should:equal(_pipe@4, [1, 2, 3, 1, 2, 3, 1, 2, 3]). -spec unfold_test() -> nil. unfold_test() -> _pipe = gleam@iterator:unfold(2, fun(Acc) -> {next, Acc, Acc * 2} end), _pipe@1 = gleam@iterator:take(_pipe, 5), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [2, 4, 8, 16, 32]), _pipe@3 = gleam@iterator:unfold(2, fun(_) -> done end), _pipe@4 = gleam@iterator:take(_pipe@3, 5), _pipe@5 = gleam@iterator:to_list(_pipe@4), gleam@should:equal(_pipe@5, []), _pipe@6 = fun(N) -> case N of 0 -> done; N@1 -> {next, N@1, N@1 - 1} end end, _pipe@7 = gleam@iterator:unfold(5, _pipe@6), _pipe@8 = gleam@iterator:to_list(_pipe@7), gleam@should:equal(_pipe@8, [5, 4, 3, 2, 1]). -spec range_test() -> nil. range_test() -> Test = fun(A, B, Expected) -> _pipe = gleam@iterator:range(A, B), _pipe@1 = gleam@iterator:to_list(_pipe), gleam@should:equal(_pipe@1, Expected) end, Test(0, 0, []), Test(1, 1, []), Test(-1, -1, []), Test(0, 1, [0]), Test(0, 5, [0, 1, 2, 3, 4]), Test(1, -5, [1, 0, -1, -2, -3, -4]). -spec drop_test() -> nil. drop_test() -> _pipe = gleam@iterator:range(0, 10), _pipe@1 = gleam@iterator:drop(_pipe, 5), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [5, 6, 7, 8, 9]). -spec find_test() -> nil. find_test() -> _pipe = gleam@iterator:range(0, 10), _pipe@1 = gleam@iterator:find(_pipe, fun(E) -> E =:= 5 end), gleam@should:equal(_pipe@1, {ok, 5}), _pipe@2 = gleam@iterator:range(0, 10), _pipe@3 = gleam@iterator:find(_pipe@2, fun(E@1) -> E@1 > 10 end), gleam@should:equal(_pipe@3, {error, nil}), _pipe@4 = gleam@iterator:empty(), _pipe@5 = gleam@iterator:find(_pipe@4, fun(_) -> true end), gleam@should:equal(_pipe@5, {error, nil}), _pipe@6 = gleam@iterator:unfold( {cat, 1}, fun(Cat) -> {next, Cat, {cat, erlang:element(2, Cat) + 1}} end ), _pipe@7 = gleam@iterator:find( _pipe@6, fun(Cat@1) -> erlang:element(2, Cat@1) =:= 10 end ), gleam@should:equal(_pipe@7, {ok, {cat, 10}}). -spec index_test() -> nil. index_test() -> _pipe = gleam@iterator:from_list([<<"a"/utf8>>, <<"b"/utf8>>, <<"c"/utf8>>]), _pipe@1 = gleam@iterator:index(_pipe), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal( _pipe@2, [{0, <<"a"/utf8>>}, {1, <<"b"/utf8>>}, {2, <<"c"/utf8>>}] ). -spec iterate_test() -> nil. iterate_test() -> _pipe = fun(X) -> X * 3 end, _pipe@1 = gleam@iterator:iterate(1, _pipe), _pipe@2 = gleam@iterator:take(_pipe@1, 5), _pipe@3 = gleam@iterator:to_list(_pipe@2), gleam@should:equal(_pipe@3, [1, 3, 9, 27, 81]). -spec take_while_test() -> nil. take_while_test() -> _pipe = gleam@iterator:from_list([1, 2, 3, 2, 4]), _pipe@1 = gleam@iterator:take_while(_pipe, fun(X) -> X < 3 end), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [1, 2]). -spec drop_while_test() -> nil. drop_while_test() -> _pipe = gleam@iterator:from_list([1, 2, 3, 4, 2, 5]), _pipe@1 = gleam@iterator:drop_while(_pipe, fun(X) -> X < 4 end), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [4, 2, 5]). -spec scan_test() -> nil. scan_test() -> _pipe = gleam@iterator:from_list([1, 2, 3, 4, 5]), _pipe@1 = gleam@iterator:scan(_pipe, 0, fun(El, Acc) -> Acc + El end), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [1, 3, 6, 10, 15]). -spec zip_test() -> nil. zip_test() -> _pipe = gleam@iterator:from_list([<<"a"/utf8>>, <<"b"/utf8>>, <<"c"/utf8>>]), _pipe@1 = gleam@iterator:zip(_pipe, gleam@iterator:range(20, 30)), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal( _pipe@2, [{<<"a"/utf8>>, 20}, {<<"b"/utf8>>, 21}, {<<"c"/utf8>>, 22}] ). -spec chunk_test() -> nil. chunk_test() -> _pipe = gleam@iterator:from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]), _pipe@1 = gleam@iterator:chunk(_pipe, fun(N) -> N rem 2 end), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [[1], [2, 2], [3], [4, 4, 6], [7, 7]]). -spec sized_chunk_test() -> nil. sized_chunk_test() -> _pipe = gleam@iterator:from_list([1, 2, 3, 4, 5, 6]), _pipe@1 = gleam@iterator:sized_chunk(_pipe, 2), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [[1, 2], [3, 4], [5, 6]]), _pipe@3 = gleam@iterator:from_list([1, 2, 3, 4, 5, 6, 7, 8]), _pipe@4 = gleam@iterator:sized_chunk(_pipe@3, 3), _pipe@5 = gleam@iterator:to_list(_pipe@4), gleam@should:equal(_pipe@5, [[1, 2, 3], [4, 5, 6], [7, 8]]). -spec intersperse_test() -> nil. intersperse_test() -> _pipe = gleam@iterator:empty(), _pipe@1 = gleam@iterator:intersperse(_pipe, 0), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, []), _pipe@3 = gleam@iterator:from_list([1]), _pipe@4 = gleam@iterator:intersperse(_pipe@3, 0), _pipe@5 = gleam@iterator:to_list(_pipe@4), gleam@should:equal(_pipe@5, [1]), _pipe@6 = gleam@iterator:from_list([1, 2, 3, 4, 5]), _pipe@7 = gleam@iterator:intersperse(_pipe@6, 0), _pipe@8 = gleam@iterator:to_list(_pipe@7), gleam@should:equal(_pipe@8, [1, 0, 2, 0, 3, 0, 4, 0, 5]). -spec any_test() -> nil. any_test() -> _pipe = gleam@iterator:empty(), _pipe@1 = gleam@iterator:any(_pipe, fun(N) -> (N rem 2) =:= 0 end), gleam@should:be_false(_pipe@1), _pipe@2 = gleam@iterator:from_list([1, 2, 5, 7, 9]), _pipe@3 = gleam@iterator:any(_pipe@2, fun(N@1) -> (N@1 rem 2) =:= 0 end), gleam@should:be_true(_pipe@3), _pipe@4 = gleam@iterator:from_list([1, 3, 5, 7, 9]), _pipe@5 = gleam@iterator:any(_pipe@4, fun(N@2) -> (N@2 rem 2) =:= 0 end), gleam@should:be_false(_pipe@5). -spec all_test() -> nil. all_test() -> _pipe = gleam@iterator:empty(), _pipe@1 = gleam@iterator:all(_pipe, fun(N) -> (N rem 2) =:= 0 end), gleam@should:be_true(_pipe@1), _pipe@2 = gleam@iterator:from_list([2, 4, 6, 8]), _pipe@3 = gleam@iterator:all(_pipe@2, fun(N@1) -> (N@1 rem 2) =:= 0 end), gleam@should:be_true(_pipe@3), _pipe@4 = gleam@iterator:from_list([2, 4, 5, 8]), _pipe@5 = gleam@iterator:all(_pipe@4, fun(N@2) -> (N@2 rem 2) =:= 0 end), gleam@should:be_false(_pipe@5). -spec group_test() -> nil. group_test() -> _pipe = gleam@iterator:from_list([1, 2, 3, 4, 5, 6]), _pipe@1 = gleam@iterator:group(_pipe, fun(N) -> N rem 3 end), gleam@should:equal( _pipe@1, gleam@map:from_list([{0, [3, 6]}, {1, [1, 4]}, {2, [2, 5]}]) ). -spec reduce_test() -> nil. reduce_test() -> _pipe = gleam@iterator:empty(), _pipe@1 = gleam@iterator:reduce(_pipe, fun(X, Y) -> X + Y end), gleam@should:equal(_pipe@1, {error, nil}), _pipe@2 = gleam@iterator:from_list([1, 2, 3, 4, 5]), _pipe@3 = gleam@iterator:reduce(_pipe@2, fun(X@1, Y@1) -> X@1 + Y@1 end), gleam@should:equal(_pipe@3, {ok, 15}). -spec last_test() -> nil. last_test() -> _pipe = gleam@iterator:empty(), _pipe@1 = gleam@iterator:last(_pipe), gleam@should:equal(_pipe@1, {error, nil}), _pipe@2 = gleam@iterator:range(1, 10), _pipe@3 = gleam@iterator:last(_pipe@2), gleam@should:equal(_pipe@3, {ok, 9}). -spec empty_test() -> nil. empty_test() -> _pipe = gleam@iterator:empty(), _pipe@1 = gleam@iterator:to_list(_pipe), gleam@should:equal(_pipe@1, []). -spec once_test() -> nil. once_test() -> _pipe = gleam@iterator:once(fun() -> 1 end), _pipe@1 = gleam@iterator:to_list(_pipe), gleam@should:equal(_pipe@1, [1]). -spec single_test() -> nil. single_test() -> _pipe = gleam@iterator:single(1), _pipe@1 = gleam@iterator:to_list(_pipe), gleam@should:equal(_pipe@1, [1]). -spec interleave_test() -> nil. interleave_test() -> _pipe = gleam@iterator:from_list([1, 2, 3, 4]), _pipe@1 = gleam@iterator:interleave( _pipe, gleam@iterator:from_list([11, 12, 13, 14]) ), _pipe@2 = gleam@iterator:to_list(_pipe@1), gleam@should:equal(_pipe@2, [1, 11, 2, 12, 3, 13, 4, 14]), _pipe@3 = gleam@iterator:from_list([1, 2, 3, 4]), _pipe@4 = gleam@iterator:interleave( _pipe@3, gleam@iterator:from_list([100]) ), _pipe@5 = gleam@iterator:to_list(_pipe@4), gleam@should:equal(_pipe@5, [1, 100, 2, 3, 4]). -spec fold_until_test() -> nil. fold_until_test() -> Test = fun(Subject, Acc, F) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:fold_until(_pipe@1, Acc, F), gleam@should:equal(_pipe@2, gleam@list:fold_until(Subject, Acc, F)) end, F@1 = fun(Acc@1, E) -> case E of _@1 when E < 6 -> {continue, [E | Acc@1]}; _@2 -> {stop, Acc@1} end end, Test([], [], F@1), Test([1], [], F@1), Test([1, 2, 3], [], F@1), Test([1, 2, 3, 4, 5, 6, 7, 8], [], F@1), _pipe@3 = [1, 2, 3, 4, 5, 6, 7, 8], _pipe@4 = gleam@iterator:from_list(_pipe@3), _pipe@5 = gleam@iterator:fold_until(_pipe@4, [], F@1), gleam@should:equal(_pipe@5, [5, 4, 3, 2, 1]). -spec try_fold_test() -> nil. try_fold_test() -> Test = fun(Subject, Acc, Fun) -> _pipe = Subject, _pipe@1 = gleam@iterator:from_list(_pipe), _pipe@2 = gleam@iterator:try_fold(_pipe@1, Acc, Fun), gleam@should:equal(_pipe@2, gleam@list:try_fold(Subject, Acc, Fun)) end, F = fun(E, Acc@1) -> case E rem 2 of 0 -> {ok, E + Acc@1}; _@1 -> {error, <<"tried to add an odd number"/utf8>>} end end, Test([], 0, F), Test([2, 4, 6], 0, F), Test([1, 2, 3], 0, F), Test([1, 2, 3, 4, 5, 6, 7, 8], 0, F), _pipe@3 = [0, 2, 4, 6], _pipe@4 = gleam@iterator:from_list(_pipe@3), _pipe@5 = gleam@iterator:try_fold(_pipe@4, 0, F), gleam@should:equal(_pipe@5, {ok, 12}), _pipe@6 = [1, 2, 3, 4], _pipe@7 = gleam@iterator:from_list(_pipe@6), _pipe@8 = gleam@iterator:try_fold(_pipe@7, 0, F), gleam@should:equal(_pipe@8, {error, <<"tried to add an odd number"/utf8>>}).