emqx_pqueue_SUITE.erl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2018-2024 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
  68. ({El, P}, Acc) ->
  69. ?PQ:in(El, P, Acc);
  70. (El, Acc) ->
  71. ?PQ:in(El, Acc)
  72. end,
  73. Q,
  74. Els
  75. ),
  76. ?assertEqual(
  77. {pqueue, [
  78. {infinity, {queue, [e], [], 1}},
  79. {-2, {queue, [f], [], 1}},
  80. {-1, {queue, [d], [c], 2}},
  81. {0, {queue, [b], [a], 2}}
  82. ]},
  83. Q1
  84. ).
  85. t_out(_) ->
  86. Q = ?PQ:new(),
  87. {empty, Q} = ?PQ:out(Q),
  88. {empty, Q} = ?PQ:out(0, Q),
  89. try ?PQ:out(1, Q) of
  90. _ -> ct:fail(should_throw_error)
  91. catch
  92. error:Reason ->
  93. ?assertEqual(Reason, badarg)
  94. end,
  95. {{value, a}, Q} = ?PQ:out(?PQ:from_list([{0, a}])),
  96. {{value, a}, {queue, [], [b], 1}} = ?PQ:out(?PQ:from_list([{0, a}, {0, b}])),
  97. {{value, a}, {queue, [], [], 0}} = ?PQ:out({queue, [], [a], 1}),
  98. {{value, a}, {queue, [c], [b], 2}} = ?PQ:out({queue, [c, b], [a], 3}),
  99. {{value, a}, {queue, [e, d], [b, c], 4}} = ?PQ:out({queue, [e, d, c, b], [a], 5}),
  100. {{value, a}, {queue, [c], [b], 2}} = ?PQ:out({queue, [c, b, a], [], 3}),
  101. {{value, a}, {queue, [d, c], [b], 3}} = ?PQ:out({queue, [d, c], [a, b], 4}),
  102. {{value, a}, {queue, [], [], 0}} = ?PQ:out(?PQ:from_list([{1, a}])),
  103. {{value, a}, {queue, [c], [b], 2}} = ?PQ:out(?PQ:from_list([{1, a}, {0, b}, {0, c}])),
  104. {{value, a}, {pqueue, [{-1, {queue, [b], [], 1}}]}} = ?PQ:out(?PQ:from_list([{1, b}, {2, a}])),
  105. {{value, a}, {pqueue, [{-1, {queue, [], [b], 1}}]}} = ?PQ:out(?PQ:from_list([{1, a}, {1, b}])).
  106. t_out_2(_) ->
  107. {empty, {pqueue, [{-1, {queue, [a], [], 1}}]}} = ?PQ:out(0, ?PQ:from_list([{1, a}])),
  108. {{value, a}, {queue, [], [], 0}} = ?PQ:out(1, ?PQ:from_list([{1, a}])),
  109. {{value, a}, {pqueue, [{-1, {queue, [], [b], 1}}]}} =
  110. ?PQ:out(1, ?PQ:from_list([{1, a}, {1, b}])),
  111. {{value, a}, {queue, [b], [], 1}} = ?PQ:out(1, ?PQ:from_list([{1, a}, {0, b}])).
  112. t_out_p(_) ->
  113. {empty, {queue, [], [], 0}} = ?PQ:out_p(?PQ:new()),
  114. {{value, a, 1}, {queue, [b], [], 1}} = ?PQ:out_p(?PQ:from_list([{1, a}, {0, b}])).
  115. t_join(_) ->
  116. Q = ?PQ:in(a, ?PQ:new()),
  117. Q = ?PQ:join(Q, ?PQ:new()),
  118. Q = ?PQ:join(?PQ:new(), Q),
  119. Q1 = ?PQ:in(a, ?PQ:new()),
  120. Q2 = ?PQ:in(b, Q1),
  121. Q3 = ?PQ:in(c, Q2),
  122. {queue, [c, b], [a], 3} = Q3,
  123. Q4 = ?PQ:in(x, ?PQ:new()),
  124. Q5 = ?PQ:in(y, Q4),
  125. Q6 = ?PQ:in(z, Q5),
  126. {queue, [z, y], [x], 3} = Q6,
  127. {queue, [z, y], [a, b, c, x], 6} = ?PQ:join(Q3, Q6),
  128. PQueue1 = ?PQ:from_list([{1, c}, {1, d}]),
  129. PQueue2 = ?PQ:from_list([{1, c}, {1, d}, {0, a}, {0, b}]),
  130. PQueue3 = ?PQ:from_list([{1, c}, {1, d}, {-1, a}, {-1, b}]),
  131. {pqueue, [
  132. {-1, {queue, [d], [c], 2}},
  133. {0, {queue, [z, y], [x], 3}}
  134. ]} = ?PQ:join(PQueue1, Q6),
  135. {pqueue, [
  136. {-1, {queue, [d], [c], 2}},
  137. {0, {queue, [z, y], [x], 3}}
  138. ]} = ?PQ:join(Q6, PQueue1),
  139. {pqueue, [
  140. {-1, {queue, [d], [c], 2}},
  141. {0, {queue, [z, y], [a, b, x], 5}}
  142. ]} = ?PQ:join(PQueue2, Q6),
  143. {pqueue, [
  144. {-1, {queue, [d], [c], 2}},
  145. {0, {queue, [b], [x, y, z, a], 5}}
  146. ]} = ?PQ:join(Q6, PQueue2),
  147. {pqueue, [
  148. {-1, {queue, [d], [c], 2}},
  149. {0, {queue, [z, y], [x], 3}},
  150. {1, {queue, [b], [a], 2}}
  151. ]} = ?PQ:join(PQueue3, Q6),
  152. {pqueue, [
  153. {-1, {queue, [d], [c], 2}},
  154. {0, {queue, [z, y], [x], 3}},
  155. {1, {queue, [b], [a], 2}}
  156. ]} = ?PQ:join(Q6, PQueue3),
  157. PQueue4 = ?PQ:from_list([{1, c}, {1, d}]),
  158. PQueue5 = ?PQ:from_list([{2, a}, {2, b}]),
  159. {pqueue, [
  160. {-2, {queue, [b], [a], 2}},
  161. {-1, {queue, [d], [c], 2}}
  162. ]} = ?PQ:join(PQueue4, PQueue5).
  163. t_filter(_) ->
  164. {pqueue, [
  165. {-2, {queue, [10], [4], 2}},
  166. {-1, {queue, [2], [], 1}}
  167. ]} =
  168. ?PQ:filter(
  169. fun
  170. (V) when V rem 2 =:= 0 ->
  171. true;
  172. (_) ->
  173. false
  174. end,
  175. ?PQ:from_list([{0, 1}, {0, 3}, {1, 2}, {2, 4}, {2, 10}])
  176. ).
  177. t_highest(_) ->
  178. empty = ?PQ:highest(?PQ:new()),
  179. 0 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}])),
  180. 2 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}, {1, c}, {2, d}, {2, e}])).