priority_queue_tests.erl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, Q),
  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. out2_test() ->
  39. Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
  40. Q = ?PQ:new(),
  41. Q0 = lists:foldl(
  42. fun({El, P}, Q) ->
  43. ?PQ:in(El, P, Q);
  44. (El, Q) ->
  45. ?PQ:in(El, Q)
  46. end, Q, Els),
  47. {Val, Q1} = ?PQ:out(Q0),
  48. ?assertEqual({value, d}, Val),
  49. {Val1, Q2} = ?PQ:out(2, Q1),
  50. ?assertEqual({value, e}, Val1),
  51. {Val2, Q3} = ?PQ:out(1, Q2),
  52. ?assertEqual({value, b}, Val2),
  53. {Val3, Q4} = ?PQ:out(Q3),
  54. ?assertEqual({value, f}, Val3),
  55. {Val4, Q5} = ?PQ:out(Q4),
  56. ?assertEqual({value, c}, Val4),
  57. {Val5, Q6} = ?PQ:out(Q5),
  58. ?assertEqual({value, a}, Val5),
  59. {empty, _Q7} = ?PQ:out(Q6).
  60. -endif.