emqx_pqueue_SUITE.erl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_pqueue_SUITE).
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -include_lib("eunit/include/eunit.hrl").
  20. -define(PQ, emqx_pqueue).
  21. -define(SUITE, ?MODULE).
  22. all() -> emqx_common_test_helpers:all(?SUITE).
  23. t_is_queue(_) ->
  24. Q = ?PQ:new(),
  25. ?assertEqual(true, ?PQ:is_queue(Q)),
  26. Q1 = ?PQ:in(a, 1, Q),
  27. ?assertEqual(true, ?PQ:is_queue(Q1)),
  28. ?assertEqual(false, ?PQ:is_queue(bad_queue)).
  29. t_is_empty(_) ->
  30. Q = ?PQ:new(),
  31. ?assertEqual(true, ?PQ:is_empty(Q)),
  32. ?assertEqual(false, ?PQ:is_empty(?PQ:in(a, Q))).
  33. t_len(_) ->
  34. Q = ?PQ:new(),
  35. Q1 = ?PQ:in(a, Q),
  36. ?assertEqual(1, ?PQ:len(Q1)),
  37. Q2 = ?PQ:in(b, 1, Q1),
  38. ?assertEqual(2, ?PQ:len(Q2)).
  39. t_plen(_) ->
  40. Q = ?PQ:new(),
  41. Q1 = ?PQ:in(a, Q),
  42. ?assertEqual(1, ?PQ:plen(0, Q1)),
  43. ?assertEqual(0, ?PQ:plen(1, Q1)),
  44. Q2 = ?PQ:in(b, 1, Q1),
  45. Q3 = ?PQ:in(c, 1, Q2),
  46. ?assertEqual(2, ?PQ:plen(1, Q3)),
  47. ?assertEqual(1, ?PQ:plen(0, Q3)),
  48. ?assertEqual(0, ?PQ:plen(0, {pqueue, []})).
  49. t_to_list(_) ->
  50. Q = ?PQ:new(),
  51. ?assertEqual([], ?PQ:to_list(Q)),
  52. Q1 = ?PQ:in(a, Q),
  53. L1 = ?PQ:to_list(Q1),
  54. ?assertEqual([{0, a}], L1),
  55. Q2 = ?PQ:in(b, 1, Q1),
  56. L2 = ?PQ:to_list(Q2),
  57. ?assertEqual([{1, b}, {0, a}], L2).
  58. t_from_list(_) ->
  59. Q = ?PQ:from_list([{1, c}, {1, d}, {0, a}, {0, b}]),
  60. ?assertEqual({pqueue, [{-1, {queue, [d], [c], 2}}, {0, {queue, [b], [a], 2}}]}, Q),
  61. ?assertEqual(true, ?PQ:is_queue(Q)),
  62. ?assertEqual(4, ?PQ:len(Q)).
  63. t_in(_) ->
  64. Q = ?PQ:new(),
  65. Els = [a, b, {c, 1}, {d, 1}, {e, infinity}, {f, 2}],
  66. Q1 = lists:foldl(
  67. fun({El, P}, Acc) ->
  68. ?PQ:in(El, P, Acc);
  69. (El, Acc) ->
  70. ?PQ:in(El, Acc)
  71. end, Q, Els),
  72. ?assertEqual({pqueue, [{infinity, {queue, [e], [], 1}},
  73. {-2, {queue, [f], [], 1}},
  74. {-1, {queue, [d], [c], 2}},
  75. {0, {queue, [b], [a], 2}}]}, Q1).
  76. t_out(_) ->
  77. Q = ?PQ:new(),
  78. {empty, Q} = ?PQ:out(Q),
  79. {empty, Q} = ?PQ:out(0, Q),
  80. try ?PQ:out(1, Q) of
  81. _ -> ct:fail(should_throw_error)
  82. catch error:Reason ->
  83. ?assertEqual(Reason, badarg)
  84. end,
  85. {{value, a}, Q} = ?PQ:out(?PQ:from_list([{0, a}])),
  86. {{value, a}, {queue, [], [b], 1}} = ?PQ:out(?PQ:from_list([{0, a}, {0, b}])),
  87. {{value, a}, {queue, [], [], 0}} = ?PQ:out({queue, [], [a], 1}),
  88. {{value, a}, {queue, [c], [b], 2}} = ?PQ:out({queue, [c, b], [a], 3}),
  89. {{value, a}, {queue, [e, d], [b, c], 4}} = ?PQ:out({queue, [e, d, c, b], [a], 5}),
  90. {{value, a}, {queue, [c], [b], 2}} = ?PQ:out({queue, [c, b, a], [], 3}),
  91. {{value, a}, {queue, [d, c], [b], 3}} = ?PQ:out({queue, [d, c], [a, b], 4}),
  92. {{value, a}, {queue, [], [], 0}} = ?PQ:out(?PQ:from_list([{1, a}])),
  93. {{value, a}, {queue, [c], [b], 2}} = ?PQ:out(?PQ:from_list([{1, a}, {0, b}, {0, c}])),
  94. {{value, a}, {pqueue, [{-1, {queue, [b], [], 1}}]}} = ?PQ:out(?PQ:from_list([{1, b}, {2, a}])),
  95. {{value, a}, {pqueue, [{-1, {queue, [], [b], 1}}]}} = ?PQ:out(?PQ:from_list([{1, a}, {1, b}])).
  96. t_out_2(_) ->
  97. {empty, {pqueue, [{-1, {queue, [a], [], 1}}]}} = ?PQ:out(0, ?PQ:from_list([{1, a}])),
  98. {{value, a}, {queue, [], [], 0}} = ?PQ:out(1, ?PQ:from_list([{1, a}])),
  99. {{value, a}, {pqueue, [{-1, {queue, [], [b], 1}}]}} =
  100. ?PQ:out(1, ?PQ:from_list([{1, a}, {1, b}])),
  101. {{value, a}, {queue, [b], [], 1}} = ?PQ:out(1, ?PQ:from_list([{1, a}, {0, b}])).
  102. t_out_p(_) ->
  103. {empty, {queue, [], [], 0}} = ?PQ:out_p(?PQ:new()),
  104. {{value, a, 1}, {queue, [b], [], 1}} = ?PQ:out_p(?PQ:from_list([{1, a}, {0, b}])).
  105. t_join(_) ->
  106. Q = ?PQ:in(a, ?PQ:new()),
  107. Q = ?PQ:join(Q, ?PQ:new()),
  108. Q = ?PQ:join(?PQ:new(), Q),
  109. Q1 = ?PQ:in(a, ?PQ:new()),
  110. Q2 = ?PQ:in(b, Q1),
  111. Q3 = ?PQ:in(c, Q2),
  112. {queue,[c,b],[a],3} = Q3,
  113. Q4 = ?PQ:in(x, ?PQ:new()),
  114. Q5 = ?PQ:in(y, Q4),
  115. Q6 = ?PQ:in(z, Q5),
  116. {queue,[z,y],[x],3} = Q6,
  117. {queue,[z,y],[a,b,c,x],6} = ?PQ:join(Q3, Q6),
  118. PQueue1 = ?PQ:from_list([{1, c}, {1, d}]),
  119. PQueue2 = ?PQ:from_list([{1, c}, {1, d}, {0, a}, {0, b}]),
  120. PQueue3 = ?PQ:from_list([{1, c}, {1, d}, {-1, a}, {-1, b}]),
  121. {pqueue,[{-1,{queue,[d],[c],2}},
  122. {0,{queue,[z,y],[x],3}}]} = ?PQ:join(PQueue1, Q6),
  123. {pqueue,[{-1,{queue,[d],[c],2}},
  124. {0,{queue,[z,y],[x],3}}]} = ?PQ:join(Q6, PQueue1),
  125. {pqueue,[{-1,{queue,[d],[c],2}},
  126. {0,{queue,[z,y],[a,b,x],5}}]} = ?PQ:join(PQueue2, Q6),
  127. {pqueue,[{-1,{queue,[d],[c],2}},
  128. {0,{queue,[b],[x,y,z,a],5}}]} = ?PQ:join(Q6, PQueue2),
  129. {pqueue,[{-1,{queue,[d],[c],2}},
  130. {0,{queue,[z,y],[x],3}},
  131. {1,{queue,[b],[a],2}}]} = ?PQ:join(PQueue3, Q6),
  132. {pqueue,[{-1,{queue,[d],[c],2}},
  133. {0,{queue,[z,y],[x],3}},
  134. {1,{queue,[b],[a],2}}]} = ?PQ:join(Q6, PQueue3),
  135. PQueue4 = ?PQ:from_list([{1, c}, {1, d}]),
  136. PQueue5 = ?PQ:from_list([{2, a}, {2, b}]),
  137. {pqueue,[{-2,{queue,[b],[a],2}},
  138. {-1,{queue,[d],[c],2}}]} = ?PQ:join(PQueue4, PQueue5).
  139. t_filter(_) ->
  140. {pqueue, [{-2, {queue, [10], [4], 2}},
  141. {-1, {queue, [2], [], 1}}]} =
  142. ?PQ:filter(fun(V) when V rem 2 =:= 0 ->
  143. true;
  144. (_) ->
  145. false
  146. end, ?PQ:from_list([{0, 1}, {0, 3}, {1, 2}, {2, 4}, {2, 10}])).
  147. t_highest(_) ->
  148. empty = ?PQ:highest(?PQ:new()),
  149. 0 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}])),
  150. 2 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}, {1, c}, {2, d}, {2, e}])).