emqx_pqueue_SUITE.erl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019 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_ct:all(?SUITE).
  23. t_priority_queue_plen(_) ->
  24. Q = ?PQ:new(),
  25. 0 = ?PQ:plen(0, Q),
  26. Q0 = ?PQ:in(z, Q),
  27. 1 = ?PQ:plen(0, Q0),
  28. Q1 = ?PQ:in(x, 1, Q0),
  29. 1 = ?PQ:plen(1, Q1),
  30. Q2 = ?PQ:in(y, 2, Q1),
  31. 1 = ?PQ:plen(2, Q2),
  32. Q3 = ?PQ:in(z, 2, Q2),
  33. 2 = ?PQ:plen(2, Q3),
  34. {_, Q4} = ?PQ:out(1, Q3),
  35. 0 = ?PQ:plen(1, Q4),
  36. {_, Q5} = ?PQ:out(Q4),
  37. 1 = ?PQ:plen(2, Q5),
  38. {_, Q6} = ?PQ:out(Q5),
  39. 0 = ?PQ:plen(2, Q6),
  40. 1 = ?PQ:len(Q6),
  41. {_, Q7} = ?PQ:out(Q6),
  42. 0 = ?PQ:len(Q7).
  43. t_priority_queue_out2(_) ->
  44. Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
  45. Q = ?PQ:new(),
  46. Q0 = lists:foldl(
  47. fun({El, P}, Acc) ->
  48. ?PQ:in(El, P, Acc);
  49. (El, Acc) ->
  50. ?PQ:in(El, Acc)
  51. end, Q, Els),
  52. {Val, Q1} = ?PQ:out(Q0),
  53. {value, d} = Val,
  54. {Val1, Q2} = ?PQ:out(2, Q1),
  55. {value, e} = Val1,
  56. {Val2, Q3} = ?PQ:out(1, Q2),
  57. {value, b} = Val2,
  58. {Val3, Q4} = ?PQ:out(Q3),
  59. {value, f} = Val3,
  60. {Val4, Q5} = ?PQ:out(Q4),
  61. {value, c} = Val4,
  62. {Val5, Q6} = ?PQ:out(Q5),
  63. {value, a} = Val5,
  64. {empty, _Q7} = ?PQ:out(Q6).
  65. t_priority_queues(_) ->
  66. Q0 = ?PQ:new(),
  67. Q1 = ?PQ:new(),
  68. PQueue = {pqueue, [{0, Q0}, {1, Q1}]},
  69. ?assert(?PQ:is_queue(PQueue)),
  70. [] = ?PQ:to_list(PQueue),
  71. PQueue1 = ?PQ:in(a, 0, ?PQ:new()),
  72. PQueue2 = ?PQ:in(b, 0, PQueue1),
  73. PQueue3 = ?PQ:in(c, 1, PQueue2),
  74. PQueue4 = ?PQ:in(d, 1, PQueue3),
  75. 4 = ?PQ:len(PQueue4),
  76. [{1, c}, {1, d}, {0, a}, {0, b}] = ?PQ:to_list(PQueue4),
  77. PQueue4 = ?PQ:from_list([{1, c}, {1, d}, {0, a}, {0, b}]),
  78. empty = ?PQ:highest(?PQ:new()),
  79. 0 = ?PQ:highest(PQueue1),
  80. 1 = ?PQ:highest(PQueue4),
  81. PQueue5 = ?PQ:in(e, infinity, PQueue4),
  82. PQueue6 = ?PQ:in(f, 1, PQueue5),
  83. {{value, e}, PQueue7} = ?PQ:out(PQueue6),
  84. {empty, _} = ?PQ:out(0, ?PQ:new()),
  85. {empty, Q0} = ?PQ:out_p(Q0),
  86. Q2 = ?PQ:in(a, Q0),
  87. Q3 = ?PQ:in(b, Q2),
  88. Q4 = ?PQ:in(c, Q3),
  89. {{value, a, 0}, _Q5} = ?PQ:out_p(Q4),
  90. {{value,c,1}, PQueue8} = ?PQ:out_p(PQueue7),
  91. Q4 = ?PQ:join(Q4, ?PQ:new()),
  92. Q4 = ?PQ:join(?PQ:new(), Q4),
  93. {queue, [a], [a], 2} = ?PQ:join(Q2, Q2),
  94. {pqueue,[{-1,{queue,[f],[d],2}},
  95. {0,{queue,[a],[a,b],3}}]} = ?PQ:join(PQueue8, Q2),
  96. {pqueue,[{-1,{queue,[f],[d],2}},
  97. {0,{queue,[b],[a,a],3}}]} = ?PQ:join(Q2, PQueue8),
  98. {pqueue,[{-1,{queue,[f],[d,f,d],4}},
  99. {0,{queue,[b],[a,b,a],4}}]} = ?PQ:join(PQueue8, PQueue8).