| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- %%--------------------------------------------------------------------
- %% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
- %%
- %% Licensed under the Apache License, Version 2.0 (the "License");
- %% you may not use this file except in compliance with the License.
- %% You may obtain a copy of the License at
- %%
- %% http://www.apache.org/licenses/LICENSE-2.0
- %%
- %% Unless required by applicable law or agreed to in writing, software
- %% distributed under the License is distributed on an "AS IS" BASIS,
- %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- %% See the License for the specific language governing permissions and
- %% limitations under the License.
- %%--------------------------------------------------------------------
- -module(emqx_utils_stream_tests).
- -include_lib("eunit/include/eunit.hrl").
- empty_test() ->
- S = emqx_utils_stream:empty(),
- ?assertEqual([], emqx_utils_stream:next(S)).
- empty_consume_test() ->
- S = emqx_utils_stream:empty(),
- ?assertEqual([], emqx_utils_stream:consume(S)).
- chain_empties_test() ->
- S = emqx_utils_stream:chain(
- emqx_utils_stream:empty(),
- emqx_utils_stream:empty()
- ),
- ?assertEqual([], emqx_utils_stream:next(S)).
- chain_list_test() ->
- S = emqx_utils_stream:chain(
- emqx_utils_stream:list([1, 2, 3]),
- emqx_utils_stream:list([4, 5, 6])
- ),
- ?assertEqual(
- [1, 2, 3, 4, 5, 6],
- emqx_utils_stream:consume(S)
- ).
- chain_take_test() ->
- S = emqx_utils_stream:chain(
- emqx_utils_stream:list([1, 2, 3]),
- emqx_utils_stream:list([4, 5, 6, 7, 8])
- ),
- ?assertMatch(
- {[1, 2, 3, 4, 5], _SRest},
- emqx_utils_stream:consume(5, S)
- ),
- {_, SRest} = emqx_utils_stream:consume(5, S),
- ?assertEqual(
- [6, 7, 8],
- emqx_utils_stream:consume(5, SRest)
- ).
- chain_list_map_test() ->
- S = emqx_utils_stream:map(
- fun integer_to_list/1,
- emqx_utils_stream:chain(
- emqx_utils_stream:list([1, 2, 3]),
- emqx_utils_stream:chain(
- emqx_utils_stream:empty(),
- emqx_utils_stream:list([4, 5, 6])
- )
- )
- ),
- ?assertEqual(
- ["1", "2", "3", "4", "5", "6"],
- emqx_utils_stream:consume(S)
- ).
- transpose_test() ->
- S = emqx_utils_stream:transpose([
- emqx_utils_stream:list([1, 2, 3]),
- emqx_utils_stream:list([4, 5, 6, 7])
- ]),
- ?assertEqual(
- [[1, 4], [2, 5], [3, 6]],
- emqx_utils_stream:consume(S)
- ).
- transpose_none_test() ->
- ?assertEqual(
- [],
- emqx_utils_stream:consume(emqx_utils_stream:transpose([]))
- ).
- transpose_one_test() ->
- S = emqx_utils_stream:transpose([emqx_utils_stream:list([1, 2, 3])]),
- ?assertEqual(
- [[1], [2], [3]],
- emqx_utils_stream:consume(S)
- ).
- transpose_many_test() ->
- S = emqx_utils_stream:transpose([
- emqx_utils_stream:list([1, 2, 3]),
- emqx_utils_stream:list([4, 5, 6, 7]),
- emqx_utils_stream:list([8, 9])
- ]),
- ?assertEqual(
- [[1, 4, 8], [2, 5, 9]],
- emqx_utils_stream:consume(S)
- ).
- transpose_many_empty_test() ->
- S = emqx_utils_stream:transpose([
- emqx_utils_stream:list([1, 2, 3]),
- emqx_utils_stream:list([4, 5, 6, 7]),
- emqx_utils_stream:empty()
- ]),
- ?assertEqual(
- [],
- emqx_utils_stream:consume(S)
- ).
- repeat_test() ->
- S = emqx_utils_stream:repeat(emqx_utils_stream:list([1, 2, 3])),
- ?assertMatch(
- {[1, 2, 3, 1, 2, 3, 1, 2], _},
- emqx_utils_stream:consume(8, S)
- ),
- {_, SRest} = emqx_utils_stream:consume(8, S),
- ?assertMatch(
- {[3, 1, 2, 3, 1, 2, 3, 1], _},
- emqx_utils_stream:consume(8, SRest)
- ).
- repeat_empty_test() ->
- S = emqx_utils_stream:repeat(emqx_utils_stream:list([])),
- ?assertEqual(
- [],
- emqx_utils_stream:consume(8, S)
- ).
- transpose_repeat_test() ->
- S = emqx_utils_stream:transpose([
- emqx_utils_stream:repeat(emqx_utils_stream:list([1, 2])),
- emqx_utils_stream:list([4, 5, 6, 7, 8])
- ]),
- ?assertEqual(
- [[1, 4], [2, 5], [1, 6], [2, 7], [1, 8]],
- emqx_utils_stream:consume(S)
- ).
- mqueue_test() ->
- _ = erlang:send_after(1, self(), 1),
- _ = erlang:send_after(100, self(), 2),
- _ = erlang:send_after(20, self(), 42),
- ?assertEqual(
- [1, 42, 2],
- emqx_utils_stream:consume(emqx_utils_stream:mqueue(400))
- ).
- csv_test() ->
- Data1 = <<"h1,h2,h3\r\nvv1,vv2,vv3\r\nvv4,vv5,vv6">>,
- ?assertEqual(
- [
- #{<<"h1">> => <<"vv1">>, <<"h2">> => <<"vv2">>, <<"h3">> => <<"vv3">>},
- #{<<"h1">> => <<"vv4">>, <<"h2">> => <<"vv5">>, <<"h3">> => <<"vv6">>}
- ],
- emqx_utils_stream:consume(emqx_utils_stream:csv(Data1))
- ),
- Data2 = <<"h1, h2, h3\nvv1, vv2, vv3\nvv4,vv5,vv6\n">>,
- ?assertEqual(
- [
- #{<<"h1">> => <<"vv1">>, <<"h2">> => <<"vv2">>, <<"h3">> => <<"vv3">>},
- #{<<"h1">> => <<"vv4">>, <<"h2">> => <<"vv5">>, <<"h3">> => <<"vv6">>}
- ],
- emqx_utils_stream:consume(emqx_utils_stream:csv(Data2))
- ),
- ?assertEqual(
- [],
- emqx_utils_stream:consume(emqx_utils_stream:csv(<<"">>))
- ),
- BadData = <<"h1,h2,h3\r\nv1,v2,v3\r\nv4,v5">>,
- ?assertException(
- error,
- bad_format,
- emqx_utils_stream:consume(emqx_utils_stream:csv(BadData))
- ).
|