priority_queue_tests.erl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. %%%-----------------------------------------------------------------------------
  2. %%% @Copyright (C) 2012-2016, Feng Lee <feng@emqtt.io>
  3. %%%
  4. %%% Permission is hereby granted, free of charge, to any person obtaining a copy
  5. %%% of this software and associated documentation files (the "Software"), to deal
  6. %%% in the Software without restriction, including without limitation the rights
  7. %%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. %%% copies of the Software, and to permit persons to whom the Software is
  9. %%% furnished to do so, subject to the following conditions:
  10. %%%
  11. %%% The above copyright notice and this permission notice shall be included in all
  12. %%% copies or substantial portions of the Software.
  13. %%%
  14. %%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. %%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. %%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. %%% SOFTWARE.
  21. %%%-----------------------------------------------------------------------------
  22. -module(priority_queue_tests).
  23. -include("emqttd.hrl").
  24. -ifdef(TEST).
  25. -include_lib("eunit/include/eunit.hrl").
  26. -define(PQ, priority_queue).
  27. plen_test() ->
  28. Q = ?PQ:new(),
  29. ?assertEqual(0, ?PQ:plen(0, Q)),
  30. Q0 = ?PQ:in(z, Q),
  31. ?assertEqual(1, ?PQ:plen(0, Q0)),
  32. Q1 = ?PQ:in(x, 1, Q0),
  33. ?assertEqual(1, ?PQ:plen(1, Q1)),
  34. Q2 = ?PQ:in(y, 2, Q1),
  35. ?assertEqual(1, ?PQ:plen(2, Q2)),
  36. Q3 = ?PQ:in(z, 2, Q2),
  37. ?assertEqual(2, ?PQ:plen(2, Q3)),
  38. {_, Q4} = ?PQ:out(1, Q3),
  39. ?assertEqual(0, ?PQ:plen(1, Q4)),
  40. {_, Q5} = ?PQ:out(Q4),
  41. ?assertEqual(1, ?PQ:plen(2, Q5)),
  42. {_, Q6} = ?PQ:out(Q5),
  43. ?assertEqual(0, ?PQ:plen(2, Q6)),
  44. ?assertEqual(1, ?PQ:len(Q6)),
  45. {_, Q7} = ?PQ:out(Q6),
  46. ?assertEqual(0, ?PQ:len(Q7)).
  47. out2_test() ->
  48. Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
  49. Q = ?PQ:new(),
  50. Q0 = lists:foldl(
  51. fun({El, P}, Q) ->
  52. ?PQ:in(El, P, Q);
  53. (El, Q) ->
  54. ?PQ:in(El, Q)
  55. end, Q, Els),
  56. {Val, Q1} = ?PQ:out(Q0),
  57. ?assertEqual({value, d}, Val),
  58. {Val1, Q2} = ?PQ:out(2, Q1),
  59. ?assertEqual({value, e}, Val1),
  60. {Val2, Q3} = ?PQ:out(1, Q2),
  61. ?assertEqual({value, b}, Val2),
  62. {Val3, Q4} = ?PQ:out(Q3),
  63. ?assertEqual({value, f}, Val3),
  64. {Val4, Q5} = ?PQ:out(Q4),
  65. ?assertEqual({value, c}, Val4),
  66. {Val5, Q6} = ?PQ:out(Q5),
  67. ?assertEqual({value, a}, Val5),
  68. {empty, _Q7} = ?PQ:out(Q6).
  69. -endif.