diff options
| author | Matthias Radestock <matthias@lshift.net> | 2009-08-19 21:38:14 +0100 |
|---|---|---|
| committer | Matthias Radestock <matthias@lshift.net> | 2009-08-19 21:38:14 +0100 |
| commit | 317153bf6145a054985109d33b77d5a584e28784 (patch) | |
| tree | ee2bfa13f786c88f845038f5638c069308cca0c2 /src | |
| parent | 4de3d262e5c23a8e9e357c7984dbc9a267d0600b (diff) | |
| parent | c1c9fdde82ab9b07769e0fdb1e4a9f8abf2eaaf4 (diff) | |
| download | rabbitmq-server-git-317153bf6145a054985109d33b77d5a584e28784.tar.gz | |
merge bug21428 into default
Diffstat (limited to 'src')
| -rw-r--r-- | src/rabbit_misc.erl | 20 | ||||
| -rw-r--r-- | src/rabbit_tests.erl | 9 |
2 files changed, 28 insertions, 1 deletions
diff --git a/src/rabbit_misc.erl b/src/rabbit_misc.erl index 13a2aa05ec..ed8b4165d2 100644 --- a/src/rabbit_misc.erl +++ b/src/rabbit_misc.erl @@ -53,6 +53,7 @@ -export([append_file/2, ensure_parent_dirs_exist/1]). -export([format_stderr/2]). -export([start_applications/1, stop_applications/1]). +-export([unfold/2, ceil/1]). -import(mnesia). -import(lists). @@ -116,7 +117,9 @@ -spec(format_stderr/2 :: (string(), [any()]) -> 'ok'). -spec(start_applications/1 :: ([atom()]) -> 'ok'). -spec(stop_applications/1 :: ([atom()]) -> 'ok'). - +-spec(unfold/2 :: (fun ((A) -> ({'true', B, A} | 'false')), A) -> {[B], A}). +-spec(ceil/1 :: (number()) -> number()). + -endif. %%---------------------------------------------------------------------------- @@ -446,3 +449,18 @@ stop_applications(Apps) -> cannot_stop_application, Apps). +unfold(Fun, Init) -> + unfold(Fun, [], Init). + +unfold(Fun, Acc, Init) -> + case Fun(Init) of + {true, E, I} -> unfold(Fun, [E|Acc], I); + false -> {Acc, Init} + end. + +ceil(N) -> + T = trunc(N), + case N - T of + 0 -> N; + _ -> 1 + T + end. diff --git a/src/rabbit_tests.erl b/src/rabbit_tests.erl index 5e1cdfb18a..e7aa97e5ed 100644 --- a/src/rabbit_tests.erl +++ b/src/rabbit_tests.erl @@ -49,6 +49,7 @@ test_content_prop_roundtrip(Datum, Binary) -> all_tests() -> passed = test_priority_queue(), + passed = test_unfold(), passed = test_parsing(), passed = test_topic_matching(), passed = test_log_management(), @@ -182,6 +183,14 @@ test_simple_n_element_queue(N) -> {true, false, N, ToListRes, Items} = test_priority_queue(Q), passed. +test_unfold() -> + {[], test} = rabbit_misc:unfold(fun (V) -> false end, test), + List = lists:seq(2,20,2), + {List, 0} = rabbit_misc:unfold(fun (0) -> false; + (N) -> {true, N*2, N-1} + end, 10), + passed. + test_parsing() -> passed = test_content_properties(), passed. |
