From f2267f594fdd8f45e373111f46ee977897680a17 Mon Sep 17 00:00:00 2001 From: Feng Date: Sat, 23 Jan 2016 22:05:41 +0800 Subject: [PATCH] test plen/2, out/2 --- test/emqttd_mqueue_tests.erl | 2 +- test/emqttd_router_tests.erl | 21 +++++++++++ test/priority_queue_tests.erl | 69 +++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/priority_queue_tests.erl diff --git a/test/emqttd_mqueue_tests.erl b/test/emqttd_mqueue_tests.erl index fd5339c96..e742b8fca 100644 --- a/test/emqttd_mqueue_tests.erl +++ b/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}, diff --git a/test/emqttd_router_tests.erl b/test/emqttd_router_tests.erl index 14cad08e3..be0e0cf4c 100644 --- a/test/emqttd_router_tests.erl +++ b/test/emqttd_router_tests.erl @@ -1,3 +1,24 @@ +%%%----------------------------------------------------------------------------- +%%% @Copyright (C) 2012-2016, Feng Lee +%%% +%%% 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). diff --git a/test/priority_queue_tests.erl b/test/priority_queue_tests.erl new file mode 100644 index 000000000..8e044ee4e --- /dev/null +++ b/test/priority_queue_tests.erl @@ -0,0 +1,69 @@ +%%%----------------------------------------------------------------------------- +%%% @Copyright (C) 2012-2016, Feng Lee +%%% +%%% 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. +