瀏覽代碼

test plen/2, out/2

Feng 10 年之前
父節點
當前提交
f2267f594f
共有 3 個文件被更改,包括 91 次插入1 次删除
  1. 1 1
      test/emqttd_mqueue_tests.erl
  2. 21 0
      test/emqttd_router_tests.erl
  3. 69 0
      test/priority_queue_tests.erl

+ 1 - 1
test/emqttd_mqueue_tests.erl

@@ -42,7 +42,7 @@ in_test() ->
     Q3 = ?QM:in(#mqtt_message{qos = 2}, Q2),
     Q4 = ?QM:in(#mqtt_message{}, Q3),
     Q5 = ?QM:in(#mqtt_message{}, Q4),
-    ?assertEqual(true, ?QM:is_full(Q5)).
+    ?assertEqual(5, ?QM:len(Q5)).
     
 in_qos0_test() ->
     Opts = [{max_length, 5},

+ 21 - 0
test/emqttd_router_tests.erl

@@ -1,3 +1,24 @@
+%%%-----------------------------------------------------------------------------
+%%% @Copyright (C) 2012-2016, Feng Lee <feng@emqtt.io>
+%%%
+%%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%%% of this software and associated documentation files (the "Software"), to deal
+%%% in the Software without restriction, including without limitation the rights
+%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%%% copies of the Software, and to permit persons to whom the Software is
+%%% furnished to do so, subject to the following conditions:
+%%%
+%%% The above copyright notice and this permission notice shall be included in all
+%%% copies or substantial portions of the Software.
+%%%
+%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+%%% SOFTWARE.
+%%%-----------------------------------------------------------------------------
 
 -module(emqttd_router_tests).
 

+ 69 - 0
test/priority_queue_tests.erl

@@ -0,0 +1,69 @@
+%%%-----------------------------------------------------------------------------
+%%% @Copyright (C) 2012-2016, Feng Lee <feng@emqtt.io>
+%%%
+%%% Permission is hereby granted, free of charge, to any person obtaining a copy
+%%% of this software and associated documentation files (the "Software"), to deal
+%%% in the Software without restriction, including without limitation the rights
+%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+%%% copies of the Software, and to permit persons to whom the Software is
+%%% furnished to do so, subject to the following conditions:
+%%%
+%%% The above copyright notice and this permission notice shall be included in all
+%%% copies or substantial portions of the Software.
+%%%
+%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+%%% SOFTWARE.
+%%%-----------------------------------------------------------------------------
+
+-module(priority_queue_tests).
+
+-include("emqttd.hrl").
+
+-ifdef(TEST).
+
+-include_lib("eunit/include/eunit.hrl").
+
+-define(PQ, priority_queue).
+
+plen_test() ->
+    Q = ?PQ:new(),
+    ?assertEqual(0, ?PQ:plen(0, Q)),
+    Q0 = ?PQ:in(z, Q),
+    ?assertEqual(1, ?PQ:plen(0, Q0)),
+    Q1 = ?PQ:in(x, 1, Q),
+    ?assertEqual(1, ?PQ:plen(1, Q1)),
+    Q2 = ?PQ:in(y, 2, Q1),
+    ?assertEqual(1, ?PQ:plen(2, Q2)),
+    Q3 = ?PQ:in(z, 2, Q2),
+    ?assertEqual(2, ?PQ:plen(2, Q3)).
+
+out2_test() ->
+    Els = [a, {b, 1}, {c, 1}, {d, 2}, {e, 2}, {f, 2}],
+    Q  = ?PQ:new(),
+    Q0 = lists:foldl(
+            fun({El, P}, Q) ->
+                    ?PQ:in(El, P, Q);
+                (El, Q) ->
+                    ?PQ:in(El, Q)
+            end, Q, Els),
+    {Val, Q1} = ?PQ:out(Q0),
+    ?assertEqual({value, d}, Val),
+    {Val1, Q2} = ?PQ:out(2, Q1),
+    ?assertEqual({value, e}, Val1),
+    {Val2, Q3} = ?PQ:out(1, Q2),
+    ?assertEqual({value, b}, Val2),
+    {Val3, Q4} = ?PQ:out(Q3),
+    ?assertEqual({value, f}, Val3),
+    {Val4, Q5} = ?PQ:out(Q4),
+    ?assertEqual({value, c}, Val4),
+    {Val5, Q6} = ?PQ:out(Q5),
+    ?assertEqual({value, a}, Val5),
+    {empty, _Q7} = ?PQ:out(Q6).
+
+-endif.
+