emqx_pqueue_SUITE.erl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_is_queue(_) ->
  24. error('TODO').
  25. t_is_empty(_) ->
  26. error('TODO').
  27. t_to_list(_) ->
  28. error('TODO').
  29. t_from_list(_) ->
  30. error('TODO').
  31. t_in(_) ->
  32. error('TODO').
  33. t_out_p(_) ->
  34. error('TODO').
  35. t_join(_) ->
  36. error('TODO').
  37. t_filter(_) ->
  38. error('TODO').
  39. t_fold(_) ->
  40. error('TODO').
  41. t_highest(_) ->
  42. error('TODO').
  43. t_out(_) ->
  44. error('TODO').
  45. t_len(_) ->
  46. error('TODO').
  47. t_plen(_) ->
  48. error('TODO').
  49. t_new(_) ->
  50. error('TODO').
  51. t_priority_queue_plen(_) ->
  52. Q = ?PQ:new(),
  53. 0 = ?PQ:plen(0, Q),
  54. Q0 = ?PQ:in(z, Q),
  55. 1 = ?PQ:plen(0, Q0),
  56. Q1 = ?PQ:in(x, 1, Q0),
  57. 1 = ?PQ:plen(1, Q1),
  58. Q2 = ?PQ:in(y, 2, Q1),
  59. 1 = ?PQ:plen(2, Q2),
  60. Q3 = ?PQ:in(z, 2, Q2),
  61. 2 = ?PQ:plen(2, Q3),
  62. {_, Q4} = ?PQ:out(1, Q3),
  63. 0 = ?PQ:plen(1, Q4),
  64. {_, Q5} = ?PQ:out(Q4),
  65. 1 = ?PQ:plen(2, Q5),
  66. {_, Q6} = ?PQ:out(Q5),
  67. 0 = ?PQ:plen(2, Q6),
  68. 1 = ?PQ:len(Q6),
  69. {_, Q7} = ?PQ:out(Q6),
  70. 0 = ?PQ:len(Q7).
  71. t_priority_queue_out2(_) ->
  72. Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
  73. Q = ?PQ:new(),
  74. Q0 = lists:foldl(
  75. fun({El, P}, Acc) ->
  76. ?PQ:in(El, P, Acc);
  77. (El, Acc) ->
  78. ?PQ:in(El, Acc)
  79. end, Q, Els),
  80. {Val, Q1} = ?PQ:out(Q0),
  81. {value, d} = Val,
  82. {Val1, Q2} = ?PQ:out(2, Q1),
  83. {value, e} = Val1,
  84. {Val2, Q3} = ?PQ:out(1, Q2),
  85. {value, b} = Val2,
  86. {Val3, Q4} = ?PQ:out(Q3),
  87. {value, f} = Val3,
  88. {Val4, Q5} = ?PQ:out(Q4),
  89. {value, c} = Val4,
  90. {Val5, Q6} = ?PQ:out(Q5),
  91. {value, a} = Val5,
  92. {empty, _Q7} = ?PQ:out(Q6).
  93. t_priority_queues(_) ->
  94. Q0 = ?PQ:new(),
  95. Q1 = ?PQ:new(),
  96. PQueue = {pqueue, [{0, Q0}, {1, Q1}]},
  97. ?assert(?PQ:is_queue(PQueue)),
  98. [] = ?PQ:to_list(PQueue),
  99. PQueue1 = ?PQ:in(a, 0, ?PQ:new()),
  100. PQueue2 = ?PQ:in(b, 0, PQueue1),
  101. PQueue3 = ?PQ:in(c, 1, PQueue2),
  102. PQueue4 = ?PQ:in(d, 1, PQueue3),
  103. 4 = ?PQ:len(PQueue4),
  104. [{1, c}, {1, d}, {0, a}, {0, b}] = ?PQ:to_list(PQueue4),
  105. PQueue4 = ?PQ:from_list([{1, c}, {1, d}, {0, a}, {0, b}]),
  106. empty = ?PQ:highest(?PQ:new()),
  107. 0 = ?PQ:highest(PQueue1),
  108. 1 = ?PQ:highest(PQueue4),
  109. PQueue5 = ?PQ:in(e, infinity, PQueue4),
  110. PQueue6 = ?PQ:in(f, 1, PQueue5),
  111. {{value, e}, PQueue7} = ?PQ:out(PQueue6),
  112. {empty, _} = ?PQ:out(0, ?PQ:new()),
  113. {empty, Q0} = ?PQ:out_p(Q0),
  114. Q2 = ?PQ:in(a, Q0),
  115. Q3 = ?PQ:in(b, Q2),
  116. Q4 = ?PQ:in(c, Q3),
  117. {{value, a, 0}, _Q5} = ?PQ:out_p(Q4),
  118. {{value,c,1}, PQueue8} = ?PQ:out_p(PQueue7),
  119. Q4 = ?PQ:join(Q4, ?PQ:new()),
  120. Q4 = ?PQ:join(?PQ:new(), Q4),
  121. {queue, [a], [a], 2} = ?PQ:join(Q2, Q2),
  122. {pqueue,[{-1,{queue,[f],[d],2}},
  123. {0,{queue,[a],[a,b],3}}]} = ?PQ:join(PQueue8, Q2),
  124. {pqueue,[{-1,{queue,[f],[d],2}},
  125. {0,{queue,[b],[a,a],3}}]} = ?PQ:join(Q2, PQueue8),
  126. {pqueue,[{-1,{queue,[f],[d,f,d],4}},
  127. {0,{queue,[b],[a,b,a],4}}]} = ?PQ:join(PQueue8, PQueue8).