emqx_pqueue_SUITE.erl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. %% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
  2. %%
  3. %% Licensed under the Apache License, Version 2.0 (the "License");
  4. %% you may not use this file except in compliance with the License.
  5. %% You may obtain a copy of the License at
  6. %%
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. -module(emqx_pqueue_SUITE).
  15. -include("emqx_mqtt.hrl").
  16. -include_lib("eunit/include/eunit.hrl").
  17. -compile(export_all).
  18. -compile(nowarn_export_all).
  19. -define(PQ, emqx_pqueue).
  20. all() -> [t_priority_queue_plen, t_priority_queue_out2, t_priority_queues].
  21. t_priority_queue_plen(_) ->
  22. Q = ?PQ:new(),
  23. 0 = ?PQ:plen(0, Q),
  24. Q0 = ?PQ:in(z, Q),
  25. 1 = ?PQ:plen(0, Q0),
  26. Q1 = ?PQ:in(x, 1, Q0),
  27. 1 = ?PQ:plen(1, Q1),
  28. Q2 = ?PQ:in(y, 2, Q1),
  29. 1 = ?PQ:plen(2, Q2),
  30. Q3 = ?PQ:in(z, 2, Q2),
  31. 2 = ?PQ:plen(2, Q3),
  32. {_, Q4} = ?PQ:out(1, Q3),
  33. 0 = ?PQ:plen(1, Q4),
  34. {_, Q5} = ?PQ:out(Q4),
  35. 1 = ?PQ:plen(2, Q5),
  36. {_, Q6} = ?PQ:out(Q5),
  37. 0 = ?PQ:plen(2, Q6),
  38. 1 = ?PQ:len(Q6),
  39. {_, Q7} = ?PQ:out(Q6),
  40. 0 = ?PQ:len(Q7).
  41. t_priority_queue_out2(_) ->
  42. Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
  43. Q = ?PQ:new(),
  44. Q0 = lists:foldl(
  45. fun({El, P}, Acc) ->
  46. ?PQ:in(El, P, Acc);
  47. (El, Acc) ->
  48. ?PQ:in(El, Acc)
  49. end, Q, Els),
  50. {Val, Q1} = ?PQ:out(Q0),
  51. {value, d} = Val,
  52. {Val1, Q2} = ?PQ:out(2, Q1),
  53. {value, e} = Val1,
  54. {Val2, Q3} = ?PQ:out(1, Q2),
  55. {value, b} = Val2,
  56. {Val3, Q4} = ?PQ:out(Q3),
  57. {value, f} = Val3,
  58. {Val4, Q5} = ?PQ:out(Q4),
  59. {value, c} = Val4,
  60. {Val5, Q6} = ?PQ:out(Q5),
  61. {value, a} = Val5,
  62. {empty, _Q7} = ?PQ:out(Q6).
  63. t_priority_queues(_) ->
  64. Q0 = ?PQ:new(),
  65. Q1 = ?PQ:new(),
  66. PQueue = {pqueue, [{0, Q0}, {1, Q1}]},
  67. ?assert(?PQ:is_queue(PQueue)),
  68. [] = ?PQ:to_list(PQueue),
  69. PQueue1 = ?PQ:in(a, 0, ?PQ:new()),
  70. PQueue2 = ?PQ:in(b, 0, PQueue1),
  71. PQueue3 = ?PQ:in(c, 1, PQueue2),
  72. PQueue4 = ?PQ:in(d, 1, PQueue3),
  73. 4 = ?PQ:len(PQueue4),
  74. [{1, c}, {1, d}, {0, a}, {0, b}] = ?PQ:to_list(PQueue4),
  75. PQueue4 = ?PQ:from_list([{1, c}, {1, d}, {0, a}, {0, b}]),
  76. empty = ?PQ:highest(?PQ:new()),
  77. 0 = ?PQ:highest(PQueue1),
  78. 1 = ?PQ:highest(PQueue4),
  79. PQueue5 = ?PQ:in(e, infinity, PQueue4),
  80. PQueue6 = ?PQ:in(f, 1, PQueue5),
  81. {{value, e}, PQueue7} = ?PQ:out(PQueue6),
  82. {empty, _} = ?PQ:out(0, ?PQ:new()),
  83. {empty, Q0} = ?PQ:out_p(Q0),
  84. Q2 = ?PQ:in(a, Q0),
  85. Q3 = ?PQ:in(b, Q2),
  86. Q4 = ?PQ:in(c, Q3),
  87. {{value, a, 0}, _Q5} = ?PQ:out_p(Q4),
  88. {{value,c,1}, PQueue8} = ?PQ:out_p(PQueue7),
  89. Q4 = ?PQ:join(Q4, ?PQ:new()),
  90. Q4 = ?PQ:join(?PQ:new(), Q4),
  91. {queue, [a], [a], 2} = ?PQ:join(Q2, Q2),
  92. {pqueue,[{-1,{queue,[f],[d],2}},
  93. {0,{queue,[a],[a,b],3}}]} = ?PQ:join(PQueue8, Q2),
  94. {pqueue,[{-1,{queue,[f],[d],2}},
  95. {0,{queue,[b],[a,a],3}}]} = ?PQ:join(Q2, PQueue8),
  96. {pqueue,[{-1,{queue,[f],[d,f,d],4}},
  97. {0,{queue,[b],[a,b,a],4}}]} = ?PQ:join(PQueue8, PQueue8).